| 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 <SapphireCompatClasses/SapphireFrontRowCompat.h> |
|---|
| 42 | #import <SapphireCompatClasses/SapphireDVDLoadingController.h> |
|---|
| 43 | |
|---|
| 44 | #import <objc/objc-class.h> |
|---|
| 45 | |
|---|
| 46 | #import "NSFileManager-Extensions.h" |
|---|
| 47 | |
|---|
| 48 | @interface SapphireBrowser (private) |
|---|
| 49 | - (void)reloadDirectoryContents; |
|---|
| 50 | - (void)setNewPredicate:(NSPredicate *)newPredicate; |
|---|
| 51 | @end |
|---|
| 52 | |
|---|
| 53 | @interface BRMusicNowPlayingController (bypassAccess) |
|---|
| 54 | - (void)setPlayer:(BRMusicPlayer *)player; |
|---|
| 55 | - (BRMusicPlayer *)player; |
|---|
| 56 | @end |
|---|
| 57 | |
|---|
| 58 | @implementation BRMusicNowPlayingController (bypassAccess) |
|---|
| 59 | - (void)setPlayer:(BRMusicPlayer *)player |
|---|
| 60 | { |
|---|
| 61 | Class myClass = [self class]; |
|---|
| 62 | Ivar ret = class_getInstanceVariable(myClass, "_player"); |
|---|
| 63 | BRMusicPlayer * *thePlayer = (BRMusicPlayer * *)(((char *)self)+ret->ivar_offset); |
|---|
| 64 | |
|---|
| 65 | [*thePlayer release]; |
|---|
| 66 | *thePlayer = [player retain]; |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | - (BRMusicPlayer *)player |
|---|
| 70 | { |
|---|
| 71 | Class myClass = [self class]; |
|---|
| 72 | Ivar ret = class_getInstanceVariable(myClass, "_player"); |
|---|
| 73 | return *(BRMusicPlayer * *)(((char *)self)+ret->ivar_offset); |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | @end |
|---|
| 77 | |
|---|
| 78 | static BOOL is10Version = NO; |
|---|
| 79 | |
|---|
| 80 | @implementation SapphireBrowser |
|---|
| 81 | |
|---|
| 82 | - (id) initWithScene: (BRRenderScene *) scene metaData:(id <SapphireDirectory>)meta |
|---|
| 83 | { |
|---|
| 84 | if([super initWithScene:scene] == nil) |
|---|
| 85 | return nil; |
|---|
| 86 | |
|---|
| 87 | _names = [NSMutableArray new]; |
|---|
| 88 | items = [NSMutableDictionary new]; |
|---|
| 89 | metaData = [meta retain]; |
|---|
| 90 | predicate = [[SapphireApplianceController predicate] retain]; |
|---|
| 91 | [metaData setFilterPredicate:predicate]; |
|---|
| 92 | |
|---|
| 93 | // set the datasource *after* you've setup your array |
|---|
| 94 | [[self list] setDatasource: self] ; |
|---|
| 95 | |
|---|
| 96 | return ( self ); |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | - (void) dealloc |
|---|
| 100 | { |
|---|
| 101 | // always remember to deallocate your resources |
|---|
| 102 | [_names release]; |
|---|
| 103 | [items release]; |
|---|
| 104 | if([metaData delegate] == self) |
|---|
| 105 | [metaData setDelegate:nil]; |
|---|
| 106 | [metaData release]; |
|---|
| 107 | [predicate release]; |
|---|
| 108 | [super dealloc]; |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | - (void)reloadDirectoryContents |
|---|
| 112 | { |
|---|
| 113 | /*Tell the metadata to get new data*/ |
|---|
| 114 | [metaData reloadDirectoryContents]; |
|---|
| 115 | } |
|---|
| 116 | |
|---|
| 117 | - (void)directoryContentsChanged |
|---|
| 118 | { |
|---|
| 119 | NSString *oldName=nil; |
|---|
| 120 | int row = [self getSelection]; |
|---|
| 121 | |
|---|
| 122 | if(row < [_names count]) |
|---|
| 123 | oldName = [[_names objectAtIndex:row] retain]; |
|---|
| 124 | |
|---|
| 125 | /*Flush our cache*/ |
|---|
| 126 | [_names removeAllObjects]; |
|---|
| 127 | [items removeAllObjects]; |
|---|
| 128 | /*Set up the dirs and files into names and the counts*/ |
|---|
| 129 | NSArray *dirs = [metaData directories]; |
|---|
| 130 | [_names addObjectsFromArray:dirs]; |
|---|
| 131 | NSArray *files = [metaData files]; |
|---|
| 132 | [_names addObjectsFromArray:files]; |
|---|
| 133 | dirCount = [dirs count]; |
|---|
| 134 | fileCount = [files count]; |
|---|
| 135 | if(predicate != NULL) |
|---|
| 136 | { |
|---|
| 137 | /*Put in the menu for rescan if fast switching enabled*/ |
|---|
| 138 | if([[SapphireSettings sharedSettings] fastSwitching]) |
|---|
| 139 | [_names addObject:BRLocalizedString(@" < Scan for new files >", @"Conduct a scan of the directory for new files")]; |
|---|
| 140 | } |
|---|
| 141 | /*Remove the dividers*/ |
|---|
| 142 | BRListControl *list = [self list]; |
|---|
| 143 | if([list respondsToSelector:@selector(removeDividers)]) |
|---|
| 144 | [list removeDividers]; |
|---|
| 145 | else |
|---|
| 146 | [list setDividerIndex:0]; |
|---|
| 147 | [list removeDividerAtIndex:0]; |
|---|
| 148 | /*Do a reload*/ |
|---|
| 149 | [list reload]; |
|---|
| 150 | /*Add dividers*/ |
|---|
| 151 | int indexOffset = 0; |
|---|
| 152 | if(dirCount && fileCount) |
|---|
| 153 | { |
|---|
| 154 | [SapphireFrontRowCompat addDividerAtIndex:dirCount toList:list]; |
|---|
| 155 | if(!is10Version) |
|---|
| 156 | indexOffset++; |
|---|
| 157 | } |
|---|
| 158 | if(predicate != NULL && [[SapphireSettings sharedSettings] fastSwitching]) |
|---|
| 159 | [SapphireFrontRowCompat addDividerAtIndex:dirCount + fileCount + indexOffset toList:list]; |
|---|
| 160 | /*Draw*/ |
|---|
| 161 | justReloaded = YES; |
|---|
| 162 | [self updatePreviewController]; |
|---|
| 163 | [SapphireFrontRowCompat renderScene:[self scene]]; |
|---|
| 164 | if([[self stack] peekController] == self) |
|---|
| 165 | [metaData resumeImport]; |
|---|
| 166 | |
|---|
| 167 | /*Attempt to preserve the user's current highlighted selection*/ |
|---|
| 168 | if(oldName) |
|---|
| 169 | { |
|---|
| 170 | row = [_names indexOfObject:oldName]; |
|---|
| 171 | if(row != NSNotFound) |
|---|
| 172 | [(BRListControl *)[self list] setSelection:row]; |
|---|
| 173 | else |
|---|
| 174 | [(BRListControl *)[self list] setSelection:0]; |
|---|
| 175 | [oldName release]; |
|---|
| 176 | } |
|---|
| 177 | } |
|---|
| 178 | |
|---|
| 179 | - (id <SapphireDirectory>)metaData |
|---|
| 180 | { |
|---|
| 181 | return metaData; |
|---|
| 182 | } |
|---|
| 183 | |
|---|
| 184 | - (void) willBePushed |
|---|
| 185 | { |
|---|
| 186 | // We're about to be placed on screen, but we're not yet there |
|---|
| 187 | |
|---|
| 188 | // always call super |
|---|
| 189 | [super willBePushed]; |
|---|
| 190 | /*Reload upon display*/ |
|---|
| 191 | [metaData setDelegate:self]; |
|---|
| 192 | [self setNewPredicate:[SapphireApplianceController predicate]]; |
|---|
| 193 | } |
|---|
| 194 | |
|---|
| 195 | - (void) wasPushed |
|---|
| 196 | { |
|---|
| 197 | // We've just been put on screen, the user can see this controller's content now |
|---|
| 198 | |
|---|
| 199 | // always call super |
|---|
| 200 | [super wasPushed]; |
|---|
| 201 | /*Get metadata when we can*/ |
|---|
| 202 | [metaData resumeImport]; |
|---|
| 203 | } |
|---|
| 204 | |
|---|
| 205 | - (void) willBePopped |
|---|
| 206 | { |
|---|
| 207 | // The user pressed Menu, but we've not been removed from the screen yet |
|---|
| 208 | |
|---|
| 209 | // always call super |
|---|
| 210 | [super willBePopped]; |
|---|
| 211 | /*Cancel everything we were doing*/ |
|---|
| 212 | [metaData cancelImport]; |
|---|
| 213 | cancelScan = YES; |
|---|
| 214 | [metaData setDelegate:nil]; |
|---|
| 215 | } |
|---|
| 216 | |
|---|
| 217 | - (void) wasPopped |
|---|
| 218 | { |
|---|
| 219 | // The user pressed Menu, removing us from the screen |
|---|
| 220 | // always call super |
|---|
| 221 | [super wasPopped]; |
|---|
| 222 | } |
|---|
| 223 | |
|---|
| 224 | - (void) willBeBuried |
|---|
| 225 | { |
|---|
| 226 | // The user just chose an option, and we will be taken off the screen |
|---|
| 227 | |
|---|
| 228 | /*Cancel everything we were doing*/ |
|---|
| 229 | [metaData cancelImport]; |
|---|
| 230 | cancelScan = YES; |
|---|
| 231 | // always call super |
|---|
| 232 | [super willBeBuried]; |
|---|
| 233 | } |
|---|
| 234 | |
|---|
| 235 | - (void) wasBuriedByPushingController: (BRLayerController *) controller |
|---|
| 236 | { |
|---|
| 237 | // The user chose an option and this controller is no longer on screen |
|---|
| 238 | |
|---|
| 239 | // always call super |
|---|
| 240 | [super wasBuriedByPushingController: controller]; |
|---|
| 241 | } |
|---|
| 242 | |
|---|
| 243 | - (void) willBeExhumed |
|---|
| 244 | { |
|---|
| 245 | // the user pressed Menu, but we've not been revealed yet |
|---|
| 246 | |
|---|
| 247 | /*Reload our display*/ |
|---|
| 248 | [self setNewPredicate:[SapphireApplianceController predicate]]; |
|---|
| 249 | // always call super |
|---|
| 250 | [super willBeExhumed]; |
|---|
| 251 | } |
|---|
| 252 | |
|---|
| 253 | - (void) wasExhumedByPoppingController: (BRLayerController *) controller |
|---|
| 254 | { |
|---|
| 255 | // handle being revealed when the user presses Menu |
|---|
| 256 | |
|---|
| 257 | // always call super |
|---|
| 258 | [super wasExhumedByPoppingController: controller]; |
|---|
| 259 | /*Check to see if dir is empty*/ |
|---|
| 260 | if(fileCount + dirCount == 0) |
|---|
| 261 | [[self stack] popController]; |
|---|
| 262 | else |
|---|
| 263 | /*Resume importing now that we are up again*/ |
|---|
| 264 | [metaData resumeImport]; |
|---|
| 265 | } |
|---|
| 266 | |
|---|
| 267 | - (long) itemCount |
|---|
| 268 | { |
|---|
| 269 | // return the number of items in your menu list here |
|---|
| 270 | if([_names count]) |
|---|
| 271 | return ( [ _names count]); |
|---|
| 272 | // Put up an empty item |
|---|
| 273 | return 1; |
|---|
| 274 | } |
|---|
| 275 | |
|---|
| 276 | - (id<BRMenuItemLayer>) itemForRow: (long) row |
|---|
| 277 | { |
|---|
| 278 | NSString * displayName=nil ; |
|---|
| 279 | /*Check for no items*/ |
|---|
| 280 | int nameCount = [_names count]; |
|---|
| 281 | FileClass fileCls=0; |
|---|
| 282 | if( nameCount == 0) |
|---|
| 283 | { |
|---|
| 284 | BRAdornedMenuItemLayer *result = [SapphireFrontRowCompat textMenuItemForScene:[self scene] folder:NO]; |
|---|
| 285 | [SapphireFrontRowCompat setTitle:BRLocalizedString(@"< EMPTY >", @"Empty directory") forMenu:result]; |
|---|
| 286 | return result; |
|---|
| 287 | } |
|---|
| 288 | if( row >= nameCount ) return ( nil ) ; |
|---|
| 289 | |
|---|
| 290 | /*Check our cache*/ |
|---|
| 291 | NSString *name = [_names objectAtIndex:row]; |
|---|
| 292 | id cached = [items objectForKey:name]; |
|---|
| 293 | if(cached != nil) |
|---|
| 294 | return cached; |
|---|
| 295 | // Pad filename to correcrtly display gem icons |
|---|
| 296 | BRAdornedMenuItemLayer * result = nil; |
|---|
| 297 | BOOL watched = NO; |
|---|
| 298 | BOOL favorite = NO; |
|---|
| 299 | BOOL utility = NO; |
|---|
| 300 | BRRenderScene *scene = [self scene]; |
|---|
| 301 | SapphireTheme *theme = [SapphireTheme sharedTheme]; |
|---|
| 302 | /*Is this a dir*/ |
|---|
| 303 | if(row < dirCount) |
|---|
| 304 | { |
|---|
| 305 | result = [SapphireFrontRowCompat textMenuItemForScene:scene folder:YES]; |
|---|
| 306 | id <SapphireDirectory> meta = [metaData metaDataForDirectory:name]; |
|---|
| 307 | watched = [meta watchedValue]; |
|---|
| 308 | favorite = [meta favoriteValue]; |
|---|
| 309 | } |
|---|
| 310 | /*Check for a file next*/ |
|---|
| 311 | else if(row < dirCount + fileCount) |
|---|
| 312 | { |
|---|
| 313 | result = [SapphireFrontRowCompat textMenuItemForScene:scene folder:NO]; |
|---|
| 314 | SapphireFileMetaData *meta = [metaData metaDataForFile:name]; |
|---|
| 315 | if(meta != nil) |
|---|
| 316 | { |
|---|
| 317 | fileCls=[meta fileClassValue]; |
|---|
| 318 | BOOL rightTextSet; |
|---|
| 319 | if(fileCls==FILE_CLASS_TV_SHOW) |
|---|
| 320 | { |
|---|
| 321 | SapphireEpisode *ep = [meta tvEpisode]; |
|---|
| 322 | /*Display episode number if availble*/ |
|---|
| 323 | int eps= [ep episodeNumberValue]; |
|---|
| 324 | int ep2= [ep lastEpisodeNumberValue]; |
|---|
| 325 | displayName=[ep episodeTitle] ; |
|---|
| 326 | if(eps>0) |
|---|
| 327 | { |
|---|
| 328 | NSArray *comp = [[metaData path] pathComponents]; |
|---|
| 329 | NSString *prefix = @""; |
|---|
| 330 | if([comp count] == 2 && [[comp objectAtIndex:0] isEqual:@"@TV"]) |
|---|
| 331 | /*Eps listed outside of seasons*/ |
|---|
| 332 | prefix = [NSString stringWithFormat:@"%02d/", [[ep season] seasonNumberValue]]; |
|---|
| 333 | if(ep2>0 && ep2 != eps) |
|---|
| 334 | [SapphireFrontRowCompat setRightJustifiedText:[NSString stringWithFormat:@" %@%02d-%02d", prefix, eps, ep2] forMenu:result]; |
|---|
| 335 | else |
|---|
| 336 | [SapphireFrontRowCompat setRightJustifiedText:[NSString stringWithFormat:@" %@%02d", prefix, eps] forMenu:result]; |
|---|
| 337 | rightTextSet = YES; |
|---|
| 338 | } |
|---|
| 339 | } |
|---|
| 340 | if(fileCls==FILE_CLASS_MOVIE) |
|---|
| 341 | { |
|---|
| 342 | SapphireMovie *movie = [meta movie]; |
|---|
| 343 | displayName=[movie title]; |
|---|
| 344 | /* Find out if we are displaying a virtual directoy we need to filter for */ |
|---|
| 345 | NSString *dirFilter=[[[metaData path] pathComponents] objectAtIndex:1]; |
|---|
| 346 | /*Add icons & stats (RIGHT)*/ |
|---|
| 347 | int top250 = [movie imdbTop250RankingValue]; |
|---|
| 348 | if([dirFilter isEqualToString:VIRTUAL_DIR_TOP250_KEY] && top250 > 0) |
|---|
| 349 | { |
|---|
| 350 | NSString *movieStatsTop250 = [NSString stringWithFormat:@"#%d ", top250]; |
|---|
| 351 | /* This list is already filtered so all displayed movies will have a top250 stat */ |
|---|
| 352 | [SapphireFrontRowCompat setRightJustifiedText:movieStatsTop250 forMenu:result]; |
|---|
| 353 | [SapphireFrontRowCompat setRightIcon:[theme gem:IMDB_GEM_KEY] forMenu:result]; |
|---|
| 354 | rightTextSet = YES; |
|---|
| 355 | } |
|---|
| 356 | else if([movie oscarsWonValue] > 0) |
|---|
| 357 | { |
|---|
| 358 | NSString *movieStatsOscar = [NSString stringWithFormat:@"%dx", [movie oscarsWonValue]]; |
|---|
| 359 | [SapphireFrontRowCompat setRightJustifiedText:movieStatsOscar forMenu:result]; |
|---|
| 360 | [SapphireFrontRowCompat setRightIcon:[theme gem:OSCAR_GEM_KEY] forMenu:result]; |
|---|
| 361 | rightTextSet = YES; |
|---|
| 362 | } |
|---|
| 363 | else if(top250 > 0) |
|---|
| 364 | { |
|---|
| 365 | NSString *movieStatsTop250 = [NSString stringWithFormat:@"#%d ", top250]; |
|---|
| 366 | [SapphireFrontRowCompat setRightJustifiedText:movieStatsTop250 forMenu:result]; |
|---|
| 367 | [SapphireFrontRowCompat setRightIcon:[theme gem:IMDB_GEM_KEY] forMenu:result]; |
|---|
| 368 | rightTextSet = YES; |
|---|
| 369 | } |
|---|
| 370 | } |
|---|
| 371 | watched = [meta watchedValue]; |
|---|
| 372 | favorite = [meta favoriteValue]; |
|---|
| 373 | NSString *sizeString = [meta sizeString]; |
|---|
| 374 | if(!rightTextSet && [sizeString length] > 1) |
|---|
| 375 | /*Fallback to size*/ |
|---|
| 376 | [SapphireFrontRowCompat setRightJustifiedText:sizeString forMenu:result]; |
|---|
| 377 | |
|---|
| 378 | } |
|---|
| 379 | } |
|---|
| 380 | /*Utility*/ |
|---|
| 381 | else |
|---|
| 382 | { |
|---|
| 383 | result = [SapphireFrontRowCompat textMenuItemForScene:scene folder:NO]; |
|---|
| 384 | utility = YES; |
|---|
| 385 | } |
|---|
| 386 | /*Add icons (LEFT)*/ |
|---|
| 387 | if(utility) [SapphireFrontRowCompat setLeftIcon:[theme gem:FAST_GEM_KEY] forMenu:result]; |
|---|
| 388 | else if(!watched) [SapphireFrontRowCompat setLeftIcon:[theme gem:BLUE_GEM_KEY] forMenu:result]; |
|---|
| 389 | else if(favorite) [SapphireFrontRowCompat setLeftIcon:[theme gem:YELLOW_GEM_KEY] forMenu:result]; |
|---|
| 390 | else if(fileCls==FILE_CLASS_AUDIO)[SapphireFrontRowCompat setLeftIcon:[theme gem:GREEN_GEM_KEY] forMenu:result]; |
|---|
| 391 | else [SapphireFrontRowCompat setLeftIcon:[theme gem:RED_GEM_KEY] forMenu:result]; |
|---|
| 392 | |
|---|
| 393 | // add text |
|---|
| 394 | if(displayName)name= displayName ; |
|---|
| 395 | name=[@" " stringByAppendingString: name] ; |
|---|
| 396 | [SapphireFrontRowCompat setTitle:name forMenu:result]; |
|---|
| 397 | [items setObject:result forKey:name]; |
|---|
| 398 | |
|---|
| 399 | return ( result ) ; |
|---|
| 400 | } |
|---|
| 401 | |
|---|
| 402 | - (NSString *) titleForRow: (long) row |
|---|
| 403 | { |
|---|
| 404 | |
|---|
| 405 | if ( row >= [ _names count] ) return ( nil ); |
|---|
| 406 | |
|---|
| 407 | NSString *result = [ _names objectAtIndex: row] ; |
|---|
| 408 | |
|---|
| 409 | return ([@" ????? " stringByAppendingString: result] ) ; |
|---|
| 410 | } |
|---|
| 411 | |
|---|
| 412 | - (long) rowForTitle: (NSString *) title |
|---|
| 413 | { |
|---|
| 414 | long result = -1; |
|---|
| 415 | long i, count = [self itemCount]; |
|---|
| 416 | for ( i = 0; i < count; i++ ) |
|---|
| 417 | { |
|---|
| 418 | if ( [title isEqualToString: [self titleForRow: i]] ) |
|---|
| 419 | { |
|---|
| 420 | result = i; |
|---|
| 421 | break; |
|---|
| 422 | } |
|---|
| 423 | } |
|---|
| 424 | |
|---|
| 425 | return ( result ); |
|---|
| 426 | } |
|---|
| 427 | |
|---|
| 428 | - (void)setNewPredicate:(NSPredicate *)newPredicate |
|---|
| 429 | { |
|---|
| 430 | [newPredicate retain]; |
|---|
| 431 | [predicate release]; |
|---|
| 432 | predicate = newPredicate; |
|---|
| 433 | if([metaData filterPredicate] != predicate) |
|---|
| 434 | [metaData setFilterPredicate:predicate]; |
|---|
| 435 | [self setListIcon:[SapphireApplianceController gemForPredicate:predicate]]; |
|---|
| 436 | [self reloadDirectoryContents]; |
|---|
| 437 | } |
|---|
| 438 | |
|---|
| 439 | - (void) itemSelected: (long) row |
|---|
| 440 | { |
|---|
| 441 | // This is called when the user presses play/pause on a list item |
|---|
| 442 | |
|---|
| 443 | if([_names count] == 0) |
|---|
| 444 | { |
|---|
| 445 | /*No items, so leave the empty dir*/ |
|---|
| 446 | [[self stack] popController]; |
|---|
| 447 | return; |
|---|
| 448 | } |
|---|
| 449 | |
|---|
| 450 | NSString *name = [_names objectAtIndex:row]; |
|---|
| 451 | |
|---|
| 452 | /*Check for dir*/ |
|---|
| 453 | if(row < dirCount) |
|---|
| 454 | { |
|---|
| 455 | /*Browse the subdir*/ |
|---|
| 456 | id controller = [[SapphireBrowser alloc] initWithScene:[self scene] metaData:[metaData metaDataForDirectory:name]]; |
|---|
| 457 | [controller setListTitle:[NSString stringWithFormat:@" %@",name]]; |
|---|
| 458 | [controller setListIcon:[self listIcon]]; |
|---|
| 459 | [[self stack] pushController:controller]; |
|---|
| 460 | [controller release]; |
|---|
| 461 | } |
|---|
| 462 | else if(row < dirCount + fileCount) |
|---|
| 463 | { |
|---|
| 464 | /*Play the file*/ |
|---|
| 465 | SapphireFileMetaData *currentPlayFile = [[metaData metaDataForFile:name] retain]; |
|---|
| 466 | if([currentPlayFile updateMetaData]) |
|---|
| 467 | [SapphireMetaDataSupport save:[currentPlayFile managedObjectContext]]; |
|---|
| 468 | |
|---|
| 469 | NSString *path = [currentPlayFile path]; |
|---|
| 470 | |
|---|
| 471 | /*Anonymous reporting*/ |
|---|
| 472 | SapphireSettings *settings = [SapphireSettings sharedSettings]; |
|---|
| 473 | if(![settings disableAnonymousReporting]) |
|---|
| 474 | { |
|---|
| 475 | NSMutableString *reqData = [NSMutableString string]; |
|---|
| 476 | NSMutableArray *reqComp = [NSMutableArray array]; |
|---|
| 477 | NSMutableURLRequest *request=nil; |
|---|
| 478 | NSString *ext=nil; |
|---|
| 479 | int fileClass=-1; |
|---|
| 480 | |
|---|
| 481 | if(path != 0) |
|---|
| 482 | { |
|---|
| 483 | [reqComp addObject:[NSString stringWithFormat:@"path=%@", [[path lastPathComponent]lowercaseString]]]; |
|---|
| 484 | ext=[[path pathExtension]lowercaseString] ; |
|---|
| 485 | } |
|---|
| 486 | |
|---|
| 487 | if([currentPlayFile fileClassValue] == FILE_CLASS_TV_SHOW) |
|---|
| 488 | { |
|---|
| 489 | SapphireEpisode *episode = [currentPlayFile tvEpisode]; |
|---|
| 490 | fileClass=1; |
|---|
| 491 | request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://appletv.nanopi.net/show.php"]]; |
|---|
| 492 | int ep = [episode episodeNumberValue]; |
|---|
| 493 | int season = [[episode season] seasonNumberValue]; |
|---|
| 494 | SapphireTVShow *show = [episode tvShow]; |
|---|
| 495 | NSString *showID = [show showPath]; |
|---|
| 496 | NSString *showName= [show name]; |
|---|
| 497 | |
|---|
| 498 | if(season != 0) |
|---|
| 499 | [reqComp addObject:[NSString stringWithFormat:@"season=%d", season]]; |
|---|
| 500 | if(ep != 0) |
|---|
| 501 | [reqComp addObject:[NSString stringWithFormat:@"ep=%d", ep]]; |
|---|
| 502 | if(showName != 0) |
|---|
| 503 | [reqComp addObject:[NSString stringWithFormat:@"showname=%@", showName]]; |
|---|
| 504 | if(showID != 0) |
|---|
| 505 | [reqComp addObject:[NSString stringWithFormat:@"showid=%@", showID]]; |
|---|
| 506 | } |
|---|
| 507 | else if([currentPlayFile fileClassValue] == FILE_CLASS_MOVIE) |
|---|
| 508 | { |
|---|
| 509 | fileClass=2; |
|---|
| 510 | request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://appletv.nanopi.net/movie.php"]]; |
|---|
| 511 | SapphireMovie *movie = [currentPlayFile movie]; |
|---|
| 512 | NSString *movieTitle=[movie title]; |
|---|
| 513 | int movieID=[movie imdbNumberValue]; |
|---|
| 514 | NSDate * releaseDate=[movie releaseDate]; |
|---|
| 515 | if(movieTitle != 0) |
|---|
| 516 | [reqComp addObject:[NSString stringWithFormat:@"title=%@", movieTitle]]; |
|---|
| 517 | if(releaseDate != 0) |
|---|
| 518 | [reqComp addObject:[NSString stringWithFormat:@"year=%@", [releaseDate descriptionWithCalendarFormat:@"%Y" timeZone:nil locale:[[NSUserDefaults standardUserDefaults] dictionaryRepresentation]]]]; |
|---|
| 519 | if(movieID != 0) |
|---|
| 520 | [reqComp addObject:[NSString stringWithFormat:@"movieid=%d", movieID]]; |
|---|
| 521 | } |
|---|
| 522 | else |
|---|
| 523 | { |
|---|
| 524 | request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://appletv.nanopi.net/ext.php"]]; |
|---|
| 525 | if([currentPlayFile fileClassValue] == FILE_CLASS_UNKNOWN) |
|---|
| 526 | fileClass=0; |
|---|
| 527 | if([currentPlayFile fileClassValue] == FILE_CLASS_AUDIO) |
|---|
| 528 | fileClass=3; |
|---|
| 529 | if([currentPlayFile fileClassValue] == FILE_CLASS_OTHER) |
|---|
| 530 | fileClass=5; |
|---|
| 531 | else |
|---|
| 532 | fileClass=99; |
|---|
| 533 | } |
|---|
| 534 | |
|---|
| 535 | if(ext!=0) |
|---|
| 536 | { |
|---|
| 537 | [reqComp addObject:[NSString stringWithFormat:@"filetype=%d",fileClass]]; |
|---|
| 538 | [reqComp addObject:[NSString stringWithFormat:@"extension=%@", ext]]; |
|---|
| 539 | if([SapphireFrontRowCompat usingFrontRow]) |
|---|
| 540 | [reqComp addObject:[NSString stringWithFormat:@"ckey=FRONTROW-%@-%d",ext,fileClass]]; |
|---|
| 541 | else |
|---|
| 542 | [reqComp addObject:[NSString stringWithFormat:@"ckey=ATV-%@-%d",ext,fileClass]]; |
|---|
| 543 | } |
|---|
| 544 | |
|---|
| 545 | int count = [reqComp count]; |
|---|
| 546 | int i; |
|---|
| 547 | for(i=0; i<count-1; i++) |
|---|
| 548 | { |
|---|
| 549 | [reqData appendFormat:@"%@&", [reqComp objectAtIndex:i]]; |
|---|
| 550 | } |
|---|
| 551 | if(count) |
|---|
| 552 | [reqData appendFormat:@"%@", [reqComp objectAtIndex:i]]; |
|---|
| 553 | |
|---|
| 554 | NSData *postData = [reqData dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES]; |
|---|
| 555 | |
|---|
| 556 | [request setHTTPMethod:@"POST"]; |
|---|
| 557 | [request setValue:[NSString stringWithFormat:@"%d", [postData length]] forHTTPHeaderField:@"Content-Length"]; |
|---|
| 558 | [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; |
|---|
| 559 | [request setHTTPBody:postData]; |
|---|
| 560 | [request setCachePolicy:NSURLRequestReloadIgnoringCacheData]; |
|---|
| 561 | /*Trigger the req*/ |
|---|
| 562 | NSURLDownload *download = [[NSURLDownload alloc] initWithRequest:request delegate:nil]; |
|---|
| 563 | [download autorelease]; |
|---|
| 564 | } |
|---|
| 565 | |
|---|
| 566 | if ([currentPlayFile fileContainerTypeValue] == FILE_CONTAINER_TYPE_VIDEO_TS) |
|---|
| 567 | { |
|---|
| 568 | BRDVDMediaAsset *asset = [[BRDVDMediaAsset alloc] initWithPath:path]; |
|---|
| 569 | SapphireDVDLoadingController *controller = [[SapphireDVDLoadingController alloc] initWithScene:[self scene] forAsset:asset]; |
|---|
| 570 | [asset release]; |
|---|
| 571 | [[self stack] pushController:controller]; |
|---|
| 572 | [controller release]; |
|---|
| 573 | } |
|---|
| 574 | else if([[NSFileManager defaultManager] acceptFilePath:path] && [currentPlayFile hasVideoValue]) |
|---|
| 575 | { |
|---|
| 576 | /*Video*/ |
|---|
| 577 | /*Set the asset resume time*/ |
|---|
| 578 | NSURL *url = [NSURL fileURLWithPath:path]; |
|---|
| 579 | SapphireMedia *asset =[[SapphireMedia alloc] initWithMediaURL:url]; |
|---|
| 580 | [asset setResumeTime:[currentPlayFile resumeTimeValue]]; |
|---|
| 581 | |
|---|
| 582 | /*Get the player*/ |
|---|
| 583 | SapphireVideoPlayer *player = [[SapphireVideoPlayer alloc] init]; |
|---|
| 584 | NSError *error = nil; |
|---|
| 585 | [player setMedia:asset error:&error]; |
|---|
| 586 | |
|---|
| 587 | /*and go*/ |
|---|
| 588 | SapphireVideoPlayerController *controller = [[SapphireVideoPlayerController alloc] initWithScene:[self scene]]; |
|---|
| 589 | [controller setPlayFile:currentPlayFile]; |
|---|
| 590 | [controller setAllowsResume:YES]; |
|---|
| 591 | [controller setVideoPlayer:player]; |
|---|
| 592 | [[self stack] pushController:controller]; |
|---|
| 593 | |
|---|
| 594 | [asset release]; |
|---|
| 595 | [player release]; |
|---|
| 596 | [controller release]; |
|---|
| 597 | } |
|---|
| 598 | else |
|---|
| 599 | { |
|---|
| 600 | /*Audio*/ |
|---|
| 601 | /*Set the asset*/ |
|---|
| 602 | NSURL *url = [NSURL fileURLWithPath:path]; |
|---|
| 603 | SapphireAudioMedia *asset =[[SapphireAudioMedia alloc] initWithMediaURL:url]; |
|---|
| 604 | [asset setResumeTime:[currentPlayFile resumeTimeValue]]; |
|---|
| 605 | |
|---|
| 606 | SapphireAudioPlayer *player = [[SapphireAudioPlayer alloc] init]; |
|---|
| 607 | NSError *error = nil; |
|---|
| 608 | [player setMedia:asset inTracklist:[NSArray arrayWithObject:asset] error:&error]; |
|---|
| 609 | |
|---|
| 610 | /*and go*/ |
|---|
| 611 | BRMusicNowPlayingController *controller = [BRMusicNowPlayingController alloc]; |
|---|
| 612 | if([controller respondsToSelector:@selector(initWithScene:)]) |
|---|
| 613 | controller = [controller initWithScene:[self scene]]; |
|---|
| 614 | else |
|---|
| 615 | controller = [controller init]; |
|---|
| 616 | [controller setPlayer:player]; |
|---|
| 617 | [player setElapsedPlaybackTime:[currentPlayFile resumeTimeValue]]; |
|---|
| 618 | [player play]; |
|---|
| 619 | [[self stack] pushController:controller]; |
|---|
| 620 | |
|---|
| 621 | [asset release]; |
|---|
| 622 | [player release]; |
|---|
| 623 | [controller release]; |
|---|
| 624 | } |
|---|
| 625 | } |
|---|
| 626 | else |
|---|
| 627 | { |
|---|
| 628 | /*Do a scan*/ |
|---|
| 629 | cancelScan = NO; |
|---|
| 630 | [metaData scanForNewFilesWithDelegate:self skipDirectories:[NSMutableSet set]]; |
|---|
| 631 | } |
|---|
| 632 | } |
|---|
| 633 | |
|---|
| 634 | - (void)gotSubFiles:(NSArray *)subs |
|---|
| 635 | { |
|---|
| 636 | [self reloadDirectoryContents]; |
|---|
| 637 | } |
|---|
| 638 | |
|---|
| 639 | - (void)scanningDir:(NSString *)dir |
|---|
| 640 | { |
|---|
| 641 | } |
|---|
| 642 | |
|---|
| 643 | - (BOOL)getSubFilesCanceled |
|---|
| 644 | { |
|---|
| 645 | return cancelScan; |
|---|
| 646 | } |
|---|
| 647 | |
|---|
| 648 | - (id<BRMediaPreviewController>) previewControlForItem: (long) row |
|---|
| 649 | { |
|---|
| 650 | return [self previewControllerForItem:row]; |
|---|
| 651 | } |
|---|
| 652 | |
|---|
| 653 | - (id<BRMediaPreviewController>) previewControllerForItem: (long) row |
|---|
| 654 | { |
|---|
| 655 | // If subclassing BRMediaMenuController, this function is called when the selection cursor |
|---|
| 656 | // passes over an item. |
|---|
| 657 | if(row >= [_names count]) |
|---|
| 658 | return nil; |
|---|
| 659 | /*Check to see if it is a dir or file*/ |
|---|
| 660 | NSString *name = [_names objectAtIndex:row]; |
|---|
| 661 | if(row < dirCount + fileCount) |
|---|
| 662 | { |
|---|
| 663 | SapphireMediaPreview *preview = [[SapphireMediaPreview alloc] initWithScene:[self scene]]; |
|---|
| 664 | /*Check for dir*/ |
|---|
| 665 | if(row < dirCount) |
|---|
| 666 | [preview setMetaData:[metaData metaDataForDirectory:name] inMetaData:metaData]; |
|---|
| 667 | else |
|---|
| 668 | [preview setMetaData:[metaData metaDataForFile:name] inMetaData:metaData]; |
|---|
| 669 | [preview setShowsMetadataImmediately:justReloaded]; |
|---|
| 670 | justReloaded = NO; |
|---|
| 671 | /*And go*/ |
|---|
| 672 | return [preview autorelease]; |
|---|
| 673 | } |
|---|
| 674 | return ( nil ); |
|---|
| 675 | } |
|---|
| 676 | |
|---|
| 677 | - (BOOL)brEventAction:(BREvent *)event |
|---|
| 678 | { |
|---|
| 679 | BREventPageUsageHash hashVal = [event pageUsageHash]; |
|---|
| 680 | if ([(BRControllerStack *)[self stack] peekController] != self) |
|---|
| 681 | hashVal = 0; |
|---|
| 682 | |
|---|
| 683 | int row = [self getSelection]; |
|---|
| 684 | |
|---|
| 685 | switch (hashVal) |
|---|
| 686 | { |
|---|
| 687 | case kBREventTapRight: |
|---|
| 688 | { |
|---|
| 689 | id meta = nil; |
|---|
| 690 | if(row >= [_names count]) |
|---|
| 691 | break; |
|---|
| 692 | |
|---|
| 693 | NSString *name = [_names objectAtIndex:row]; |
|---|
| 694 | |
|---|
| 695 | /*Get metadata*/ |
|---|
| 696 | if(row < dirCount) |
|---|
| 697 | meta = [metaData metaDataForDirectory:name]; |
|---|
| 698 | else if (row < dirCount + fileCount) |
|---|
| 699 | meta = [metaData metaDataForFile:name]; |
|---|
| 700 | else |
|---|
| 701 | break; |
|---|
| 702 | /*Do mark menu*/ |
|---|
| 703 | id controller = [[SapphireMarkMenu alloc] initWithScene:[self scene] metaData:meta]; |
|---|
| 704 | [(SapphireMarkMenu *)controller setListTitle:name]; |
|---|
| 705 | [[self stack] pushController:controller]; |
|---|
| 706 | [controller release]; |
|---|
| 707 | return YES; |
|---|
| 708 | } |
|---|
| 709 | case kBREventTapLeft: |
|---|
| 710 | { |
|---|
| 711 | [self setNewPredicate:[SapphireApplianceController nextPredicate]]; |
|---|
| 712 | return YES; |
|---|
| 713 | } |
|---|
| 714 | } |
|---|
| 715 | return [super brEventAction:event]; |
|---|
| 716 | } |
|---|
| 717 | |
|---|
| 718 | - (void)updateCompleteForFile:(NSString *)file |
|---|
| 719 | { |
|---|
| 720 | /*Get the file*/ |
|---|
| 721 | [items removeObjectForKey:file]; |
|---|
| 722 | /*Relead the list and render*/ |
|---|
| 723 | BRListControl *list = [self list]; |
|---|
| 724 | [list reload]; |
|---|
| 725 | [SapphireFrontRowCompat renderScene:[self scene]]; |
|---|
| 726 | } |
|---|
| 727 | |
|---|
| 728 | @end |
|---|