| 1 |
masaki |
2 |
// |
| 2 |
masaki |
72 |
// XspfQTDocument.m |
| 3 |
masaki |
2 |
// XspfQT |
| 4 |
|
|
// |
| 5 |
|
|
// Created by Hori,Masaki on 08/08/29. |
| 6 |
|
|
// Copyright masakih 2008 . All rights reserved. |
| 7 |
|
|
// |
| 8 |
|
|
|
| 9 |
masaki |
72 |
#import "XspfQTDocument.h" |
| 10 |
|
|
#import "XspfQTComponent.h" |
| 11 |
|
|
#import "XspfQTMovieWindowController.h" |
| 12 |
|
|
#import "XspfQTPlayListWindowController.h" |
| 13 |
masaki |
2 |
|
| 14 |
masaki |
72 |
@interface XspfQTDocument (Private) |
| 15 |
masaki |
116 |
- (void)setPlaylist:(XspfQTComponent *)newList; |
| 16 |
|
|
- (XspfQTComponent *)playlist; |
| 17 |
masaki |
31 |
- (NSXMLDocument *)XMLDocument; |
| 18 |
|
|
- (NSData *)outputData; |
| 19 |
masaki |
141 |
|
| 20 |
|
|
- (NSData *)dataFromURL:(NSURL *)url error:(NSError **)outError; |
| 21 |
masaki |
2 |
@end |
| 22 |
|
|
|
| 23 |
masaki |
72 |
@implementation XspfQTDocument |
| 24 |
masaki |
2 |
|
| 25 |
masaki |
72 |
NSString *XspfQTDocumentWillCloseNotification = @"XspfQTDocumentWillCloseNotification"; |
| 26 |
masaki |
61 |
|
| 27 |
masaki |
95 |
- (id)initWithType:(NSString *)typeName error:(NSError **)outError |
| 28 |
|
|
{ |
| 29 |
|
|
[self init]; |
| 30 |
|
|
|
| 31 |
masaki |
116 |
id newPlaylist = [XspfQTComponent xspfPlaylist]; |
| 32 |
|
|
if(!newPlaylist) { |
| 33 |
masaki |
95 |
[self autorelease]; |
| 34 |
|
|
return nil; |
| 35 |
|
|
} |
| 36 |
|
|
|
| 37 |
masaki |
116 |
[self setPlaylist:newPlaylist]; |
| 38 |
masaki |
121 |
// NSLog(@"new playlist is (%@)%@", NSStringFromClass([[self playlist] class]), [self playlist]); |
| 39 |
masaki |
95 |
|
| 40 |
|
|
return self; |
| 41 |
|
|
} |
| 42 |
masaki |
2 |
|
| 43 |
|
|
- (void)makeWindowControllers |
| 44 |
|
|
{ |
| 45 |
masaki |
72 |
playListWindowController = [[XspfQTPlayListWindowController alloc] init]; |
| 46 |
masaki |
2 |
[self addWindowController:playListWindowController]; |
| 47 |
|
|
|
| 48 |
masaki |
72 |
movieWindowController = [[XspfQTMovieWindowController alloc] init]; |
| 49 |
masaki |
42 |
[movieWindowController setShouldCloseDocument:YES]; |
| 50 |
masaki |
2 |
[self addWindowController:movieWindowController]; |
| 51 |
|
|
} |
| 52 |
|
|
|
| 53 |
|
|
- (NSData *)dataOfType:(NSString *)typeName error:(NSError **)outError |
| 54 |
|
|
{ |
| 55 |
masaki |
31 |
return [self outputData]; |
| 56 |
masaki |
2 |
} |
| 57 |
|
|
|
| 58 |
masaki |
141 |
- (BOOL)readFromURL:(NSURL *)absoluteURL ofType:(NSString *)typeName error:(NSError **)outError |
| 59 |
|
|
{ |
| 60 |
|
|
*outError = nil; |
| 61 |
|
|
|
| 62 |
masaki |
162 |
if(![typeName isEqualToString:@"QuickTime Movie"] |
| 63 |
masaki |
169 |
|| ![typeName isEqualToString:@"Matroska Video"] |
| 64 |
|
|
|| ![typeName isEqualToString:@"DivX Media Format"]) { |
| 65 |
masaki |
141 |
NSData *data = [self dataFromURL:absoluteURL error:outError]; |
| 66 |
|
|
if(!data) return NO; |
| 67 |
|
|
|
| 68 |
|
|
return [self readFromData:data ofType:typeName error:outError]; |
| 69 |
|
|
} |
| 70 |
|
|
|
| 71 |
|
|
NSString *xmlElem; |
| 72 |
|
|
xmlElem = [NSString stringWithFormat:@"<track><location>%@</location></track>", |
| 73 |
|
|
[absoluteURL absoluteString]]; |
| 74 |
|
|
|
| 75 |
|
|
NSError *error = nil; |
| 76 |
|
|
id new = [XspfQTComponent xspfComponentWithXMLElementString:xmlElem |
| 77 |
|
|
error:&error]; |
| 78 |
|
|
if(error) { |
| 79 |
|
|
NSLog(@"%@", error); |
| 80 |
|
|
if(outError) { |
| 81 |
|
|
*outError = error; |
| 82 |
|
|
} |
| 83 |
|
|
return NO; |
| 84 |
|
|
} |
| 85 |
|
|
|
| 86 |
|
|
id pl = [XspfQTComponent xspfPlaylist]; |
| 87 |
|
|
if(!pl) { |
| 88 |
|
|
return NO; |
| 89 |
|
|
} |
| 90 |
|
|
|
| 91 |
|
|
[[[pl children] objectAtIndex:0] addChild:new]; |
| 92 |
|
|
|
| 93 |
|
|
[self setPlaylist:pl]; |
| 94 |
|
|
id t = [self trackList]; |
| 95 |
|
|
if(![t title]) { |
| 96 |
|
|
[t setTitle:[[[self fileURL] path] lastPathComponent]]; |
| 97 |
|
|
} |
| 98 |
|
|
|
| 99 |
|
|
[self setFileType:@"XML Shareable Playlist Format"]; |
| 100 |
|
|
[self setFileURL:nil]; |
| 101 |
|
|
|
| 102 |
|
|
return YES; |
| 103 |
|
|
} |
| 104 |
masaki |
2 |
- (BOOL)readFromData:(NSData *)data ofType:(NSString *)typeName error:(NSError **)outError |
| 105 |
|
|
{ |
| 106 |
masaki |
141 |
*outError = nil; |
| 107 |
|
|
|
| 108 |
|
|
if(![typeName isEqualToString:@"XML Shareable Playlist Format"]) { |
| 109 |
|
|
return NO; |
| 110 |
|
|
} |
| 111 |
|
|
|
| 112 |
masaki |
2 |
NSError *error = nil; |
| 113 |
masaki |
10 |
NSXMLDocument *d = [[[NSXMLDocument alloc] initWithData:data |
| 114 |
|
|
options:0 |
| 115 |
|
|
error:&error] autorelease]; |
| 116 |
masaki |
121 |
if(error) { |
| 117 |
|
|
NSLog(@"%@", error); |
| 118 |
|
|
*outError = error; |
| 119 |
|
|
return NO; |
| 120 |
|
|
} |
| 121 |
masaki |
2 |
NSXMLElement *root = [d rootElement]; |
| 122 |
masaki |
116 |
id pl = [XspfQTComponent xspfComponemtWithXMLElement:root]; |
| 123 |
masaki |
121 |
if(!pl) { |
| 124 |
|
|
NSLog(@"Can not create XspfQTComponent."); |
| 125 |
|
|
return NO; |
| 126 |
|
|
} |
| 127 |
masaki |
116 |
[self setPlaylist:pl]; |
| 128 |
masaki |
2 |
|
| 129 |
masaki |
116 |
id t = [self trackList]; |
| 130 |
masaki |
39 |
if(![t title]) { |
| 131 |
masaki |
165 |
[t setTitle:[[[[self fileURL] path] lastPathComponent] stringByDeletingPathExtension]]; |
| 132 |
masaki |
39 |
} |
| 133 |
masaki |
2 |
|
| 134 |
masaki |
121 |
// NSLog(@"open playlist is (%@)%@", NSStringFromClass([[self playlist] class]), [self playlist]); |
| 135 |
|
|
|
| 136 |
masaki |
2 |
return YES; |
| 137 |
|
|
} |
| 138 |
|
|
|
| 139 |
|
|
- (void)dealloc |
| 140 |
|
|
{ |
| 141 |
masaki |
116 |
[playlist release]; |
| 142 |
masaki |
2 |
[playListWindowController release]; |
| 143 |
masaki |
42 |
[movieWindowController release]; |
| 144 |
masaki |
2 |
|
| 145 |
|
|
[super dealloc]; |
| 146 |
|
|
} |
| 147 |
masaki |
71 |
|
| 148 |
masaki |
42 |
- (void)close |
| 149 |
|
|
{ |
| 150 |
masaki |
61 |
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter]; |
| 151 |
masaki |
72 |
[nc postNotificationName:XspfQTDocumentWillCloseNotification object:self]; |
| 152 |
masaki |
61 |
|
| 153 |
masaki |
42 |
[self removeWindowController:playListWindowController]; |
| 154 |
|
|
[playListWindowController release]; |
| 155 |
|
|
playListWindowController = nil; |
| 156 |
|
|
|
| 157 |
|
|
[self removeWindowController:movieWindowController]; |
| 158 |
|
|
[movieWindowController release]; |
| 159 |
|
|
movieWindowController = nil; |
| 160 |
|
|
|
| 161 |
|
|
[super close]; |
| 162 |
|
|
} |
| 163 |
|
|
|
| 164 |
masaki |
33 |
- (IBAction)togglePlayAndPause:(id)sender |
| 165 |
|
|
{ |
| 166 |
|
|
[movieWindowController togglePlayAndPause:sender]; |
| 167 |
|
|
} |
| 168 |
masaki |
2 |
- (IBAction)showPlayList:(id)sender |
| 169 |
|
|
{ |
| 170 |
|
|
[playListWindowController showWindow:self]; |
| 171 |
|
|
} |
| 172 |
|
|
|
| 173 |
masaki |
116 |
- (void)setPlaylist:(XspfQTComponent *)newList |
| 174 |
masaki |
2 |
{ |
| 175 |
masaki |
116 |
if(playlist == newList) return; |
| 176 |
masaki |
2 |
|
| 177 |
masaki |
116 |
[playlist autorelease]; |
| 178 |
|
|
playlist = [newList retain]; |
| 179 |
masaki |
2 |
} |
| 180 |
masaki |
116 |
- (XspfQTComponent *)playlist |
| 181 |
|
|
{ |
| 182 |
|
|
return playlist; |
| 183 |
|
|
} |
| 184 |
|
|
|
| 185 |
masaki |
72 |
- (XspfQTComponent *)trackList |
| 186 |
masaki |
2 |
{ |
| 187 |
masaki |
116 |
return [playlist childAtIndex:0]; |
| 188 |
masaki |
2 |
} |
| 189 |
|
|
|
| 190 |
masaki |
158 |
- (void)setPlayingTrackIndex:(unsigned)index |
| 191 |
masaki |
2 |
{ |
| 192 |
masaki |
29 |
[[self trackList] setSelectionIndex:index]; |
| 193 |
masaki |
2 |
} |
| 194 |
masaki |
31 |
|
| 195 |
|
|
- (NSData *)outputData |
| 196 |
|
|
{ |
| 197 |
|
|
return [[self XMLDocument] XMLDataWithOptions:NSXMLNodePrettyPrint]; |
| 198 |
|
|
} |
| 199 |
masaki |
71 |
- (NSXMLDocument *)XMLDocument |
| 200 |
masaki |
31 |
{ |
| 201 |
masaki |
116 |
id root = [[self playlist] XMLElement]; |
| 202 |
masaki |
31 |
|
| 203 |
|
|
id d = [[[NSXMLDocument alloc] initWithRootElement:root] autorelease]; |
| 204 |
|
|
[d setVersion:@"1.0"]; |
| 205 |
|
|
[d setCharacterEncoding:@"UTF-8"]; |
| 206 |
|
|
|
| 207 |
|
|
return d; |
| 208 |
|
|
} |
| 209 |
masaki |
42 |
|
| 210 |
masaki |
89 |
- (void)insertComponentFromURL:(NSURL *)url atIndex:(NSUInteger)index |
| 211 |
|
|
{ |
| 212 |
|
|
NSString *xmlElem; |
| 213 |
|
|
xmlElem = [NSString stringWithFormat:@"<track><location>%@</location></track>", |
| 214 |
|
|
[url absoluteString]]; |
| 215 |
|
|
|
| 216 |
|
|
NSError *error = nil; |
| 217 |
masaki |
117 |
id new = [XspfQTComponent xspfComponentWithXMLElementString:xmlElem |
| 218 |
|
|
error:&error]; |
| 219 |
masaki |
89 |
if(error) { |
| 220 |
|
|
NSLog(@"%@", error); |
| 221 |
|
|
@throw self; |
| 222 |
|
|
} |
| 223 |
|
|
|
| 224 |
|
|
if(!new) { |
| 225 |
|
|
@throw self; |
| 226 |
|
|
} |
| 227 |
|
|
|
| 228 |
|
|
[self insertComponent:new atIndex:index]; |
| 229 |
|
|
} |
| 230 |
masaki |
86 |
- (void)insertComponent:(XspfQTComponent *)item atIndex:(NSUInteger)index |
| 231 |
masaki |
55 |
{ |
| 232 |
masaki |
83 |
id undo = [self undoManager]; |
| 233 |
masaki |
86 |
[undo registerUndoWithTarget:self selector:@selector(removeComponent:) object:item]; |
| 234 |
masaki |
78 |
[[self trackList] insertChild:item atIndex:index]; |
| 235 |
masaki |
55 |
} |
| 236 |
masaki |
86 |
- (void)removeComponent:(XspfQTComponent *)item |
| 237 |
masaki |
55 |
{ |
| 238 |
masaki |
83 |
NSUInteger index = [[self trackList] indexOfChild:item]; |
| 239 |
masaki |
121 |
if(index == NSNotFound) { |
| 240 |
|
|
NSLog(@"Con not found item (%@)", item); |
| 241 |
|
|
return; |
| 242 |
|
|
} |
| 243 |
masaki |
83 |
|
| 244 |
|
|
id undo = [self undoManager]; |
| 245 |
masaki |
86 |
[[undo prepareWithInvocationTarget:self] insertComponent:item atIndex:index]; |
| 246 |
masaki |
55 |
[[self trackList] removeChild:item]; |
| 247 |
|
|
} |
| 248 |
|
|
|
| 249 |
masaki |
141 |
- (NSData *)dataFromURL:(NSURL *)url error:(NSError **)outError |
| 250 |
|
|
{ |
| 251 |
|
|
NSURLRequest *req = [NSURLRequest requestWithURL:url]; |
| 252 |
|
|
NSURLResponse *res = nil; |
| 253 |
|
|
NSError *err = nil; |
| 254 |
|
|
NSData *data = [NSURLConnection sendSynchronousRequest:req |
| 255 |
|
|
returningResponse:&res |
| 256 |
|
|
error:&err]; |
| 257 |
|
|
if(err) { |
| 258 |
|
|
if(outError) { |
| 259 |
|
|
*outError = err; |
| 260 |
|
|
} |
| 261 |
|
|
return nil; |
| 262 |
|
|
} |
| 263 |
|
|
|
| 264 |
|
|
return data; |
| 265 |
|
|
} |
| 266 |
|
|
|
| 267 |
masaki |
31 |
- (IBAction)dump:(id)sender |
| 268 |
|
|
{ |
| 269 |
|
|
NSString *s = [[[NSString alloc] initWithData:[self outputData] |
| 270 |
|
|
encoding:NSUTF8StringEncoding] autorelease]; |
| 271 |
|
|
|
| 272 |
|
|
NSLog(@"%@", s); |
| 273 |
|
|
} |
| 274 |
masaki |
2 |
@end |
| 275 |
|
|
|