| 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 |
|
|
- (void)setTrackList:(XspfQTComponent *)newList; |
| 16 |
|
|
- (XspfQTComponent *)trackList; |
| 17 |
masaki |
31 |
- (NSXMLDocument *)XMLDocument; |
| 18 |
|
|
- (NSData *)outputData; |
| 19 |
masaki |
2 |
@end |
| 20 |
|
|
|
| 21 |
masaki |
72 |
@implementation XspfQTDocument |
| 22 |
masaki |
2 |
|
| 23 |
masaki |
72 |
NSString *XspfQTDocumentWillCloseNotification = @"XspfQTDocumentWillCloseNotification"; |
| 24 |
masaki |
61 |
|
| 25 |
|
|
|
| 26 |
masaki |
2 |
- (id)init |
| 27 |
|
|
{ |
| 28 |
|
|
self = [super init]; |
| 29 |
|
|
if (self) { |
| 30 |
|
|
|
| 31 |
|
|
// Add your subclass-specific initialization here. |
| 32 |
|
|
// If an error occurs here, send a [self release] message and return nil. |
| 33 |
|
|
|
| 34 |
|
|
} |
| 35 |
|
|
return self; |
| 36 |
|
|
} |
| 37 |
masaki |
95 |
- (id)initWithType:(NSString *)typeName error:(NSError **)outError |
| 38 |
|
|
{ |
| 39 |
|
|
[self init]; |
| 40 |
|
|
|
| 41 |
|
|
NSString *xmlElem; |
| 42 |
|
|
xmlElem = [NSString stringWithString:@"<trackList></trackList>"]; |
| 43 |
|
|
|
| 44 |
|
|
NSError *error = nil; |
| 45 |
|
|
NSXMLElement *element = [[[NSXMLElement alloc] initWithXMLString:xmlElem error:&error] autorelease]; |
| 46 |
|
|
if(error) { |
| 47 |
|
|
if(outError) { |
| 48 |
|
|
*outError = error; |
| 49 |
|
|
} |
| 50 |
|
|
[self autorelease]; |
| 51 |
|
|
return nil; |
| 52 |
|
|
} |
| 53 |
|
|
|
| 54 |
|
|
id new = [XspfQTComponent xspfComponemtWithXMLElement:element]; |
| 55 |
|
|
if(!new) { |
| 56 |
|
|
[self autorelease]; |
| 57 |
|
|
return nil; |
| 58 |
|
|
} |
| 59 |
|
|
|
| 60 |
|
|
[new setTitle:@"Untitled"]; |
| 61 |
|
|
[self setTrackList:new]; |
| 62 |
|
|
|
| 63 |
|
|
return self; |
| 64 |
|
|
} |
| 65 |
masaki |
2 |
|
| 66 |
|
|
- (void)makeWindowControllers |
| 67 |
|
|
{ |
| 68 |
masaki |
72 |
playListWindowController = [[XspfQTPlayListWindowController alloc] init]; |
| 69 |
masaki |
2 |
[self addWindowController:playListWindowController]; |
| 70 |
|
|
|
| 71 |
masaki |
72 |
movieWindowController = [[XspfQTMovieWindowController alloc] init]; |
| 72 |
masaki |
42 |
[movieWindowController setShouldCloseDocument:YES]; |
| 73 |
masaki |
2 |
[self addWindowController:movieWindowController]; |
| 74 |
|
|
[movieWindowController setQtMovie:[[self trackList] qtMovie]]; |
| 75 |
|
|
} |
| 76 |
|
|
|
| 77 |
|
|
- (void)windowControllerDidLoadNib:(NSWindowController *)windowController |
| 78 |
|
|
{ |
| 79 |
|
|
[super windowControllerDidLoadNib:windowController]; |
| 80 |
|
|
// Add any code here that needs to be executed once the windowController has loaded the document's window. |
| 81 |
|
|
} |
| 82 |
|
|
|
| 83 |
|
|
- (NSData *)dataOfType:(NSString *)typeName error:(NSError **)outError |
| 84 |
|
|
{ |
| 85 |
|
|
// Insert code here to write your document to data of the specified type. If the given outError != NULL, ensure that you set *outError when returning nil. |
| 86 |
|
|
|
| 87 |
|
|
// You can also choose to override -fileWrapperOfType:error:, -writeToURL:ofType:error:, or -writeToURL:ofType:forSaveOperation:originalContentsURL:error: instead. |
| 88 |
|
|
|
| 89 |
|
|
// For applications targeted for Panther or earlier systems, you should use the deprecated API -dataRepresentationOfType:. In this case you can also choose to override -fileWrapperRepresentationOfType: or -writeToFile:ofType: instead. |
| 90 |
masaki |
31 |
|
| 91 |
|
|
return [self outputData]; |
| 92 |
|
|
// |
| 93 |
|
|
// |
| 94 |
|
|
// |
| 95 |
|
|
|
| 96 |
masaki |
2 |
if ( outError != NULL ) { |
| 97 |
|
|
*outError = [NSError errorWithDomain:NSOSStatusErrorDomain code:unimpErr userInfo:NULL]; |
| 98 |
|
|
} |
| 99 |
|
|
return nil; |
| 100 |
|
|
} |
| 101 |
|
|
|
| 102 |
|
|
- (BOOL)readFromData:(NSData *)data ofType:(NSString *)typeName error:(NSError **)outError |
| 103 |
|
|
{ |
| 104 |
|
|
// Insert code here to read your document from the given data of the specified type. If the given outError != NULL, ensure that you set *outError when returning NO. |
| 105 |
|
|
|
| 106 |
|
|
// You can also choose to override -readFromFileWrapper:ofType:error: or -readFromURL:ofType:error: instead. |
| 107 |
|
|
|
| 108 |
|
|
// For applications targeted for Panther or earlier systems, you should use the deprecated API -loadDataRepresentation:ofType. In this case you can also choose to override -readFromFile:ofType: or -loadFileWrapperRepresentation:ofType: instead. |
| 109 |
|
|
|
| 110 |
|
|
NSError *error = nil; |
| 111 |
masaki |
10 |
NSXMLDocument *d = [[[NSXMLDocument alloc] initWithData:data |
| 112 |
|
|
options:0 |
| 113 |
|
|
error:&error] autorelease]; |
| 114 |
masaki |
2 |
NSXMLElement *root = [d rootElement]; |
| 115 |
|
|
|
| 116 |
|
|
NSArray *trackListElems; |
| 117 |
masaki |
14 |
trackListElems = [root elementsForName:@"trackList"]; |
| 118 |
|
|
if(!trackListElems || [trackListElems count] < 1) { |
| 119 |
masaki |
2 |
if ( outError != NULL ) { |
| 120 |
|
|
*outError = [NSError errorWithDomain:NSOSStatusErrorDomain code:unimpErr userInfo:NULL]; |
| 121 |
|
|
} |
| 122 |
|
|
return NO; |
| 123 |
|
|
} |
| 124 |
|
|
|
| 125 |
masaki |
72 |
id t = [XspfQTComponent xspfComponemtWithXMLElement:[trackListElems objectAtIndex:0]]; |
| 126 |
masaki |
39 |
if(![t title]) { |
| 127 |
|
|
[t setTitle:[[[self fileURL] path] lastPathComponent]]; |
| 128 |
|
|
} |
| 129 |
masaki |
71 |
[self setTrackList:t]; |
| 130 |
masaki |
2 |
|
| 131 |
|
|
return YES; |
| 132 |
|
|
} |
| 133 |
|
|
|
| 134 |
|
|
- (void)dealloc |
| 135 |
|
|
{ |
| 136 |
|
|
[trackList release]; |
| 137 |
|
|
[playListWindowController release]; |
| 138 |
masaki |
42 |
[movieWindowController release]; |
| 139 |
masaki |
2 |
|
| 140 |
|
|
[super dealloc]; |
| 141 |
|
|
} |
| 142 |
masaki |
71 |
|
| 143 |
masaki |
42 |
- (void)close |
| 144 |
|
|
{ |
| 145 |
masaki |
61 |
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter]; |
| 146 |
masaki |
72 |
[nc postNotificationName:XspfQTDocumentWillCloseNotification object:self]; |
| 147 |
masaki |
61 |
|
| 148 |
masaki |
42 |
[self removeWindowController:playListWindowController]; |
| 149 |
|
|
[playListWindowController release]; |
| 150 |
|
|
playListWindowController = nil; |
| 151 |
|
|
|
| 152 |
|
|
[self removeWindowController:movieWindowController]; |
| 153 |
|
|
[movieWindowController release]; |
| 154 |
|
|
movieWindowController = nil; |
| 155 |
|
|
|
| 156 |
|
|
[super close]; |
| 157 |
|
|
} |
| 158 |
|
|
|
| 159 |
masaki |
33 |
- (IBAction)togglePlayAndPause:(id)sender |
| 160 |
|
|
{ |
| 161 |
|
|
[movieWindowController togglePlayAndPause:sender]; |
| 162 |
|
|
} |
| 163 |
masaki |
2 |
- (IBAction)showPlayList:(id)sender |
| 164 |
|
|
{ |
| 165 |
|
|
[playListWindowController showWindow:self]; |
| 166 |
|
|
} |
| 167 |
|
|
|
| 168 |
masaki |
72 |
- (void)setTrackList:(XspfQTComponent *)newList |
| 169 |
masaki |
2 |
{ |
| 170 |
|
|
if(trackList == newList) return; |
| 171 |
|
|
|
| 172 |
|
|
[trackList autorelease]; |
| 173 |
|
|
trackList = [newList retain]; |
| 174 |
|
|
} |
| 175 |
masaki |
72 |
- (XspfQTComponent *)trackList |
| 176 |
masaki |
2 |
{ |
| 177 |
|
|
return trackList; |
| 178 |
|
|
} |
| 179 |
|
|
|
| 180 |
|
|
- (void)setPlayTrackindex:(unsigned)index |
| 181 |
|
|
{ |
| 182 |
masaki |
29 |
[[self trackList] setSelectionIndex:index]; |
| 183 |
masaki |
2 |
} |
| 184 |
masaki |
31 |
|
| 185 |
|
|
- (NSData *)outputData |
| 186 |
|
|
{ |
| 187 |
|
|
return [[self XMLDocument] XMLDataWithOptions:NSXMLNodePrettyPrint]; |
| 188 |
|
|
} |
| 189 |
masaki |
71 |
- (NSXMLDocument *)XMLDocument |
| 190 |
masaki |
31 |
{ |
| 191 |
|
|
id element = [[self trackList] XMLElement]; |
| 192 |
|
|
|
| 193 |
|
|
id root = [NSXMLElement elementWithName:@"playlist"]; |
| 194 |
|
|
[root addChild:element]; |
| 195 |
|
|
[root addAttribute:[NSXMLNode attributeWithName:@"version" |
| 196 |
|
|
stringValue:@"0"]]; |
| 197 |
|
|
[root addAttribute:[NSXMLNode attributeWithName:@"xmlns" |
| 198 |
|
|
stringValue:@"http://xspf.org/ns/0/"]]; |
| 199 |
|
|
|
| 200 |
|
|
|
| 201 |
|
|
id d = [[[NSXMLDocument alloc] initWithRootElement:root] autorelease]; |
| 202 |
|
|
[d setVersion:@"1.0"]; |
| 203 |
|
|
[d setCharacterEncoding:@"UTF-8"]; |
| 204 |
|
|
|
| 205 |
|
|
return d; |
| 206 |
|
|
} |
| 207 |
masaki |
42 |
|
| 208 |
masaki |
89 |
- (void)insertComponentFromURL:(NSURL *)url atIndex:(NSUInteger)index |
| 209 |
|
|
{ |
| 210 |
|
|
NSString *xmlElem; |
| 211 |
|
|
xmlElem = [NSString stringWithFormat:@"<track><location>%@</location></track>", |
| 212 |
|
|
[url absoluteString]]; |
| 213 |
|
|
|
| 214 |
|
|
NSError *error = nil; |
| 215 |
|
|
NSXMLElement *element = [[[NSXMLElement alloc] initWithXMLString:xmlElem error:&error] autorelease]; |
| 216 |
|
|
if(error) { |
| 217 |
|
|
NSLog(@"%@", error); |
| 218 |
|
|
@throw self; |
| 219 |
|
|
} |
| 220 |
|
|
|
| 221 |
|
|
id new = [XspfQTComponent xspfComponemtWithXMLElement:element]; |
| 222 |
|
|
if(!new) { |
| 223 |
|
|
@throw self; |
| 224 |
|
|
} |
| 225 |
|
|
|
| 226 |
|
|
[self insertComponent:new atIndex:index]; |
| 227 |
|
|
} |
| 228 |
masaki |
86 |
- (void)insertComponent:(XspfQTComponent *)item atIndex:(NSUInteger)index |
| 229 |
masaki |
55 |
{ |
| 230 |
masaki |
83 |
id undo = [self undoManager]; |
| 231 |
masaki |
86 |
[undo registerUndoWithTarget:self selector:@selector(removeComponent:) object:item]; |
| 232 |
masaki |
78 |
[[self trackList] insertChild:item atIndex:index]; |
| 233 |
masaki |
55 |
} |
| 234 |
masaki |
86 |
- (void)removeComponent:(XspfQTComponent *)item |
| 235 |
masaki |
55 |
{ |
| 236 |
masaki |
83 |
NSUInteger index = [[self trackList] indexOfChild:item]; |
| 237 |
|
|
if(index == NSNotFound) return; |
| 238 |
|
|
|
| 239 |
|
|
id undo = [self undoManager]; |
| 240 |
masaki |
86 |
[[undo prepareWithInvocationTarget:self] insertComponent:item atIndex:index]; |
| 241 |
masaki |
55 |
[[self trackList] removeChild:item]; |
| 242 |
|
|
} |
| 243 |
|
|
|
| 244 |
masaki |
31 |
- (IBAction)dump:(id)sender |
| 245 |
|
|
{ |
| 246 |
|
|
NSString *s = [[[NSString alloc] initWithData:[self outputData] |
| 247 |
|
|
encoding:NSUTF8StringEncoding] autorelease]; |
| 248 |
|
|
|
| 249 |
|
|
NSLog(@"%@", s); |
| 250 |
|
|
} |
| 251 |
masaki |
2 |
@end |
| 252 |
|
|
|