| 1 |
// |
| 2 |
// HMXSPFComponent.m |
| 3 |
// HMXSPFComponent |
| 4 |
// |
| 5 |
// Created by Hori,Masaki on 08/08/29. |
| 6 |
// |
| 7 |
|
| 8 |
/* |
| 9 |
This source code is release under the New BSD License. |
| 10 |
Copyright (c) 2008-2010,2012, masakih |
| 11 |
All rights reserved. |
| 12 |
|
| 13 |
ソースコード形式かバイナリ形式か、変更するかしないかを問わず、以下の条件を満たす場合に |
| 14 |
限り、再頒布および使用が許可されます。 |
| 15 |
|
| 16 |
1, ソースコードを再頒布する場合、上記の著作権表示、本条件一覧、および下記免責条項を含 |
| 17 |
めること。 |
| 18 |
2, バイナリ形式で再頒布する場合、頒布物に付属のドキュメント等の資料に、上記の著作権表 |
| 19 |
示、本条件一覧、および下記免責条項を含めること。 |
| 20 |
3, 書面による特別の許可なしに、本ソフトウェアから派生した製品の宣伝または販売促進に、 |
| 21 |
コントリビューターの名前を使用してはならない。 |
| 22 |
本ソフトウェアは、著作権者およびコントリビューターによって「現状のまま」提供されており、 |
| 23 |
明示黙示を問わず、商業的な使用可能性、および特定の目的に対する適合性に関する暗黙の保証 |
| 24 |
も含め、またそれに限定されない、いかなる保証もありません。著作権者もコントリビューター |
| 25 |
も、事由のいかんを問わず、 損害発生の原因いかんを問わず、かつ責任の根拠が契約であるか |
| 26 |
厳格責任であるか(過失その他の)不法行為であるかを問わず、仮にそのような損害が発生する |
| 27 |
可能性を知らされていたとしても、本ソフトウェアの使用によって発生した(代替品または代用 |
| 28 |
サービスの調達、使用の喪失、データの喪失、利益の喪失、業務の中断も含め、またそれに限定 |
| 29 |
されない)直接損害、間接損害、偶発的な損害、特別損害、懲罰的損害、または結果損害につい |
| 30 |
て、一切責任を負わないものとします。 |
| 31 |
------------------------------------------------------------------- |
| 32 |
Copyright (c) 2008-2010,2012, masakih |
| 33 |
All rights reserved. |
| 34 |
|
| 35 |
Redistribution and use in source and binary forms, with or without |
| 36 |
modification, are permitted provided that the following conditions |
| 37 |
are met: |
| 38 |
|
| 39 |
1, Redistributions of source code must retain the above copyright |
| 40 |
notice, this list of conditions and the following disclaimer. |
| 41 |
2, Redistributions in binary form must reproduce the above copyright |
| 42 |
notice, this list of conditions and the following disclaimer in |
| 43 |
the documentation and/or other materials provided with the |
| 44 |
distribution. |
| 45 |
3, The names of its contributors may be used to endorse or promote |
| 46 |
products derived from this software without specific prior |
| 47 |
written permission. |
| 48 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 49 |
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 50 |
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 51 |
FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 52 |
COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 53 |
INCIDENTAL, SPECIAL,EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 54 |
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 55 |
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 56 |
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 57 |
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
| 58 |
ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 59 |
POSSIBILITY OF SUCH DAMAGE. |
| 60 |
*/ |
| 61 |
|
| 62 |
|
| 63 |
#import "HMXSPFComponent.h" |
| 64 |
#import "HMXSPFPlaceholderComponent.h" |
| 65 |
|
| 66 |
NSString *XspfQTXMLTrackElementName = @"track"; |
| 67 |
NSString *XspfQTXMLTrackListElementName = @"trackList"; |
| 68 |
NSString *XspfQTXMLPlaylistElementName = @"playlist"; |
| 69 |
NSString *XspfQTXMLTitleElementName = @"title"; |
| 70 |
NSString *XspfQTXMLLocationElementName = @"location"; |
| 71 |
NSString *XspfQTXMLDurationElementName = @"duration"; |
| 72 |
|
| 73 |
NSString *XspfQTXMLExtensionElementName = @"extension"; |
| 74 |
NSString *XspfQTXMLApplicationAttributeName = @"application"; |
| 75 |
|
| 76 |
NSString *XspfQTXMLNamespaceseURI = @"http://masakih.com"; |
| 77 |
NSString *XspfQTXMLNamespacesePrefix = @"hm"; |
| 78 |
NSString *XspfQTXMLAliasElement = @"hm:alias"; |
| 79 |
NSString *XspfQTXMLThumbnailElementName = @"hm:thumbnail"; |
| 80 |
NSString *XspfQTXMLThumbnailTrackNumAttributeName = @"trackNumber"; |
| 81 |
NSString *XspfQTXMLThumbnailTimeAttributeName = @"time"; |
| 82 |
|
| 83 |
@interface HMXSPFComponent() |
| 84 |
@property (readwrite) BOOL isSelected; |
| 85 |
@property (readwrite, assign) HMXSPFComponent *parent; |
| 86 |
@end |
| 87 |
|
| 88 |
@implementation HMXSPFComponent |
| 89 |
@synthesize title = _title; |
| 90 |
@synthesize isSelected = _isSelected; |
| 91 |
@synthesize parent = _parent; |
| 92 |
|
| 93 |
static NSString *const XspfQTComponentXMLStringCodingKey = @"XspfQTComponentXMLStringCodingKey"; |
| 94 |
|
| 95 |
+ (id) allocWithZone:(NSZone *) zone |
| 96 |
{ |
| 97 |
if ([self class] == [HMXSPFComponent class]) { |
| 98 |
return [[HMXSPFPlaceholderComponent sharedInstance] retain]; |
| 99 |
} |
| 100 |
|
| 101 |
return [super allocWithZone:zone]; |
| 102 |
} |
| 103 |
|
| 104 |
+ (id)xspfPlaylist |
| 105 |
{ |
| 106 |
return [HMXSPFPlaceholderComponent xspfPlaylist]; |
| 107 |
} |
| 108 |
+ (id)xspfTrackList |
| 109 |
{ |
| 110 |
return [HMXSPFPlaceholderComponent xspfTrackList]; |
| 111 |
} |
| 112 |
+ (id)xspfTrackWithLocation:(NSURL *)location |
| 113 |
{ |
| 114 |
return [HMXSPFPlaceholderComponent xspfTrackWithLocation:location]; |
| 115 |
} |
| 116 |
+ (id)xspfComponentWithXMLElementString:(NSString *)string error:(NSError **)outError |
| 117 |
{ |
| 118 |
return [HMXSPFPlaceholderComponent xspfComponentWithXMLElementString:string error:outError]; |
| 119 |
} |
| 120 |
+ (id)xspfComponentWithContentsOfURL:(NSURL *)url error:(NSError **)error |
| 121 |
{ |
| 122 |
return [[(HMXSPFComponent *)[self alloc] initWithContentsOfURL:url error:error] autorelease]; |
| 123 |
} |
| 124 |
- (id)initWithContentsOfURL:(NSURL *)url error:(NSError **)error |
| 125 |
{ |
| 126 |
NSXMLDocument *doc = [[[NSXMLDocument alloc] initWithContentsOfURL:url |
| 127 |
options:0 |
| 128 |
error:error] autorelease]; |
| 129 |
if(*error) { |
| 130 |
NSLog(@"%@", *error); |
| 131 |
[self release]; |
| 132 |
return nil; |
| 133 |
} |
| 134 |
|
| 135 |
return [self initWithXMLElement:[doc rootElement]]; |
| 136 |
} |
| 137 |
|
| 138 |
+ (id)xspfComponentWithXMLElement:(NSXMLElement *)element |
| 139 |
{ |
| 140 |
return [[[self alloc] initWithXMLElement:element] autorelease]; |
| 141 |
} |
| 142 |
- (id)initWithXMLElement:(NSXMLElement *)element |
| 143 |
{ |
| 144 |
[super init]; |
| 145 |
[self release]; |
| 146 |
|
| 147 |
[self doesNotRecognizeSelector:_cmd]; |
| 148 |
|
| 149 |
return nil; |
| 150 |
} |
| 151 |
- (void)dealloc |
| 152 |
{ |
| 153 |
[_title release]; |
| 154 |
[_selectionIndexPath release]; |
| 155 |
|
| 156 |
[super dealloc]; |
| 157 |
} |
| 158 |
|
| 159 |
- (NSXMLElement *)XMLElement |
| 160 |
{ |
| 161 |
[self doesNotRecognizeSelector:_cmd]; |
| 162 |
|
| 163 |
return nil; |
| 164 |
} |
| 165 |
- (void)setDuration:(NSDate *)duration {} |
| 166 |
- (NSDate *)duration { return nil; } |
| 167 |
- (NSURL *)movieLocation |
| 168 |
{ |
| 169 |
return nil; |
| 170 |
} |
| 171 |
|
| 172 |
- (void)encodeWithCoder:(NSCoder *)aCoder |
| 173 |
{ |
| 174 |
NSString *string = [self.XMLElement XMLString]; |
| 175 |
[aCoder encodeObject:string forKey:XspfQTComponentXMLStringCodingKey]; |
| 176 |
} |
| 177 |
- (id)initWithCoder:(NSCoder *)aDecoder |
| 178 |
{ |
| 179 |
[super init]; |
| 180 |
[self autorelease]; |
| 181 |
|
| 182 |
id string = [aDecoder decodeObjectForKey:XspfQTComponentXMLStringCodingKey]; |
| 183 |
|
| 184 |
NSError *error = nil; |
| 185 |
NSXMLElement *element = [[[NSXMLElement alloc] initWithXMLString:string error:&error] autorelease]; |
| 186 |
if(error) { |
| 187 |
NSLog(@"%@", error); |
| 188 |
return nil; |
| 189 |
} |
| 190 |
|
| 191 |
return [[[self class] alloc] initWithXMLElement:element]; |
| 192 |
} |
| 193 |
|
| 194 |
- (NSUInteger)hash |
| 195 |
{ |
| 196 |
return [self.title hash]; |
| 197 |
} |
| 198 |
- (BOOL)isEqual:(id)other |
| 199 |
{ |
| 200 |
if(![other isMemberOfClass:[self class]]) return NO; |
| 201 |
if(![self.title isEqualToString:[other title]]) return NO; |
| 202 |
|
| 203 |
return YES; |
| 204 |
} |
| 205 |
@end |
| 206 |
|
| 207 |
@implementation HMXSPFComponent(XspfComponentOtherMethods) |
| 208 |
#pragma mark #### XspfComponentSelection #### |
| 209 |
- (void)select |
| 210 |
{ |
| 211 |
self.isSelected = YES; |
| 212 |
} |
| 213 |
- (void)deselect |
| 214 |
{ |
| 215 |
self.isSelected = NO; |
| 216 |
} |
| 217 |
- (void)setSelectionIndexPath:(NSIndexPath *)indexPath |
| 218 |
{ |
| 219 |
NSUInteger length = [indexPath length]; |
| 220 |
if(length == 0) { |
| 221 |
@throw self; |
| 222 |
} |
| 223 |
NSUInteger firstIndex = [indexPath indexAtPosition:0]; |
| 224 |
if(firstIndex > self.childrenCount) { |
| 225 |
@throw self; |
| 226 |
} |
| 227 |
|
| 228 |
HMXSPFComponent *firstIndexedChild = [self childAtIndex:firstIndex]; |
| 229 |
if(length != 1) { |
| 230 |
NSIndexPath *deletedFirstIndex = nil; |
| 231 |
NSUInteger *indexP = NULL; |
| 232 |
@try { |
| 233 |
indexP = calloc(sizeof(NSUInteger), length); |
| 234 |
if(!indexP) { |
| 235 |
[NSException raise:NSMallocException |
| 236 |
format:@"Not enough memory"]; |
| 237 |
} |
| 238 |
[indexPath getIndexes:indexP]; |
| 239 |
deletedFirstIndex = [NSIndexPath indexPathWithIndexes:indexP + 1 |
| 240 |
length:length - 1]; |
| 241 |
} |
| 242 |
@finally{ |
| 243 |
free(indexP); |
| 244 |
} |
| 245 |
if(!deletedFirstIndex) return; |
| 246 |
@try { |
| 247 |
firstIndexedChild.selectionIndexPath = deletedFirstIndex; |
| 248 |
} |
| 249 |
@catch (HMXSPFComponent *me) { |
| 250 |
return; |
| 251 |
} |
| 252 |
} else { |
| 253 |
self.selectionIndex = firstIndex; |
| 254 |
} |
| 255 |
if(!self.isSelected) { |
| 256 |
[self select]; |
| 257 |
} |
| 258 |
[_selectionIndexPath autorelease]; |
| 259 |
_selectionIndexPath = [indexPath retain]; |
| 260 |
} |
| 261 |
- (NSIndexPath *)selectionIndexPath |
| 262 |
{ |
| 263 |
return _selectionIndexPath; |
| 264 |
} |
| 265 |
- (void)setIsPlayed:(BOOL)state {} // do nothing. |
| 266 |
- (BOOL)isPlayed |
| 267 |
{ |
| 268 |
return NO; |
| 269 |
} |
| 270 |
- (HMXSPFComponent *)currentTrack |
| 271 |
{ |
| 272 |
return self; |
| 273 |
} |
| 274 |
- (void)setCurrentTrackDuration:(NSDate *)duration |
| 275 |
{ |
| 276 |
self.currentTrack.duration = duration; |
| 277 |
} |
| 278 |
- (NSDate *)currentTrackDuration |
| 279 |
{ |
| 280 |
return self.currentTrack.duration; |
| 281 |
} |
| 282 |
|
| 283 |
#pragma mark #### XspfConainerComponent #### |
| 284 |
|
| 285 |
- (NSArray *)children |
| 286 |
{ |
| 287 |
return nil; |
| 288 |
} |
| 289 |
- (NSUInteger)childrenCount |
| 290 |
{ |
| 291 |
return [self.children count]; |
| 292 |
} |
| 293 |
- (BOOL)isLeaf |
| 294 |
{ |
| 295 |
return YES; |
| 296 |
} |
| 297 |
- (NSUInteger)indexOfChild:(HMXSPFComponent *)child |
| 298 |
{ |
| 299 |
return [self.children indexOfObject:child]; |
| 300 |
} |
| 301 |
- (HMXSPFComponent *)childAtIndex:(NSUInteger)index |
| 302 |
{ |
| 303 |
return [self.children objectAtIndex:index]; |
| 304 |
} |
| 305 |
|
| 306 |
@end |
| 307 |
|
| 308 |
@implementation HMXSPFComponent(XspfThumbnailSupport) |
| 309 |
- (void)setThumbnailTrackNum:(NSUInteger)trackNum timeIntarval:(NSTimeInterval)timeIntarval; |
| 310 |
{ |
| 311 |
[self.parent setThumbnailTrackNum:trackNum timeIntarval:timeIntarval]; |
| 312 |
} |
| 313 |
- (void)setThumbnailComponent:(HMXSPFComponent *)item timeIntarval:(NSTimeInterval)timeIntarval |
| 314 |
{ |
| 315 |
[self.parent setThumbnailComponent:item timeIntarval:timeIntarval]; |
| 316 |
} |
| 317 |
- (HMXSPFComponent *)thumbnailTrack |
| 318 |
{ |
| 319 |
return self.parent.thumbnailTrack; |
| 320 |
} |
| 321 |
- (NSTimeInterval)thumbnailTimeInterval |
| 322 |
{ |
| 323 |
if(self.parent) { |
| 324 |
return self.parent.thumbnailTimeInterval; |
| 325 |
} |
| 326 |
return DBL_MIN; |
| 327 |
} |
| 328 |
- (void)removeThumbnailFrame |
| 329 |
{ |
| 330 |
[self.parent removeThumbnailFrame]; |
| 331 |
} |
| 332 |
@end |