| 1 | // |
|---|
| 2 | // SapphireMediaPreview.m |
|---|
| 3 | // Sapphire |
|---|
| 4 | // |
|---|
| 5 | // Created by Graham Booker on 6/26/07. |
|---|
| 6 | // Copyright 2007 __MyCompanyName__. All rights reserved. |
|---|
| 7 | // |
|---|
| 8 | |
|---|
| 9 | #import "SapphireMediaPreview.h" |
|---|
| 10 | #import "SapphireMetaData.h" |
|---|
| 11 | #import "SapphireMedia.h" |
|---|
| 12 | |
|---|
| 13 | @interface BRMetadataLayer (protectedAccess) |
|---|
| 14 | - (NSArray *)gimmieMetadataObjs; |
|---|
| 15 | @end |
|---|
| 16 | |
|---|
| 17 | @implementation BRMetadataLayer (protectedAccess) |
|---|
| 18 | - (NSArray *)gimmieMetadataObjs |
|---|
| 19 | { |
|---|
| 20 | return _metadataObjs; |
|---|
| 21 | } |
|---|
| 22 | @end |
|---|
| 23 | |
|---|
| 24 | @implementation SapphireMediaPreview |
|---|
| 25 | |
|---|
| 26 | static NSSet *coverArtExtentions = nil; |
|---|
| 27 | |
|---|
| 28 | + (void)initialize |
|---|
| 29 | { |
|---|
| 30 | coverArtExtentions = [[NSSet alloc] initWithObjects: |
|---|
| 31 | @"jpg", |
|---|
| 32 | @"tif", |
|---|
| 33 | @"tiff", |
|---|
| 34 | @"png", |
|---|
| 35 | nil]; |
|---|
| 36 | } |
|---|
| 37 | |
|---|
| 38 | - (id) initWithScene: (BRRenderScene *) scene |
|---|
| 39 | { |
|---|
| 40 | self = [super initWithScene:scene]; |
|---|
| 41 | if(!self) |
|---|
| 42 | return nil; |
|---|
| 43 | |
|---|
| 44 | return self; |
|---|
| 45 | } |
|---|
| 46 | |
|---|
| 47 | - (void)dealloc |
|---|
| 48 | { |
|---|
| 49 | [meta release]; |
|---|
| 50 | [super dealloc]; |
|---|
| 51 | } |
|---|
| 52 | |
|---|
| 53 | - (void)setMetaData:(SapphireFileMetaData *)newMeta |
|---|
| 54 | { |
|---|
| 55 | [meta release]; |
|---|
| 56 | meta = [newMeta retain]; |
|---|
| 57 | NSURL *url = [NSURL fileURLWithPath:[meta path]]; |
|---|
| 58 | SapphireMedia *asset =[[SapphireMedia alloc] initWithMediaURL:url]; |
|---|
| 59 | [self setAsset:asset]; |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | - (NSString *)coverArtForPath |
|---|
| 63 | { |
|---|
| 64 | NSString *subPath = [[meta path] stringByDeletingPathExtension]; |
|---|
| 65 | NSFileManager *fm = [NSFileManager defaultManager]; |
|---|
| 66 | |
|---|
| 67 | BOOL isDir = NO; |
|---|
| 68 | NSEnumerator *extEnum = [coverArtExtentions objectEnumerator]; |
|---|
| 69 | NSString *ext = nil; |
|---|
| 70 | while((ext = [extEnum nextObject]) != nil) |
|---|
| 71 | { |
|---|
| 72 | NSString *candidate = [subPath stringByAppendingPathExtension:ext]; |
|---|
| 73 | if([fm fileExistsAtPath:candidate isDirectory:&isDir] && !isDir) |
|---|
| 74 | return candidate; |
|---|
| 75 | } |
|---|
| 76 | return [[[NSBundle bundleForClass:[self class]] bundlePath] stringByAppendingString:@"/Contents/Resources/ApplianceIcon.png"]; |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | - (void)_loadCoverArt |
|---|
| 80 | { |
|---|
| 81 | [super _loadCoverArt]; |
|---|
| 82 | |
|---|
| 83 | if([_coverArtLayer texture] != nil) |
|---|
| 84 | return; |
|---|
| 85 | |
|---|
| 86 | NSString *path = [self coverArtForPath]; |
|---|
| 87 | NSURL *url = [NSURL fileURLWithPath:path]; |
|---|
| 88 | CGImageSourceRef sourceRef = CGImageSourceCreateWithURL((CFURLRef)url, NULL); |
|---|
| 89 | CGImageRef imageRef = nil; |
|---|
| 90 | if(sourceRef) |
|---|
| 91 | { |
|---|
| 92 | imageRef = CGImageSourceCreateImageAtIndex(sourceRef, 0, NULL); |
|---|
| 93 | CFRelease(sourceRef); |
|---|
| 94 | } |
|---|
| 95 | if(imageRef) |
|---|
| 96 | { |
|---|
| 97 | [_coverArtLayer setImage:imageRef]; |
|---|
| 98 | CFRelease(imageRef); |
|---|
| 99 | } |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | - (void)_populateMetadata |
|---|
| 103 | { |
|---|
| 104 | [super _populateMetadata]; |
|---|
| 105 | if([[_metadataLayer gimmieMetadataObjs] count]) |
|---|
| 106 | return; |
|---|
| 107 | NSMutableDictionary *allMeta = [[meta getAllMetaData] mutableCopy]; |
|---|
| 108 | NSString *value = [allMeta objectForKey:META_TITLE_KEY]; |
|---|
| 109 | if(value != nil) |
|---|
| 110 | { |
|---|
| 111 | [_metadataLayer setTitle:value]; |
|---|
| 112 | [allMeta removeObjectForKey:META_TITLE_KEY]; |
|---|
| 113 | } |
|---|
| 114 | value = [allMeta objectForKey:META_RATING_KEY]; |
|---|
| 115 | if(value != nil) |
|---|
| 116 | { |
|---|
| 117 | [_metadataLayer setRating:value]; |
|---|
| 118 | [allMeta removeObjectForKey:META_RATING_KEY]; |
|---|
| 119 | } |
|---|
| 120 | value = [allMeta objectForKey:META_SUMMARY_KEY]; |
|---|
| 121 | if(value != nil) |
|---|
| 122 | { |
|---|
| 123 | [_metadataLayer setSummary:value]; |
|---|
| 124 | [allMeta removeObjectForKey:META_SUMMARY_KEY]; |
|---|
| 125 | } |
|---|
| 126 | value = [allMeta objectForKey:META_COPYRIGHT_KEY]; |
|---|
| 127 | if(value != nil) |
|---|
| 128 | { |
|---|
| 129 | [_metadataLayer setCopyright:value]; |
|---|
| 130 | [allMeta removeObjectForKey:META_COPYRIGHT_KEY]; |
|---|
| 131 | } |
|---|
| 132 | [_metadataLayer setMetadata:[allMeta allValues] withLabels:[allMeta allKeys]]; |
|---|
| 133 | } |
|---|
| 134 | |
|---|
| 135 | - (BOOL)_assetHasMetadata |
|---|
| 136 | { |
|---|
| 137 | return YES; |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | |
|---|
| 141 | @end |
|---|