source: trunk/SapphireMediaPreview.m @ 113

Revision 113, 4.5 KB checked in by pmerrill, 7 years ago (diff)

changed <Cover Art> folder name to Cover Art to support NFS mounts

Line 
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#import "SapphireSettings.h"
13#import <objc/objc-class.h>
14
15@interface BRMetadataLayer (protectedAccess)
16- (NSArray *)gimmieMetadataObjs;
17@end
18
19@implementation BRMetadataLayer (protectedAccess)
20- (NSArray *)gimmieMetadataObjs
21{
22        Class myClass = [self class];
23        Ivar ret = class_getInstanceVariable(myClass,"_metadataLabels");
24       
25        return *(NSArray * *)(((char *)self)+ret->ivar_offset);
26}
27@end
28
29@implementation SapphireMediaPreview
30
31static NSSet *coverArtExtentions = nil;
32
33+ (void)initialize
34{
35        coverArtExtentions = [[NSSet alloc] initWithObjects:
36                @"jpg",
37                @"tif",
38                @"tiff",
39                @"png",
40                nil];
41}
42
43- (id) initWithScene: (BRRenderScene *) scene
44{
45        self = [super initWithScene:scene];
46        if(!self)
47                return nil;
48       
49        return self;
50}
51
52- (void)dealloc
53{
54        [meta release];
55        [super dealloc];
56}
57
58- (void)setMetaData:(SapphireMetaData *)newMeta
59{
60        [meta release];
61        meta = [newMeta retain];
62        NSURL *url = [NSURL fileURLWithPath:[meta path]];
63        SapphireMedia *asset  =[[SapphireMedia alloc] initWithMediaURL:url];
64        [self setAsset:asset];
65}
66
67- (NSString *)searchExtForPath:(NSString *)path
68{
69        NSFileManager *fm = [NSFileManager defaultManager];
70        BOOL isDir = NO;
71        NSEnumerator *extEnum = [coverArtExtentions objectEnumerator];
72        NSString *ext = nil;
73        while((ext = [extEnum nextObject]) != nil)
74        {
75                NSString *candidate = [path stringByAppendingPathExtension:ext];
76                if([fm fileExistsAtPath:candidate isDirectory:&isDir] && !isDir)
77                        return candidate;
78        }
79        return nil;
80}
81
82- (NSString *)coverArtForDir:(NSString *)dir parents:(int)parents
83{
84        NSString *ret = [self searchExtForPath:[dir stringByAppendingPathComponent:@"Cover Art/cover"]];
85        if(ret != nil)
86                return ret;
87        ret = [self searchExtForPath:[dir stringByAppendingPathComponent:@"cover"]];
88        if(ret != nil)
89                return ret;
90        if(parents != 0)
91                return [self coverArtForDir:[dir stringByDeletingLastPathComponent] parents:parents -1];
92        return nil;
93}
94
95- (NSString *)coverArtForPath
96{
97        if([meta isKindOfClass:[SapphireDirectoryMetaData class]])
98        {
99                NSString *ret = [self coverArtForDir:[meta path] parents:1];
100                if(ret != nil)
101                        return ret;
102                return [[[NSBundle bundleForClass:[self class]] bundlePath] stringByAppendingString:@"/Contents/Resources/DefaultPreview.png"];
103        }
104
105        NSString *subPath = [[meta path] stringByDeletingPathExtension];
106        NSString *fileName = [subPath lastPathComponent];
107        NSString *ret = [self searchExtForPath:[[[subPath stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"Cover Art"] stringByAppendingPathComponent:fileName]];
108        if(ret != nil)
109                return ret;
110       
111        ret = [self searchExtForPath:subPath];
112        if(ret != nil)
113                return ret;
114       
115        ret = [self coverArtForDir:[subPath stringByDeletingLastPathComponent] parents:2];
116        if(ret != nil)
117                return ret;
118       
119        return [[[NSBundle bundleForClass:[self class]] bundlePath] stringByAppendingString:@"/Contents/Resources/DefaultPreview.png"];
120}
121
122- (void)_loadCoverArt
123{
124        [super _loadCoverArt];
125       
126        if([_coverArtLayer texture] != nil)
127                return;
128       
129        NSString *path = [self coverArtForPath];
130        NSURL *url = [NSURL fileURLWithPath:path];
131        CGImageSourceRef sourceRef = CGImageSourceCreateWithURL((CFURLRef)url, NULL);
132        CGImageRef imageRef = nil;
133        if(sourceRef)
134        {
135                imageRef = CGImageSourceCreateImageAtIndex(sourceRef, 0, NULL);
136                CFRelease(sourceRef);
137        }
138        if(imageRef)
139        {
140                [_coverArtLayer setImage:imageRef];
141                CFRelease(imageRef);
142        }       
143}
144
145- (void)_populateMetadata
146{
147        [super _populateMetadata];
148        if([[_metadataLayer gimmieMetadataObjs] count])
149                return;
150        NSMutableDictionary *allMeta = [meta getDisplayedMetaData];
151        NSString *value = [allMeta objectForKey:META_TITLE_KEY];
152        if(value != nil)
153        {
154                [_metadataLayer setTitle:value];
155                [allMeta removeObjectForKey:META_TITLE_KEY];
156        }
157        value = [allMeta objectForKey:META_RATING_KEY];
158        if(value != nil)
159        {
160                [_metadataLayer setRating:value];
161                [allMeta removeObjectForKey:META_RATING_KEY];
162        }
163        value = [allMeta objectForKey:META_SUMMARY_KEY];
164        if(value != nil)
165        {
166                if([[SapphireSettings sharedSettings] displaySpoilers])
167                        [_metadataLayer setSummary:value];
168                [allMeta removeObjectForKey:META_SUMMARY_KEY];
169        }
170        value = [allMeta objectForKey:META_COPYRIGHT_KEY];
171        if(value != nil)
172        {
173                [_metadataLayer setCopyright:value];
174                [allMeta removeObjectForKey:META_COPYRIGHT_KEY];
175        }
176        [_metadataLayer setMetadata:[allMeta allValues] withLabels:[allMeta allKeys]];
177}
178
179- (BOOL)_assetHasMetadata
180{
181        return YES;
182}
183
184
185@end
Note: See TracBrowser for help on using the repository browser.