| 1 | /* |
|---|
| 2 | * SapphireBrowser.m |
|---|
| 3 | * Sapphire |
|---|
| 4 | * |
|---|
| 5 | * Created by pnmerrill on Jun. 20, 2007. |
|---|
| 6 | * Copyright 2007 Sapphire Development Team and/or www.nanopi.net |
|---|
| 7 | * All rights reserved. |
|---|
| 8 | * |
|---|
| 9 | * This program is free software; you can redistribute it and/or modify it under the terms of the GNU |
|---|
| 10 | * General Public License as published by the Free Software Foundation; either version 3 of the License, |
|---|
| 11 | * or (at your option) any later version. |
|---|
| 12 | * |
|---|
| 13 | * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even |
|---|
| 14 | * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General |
|---|
| 15 | * Public License for more details. |
|---|
| 16 | * |
|---|
| 17 | * You should have received a copy of the GNU General Public License along with this program; if not, |
|---|
| 18 | * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|---|
| 19 | */ |
|---|
| 20 | |
|---|
| 21 | #import "SapphireBrowser.h" |
|---|
| 22 | #import <BackRow/BackRow.h> |
|---|
| 23 | #import "SapphireDirectoryMetaData.h" |
|---|
| 24 | #import "SapphireFileMetaData.h" |
|---|
| 25 | #import "SapphireEpisode.h" |
|---|
| 26 | #import "SapphireSeason.h" |
|---|
| 27 | #import "SapphireTVShow.h" |
|---|
| 28 | #import "SapphireMovie.h" |
|---|
| 29 | #import "SapphireMarkMenu.h" |
|---|
| 30 | #import "SapphireMedia.h" |
|---|
| 31 | #import "SapphireVideoPlayer.h" |
|---|
| 32 | #import "SapphireMediaPreview.h" |
|---|
| 33 | #import "SapphireTheme.h" |
|---|
| 34 | #import "SapphireSettings.h" |
|---|
| 35 | #import "NSString-Extensions.h" |
|---|
| 36 | #import "SapphireAudioPlayer.h" |
|---|
| 37 | #import "SapphireAudioMedia.h" |
|---|
| 38 | #import "SapphireApplianceController.h" |
|---|
| 39 | #import "SapphireVideoPlayerController.h" |
|---|
| 40 | #import "SapphireMetaDataSupport.h" |
|---|
| 41 | #import "SapphireDisplayMenu.h" |
|---|
| 42 | #import <SapphireCompatClasses/SapphireFrontRowCompat.h> |
|---|
| 43 | #import <SapphireCompatClasses/SapphireDVDLoadingController.h> |
|---|
| 44 | #import "SapphireErrorDisplayController.h" |
|---|
| 45 | #import "SapphireAudioNowPlayingController.h" |
|---|
| 46 | |
|---|
| 47 | |
|---|
| 48 | #import "SapphireCMPWrapper.h" |
|---|
| 49 | |
|---|
| 50 | #import <objc/objc-class.h> |
|---|
| 51 | |
|---|
| 52 | #import "NSFileManager-Extensions.h" |
|---|
| 53 | |
|---|
| 54 | @interface SapphireBrowser () |
|---|
| 55 | - (void)reloadDirectoryContents; |
|---|
| 56 | - (void)setNewPredicate:(NSPredicate *)newPredicate; |
|---|
| 57 | @end |
|---|
| 58 | |
|---|
| 59 | static BOOL is10Version = NO; |
|---|
| 60 | |
|---|
| 61 | @implementation SapphireBrowser |
|---|
| 62 | |
|---|
| 63 | - (id) initWithScene: (BRRenderScene *) scene metaData:(id <SapphireDirectory>)meta |
|---|
| 64 | { |
|---|
| 65 | if([super initWithScene:scene] == nil) |
|---|
| 66 | return nil; |
|---|
| 67 | |
|---|
| 68 | _names = [NSMutableArray new]; |
|---|
| 69 | items = [NSMutableDictionary new]; |
|---|
| 70 | metaData = [meta retain]; |
|---|
| 71 | predicate = [[SapphireApplianceController predicate] retain]; |
|---|
| 72 | [metaData setFilterPredicate:predicate]; |
|---|
| 73 | |
|---|
| 74 | // set the datasource *after* you've setup your array |
|---|
| 75 | [[self list] setDatasource: self] ; |
|---|
| 76 | |
|---|
| 77 | return ( self ); |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | - (void) dealloc |
|---|
| 81 | { |
|---|
| 82 | // always remember to deallocate your resources |
|---|
| 83 | [_names release]; |
|---|
| 84 | [items release]; |
|---|
| 85 | if([metaData delegate] == self) |
|---|
| 86 | [metaData setDelegate:nil]; |
|---|
| 87 | [metaData release]; |
|---|
| 88 | [predicate release]; |
|---|
| 89 | [super dealloc]; |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | - (void)reloadDirectoryContents |
|---|
| 93 | { |
|---|
| 94 | /*Tell the metadata to get new data*/ |
|---|
| 95 | if(![metaData objectIsDeleted]) |
|---|
| 96 | [metaData reloadDirectoryContents]; |
|---|
| 97 | else |
|---|
| 98 | { |
|---|
| 99 | [_names removeAllObjects]; |
|---|
| 100 | [items removeAllObjects]; |
|---|
| 101 | dirCount = fileCount = 0; |
|---|
| 102 | [[self list] reload]; |
|---|
| 103 | [self resetPreviewController]; |
|---|
| 104 | [SapphireFrontRowCompat renderScene:[self scene]]; |
|---|
| 105 | } |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | - (void)directoryContentsChanged |
|---|
| 109 | { |
|---|
| 110 | BOOL isTake2 = [SapphireFrontRowCompat usingATypeOfTakeTwo]; |
|---|
| 111 | BOOL isReallyFrontrow = [SapphireFrontRowCompat usingLeopard]; |
|---|
| 112 | NSString *oldName=nil; |
|---|
| 113 | int oldRow = [self getSelection]; |
|---|
| 114 | |
|---|
| 115 | if(oldRow < [_names count] && oldRow > 0) |
|---|
| 116 | oldName = [[_names objectAtIndex:oldRow] retain]; |
|---|
| 117 | |
|---|
| 118 | /*Flush our cache*/ |
|---|
| 119 | [_names removeAllObjects]; |
|---|
| 120 | [items removeAllObjects]; |
|---|
| 121 | /*Set up the dirs and files into names and the counts*/ |
|---|
| 122 | if(![metaData objectIsDeleted]) |
|---|
| 123 | { |
|---|
| 124 | NSArray *dirs = [metaData directories]; |
|---|
| 125 | [_names addObjectsFromArray:dirs]; |
|---|
| 126 | NSArray *files = [metaData files]; |
|---|
| 127 | [_names addObjectsFromArray:files]; |
|---|
| 128 | dirCount = [dirs count]; |
|---|
| 129 | fileCount = [files count]; |
|---|
| 130 | } |
|---|
| 131 | else |
|---|
| 132 | { |
|---|
| 133 | dirCount = 0; |
|---|
| 134 | fileCount = 0; |
|---|
| 135 | } |
|---|
| 136 | if(predicate != NULL) |
|---|
| 137 | { |
|---|
| 138 | /*Put in the menu for rescan if fast switching enabled*/ |
|---|
| 139 | if([[SapphireSettings sharedSettings] fastSwitching]) |
|---|
| 140 | [_names addObject:BRLocalizedString(@" < Scan for new files >", @"Conduct a scan of the directory for new files")]; |
|---|
| 141 | } |
|---|
| 142 | /*Remove the dividers*/ |
|---|
| 143 | BRListControl *list = [self list]; |
|---|
| 144 | if([list respondsToSelector:@selector(removeDividers)]) |
|---|
| 145 | [list removeDividers]; |
|---|
| 146 | else |
|---|
| 147 | [list setDividerIndex:0]; |
|---|
| 148 | [list removeDividerAtIndex:0]; |
|---|
| 149 | /*Do a reload*/ |
|---|
| 150 | [list reload]; |
|---|
| 151 | /*Add dividers*/ |
|---|
| 152 | int indexOffset = 0; |
|---|
| 153 | if(dirCount && fileCount) |
|---|
| 154 | { |
|---|
| 155 | [SapphireFrontRowCompat addDividerAtIndex:dirCount toList:list]; |
|---|
| 156 | if(!is10Version && !isTake2) |
|---|
| 157 | indexOffset++; |
|---|
| 158 | } |
|---|
| 159 | if(predicate != NULL && [[SapphireSettings sharedSettings] fastSwitching]) |
|---|
| 160 | if(!isReallyFrontrow || indexOffset == 0) //Frontrow cannot display a list with two dividers correctly!!!!! |
|---|
| 161 | [SapphireFrontRowCompat addDividerAtIndex:dirCount + fileCount + indexOffset toList:list]; |
|---|
| 162 | if([[self stack] peekController] == self) |
|---|
| 163 | [metaData resumeImport]; |
|---|
| 164 | |
|---|
| 165 | /*Attempt to preserve the user's current highlighted selection*/ |
|---|
| 166 | if(oldName) |
|---|
| 167 | { |
|---|
| 168 | int row = [_names indexOfObject:oldName]; |
|---|
| 169 | if(row == NSNotFound) |
|---|
| 170 | row = oldRow; |
|---|
| 171 | if(row >= [_names count]) |
|---|
| 172 | row--; |
|---|
| 173 | [(BRListControl *)[self list] setSelection:row]; |
|---|
| 174 | [oldName release]; |
|---|
| 175 | } |
|---|
| 176 | else if(fileCount != 0) |
|---|
| 177 | [(BRListControl *)[self list] setSelection:dirCount]; |
|---|
| 178 | else |
|---|
| 179 | [(BRListControl *)[self list] setSelection:0]; |
|---|
| 180 | /*Draw*/ |
|---|
| 181 | [self resetPreviewController]; |
|---|
| 182 | [SapphireFrontRowCompat renderScene:[self scene]]; |
|---|
| 183 | } |
|---|
| 184 | |
|---|
| 185 | - (id <SapphireDirectory>)metaData |
|---|
| 186 | { |
|---|
| 187 | return metaData; |
|---|
| 188 | } |
|---|
| 189 | |
|---|
| 190 | - (void)setKillMusic:(BOOL)kill |
|---|
| 191 | { |
|---|
| 192 | killMusic = kill; |
|---|
| 193 | } |
|---|
| 194 | |
|---|
| 195 | - (void)doInitialPush |
|---|
| 196 | { |
|---|
| 197 | /*Reload upon display*/ |
|---|
| 198 | @try { |
|---|
| 199 | [metaData setDelegate:self]; |
|---|
| 200 | [self setNewPredicate:[SapphireApplianceController predicate]]; |
|---|
| 201 | } |
|---|
| 202 | @catch (NSException * e) { |
|---|
| 203 | [SapphireApplianceController logException:e]; |
|---|
| 204 | } |
|---|
| 205 | [super doInitialPush]; |
|---|
| 206 | } |
|---|
| 207 | |
|---|
| 208 | - (void)wasPushed |
|---|
| 209 | { |
|---|
| 210 | [super wasPushed]; |
|---|
| 211 | /*Get metadata when we can*/ |
|---|
| 212 | [metaData resumeImport]; |
|---|
| 213 | } |
|---|
| 214 | |
|---|
| 215 | - (void)wasPopped |
|---|
| 216 | { |
|---|
| 217 | [super wasPopped]; |
|---|
| 218 | /*Cancel everything we were doing*/ |
|---|
| 219 | [metaData cancelImport]; |
|---|
| 220 | cancelScan = YES; |
|---|
| 221 | [metaData setDelegate:nil]; |
|---|
| 222 | if(killMusic) |
|---|
| 223 | [SapphireApplianceController setMusicNowPlayingController:nil]; |
|---|
| 224 | } |
|---|
| 225 | |
|---|
| 226 | - (void)wasBuried |
|---|
| 227 | { |
|---|
| 228 | /*Cancel everything we were doing*/ |
|---|
| 229 | [metaData cancelImport]; |
|---|
| 230 | cancelScan = YES; |
|---|
| 231 | [metaData setDelegate:nil]; |
|---|
| 232 | |
|---|
| 233 | // always call super |
|---|
| 234 | [super wasBuried]; |
|---|
| 235 | } |
|---|
| 236 | |
|---|
| 237 | - (void)doInitialExhume |
|---|
| 238 | { |
|---|
| 239 | /*Reload our display*/ |
|---|
| 240 | [metaData setDelegate:self]; |
|---|
| 241 | [self setNewPredicate:[SapphireApplianceController predicate]]; |
|---|
| 242 | [super doInitialExhume]; |
|---|
| 243 | } |
|---|
| 244 | |
|---|
| 245 | - (void)wasExhumed |
|---|
| 246 | { |
|---|
| 247 | [super wasExhumed]; |
|---|
| 248 | /*Check to see if dir is empty*/ |
|---|
| 249 | if(fileCount + dirCount == 0) |
|---|
| 250 | [[self stack] performSelector:@selector(popController) withObject:nil afterDelay:0.01f]; |
|---|
| 251 | else |
|---|
| 252 | /*Resume importing now that we are up again*/ |
|---|
| 253 | [metaData resumeImport]; |
|---|
| 254 | } |
|---|
| 255 | |
|---|
| 256 | - (long) itemCount |
|---|
| 257 | { |
|---|
| 258 | // return the number of items in your menu list here |
|---|
| 259 | if([_names count]) |
|---|
| 260 | return ( [ _names count]); |
|---|
| 261 | // Put up an empty item |
|---|
| 262 | return 1; |
|---|
| 263 | } |
|---|
| 264 | |
|---|
| 265 | - (id<BRMenuItemLayer>) itemForRow: (long) row |
|---|
| 266 | { |
|---|
| 267 | @try{ |
|---|
| 268 | NSString * displayName=nil ; |
|---|
| 269 | /*Check for no items*/ |
|---|
| 270 | int nameCount = [_names count]; |
|---|
| 271 | FileClass fileCls=0; |
|---|
| 272 | if( nameCount == 0) |
|---|
| 273 | { |
|---|
| 274 | BRAdornedMenuItemLayer *result = [SapphireFrontRowCompat textMenuItemForScene:[self scene] folder:NO]; |
|---|
| 275 | [SapphireFrontRowCompat setTitle:BRLocalizedString(@"< EMPTY >", @"Empty directory") forMenu:result]; |
|---|
| 276 | return result; |
|---|
| 277 | } |
|---|
| 278 | if( row >= nameCount ) return ( nil ) ; |
|---|
| 279 | |
|---|
| 280 | /*Check our cache*/ |
|---|
| 281 | NSString *lookupName = [_names objectAtIndex:row]; |
|---|
| 282 | id cached = [items objectForKey:lookupName]; |
|---|
| 283 | if(cached != nil) |
|---|
| 284 | return cached; |
|---|
| 285 | // Pad filename to correcrtly display gem icons |
|---|
| 286 | BRAdornedMenuItemLayer * result = nil; |
|---|
| 287 | BOOL watched = NO; |
|---|
| 288 | BOOL favorite = NO; |
|---|
| 289 | BOOL utility = NO; |
|---|
| 290 | BOOL partiallyWatched = NO; |
|---|
| 291 | BRRenderScene *scene = [self scene]; |
|---|
| 292 | SapphireTheme *theme = [SapphireTheme sharedTheme]; |
|---|
| 293 | /*Is this a dir*/ |
|---|
| 294 | if(row < dirCount) |
|---|
| 295 | { |
|---|
| 296 | result = [SapphireFrontRowCompat textMenuItemForScene:scene folder:YES]; |
|---|
| 297 | id <SapphireDirectory> meta = [metaData metaDataForDirectory:lookupName]; |
|---|
| 298 | watched = ![meta containsFileMatchingPredicate:[SapphireApplianceController unwatchedPredicate]]; |
|---|
| 299 | favorite = [meta containsFileMatchingPredicate:[SapphireApplianceController favoritePredicate]]; |
|---|
| 300 | } |
|---|
| 301 | /*Check for a file next*/ |
|---|
| 302 | else if(row < dirCount + fileCount) |
|---|
| 303 | { |
|---|
| 304 | result = [SapphireFrontRowCompat textMenuItemForScene:scene folder:NO]; |
|---|
| 305 | SapphireFileMetaData *meta = [metaData metaDataForFile:lookupName]; |
|---|
| 306 | if(meta != nil) |
|---|
| 307 | { |
|---|
| 308 | fileCls=[meta fileClassValue]; |
|---|
| 309 | BOOL rightTextSet = NO; |
|---|
| 310 | if(fileCls==FileClassTVShow) |
|---|
| 311 | { |
|---|
| 312 | SapphireEpisode *ep = [meta tvEpisode]; |
|---|
| 313 | /*Display episode number if available*/ |
|---|
| 314 | int eps= [ep episodeNumberValue]; |
|---|
| 315 | int ep2= [ep lastEpisodeNumberValue]; |
|---|
| 316 | displayName=[ep episodeTitle] ; |
|---|
| 317 | if(eps>0) |
|---|
| 318 | { |
|---|
| 319 | NSArray *comp = [[metaData path] pathComponents]; |
|---|
| 320 | NSString *prefix = @""; |
|---|
| 321 | if([comp count] == 2 && [[comp objectAtIndex:0] isEqual:@"@TV"]) |
|---|
| 322 | /*Eps listed outside of seasons*/ |
|---|
| 323 | prefix = [NSString stringWithFormat:@"%02dx", [[ep season] seasonNumberValue]]; |
|---|
| 324 | if(ep2>0 && ep2 != eps) |
|---|
| 325 | [SapphireFrontRowCompat setRightJustifiedText:[NSString stringWithFormat:@" %@%02d-%02d", prefix, eps, ep2] forMenu:result]; |
|---|
| 326 | else |
|---|
| 327 | [SapphireFrontRowCompat setRightJustifiedText:[NSString stringWithFormat:@" %@%02d", prefix, eps] forMenu:result]; |
|---|
| 328 | rightTextSet = YES; |
|---|
| 329 | } |
|---|
| 330 | } |
|---|
| 331 | if(fileCls==FileClassMovie) |
|---|
| 332 | { |
|---|
| 333 | SapphireMovie *movie = [meta movie]; |
|---|
| 334 | displayName=[movie title]; |
|---|
| 335 | /* Find out if we are displaying a virtual directoy we need to filter for */ |
|---|
| 336 | NSString *dirPath=[metaData path]; |
|---|
| 337 | /*Add icons & stats (RIGHT)*/ |
|---|
| 338 | int top250 = [movie imdbTop250RankingValue]; |
|---|
| 339 | if([dirPath hasPrefix:VIRTUAL_DIR_TOP250_PATH] && top250 > 0) |
|---|
| 340 | { |
|---|
| 341 | NSString *movieStatsTop250 = [NSString stringWithFormat:@"#%d ", top250]; |
|---|
| 342 | /* This list is already filtered so all displayed movies will have a top250 stat */ |
|---|
| 343 | [SapphireFrontRowCompat setRightJustifiedText:movieStatsTop250 forMenu:result]; |
|---|
| 344 | [SapphireFrontRowCompat setRightIcon:[theme gem:IMDB_GEM_KEY] forMenu:result]; |
|---|
| 345 | } |
|---|
| 346 | else if([movie oscarsWonValue] > 0) |
|---|
| 347 | { |
|---|
| 348 | NSString *movieStatsOscar = [NSString stringWithFormat:@"%dx", [movie oscarsWonValue]]; |
|---|
| 349 | [SapphireFrontRowCompat setRightJustifiedText:movieStatsOscar forMenu:result]; |
|---|
| 350 | [SapphireFrontRowCompat setRightIcon:[theme gem:OSCAR_GEM_KEY] forMenu:result]; |
|---|
| 351 | } |
|---|
| 352 | else if(top250 > 0) |
|---|
| 353 | { |
|---|
| 354 | NSString *movieStatsTop250 = [NSString stringWithFormat:@"#%d ", top250]; |
|---|
| 355 | [SapphireFrontRowCompat setRightJustifiedText:movieStatsTop250 forMenu:result]; |
|---|
| 356 | [SapphireFrontRowCompat setRightIcon:[theme gem:IMDB_GEM_KEY] forMenu:result]; |
|---|
| 357 | } |
|---|
| 358 | else |
|---|
| 359 | { |
|---|
| 360 | [SapphireFrontRowCompat setRightJustifiedText:[meta durationString] forMenu:result]; |
|---|
| 361 | } |
|---|
| 362 | rightTextSet = YES; |
|---|
| 363 | } |
|---|
| 364 | watched = [meta watchedValue]; |
|---|
| 365 | favorite = [meta favoriteValue]; |
|---|
| 366 | partiallyWatched = [meta resumeTimeValue] != 0; |
|---|
| 367 | NSString *sizeString = [meta sizeString]; |
|---|
| 368 | if(!rightTextSet && [sizeString length] > 1) |
|---|
| 369 | /*Fallback to size*/ |
|---|
| 370 | [SapphireFrontRowCompat setRightJustifiedText:sizeString forMenu:result]; |
|---|
| 371 | |
|---|
| 372 | } |
|---|
| 373 | } |
|---|
| 374 | /*Utility*/ |
|---|
| 375 | else |
|---|
| 376 | { |
|---|
| 377 | result = [SapphireFrontRowCompat textMenuItemForScene:scene folder:NO]; |
|---|
| 378 | utility = YES; |
|---|
| 379 | } |
|---|
| 380 | NSString *gemString = nil; |
|---|
| 381 | /*Add icons (LEFT)*/ |
|---|
| 382 | if(utility) gemString = FAST_GEM_KEY; |
|---|
| 383 | else if(partiallyWatched) gemString = RED_BLUE_GEM_KEY; |
|---|
| 384 | else if(!watched) gemString = BLUE_GEM_KEY; |
|---|
| 385 | else if(favorite) gemString = YELLOW_GEM_KEY; |
|---|
| 386 | else if(fileCls==FileClassAudio) gemString = GREEN_GEM_KEY; |
|---|
| 387 | else gemString = RED_GEM_KEY;; |
|---|
| 388 | [SapphireFrontRowCompat setLeftIcon:[theme gem:gemString] forMenu:result]; |
|---|
| 389 | |
|---|
| 390 | // add text |
|---|
| 391 | NSString *name; |
|---|
| 392 | if(displayName) |
|---|
| 393 | name = displayName; |
|---|
| 394 | else |
|---|
| 395 | name = lookupName; |
|---|
| 396 | name=[@" " stringByAppendingString: name] ; |
|---|
| 397 | [SapphireFrontRowCompat setTitle:name forMenu:result]; |
|---|
| 398 | [items setObject:result forKey:lookupName]; |
|---|
| 399 | |
|---|
| 400 | return ( result ) ; |
|---|
| 401 | } @catch (NSException *e) { |
|---|
| 402 | [SapphireApplianceController logException:e]; |
|---|
| 403 | } |
|---|
| 404 | return nil; |
|---|
| 405 | } |
|---|
| 406 | |
|---|
| 407 | - (NSString *) titleForRow: (long) row |
|---|
| 408 | { |
|---|
| 409 | |
|---|
| 410 | if ( row >= [ _names count] ) return ( nil ); |
|---|
| 411 | |
|---|
| 412 | NSString *result = [ _names objectAtIndex: row] ; |
|---|
| 413 | |
|---|
| 414 | return ([@" ????? " stringByAppendingString: result] ) ; |
|---|
| 415 | } |
|---|
| 416 | |
|---|
| 417 | - (long) rowForTitle: (NSString *) title |
|---|
| 418 | { |
|---|
| 419 | long result = -1; |
|---|
| 420 | long i, count = [self itemCount]; |
|---|
| 421 | for ( i = 0; i < count; i++ ) |
|---|
| 422 | { |
|---|
| 423 | if ( [title isEqualToString: [self titleForRow: i]] ) |
|---|
| 424 | { |
|---|
| 425 | result = i; |
|---|
| 426 | break; |
|---|
| 427 | } |
|---|
| 428 | } |
|---|
| 429 | |
|---|
| 430 | return ( result ); |
|---|
| 431 | } |
|---|
| 432 | |
|---|
| 433 | - (void)setNewPredicate:(NSPredicate *)newPredicate |
|---|
| 434 | { |
|---|
| 435 | [newPredicate retain]; |
|---|
| 436 | [predicate release]; |
|---|
| 437 | predicate = newPredicate; |
|---|
| 438 | if([metaData filterPredicate] != predicate) |
|---|
| 439 | [metaData setFilterPredicate:predicate]; |
|---|
| 440 | [self setListIcon:[SapphireApplianceController gemForPredicate:predicate]]; |
|---|
| 441 | [self reloadDirectoryContents]; |
|---|
| 442 | } |
|---|
| 443 | |
|---|
| 444 | - (void)anonymousReportFile:(SapphireFileMetaData *)currentPlayFile withPath:(NSString *)path |
|---|
| 445 | { |
|---|
| 446 | NSMutableString *reqData = [NSMutableString string]; |
|---|
| 447 | NSMutableArray *reqComp = [NSMutableArray array]; |
|---|
| 448 | NSMutableURLRequest *request=nil; |
|---|
| 449 | NSString *ext=nil; |
|---|
| 450 | int fileClass=-1; |
|---|
| 451 | |
|---|
| 452 | if(path != nil) |
|---|
| 453 | { |
|---|
| 454 | [reqComp addObject:[NSString stringWithFormat:@"path=%@", [[path lastPathComponent]lowercaseString]]]; |
|---|
| 455 | ext=[[path pathExtension]lowercaseString] ; |
|---|
| 456 | } |
|---|
| 457 | |
|---|
| 458 | if([currentPlayFile fileClassValue] == FileClassTVShow) |
|---|
| 459 | { |
|---|
| 460 | SapphireEpisode *episode = [currentPlayFile tvEpisode]; |
|---|
| 461 | fileClass=1; |
|---|
| 462 | request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://appletv.nanopi.net/show.php"]]; |
|---|
| 463 | int ep = [episode episodeNumberValue]; |
|---|
| 464 | int season = [[episode season] seasonNumberValue]; |
|---|
| 465 | SapphireTVShow *show = [episode tvShow]; |
|---|
| 466 | NSString *showName= [show name]; |
|---|
| 467 | |
|---|
| 468 | if(season != 0) |
|---|
| 469 | [reqComp addObject:[NSString stringWithFormat:@"season=%d", season]]; |
|---|
| 470 | if(ep != 0) |
|---|
| 471 | [reqComp addObject:[NSString stringWithFormat:@"ep=%d", ep]]; |
|---|
| 472 | if(showName != 0) |
|---|
| 473 | [reqComp addObject:[NSString stringWithFormat:@"showname=%@", showName]]; |
|---|
| 474 | } |
|---|
| 475 | else if([currentPlayFile fileClassValue] == FileClassMovie) |
|---|
| 476 | { |
|---|
| 477 | fileClass=2; |
|---|
| 478 | request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://appletv.nanopi.net/movie.php"]]; |
|---|
| 479 | SapphireMovie *movie = [currentPlayFile movie]; |
|---|
| 480 | NSString *movieTitle=[movie title]; |
|---|
| 481 | int movieID=[movie imdbNumberValue]; |
|---|
| 482 | NSDate * releaseDate=[movie releaseDate]; |
|---|
| 483 | if(movieTitle != 0) |
|---|
| 484 | [reqComp addObject:[NSString stringWithFormat:@"title=%@", movieTitle]]; |
|---|
| 485 | if(releaseDate != 0) |
|---|
| 486 | [reqComp addObject:[NSString stringWithFormat:@"year=%@", [releaseDate descriptionWithCalendarFormat:@"%Y" timeZone:nil locale:[[NSUserDefaults standardUserDefaults] dictionaryRepresentation]]]]; |
|---|
| 487 | if(movieID != 0) |
|---|
| 488 | [reqComp addObject:[NSString stringWithFormat:@"movieid=%d", movieID]]; |
|---|
| 489 | } |
|---|
| 490 | else |
|---|
| 491 | { |
|---|
| 492 | request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://appletv.nanopi.net/ext.php"]]; |
|---|
| 493 | //The fileClass is never used in these cases later |
|---|
| 494 | // if([currentPlayFile fileClassValue] == FileClassUnknown) |
|---|
| 495 | // fileClass=0; |
|---|
| 496 | // if([currentPlayFile fileClassValue] == FileClassAudio) |
|---|
| 497 | // fileClass=3; |
|---|
| 498 | if([currentPlayFile fileClassValue] == FileClassOther) |
|---|
| 499 | fileClass=5; |
|---|
| 500 | else |
|---|
| 501 | fileClass=99; |
|---|
| 502 | } |
|---|
| 503 | |
|---|
| 504 | if(ext!=0) |
|---|
| 505 | { |
|---|
| 506 | [reqComp addObject:[NSString stringWithFormat:@"filetype=%d",fileClass]]; |
|---|
| 507 | [reqComp addObject:[NSString stringWithFormat:@"extension=%@", ext]]; |
|---|
| 508 | if([SapphireFrontRowCompat usingATypeOfTakeTwo]) |
|---|
| 509 | [reqComp addObject:[NSString stringWithFormat:@"ckey=ATVT2-%@-%d",ext,fileClass]]; |
|---|
| 510 | else if([SapphireFrontRowCompat usingLeopard]) |
|---|
| 511 | [reqComp addObject:[NSString stringWithFormat:@"ckey=FRONTROW-%@-%d",ext,fileClass]]; |
|---|
| 512 | else |
|---|
| 513 | [reqComp addObject:[NSString stringWithFormat:@"ckey=ATV-%@-%d",ext,fileClass]]; |
|---|
| 514 | } |
|---|
| 515 | |
|---|
| 516 | int count = [reqComp count]; |
|---|
| 517 | int i; |
|---|
| 518 | for(i=0; i<count-1; i++) |
|---|
| 519 | { |
|---|
| 520 | [reqData appendFormat:@"%@&", [reqComp objectAtIndex:i]]; |
|---|
| 521 | } |
|---|
| 522 | if(count) |
|---|
| 523 | [reqData appendFormat:@"%@", [reqComp objectAtIndex:i]]; |
|---|
| 524 | |
|---|
| 525 | NSData *postData = [reqData dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES]; |
|---|
| 526 | |
|---|
| 527 | [request setHTTPMethod:@"POST"]; |
|---|
| 528 | [request setValue:[NSString stringWithFormat:@"%d", [postData length]] forHTTPHeaderField:@"Content-Length"]; |
|---|
| 529 | [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; |
|---|
| 530 | [request setHTTPBody:postData]; |
|---|
| 531 | [request setCachePolicy:NSURLRequestReloadIgnoringCacheData]; |
|---|
| 532 | /*Trigger the req*/ |
|---|
| 533 | NSURLDownload *download = [[NSURLDownload alloc] initWithRequest:request delegate:nil]; |
|---|
| 534 | [download autorelease]; |
|---|
| 535 | } |
|---|
| 536 | |
|---|
| 537 | - (void)playFile:(SapphireFileMetaData *)currentPlayFile |
|---|
| 538 | { |
|---|
| 539 | NSString *path = [currentPlayFile path]; |
|---|
| 540 | if(![[NSFileManager defaultManager] fileExistsAtPath:path]) |
|---|
| 541 | { |
|---|
| 542 | NSString *errorTitle = BRLocalizedString(@"File Not Present", @"File Not Present title"); |
|---|
| 543 | NSString *errorString = [NSString stringWithFormat:BRLocalizedString(@"The File %@ is not present. Either a drive is not mounted or your metadata has not been imported recently", @"File not present error string"), path]; |
|---|
| 544 | SapphireErrorDisplayController *display = [[SapphireErrorDisplayController alloc] initWithScene:[self scene] error:errorTitle longError:errorString]; |
|---|
| 545 | [[self stack] pushController:display]; |
|---|
| 546 | [display release]; |
|---|
| 547 | return; |
|---|
| 548 | } |
|---|
| 549 | if([currentPlayFile updateMetaData]) |
|---|
| 550 | [SapphireMetaDataSupport save:[currentPlayFile managedObjectContext]]; |
|---|
| 551 | |
|---|
| 552 | |
|---|
| 553 | /*Anonymous reporting*/ |
|---|
| 554 | SapphireSettings *settings = [SapphireSettings sharedSettings]; |
|---|
| 555 | if(![settings disableAnonymousReporting]) |
|---|
| 556 | { |
|---|
| 557 | [self anonymousReportFile:currentPlayFile withPath:path]; |
|---|
| 558 | } |
|---|
| 559 | |
|---|
| 560 | if([currentPlayFile fileContainerTypeValue] == FileContainerTypeVideoTS && path != nil) |
|---|
| 561 | { |
|---|
| 562 | SapphireCMPWrapper *wrapper = [[SapphireCMPWrapper alloc] initWithFile:currentPlayFile scene:[self scene]]; |
|---|
| 563 | id controller = [wrapper controller]; |
|---|
| 564 | [wrapper release]; |
|---|
| 565 | // SapphireErrorDisplayController *controller = [[SapphireErrorDisplayController alloc] initWithScene:[self scene] error:BRLocalizedString(@"Playback Error", @"Short error indicating an error while playing a file") longError:BRLocalizedString(@"DVD Playback is not supported on the AppleTV", @"Error message saying DVD on ATV not supported")]; |
|---|
| 566 | [[self stack] pushController:controller]; |
|---|
| 567 | } |
|---|
| 568 | else if([[NSFileManager defaultManager] acceptFilePath:path] && [currentPlayFile hasVideoValue]) |
|---|
| 569 | { |
|---|
| 570 | /*Video*/ |
|---|
| 571 | /*Set the asset resume time*/ |
|---|
| 572 | NSURL *url = [NSURL fileURLWithPath:path]; |
|---|
| 573 | SapphireMedia *asset =[[SapphireMedia alloc] initWithMediaURL:url]; |
|---|
| 574 | [asset setResumeTime:[currentPlayFile resumeTimeValue]]; |
|---|
| 575 | [asset setFileMetaData:currentPlayFile]; |
|---|
| 576 | |
|---|
| 577 | /*Get the player*/ |
|---|
| 578 | SapphireVideoPlayer *player = [[SapphireVideoPlayer alloc] init]; |
|---|
| 579 | NSError *error = nil; |
|---|
| 580 | [player setMedia:asset error:&error]; |
|---|
| 581 | |
|---|
| 582 | /*and go*/ |
|---|
| 583 | SapphireVideoPlayerController *controller = [[SapphireVideoPlayerController alloc] initWithScene:[self scene] player:player]; |
|---|
| 584 | [controller setPlayFile:currentPlayFile]; |
|---|
| 585 | [controller setAllowsResume:YES]; |
|---|
| 586 | [[self stack] pushController:controller]; |
|---|
| 587 | |
|---|
| 588 | [asset release]; |
|---|
| 589 | [player release]; |
|---|
| 590 | [controller release]; |
|---|
| 591 | } |
|---|
| 592 | else if(path != nil) |
|---|
| 593 | { |
|---|
| 594 | /*Audio*/ |
|---|
| 595 | /*Set the asset*/ |
|---|
| 596 | NSURL *url = [NSURL fileURLWithPath:path]; |
|---|
| 597 | SapphireAudioMedia *asset =[[SapphireAudioMedia alloc] initWithMediaURL:url]; |
|---|
| 598 | [asset setResumeTime:[currentPlayFile resumeTimeValue]]; |
|---|
| 599 | [asset setFileMetaData:currentPlayFile]; |
|---|
| 600 | |
|---|
| 601 | SapphireAudioPlayer *player = [[SapphireAudioPlayer alloc] init]; |
|---|
| 602 | /*and go*/ |
|---|
| 603 | BRMusicNowPlayingController *controller; |
|---|
| 604 | if([SapphireFrontRowCompat usingLeopardOrATypeOfTakeTwo]) |
|---|
| 605 | controller = [[SapphireAudioNowPlayingController alloc] initWithPlayer:player]; |
|---|
| 606 | else |
|---|
| 607 | { |
|---|
| 608 | controller = [[BRMusicNowPlayingController alloc] initWithScene:[self scene]]; |
|---|
| 609 | [controller setPlayer:player]; |
|---|
| 610 | } |
|---|
| 611 | NSError *error = nil; |
|---|
| 612 | [player setMedia:asset inTracklist:[NSArray arrayWithObject:asset] error:&error]; |
|---|
| 613 | [player play]; |
|---|
| 614 | [SapphireApplianceController setMusicNowPlayingController:controller]; |
|---|
| 615 | [[self stack] pushController:controller]; |
|---|
| 616 | |
|---|
| 617 | [asset release]; |
|---|
| 618 | [player release]; |
|---|
| 619 | [controller release]; |
|---|
| 620 | } |
|---|
| 621 | } |
|---|
| 622 | |
|---|
| 623 | - (void) itemSelected: (long) row |
|---|
| 624 | { |
|---|
| 625 | // This is called when the user presses play/pause on a list item |
|---|
| 626 | |
|---|
| 627 | if([_names count] == 0) |
|---|
| 628 | { |
|---|
| 629 | /*No items, so leave the empty dir*/ |
|---|
| 630 | [[self stack] popController]; |
|---|
| 631 | return; |
|---|
| 632 | } |
|---|
| 633 | |
|---|
| 634 | NSString *name = [_names objectAtIndex:row]; |
|---|
| 635 | |
|---|
| 636 | /*Check for dir*/ |
|---|
| 637 | if(row < dirCount) |
|---|
| 638 | { |
|---|
| 639 | /*Browse the subdir*/ |
|---|
| 640 | id controller = [[SapphireBrowser alloc] initWithScene:[self scene] metaData:[metaData metaDataForDirectory:name]]; |
|---|
| 641 | [controller setListTitle:[NSString stringWithFormat:@" %@",name]]; |
|---|
| 642 | [controller setListIcon:[self listIcon]]; |
|---|
| 643 | [[self stack] pushController:controller]; |
|---|
| 644 | [controller release]; |
|---|
| 645 | } |
|---|
| 646 | else if(row < dirCount + fileCount) |
|---|
| 647 | { |
|---|
| 648 | /*First stop any music*/ |
|---|
| 649 | [SapphireApplianceController setMusicNowPlayingController:nil]; |
|---|
| 650 | /*Play the file*/ |
|---|
| 651 | [self playFile:[metaData metaDataForFile:name]]; |
|---|
| 652 | } |
|---|
| 653 | else |
|---|
| 654 | { |
|---|
| 655 | /*Do a scan*/ |
|---|
| 656 | cancelScan = NO; |
|---|
| 657 | [metaData scanForNewFilesWithDelegate:self skipDirectories:[NSMutableSet set]]; |
|---|
| 658 | } |
|---|
| 659 | } |
|---|
| 660 | |
|---|
| 661 | - (void)gotSubFiles:(NSArray *)subs |
|---|
| 662 | { |
|---|
| 663 | [self reloadDirectoryContents]; |
|---|
| 664 | } |
|---|
| 665 | |
|---|
| 666 | - (void)scanningDir:(NSString *)dir |
|---|
| 667 | { |
|---|
| 668 | } |
|---|
| 669 | |
|---|
| 670 | - (BOOL)getSubFilesCanceled |
|---|
| 671 | { |
|---|
| 672 | return cancelScan; |
|---|
| 673 | } |
|---|
| 674 | |
|---|
| 675 | - (id<BRMediaPreviewController>) previewControlForItem: (long) row |
|---|
| 676 | { |
|---|
| 677 | return [self previewControllerForItem:row]; |
|---|
| 678 | } |
|---|
| 679 | |
|---|
| 680 | - (id<BRMediaPreviewController>) previewControllerForItem: (long) row |
|---|
| 681 | { |
|---|
| 682 | // If subclassing BRMediaMenuController, this function is called when the selection cursor |
|---|
| 683 | // passes over an item. |
|---|
| 684 | if(row >= [_names count]) |
|---|
| 685 | return nil; |
|---|
| 686 | /*Check to see if it is a dir or file*/ |
|---|
| 687 | NSString *name = [_names objectAtIndex:row]; |
|---|
| 688 | if(row < dirCount + fileCount) |
|---|
| 689 | { |
|---|
| 690 | SapphireMediaPreview *preview = [[SapphireMediaPreview alloc] initWithScene:[self scene]]; |
|---|
| 691 | /*Check for dir*/ |
|---|
| 692 | if(row < dirCount) |
|---|
| 693 | [preview setMetaData:[metaData metaDataForDirectory:name] inMetaData:metaData]; |
|---|
| 694 | else |
|---|
| 695 | [preview setMetaData:[metaData metaDataForFile:name] inMetaData:metaData]; |
|---|
| 696 | [preview setShowsMetadataImmediately:YES]; |
|---|
| 697 | /*And go*/ |
|---|
| 698 | return [preview autorelease]; |
|---|
| 699 | } |
|---|
| 700 | return ( nil ); |
|---|
| 701 | } |
|---|
| 702 | |
|---|
| 703 | - (BOOL)brEventAction:(BREvent *)event |
|---|
| 704 | { |
|---|
| 705 | BREventRemoteAction remoteAction = [SapphireFrontRowCompat remoteActionForEvent:event]; |
|---|
| 706 | if ([(BRControllerStack *)[self stack] peekController] != self) |
|---|
| 707 | remoteAction = 0; |
|---|
| 708 | |
|---|
| 709 | int row = [self getSelection]; |
|---|
| 710 | |
|---|
| 711 | switch (remoteAction) |
|---|
| 712 | { |
|---|
| 713 | case kBREventRemoteActionRight: |
|---|
| 714 | case kBREventRemoteActionSwipeRight: |
|---|
| 715 | { |
|---|
| 716 | id meta = nil; |
|---|
| 717 | if(row >= [_names count]) |
|---|
| 718 | break; |
|---|
| 719 | |
|---|
| 720 | NSString *name = [_names objectAtIndex:row]; |
|---|
| 721 | |
|---|
| 722 | /*Get metadata*/ |
|---|
| 723 | if(row < dirCount) |
|---|
| 724 | meta = [metaData metaDataForDirectory:name]; |
|---|
| 725 | else if (row < dirCount + fileCount) |
|---|
| 726 | meta = [metaData metaDataForFile:name]; |
|---|
| 727 | else |
|---|
| 728 | break; |
|---|
| 729 | /*Do mark menu*/ |
|---|
| 730 | id controller = [[SapphireMarkMenu alloc] initWithScene:[self scene] metaData:meta]; |
|---|
| 731 | [(SapphireMarkMenu *)controller setListTitle:[name lastPathComponent]]; |
|---|
| 732 | [[self stack] pushController:controller]; |
|---|
| 733 | [controller release]; |
|---|
| 734 | return YES; |
|---|
| 735 | } |
|---|
| 736 | case kBREventRemoteActionLeft: |
|---|
| 737 | case kBREventRemoteActionSwipeLeft: |
|---|
| 738 | { |
|---|
| 739 | id controller = [[SapphireDisplayMenu alloc] initWithScene:[self scene] directory:metaData]; |
|---|
| 740 | [(SapphireDisplayMenu *)controller setListTitle:[self listTitle]]; |
|---|
| 741 | [[self stack] pushController:controller]; |
|---|
| 742 | [controller release]; |
|---|
| 743 | return YES; |
|---|
| 744 | } |
|---|
| 745 | default: |
|---|
| 746 | break; |
|---|
| 747 | } |
|---|
| 748 | return [super brEventAction:event]; |
|---|
| 749 | } |
|---|
| 750 | |
|---|
| 751 | - (void)updateCompleteForFile:(NSString *)file |
|---|
| 752 | { |
|---|
| 753 | /*Get the file*/ |
|---|
| 754 | [items removeObjectForKey:file]; |
|---|
| 755 | /*Relead the list and render*/ |
|---|
| 756 | BRListControl *list = [self list]; |
|---|
| 757 | [list reload]; |
|---|
| 758 | /*Reload the preview pane*/ |
|---|
| 759 | [self resetPreviewController]; |
|---|
| 760 | [SapphireFrontRowCompat renderScene:[self scene]]; |
|---|
| 761 | } |
|---|
| 762 | |
|---|
| 763 | @end |
|---|