| 1 |
masaki |
2 |
// |
| 2 |
|
|
// MyDocument.m |
| 3 |
|
|
// XspfQT |
| 4 |
|
|
// |
| 5 |
|
|
// Created by Hori,Masaki on 08/08/29. |
| 6 |
|
|
// Copyright masakih 2008 . All rights reserved. |
| 7 |
|
|
// |
| 8 |
|
|
|
| 9 |
|
|
#import "XspfDocument.h" |
| 10 |
masaki |
29 |
#import "XspfComponent.h" |
| 11 |
masaki |
2 |
#import "XspfMovieWindowController.h" |
| 12 |
|
|
#import "XspfPlayListWindowController.h" |
| 13 |
|
|
|
| 14 |
|
|
@interface XspfDocument (Private) |
| 15 |
masaki |
29 |
- (void)setTrackList:(XspfComponent *)newList; |
| 16 |
|
|
- (XspfComponent *)trackList; |
| 17 |
masaki |
31 |
- (NSXMLDocument *)XMLDocument; |
| 18 |
|
|
- (NSData *)outputData; |
| 19 |
masaki |
2 |
@end |
| 20 |
|
|
|
| 21 |
|
|
@implementation XspfDocument |
| 22 |
|
|
|
| 23 |
|
|
- (id)init |
| 24 |
|
|
{ |
| 25 |
|
|
self = [super init]; |
| 26 |
|
|
if (self) { |
| 27 |
|
|
|
| 28 |
|
|
// Add your subclass-specific initialization here. |
| 29 |
|
|
// If an error occurs here, send a [self release] message and return nil. |
| 30 |
|
|
|
| 31 |
|
|
} |
| 32 |
|
|
return self; |
| 33 |
|
|
} |
| 34 |
|
|
|
| 35 |
|
|
- (void)makeWindowControllers |
| 36 |
|
|
{ |
| 37 |
|
|
playListWindowController = [[XspfPlayListWindowController alloc] init]; |
| 38 |
|
|
[self addWindowController:playListWindowController]; |
| 39 |
|
|
|
| 40 |
masaki |
33 |
movieWindowController = [[XspfMovieWindowController alloc] init]; |
| 41 |
masaki |
42 |
[movieWindowController setShouldCloseDocument:YES]; |
| 42 |
masaki |
2 |
[self addWindowController:movieWindowController]; |
| 43 |
|
|
[movieWindowController setQtMovie:[[self trackList] qtMovie]]; |
| 44 |
|
|
} |
| 45 |
|
|
//- (NSString *)windowNibName |
| 46 |
|
|
//{ |
| 47 |
|
|
// // Override returning the nib file name of the document |
| 48 |
|
|
// // If you need to use a subclass of NSWindowController or if your document supports multiple NSWindowControllers, you should remove this method and override -makeWindowControllers instead. |
| 49 |
|
|
// return @"MyDocument"; |
| 50 |
|
|
//} |
| 51 |
|
|
|
| 52 |
|
|
- (void)windowControllerDidLoadNib:(NSWindowController *)windowController |
| 53 |
|
|
{ |
| 54 |
|
|
[super windowControllerDidLoadNib:windowController]; |
| 55 |
|
|
// Add any code here that needs to be executed once the windowController has loaded the document's window. |
| 56 |
|
|
// [self setQtMovie:[[self trackList] qtMovie]]; |
| 57 |
|
|
} |
| 58 |
|
|
|
| 59 |
|
|
- (NSData *)dataOfType:(NSString *)typeName error:(NSError **)outError |
| 60 |
|
|
{ |
| 61 |
|
|
// 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. |
| 62 |
|
|
|
| 63 |
|
|
// You can also choose to override -fileWrapperOfType:error:, -writeToURL:ofType:error:, or -writeToURL:ofType:forSaveOperation:originalContentsURL:error: instead. |
| 64 |
|
|
|
| 65 |
|
|
// 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. |
| 66 |
masaki |
31 |
|
| 67 |
|
|
return [self outputData]; |
| 68 |
|
|
// |
| 69 |
|
|
// |
| 70 |
|
|
// |
| 71 |
|
|
|
| 72 |
masaki |
2 |
if ( outError != NULL ) { |
| 73 |
|
|
*outError = [NSError errorWithDomain:NSOSStatusErrorDomain code:unimpErr userInfo:NULL]; |
| 74 |
|
|
} |
| 75 |
|
|
return nil; |
| 76 |
|
|
} |
| 77 |
|
|
|
| 78 |
|
|
- (BOOL)readFromData:(NSData *)data ofType:(NSString *)typeName error:(NSError **)outError |
| 79 |
|
|
{ |
| 80 |
|
|
// 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. |
| 81 |
|
|
|
| 82 |
|
|
// You can also choose to override -readFromFileWrapper:ofType:error: or -readFromURL:ofType:error: instead. |
| 83 |
|
|
|
| 84 |
|
|
// 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. |
| 85 |
|
|
|
| 86 |
|
|
NSError *error = nil; |
| 87 |
masaki |
10 |
NSXMLDocument *d = [[[NSXMLDocument alloc] initWithData:data |
| 88 |
|
|
options:0 |
| 89 |
|
|
error:&error] autorelease]; |
| 90 |
masaki |
2 |
NSXMLElement *root = [d rootElement]; |
| 91 |
|
|
|
| 92 |
|
|
NSArray *trackListElems; |
| 93 |
masaki |
14 |
trackListElems = [root elementsForName:@"trackList"]; |
| 94 |
|
|
if(!trackListElems || [trackListElems count] < 1) { |
| 95 |
masaki |
2 |
if ( outError != NULL ) { |
| 96 |
|
|
*outError = [NSError errorWithDomain:NSOSStatusErrorDomain code:unimpErr userInfo:NULL]; |
| 97 |
|
|
} |
| 98 |
|
|
return NO; |
| 99 |
|
|
} |
| 100 |
|
|
|
| 101 |
masaki |
29 |
id t = [XspfComponent xspfComponemtWithXMLElement:[trackListElems objectAtIndex:0]]; |
| 102 |
masaki |
39 |
if(![t title]) { |
| 103 |
|
|
[t setTitle:[[[self fileURL] path] lastPathComponent]]; |
| 104 |
|
|
} |
| 105 |
masaki |
2 |
[self setTrackList:t]; |
| 106 |
|
|
// NSLog(@"trackList -> %@", trackList); |
| 107 |
|
|
|
| 108 |
|
|
// [self setQtMovie:[[self trackList] qtMovie]]; |
| 109 |
|
|
|
| 110 |
|
|
return YES; |
| 111 |
|
|
} |
| 112 |
|
|
|
| 113 |
|
|
- (void)dealloc |
| 114 |
|
|
{ |
| 115 |
|
|
[trackList release]; |
| 116 |
|
|
[playListWindowController release]; |
| 117 |
masaki |
42 |
[movieWindowController release]; |
| 118 |
masaki |
2 |
|
| 119 |
|
|
[super dealloc]; |
| 120 |
|
|
} |
| 121 |
|
|
- (NSString *)displayName |
| 122 |
|
|
{ |
| 123 |
|
|
NSString *trackTitle = [[[self trackList] currentTrack] title]; |
| 124 |
|
|
if(trackTitle) { |
| 125 |
|
|
return [NSString stringWithFormat:@"%@ - %@", |
| 126 |
|
|
[super displayName], trackTitle]; |
| 127 |
|
|
} |
| 128 |
|
|
|
| 129 |
|
|
return [super displayName]; |
| 130 |
|
|
} |
| 131 |
masaki |
42 |
- (void)close |
| 132 |
|
|
{ |
| 133 |
|
|
[self removeWindowController:playListWindowController]; |
| 134 |
|
|
[playListWindowController release]; |
| 135 |
|
|
playListWindowController = nil; |
| 136 |
|
|
|
| 137 |
|
|
[self removeWindowController:movieWindowController]; |
| 138 |
|
|
[movieWindowController release]; |
| 139 |
|
|
movieWindowController = nil; |
| 140 |
|
|
|
| 141 |
|
|
[super close]; |
| 142 |
|
|
} |
| 143 |
|
|
|
| 144 |
masaki |
33 |
- (IBAction)togglePlayAndPause:(id)sender |
| 145 |
|
|
{ |
| 146 |
|
|
[movieWindowController togglePlayAndPause:sender]; |
| 147 |
|
|
} |
| 148 |
masaki |
2 |
- (IBAction)showPlayList:(id)sender |
| 149 |
|
|
{ |
| 150 |
|
|
[playListWindowController showWindow:self]; |
| 151 |
|
|
} |
| 152 |
|
|
|
| 153 |
masaki |
29 |
- (void)setTrackList:(XspfComponent *)newList |
| 154 |
masaki |
2 |
{ |
| 155 |
|
|
if(trackList == newList) return; |
| 156 |
|
|
|
| 157 |
|
|
[trackList autorelease]; |
| 158 |
|
|
trackList = [newList retain]; |
| 159 |
|
|
} |
| 160 |
masaki |
29 |
- (XspfComponent *)trackList |
| 161 |
masaki |
2 |
{ |
| 162 |
|
|
return trackList; |
| 163 |
|
|
} |
| 164 |
|
|
|
| 165 |
|
|
- (void)setPlayTrackindex:(unsigned)index |
| 166 |
|
|
{ |
| 167 |
masaki |
29 |
[[self trackList] setSelectionIndex:index]; |
| 168 |
masaki |
2 |
} |
| 169 |
masaki |
31 |
|
| 170 |
|
|
- (NSData *)outputData |
| 171 |
|
|
{ |
| 172 |
|
|
return [[self XMLDocument] XMLDataWithOptions:NSXMLNodePrettyPrint]; |
| 173 |
|
|
} |
| 174 |
|
|
- (NSXMLDocument *)XMLDocument; |
| 175 |
|
|
{ |
| 176 |
|
|
id element = [[self trackList] XMLElement]; |
| 177 |
|
|
|
| 178 |
|
|
id root = [NSXMLElement elementWithName:@"playlist"]; |
| 179 |
|
|
[root addChild:element]; |
| 180 |
|
|
[root addAttribute:[NSXMLNode attributeWithName:@"version" |
| 181 |
|
|
stringValue:@"0"]]; |
| 182 |
|
|
[root addAttribute:[NSXMLNode attributeWithName:@"xmlns" |
| 183 |
|
|
stringValue:@"http://xspf.org/ns/0/"]]; |
| 184 |
|
|
|
| 185 |
|
|
|
| 186 |
|
|
id d = [[[NSXMLDocument alloc] initWithRootElement:root] autorelease]; |
| 187 |
|
|
[d setVersion:@"1.0"]; |
| 188 |
|
|
[d setCharacterEncoding:@"UTF-8"]; |
| 189 |
|
|
|
| 190 |
|
|
return d; |
| 191 |
|
|
} |
| 192 |
masaki |
42 |
|
| 193 |
masaki |
55 |
- (void)insertItem:(XspfComponent *)item atIndex:(NSInteger)index |
| 194 |
|
|
{ |
| 195 |
|
|
// |
| 196 |
|
|
} |
| 197 |
|
|
- (void)removeItem:(XspfComponent *)item |
| 198 |
|
|
{ |
| 199 |
|
|
[movieWindowController stop]; |
| 200 |
|
|
[[self trackList] removeChild:item]; |
| 201 |
|
|
} |
| 202 |
|
|
|
| 203 |
masaki |
31 |
- (IBAction)dump:(id)sender |
| 204 |
|
|
{ |
| 205 |
|
|
NSString *s = [[[NSString alloc] initWithData:[self outputData] |
| 206 |
|
|
encoding:NSUTF8StringEncoding] autorelease]; |
| 207 |
|
|
|
| 208 |
|
|
NSLog(@"%@", s); |
| 209 |
|
|
} |
| 210 |
masaki |
2 |
@end |
| 211 |
|
|
|