| 1 | // |
|---|
| 2 | // SapphireMediaPreview.m |
|---|
| 3 | // Sapphire |
|---|
| 4 | // |
|---|
| 5 | // Created by Graham Booker on 6/26/07. |
|---|
| 6 | // Copyright 2007 www.nanopi.net. 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 | /*These interfaces are to access variables not available*/ |
|---|
| 16 | @interface BRMetadataLayer (protectedAccess) |
|---|
| 17 | - (NSArray *)gimmieMetadataObjs; |
|---|
| 18 | @end |
|---|
| 19 | |
|---|
| 20 | @implementation BRMetadataLayer (protectedAccess) |
|---|
| 21 | - (NSArray *)gimmieMetadataObjs |
|---|
| 22 | { |
|---|
| 23 | Class myClass = [self class]; |
|---|
| 24 | Ivar ret = class_getInstanceVariable(myClass,"_metadataLabels"); |
|---|
| 25 | |
|---|
| 26 | return *(NSArray * *)(((char *)self)+ret->ivar_offset); |
|---|
| 27 | } |
|---|
| 28 | @end |
|---|
| 29 | |
|---|
| 30 | @implementation SapphireMediaPreview |
|---|
| 31 | |
|---|
| 32 | /*List of extensions to look for cover art*/ |
|---|
| 33 | static NSSet *coverArtExtentions = nil; |
|---|
| 34 | |
|---|
| 35 | + (void)initialize |
|---|
| 36 | { |
|---|
| 37 | /*Initialize the set of cover art extensions*/ |
|---|
| 38 | coverArtExtentions = [[NSSet alloc] initWithObjects: |
|---|
| 39 | @"jpg", |
|---|
| 40 | @"jpeg", |
|---|
| 41 | @"tif", |
|---|
| 42 | @"tiff", |
|---|
| 43 | @"png", |
|---|
| 44 | @"gif", |
|---|
| 45 | nil]; |
|---|
| 46 | } |
|---|
| 47 | |
|---|
| 48 | - (void)dealloc |
|---|
| 49 | { |
|---|
| 50 | [meta release]; |
|---|
| 51 | [super dealloc]; |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | /*! |
|---|
| 55 | * @brief Set the File information |
|---|
| 56 | * |
|---|
| 57 | * @param newMeta The meta data |
|---|
| 58 | */ |
|---|
| 59 | - (void)setMetaData:(SapphireMetaData *)newMeta |
|---|
| 60 | { |
|---|
| 61 | [meta release]; |
|---|
| 62 | NSString *path = [newMeta path]; |
|---|
| 63 | if(path == nil) |
|---|
| 64 | { |
|---|
| 65 | meta = nil; |
|---|
| 66 | return; |
|---|
| 67 | } |
|---|
| 68 | meta = [newMeta retain]; |
|---|
| 69 | /*Now that we know the file, set the asset*/ |
|---|
| 70 | NSURL *url = [NSURL fileURLWithPath:[meta path]]; |
|---|
| 71 | SapphireMedia *asset =[[SapphireMedia alloc] initWithMediaURL:url]; |
|---|
| 72 | [self setAsset:asset]; |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | /*! |
|---|
| 76 | * @brief Search for cover art in the current path |
|---|
| 77 | * |
|---|
| 78 | * @param path Path to search for cover art, minus the extension |
|---|
| 79 | * @return The path to the found cover art |
|---|
| 80 | */ |
|---|
| 81 | - (NSString *)searchExtForPath:(NSString *)path |
|---|
| 82 | { |
|---|
| 83 | NSFileManager *fm = [NSFileManager defaultManager]; |
|---|
| 84 | BOOL isDir = NO; |
|---|
| 85 | /*Search all extensions*/ |
|---|
| 86 | NSEnumerator *extEnum = [coverArtExtentions objectEnumerator]; |
|---|
| 87 | NSString *ext = nil; |
|---|
| 88 | while((ext = [extEnum nextObject]) != nil) |
|---|
| 89 | { |
|---|
| 90 | NSString *candidate = [path stringByAppendingPathExtension:ext]; |
|---|
| 91 | /*Check the candidate*/ |
|---|
| 92 | if([fm fileExistsAtPath:candidate isDirectory:&isDir] && !isDir) |
|---|
| 93 | return candidate; |
|---|
| 94 | } |
|---|
| 95 | /*Didn't find one*/ |
|---|
| 96 | return nil; |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | /*! |
|---|
| 100 | * @brief Searches for the directory's cover art |
|---|
| 101 | * |
|---|
| 102 | * @param dir the directory to search |
|---|
| 103 | * @param parents the number of parent directories to search after this one |
|---|
| 104 | * @return The path to the found cover art |
|---|
| 105 | */ |
|---|
| 106 | - (NSString *)coverArtForDir:(NSString *)dir parents:(int)parents |
|---|
| 107 | { |
|---|
| 108 | /*Check for cover.ext in the "Cover Art" dir*/ |
|---|
| 109 | NSString *ret = [self searchExtForPath:[dir stringByAppendingPathComponent:@"Cover Art/cover"]]; |
|---|
| 110 | if(ret != nil) |
|---|
| 111 | return ret; |
|---|
| 112 | /*Next check for cover.ext in the current dir*/ |
|---|
| 113 | ret = [self searchExtForPath:[dir stringByAppendingPathComponent:@"cover"]]; |
|---|
| 114 | if(ret != nil) |
|---|
| 115 | return ret; |
|---|
| 116 | /*Finally, try going up a dir*/ |
|---|
| 117 | if(parents != 0) |
|---|
| 118 | return [self coverArtForDir:[dir stringByDeletingLastPathComponent] parents:parents -1]; |
|---|
| 119 | /*Didn't find one*/ |
|---|
| 120 | return nil; |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | /*! |
|---|
| 124 | * @brief Search for cover art for the current metadata |
|---|
| 125 | * |
|---|
| 126 | * @return The path to the found cover art |
|---|
| 127 | */ |
|---|
| 128 | - (NSString *)coverArtForPath |
|---|
| 129 | { |
|---|
| 130 | /*See if this is a directory*/ |
|---|
| 131 | if([meta isKindOfClass:[SapphireDirectoryMetaData class]]) |
|---|
| 132 | { |
|---|
| 133 | /*Search for cover art for the current directory or a single parent dir*/ |
|---|
| 134 | NSString *ret = [self coverArtForDir:[meta path] parents:1]; |
|---|
| 135 | if(ret != nil) |
|---|
| 136 | return ret; |
|---|
| 137 | /*Fallback to default*/ |
|---|
| 138 | return [[[NSBundle bundleForClass:[self class]] bundlePath] stringByAppendingString:@"/Contents/Resources/DefaultPreview.png"]; |
|---|
| 139 | } |
|---|
| 140 | |
|---|
| 141 | /*Find cover art for the current file in the "Cover Art" dir*/ |
|---|
| 142 | NSString *subPath = [[meta path] stringByDeletingPathExtension]; |
|---|
| 143 | NSString *fileName = [subPath lastPathComponent]; |
|---|
| 144 | NSString *ret = [self searchExtForPath:[[[subPath stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"Cover Art"] stringByAppendingPathComponent:fileName]]; |
|---|
| 145 | if(ret != nil) |
|---|
| 146 | return ret; |
|---|
| 147 | |
|---|
| 148 | /*Find cover art for the current file in the current dir*/ |
|---|
| 149 | ret = [self searchExtForPath:subPath]; |
|---|
| 150 | if(ret != nil) |
|---|
| 151 | return ret; |
|---|
| 152 | |
|---|
| 153 | /*Find cover art for the parent or its parent dir*/ |
|---|
| 154 | ret = [self coverArtForDir:[subPath stringByDeletingLastPathComponent] parents:2]; |
|---|
| 155 | if(ret != nil) |
|---|
| 156 | return ret; |
|---|
| 157 | |
|---|
| 158 | /*Fallback to default*/ |
|---|
| 159 | return [[[NSBundle bundleForClass:[self class]] bundlePath] stringByAppendingString:@"/Contents/Resources/DefaultPreview.png"]; |
|---|
| 160 | } |
|---|
| 161 | |
|---|
| 162 | /*! |
|---|
| 163 | * @brief Override the loading of the cover art method |
|---|
| 164 | */ |
|---|
| 165 | - (void)_loadCoverArt |
|---|
| 166 | { |
|---|
| 167 | [super _loadCoverArt]; |
|---|
| 168 | |
|---|
| 169 | /*See if it loaded something*/ |
|---|
| 170 | if([_coverArtLayer texture] != nil) |
|---|
| 171 | return; |
|---|
| 172 | |
|---|
| 173 | /*Get our cover art*/ |
|---|
| 174 | NSString *path = [self coverArtForPath]; |
|---|
| 175 | NSURL *url = [NSURL fileURLWithPath:path]; |
|---|
| 176 | /*Create an image source*/ |
|---|
| 177 | CGImageRef imageRef = CreateImageForURL(url); |
|---|
| 178 | if(imageRef) |
|---|
| 179 | { |
|---|
| 180 | [_coverArtLayer setImage:imageRef]; |
|---|
| 181 | CFRelease(imageRef); |
|---|
| 182 | } |
|---|
| 183 | } |
|---|
| 184 | |
|---|
| 185 | |
|---|
| 186 | |
|---|
| 187 | /*! |
|---|
| 188 | * @brief populate metadata for TV Shows |
|---|
| 189 | */ |
|---|
| 190 | - (void)_populateMetadata |
|---|
| 191 | { |
|---|
| 192 | [super _populateMetadata]; |
|---|
| 193 | /*See if it loaded anything*/ |
|---|
| 194 | if([[_metadataLayer gimmieMetadataObjs] count]) |
|---|
| 195 | return; |
|---|
| 196 | |
|---|
| 197 | /*Get our data then*/ |
|---|
| 198 | NSArray *order = nil; |
|---|
| 199 | NSMutableDictionary *allMeta = [meta getDisplayedMetaDataInOrder:&order]; |
|---|
| 200 | /*Pull out the special ones*/ |
|---|
| 201 | |
|---|
| 202 | |
|---|
| 203 | // int fileCls = nil ; |
|---|
| 204 | // fileCls =[allMeta objectForKey:FILE_CLASS_KEY]; |
|---|
| 205 | /* |
|---|
| 206 | // Movie Preview Handeling |
|---|
| 207 | if(fileCls==FILE_CLASS_MOVIE) |
|---|
| 208 | { |
|---|
| 209 | NSString *value = [allMeta objectForKey:META_TITLE_KEY]; |
|---|
| 210 | [_metadataLayer setTitle:value]; |
|---|
| 211 | |
|---|
| 212 | } |
|---|
| 213 | */ |
|---|
| 214 | |
|---|
| 215 | /* TV Show Preview Handeling */ |
|---|
| 216 | // if(fileCls==FILE_CLASS_TV_SHOW) |
|---|
| 217 | // { |
|---|
| 218 | /*Get the title*/ |
|---|
| 219 | NSString *value = [allMeta objectForKey:META_TITLE_KEY]; |
|---|
| 220 | if(value != nil) |
|---|
| 221 | { |
|---|
| 222 | /*If there is an air date, put it in the title*/ |
|---|
| 223 | NSDate *airDate = [allMeta objectForKey:META_SHOW_AIR_DATE]; |
|---|
| 224 | if(airDate != nil) |
|---|
| 225 | { |
|---|
| 226 | NSDateFormatter *format = [[NSDateFormatter alloc] init]; |
|---|
| 227 | [format setDateStyle:NSDateFormatterShortStyle]; |
|---|
| 228 | [format setTimeZone:NSDateFormatterNoStyle]; |
|---|
| 229 | value = [[format stringFromDate:airDate]stringByAppendingFormat:@" - %@", value]; |
|---|
| 230 | } |
|---|
| 231 | [_metadataLayer setTitle:value]; |
|---|
| 232 | } |
|---|
| 233 | |
|---|
| 234 | /*Get the rating*/ |
|---|
| 235 | value = [allMeta objectForKey:META_RATING_KEY]; |
|---|
| 236 | if(value != nil) |
|---|
| 237 | [_metadataLayer setRating:value]; |
|---|
| 238 | |
|---|
| 239 | /*Get the description*/ |
|---|
| 240 | value = [allMeta objectForKey:META_DESCRIPTION_KEY]; |
|---|
| 241 | if(value != nil) |
|---|
| 242 | if([[SapphireSettings sharedSettings] displaySpoilers]) |
|---|
| 243 | [_metadataLayer setSummary:value]; |
|---|
| 244 | |
|---|
| 245 | /*Get the copyright*/ |
|---|
| 246 | value = [allMeta objectForKey:META_COPYRIGHT_KEY]; |
|---|
| 247 | if(value != nil) |
|---|
| 248 | [_metadataLayer setCopyright:value]; |
|---|
| 249 | |
|---|
| 250 | /*Get the season and epsiodes*/ |
|---|
| 251 | value = [allMeta objectForKey:META_EPISODE_AND_SEASON_KEY]; |
|---|
| 252 | if(value != nil) |
|---|
| 253 | { |
|---|
| 254 | /*Remove the individuals so we don't display them*/ |
|---|
| 255 | [allMeta removeObjectForKey:META_EPISODE_NUMBER_KEY]; |
|---|
| 256 | [allMeta removeObjectForKey:META_SEASON_NUMBER_KEY]; |
|---|
| 257 | } |
|---|
| 258 | // } |
|---|
| 259 | |
|---|
| 260 | |
|---|
| 261 | NSMutableArray *values = [NSMutableArray array]; |
|---|
| 262 | NSMutableArray *keys = [NSMutableArray array]; |
|---|
| 263 | |
|---|
| 264 | /*Put the metadata in order*/ |
|---|
| 265 | NSEnumerator *keyEnum = [order objectEnumerator]; |
|---|
| 266 | NSString *key = nil; |
|---|
| 267 | while((key = [keyEnum nextObject]) != nil) |
|---|
| 268 | { |
|---|
| 269 | NSString *value = [allMeta objectForKey:key]; |
|---|
| 270 | if(value != nil) |
|---|
| 271 | { |
|---|
| 272 | [values addObject:value]; |
|---|
| 273 | [keys addObject:key]; |
|---|
| 274 | } |
|---|
| 275 | } |
|---|
| 276 | |
|---|
| 277 | /*And set it*/ |
|---|
| 278 | [_metadataLayer setMetadata:values withLabels:keys]; |
|---|
| 279 | } |
|---|
| 280 | |
|---|
| 281 | /*! |
|---|
| 282 | * @brief Override the info about whether it has metadata |
|---|
| 283 | * |
|---|
| 284 | * @return We always have metadata |
|---|
| 285 | */ |
|---|
| 286 | - (BOOL)_assetHasMetadata |
|---|
| 287 | { |
|---|
| 288 | return YES; |
|---|
| 289 | } |
|---|
| 290 | |
|---|
| 291 | @end |
|---|