| 1 | // |
|---|
| 2 | // SapphireApplianceController.m |
|---|
| 3 | // Sapphire |
|---|
| 4 | // |
|---|
| 5 | // Created by pnmerrill on 6/20/07. |
|---|
| 6 | // Copyright 2007 www.nanopi.net. All rights reserved. |
|---|
| 7 | // |
|---|
| 8 | |
|---|
| 9 | #import "SapphireApplianceController.h" |
|---|
| 10 | #import <BackRow/BackRow.h> |
|---|
| 11 | #import "SapphireBrowser.h" |
|---|
| 12 | #import "SapphireMetaData.h" |
|---|
| 13 | #import "SapphirePredicates.h" |
|---|
| 14 | #import "SapphireSettings.h" |
|---|
| 15 | #import "SapphireTheme.h" |
|---|
| 16 | #import "SapphireTVDirectory.h" |
|---|
| 17 | #import "SapphireMovieDirectory.h" |
|---|
| 18 | |
|---|
| 19 | #import "SapphireImporterDataMenu.h" |
|---|
| 20 | #import "SapphireFileDataImporter.h" |
|---|
| 21 | #import "SapphireTVShowImporter.h" |
|---|
| 22 | #import "SapphireMovieImporter.h" |
|---|
| 23 | #import "SapphireAllImporter.h" |
|---|
| 24 | #import "SapphireFrontRowCompat.h" |
|---|
| 25 | |
|---|
| 26 | #define BROWSER_MENU_ITEM BRLocalizedString(@" Browse", @"Browser Menu Item") |
|---|
| 27 | #define ALL_IMPORT_MENU_ITEM BRLocalizedString(@" Import All Data", @"All Importer Menu Item") |
|---|
| 28 | #define SETTINGS_MENU_ITEM BRLocalizedString(@" Settings", @"Settings Menu Item") |
|---|
| 29 | #define RESET_MENU_ITEM BRLocalizedString(@" Reset the thing already", @"UI Quit") |
|---|
| 30 | |
|---|
| 31 | @interface SapphireApplianceController (private) |
|---|
| 32 | - (void)setMenuFromSettings; |
|---|
| 33 | - (void)recreateMenu; |
|---|
| 34 | @end |
|---|
| 35 | |
|---|
| 36 | @implementation SapphireApplianceController |
|---|
| 37 | |
|---|
| 38 | static NSArray *predicates = nil; |
|---|
| 39 | |
|---|
| 40 | + (void)initialize |
|---|
| 41 | { |
|---|
| 42 | predicates = [[NSArray alloc] initWithObjects:[[SapphireUnwatchedPredicate alloc] init], [[SapphireFavoritePredicate alloc] init], [[SapphireTopShowPredicate alloc] init], nil]; |
|---|
| 43 | [predicates makeObjectsPerformSelector:@selector(release)]; |
|---|
| 44 | if([SapphireFrontRowCompat usingFrontRow]) |
|---|
| 45 | { |
|---|
| 46 | NSString *compatPath = [[[NSBundle bundleForClass:[self class]] bundlePath] stringByAppendingString:@"/Contents/Frameworks/CompatClasses.framework"]; |
|---|
| 47 | NSBundle *compat = [NSBundle bundleWithPath:compatPath]; |
|---|
| 48 | [compat load]; |
|---|
| 49 | } |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | + (SapphirePredicate *)predicate |
|---|
| 53 | { |
|---|
| 54 | SapphireSettings *settings = [SapphireSettings sharedSettings]; |
|---|
| 55 | int index = [settings indexOfLastPredicate]; |
|---|
| 56 | if(index == NSNotFound) |
|---|
| 57 | return nil; |
|---|
| 58 | return [predicates objectAtIndex:index]; |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | + (SapphirePredicate *)nextPredicate |
|---|
| 62 | { |
|---|
| 63 | SapphireSettings *settings = [SapphireSettings sharedSettings]; |
|---|
| 64 | int index = [settings indexOfLastPredicate]; |
|---|
| 65 | int newIndex; |
|---|
| 66 | switch(index) |
|---|
| 67 | { |
|---|
| 68 | case NSNotFound: |
|---|
| 69 | newIndex = 0; |
|---|
| 70 | if([settings displayUnwatched]) |
|---|
| 71 | break; |
|---|
| 72 | case 0: |
|---|
| 73 | newIndex = 1; |
|---|
| 74 | if([settings displayFavorites]) |
|---|
| 75 | break; |
|---|
| 76 | case 1: |
|---|
| 77 | newIndex = 2; |
|---|
| 78 | if([settings displayTopShows]) |
|---|
| 79 | break; |
|---|
| 80 | default: |
|---|
| 81 | newIndex = NSNotFound; |
|---|
| 82 | } |
|---|
| 83 | [settings setIndexOfLastPredicate:newIndex]; |
|---|
| 84 | if(newIndex == NSNotFound) |
|---|
| 85 | return nil; |
|---|
| 86 | return [predicates objectAtIndex:newIndex]; |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | + (BRTexture *)gemForPredicate:(SapphirePredicate *)predicate |
|---|
| 90 | { |
|---|
| 91 | SapphireTheme *theme = [SapphireTheme sharedTheme]; |
|---|
| 92 | if(predicate == nil) |
|---|
| 93 | return [theme gem:RED_GEM_KEY]; |
|---|
| 94 | if([predicate isKindOfClass:[SapphireUnwatchedPredicate class]]) |
|---|
| 95 | return [theme gem:BLUE_GEM_KEY]; |
|---|
| 96 | if([predicate isKindOfClass:[SapphireFavoritePredicate class]]) |
|---|
| 97 | return [theme gem:YELLOW_GEM_KEY]; |
|---|
| 98 | if([predicate isKindOfClass:[SapphireTopShowPredicate class]]) |
|---|
| 99 | return [theme gem:GREEN_GEM_KEY]; |
|---|
| 100 | return nil; |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | + (NSString *) rootMenuLabel |
|---|
| 104 | { |
|---|
| 105 | return (@"net.pmerrill.Sapphire" ); |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | - (SapphireImporterDataMenu *)allImporterForCollection:(SapphireMetaDataCollection *)collection |
|---|
| 109 | { |
|---|
| 110 | SapphireFileDataImporter *fileImp = [[SapphireFileDataImporter alloc] init]; |
|---|
| 111 | SapphireTVShowImporter *tvImp = [[SapphireTVShowImporter alloc] initWithSavedSetting:[NSHomeDirectory() stringByAppendingPathComponent:@"Library/Application Support/Sapphire/tvdata.plist"]]; |
|---|
| 112 | // SapphireMovieImporter *movImp = [[SapphireMovieImporter alloc] initWithSavedSetting:[NSHomeDirectory() stringByAppendingPathComponent:@"Library/Application Support/Sapphire/movieData.plist"]]; |
|---|
| 113 | SapphireAllImporter *allImp = [[SapphireAllImporter alloc] initWithImporters:[NSArray arrayWithObjects:fileImp, tvImp,nil]]; |
|---|
| 114 | [fileImp release]; |
|---|
| 115 | [tvImp release]; |
|---|
| 116 | // [movImp release]; |
|---|
| 117 | SapphireImporterDataMenu *ret = [[SapphireImporterDataMenu alloc] initWithScene:[self scene] metaDataCollection:collection importer:allImp]; |
|---|
| 118 | [allImp release]; |
|---|
| 119 | return [ret autorelease]; |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | - (id) initWithScene: (BRRenderScene *) scene |
|---|
| 123 | { |
|---|
| 124 | self = [super initWithScene:scene]; |
|---|
| 125 | |
|---|
| 126 | //Setup the theme's scene |
|---|
| 127 | SapphireTheme *theme = [SapphireTheme sharedTheme]; |
|---|
| 128 | [theme setScene:[self scene]]; |
|---|
| 129 | |
|---|
| 130 | metaCollection = [[SapphireMetaDataCollection alloc] initWithFile:[NSHomeDirectory() stringByAppendingPathComponent:@"Library/Application Support/Sapphire/metaData.plist"]]; |
|---|
| 131 | |
|---|
| 132 | settings = [[SapphireSettings alloc] initWithScene:[self scene] settingsPath:[NSHomeDirectory() stringByAppendingPathComponent:@"Library/Application Support/Sapphire/settings.plist"] metaDataCollection:metaCollection] ; |
|---|
| 133 | [self setListTitle: BRLocalizedString(@"Main Menu", @"")]; |
|---|
| 134 | [settings setListTitle: BRLocalizedString(@"Settings", @"Settings Menu Item")] ; |
|---|
| 135 | [settings setListIcon: [theme gem:GEAR_GEM_KEY]]; |
|---|
| 136 | [[self list] setDatasource:self]; |
|---|
| 137 | |
|---|
| 138 | return self; |
|---|
| 139 | } |
|---|
| 140 | |
|---|
| 141 | - (void)dealloc |
|---|
| 142 | { |
|---|
| 143 | [names release]; |
|---|
| 144 | [controllers release]; |
|---|
| 145 | [masterNames release]; |
|---|
| 146 | [masterControllers release]; |
|---|
| 147 | [metaCollection release]; |
|---|
| 148 | [SapphireSettings relinquishSettings]; |
|---|
| 149 | [settings release]; |
|---|
| 150 | [super dealloc]; |
|---|
| 151 | } |
|---|
| 152 | |
|---|
| 153 | - (void)recreateMenu |
|---|
| 154 | { |
|---|
| 155 | SapphireImporterDataMenu *allImporter = [self allImporterForCollection:metaCollection]; |
|---|
| 156 | NSMutableArray *mutableMasterNames = [[NSMutableArray alloc] init]; |
|---|
| 157 | NSMutableArray *mutableMasterControllers = [[NSMutableArray alloc] init]; |
|---|
| 158 | BRTexture *predicateGem = [SapphireApplianceController gemForPredicate:[SapphireApplianceController predicate]]; |
|---|
| 159 | |
|---|
| 160 | SapphireTVDirectory *tvDir = [[SapphireTVDirectory alloc] initWithCollection:metaCollection]; |
|---|
| 161 | SapphireBrowser *tvBrowser = [[SapphireBrowser alloc] initWithScene:[self scene] metaData:tvDir]; |
|---|
| 162 | [tvBrowser setListTitle:BRLocalizedString(@"TV Shows", nil)]; |
|---|
| 163 | [tvBrowser setListIcon:predicateGem]; |
|---|
| 164 | [mutableMasterNames addObject:BRLocalizedString(@" TV Shows", nil)]; |
|---|
| 165 | [mutableMasterControllers addObject:tvBrowser]; |
|---|
| 166 | [tvBrowser release]; |
|---|
| 167 | |
|---|
| 168 | SapphireMovieDirectory *movieDir = [[SapphireMovieDirectory alloc] initWithCollection:metaCollection]; |
|---|
| 169 | SapphireBrowser *movieBrowser = [[SapphireBrowser alloc] initWithScene:[self scene] metaData:movieDir]; |
|---|
| 170 | [movieBrowser setListTitle:BRLocalizedString(@"Movies", nil)]; |
|---|
| 171 | [movieBrowser setListIcon:predicateGem]; |
|---|
| 172 | [mutableMasterNames addObject:BRLocalizedString(@" Movies", nil)]; |
|---|
| 173 | [mutableMasterControllers addObject:movieBrowser]; |
|---|
| 174 | [movieBrowser release]; |
|---|
| 175 | |
|---|
| 176 | NSEnumerator *browserPointsEnum = [[metaCollection collectionDirectories] objectEnumerator]; |
|---|
| 177 | NSString *browserPoint = nil; |
|---|
| 178 | while((browserPoint = [browserPointsEnum nextObject]) != nil) |
|---|
| 179 | { |
|---|
| 180 | if(![metaCollection skipCollection:browserPoint]) |
|---|
| 181 | [[metaCollection directoryForPath:browserPoint] loadMetaData]; |
|---|
| 182 | if([metaCollection hideCollection:browserPoint]) |
|---|
| 183 | continue; |
|---|
| 184 | SapphireBrowser *browser = [[SapphireBrowser alloc] initWithScene:[self scene] metaData:[metaCollection directoryForPath:browserPoint]]; |
|---|
| 185 | [browser setListTitle:[browserPoint lastPathComponent]]; |
|---|
| 186 | [browser setListIcon:predicateGem]; |
|---|
| 187 | [mutableMasterNames addObject:[NSString stringWithFormat:@" %@", browserPoint]]; |
|---|
| 188 | [mutableMasterControllers addObject:browser]; |
|---|
| 189 | [browser release]; |
|---|
| 190 | } |
|---|
| 191 | [mutableMasterNames addObjectsFromArray:[NSArray arrayWithObjects: |
|---|
| 192 | ALL_IMPORT_MENU_ITEM, |
|---|
| 193 | SETTINGS_MENU_ITEM, |
|---|
| 194 | RESET_MENU_ITEM, |
|---|
| 195 | nil]]; |
|---|
| 196 | [mutableMasterControllers addObjectsFromArray:[NSArray arrayWithObjects: |
|---|
| 197 | allImporter, |
|---|
| 198 | settings, |
|---|
| 199 | nil]]; |
|---|
| 200 | masterNames = [[NSArray alloc] initWithArray:mutableMasterNames]; |
|---|
| 201 | masterControllers = [[NSArray alloc] initWithArray:mutableMasterControllers]; |
|---|
| 202 | [mutableMasterNames release]; |
|---|
| 203 | [mutableMasterControllers release]; |
|---|
| 204 | |
|---|
| 205 | names = [[NSMutableArray alloc] init]; |
|---|
| 206 | controllers = [[NSMutableArray alloc] init]; |
|---|
| 207 | [self setMenuFromSettings]; |
|---|
| 208 | } |
|---|
| 209 | |
|---|
| 210 | - (void)setMenuFromSettings |
|---|
| 211 | { |
|---|
| 212 | [names removeAllObjects]; |
|---|
| 213 | [controllers removeAllObjects]; |
|---|
| 214 | [names addObjectsFromArray:masterNames]; |
|---|
| 215 | [controllers addObjectsFromArray:masterControllers]; |
|---|
| 216 | if([settings disableUIQuit]) |
|---|
| 217 | [names removeLastObject]; |
|---|
| 218 | } |
|---|
| 219 | |
|---|
| 220 | - (void) willBePushed |
|---|
| 221 | { |
|---|
| 222 | // We're about to be placed on screen, but we're not yet there |
|---|
| 223 | [self recreateMenu]; |
|---|
| 224 | [[self list] reload]; |
|---|
| 225 | // always call super |
|---|
| 226 | [super willBePushed]; |
|---|
| 227 | } |
|---|
| 228 | |
|---|
| 229 | - (void) wasPushed |
|---|
| 230 | { |
|---|
| 231 | // We've just been put on screen, the user can see this controller's content now |
|---|
| 232 | |
|---|
| 233 | // always call super |
|---|
| 234 | [super wasPushed]; |
|---|
| 235 | } |
|---|
| 236 | |
|---|
| 237 | - (void) willBePopped |
|---|
| 238 | { |
|---|
| 239 | // The user pressed Menu, but we've not been removed from the screen yet |
|---|
| 240 | |
|---|
| 241 | // always call super |
|---|
| 242 | [super willBePopped]; |
|---|
| 243 | } |
|---|
| 244 | |
|---|
| 245 | - (void) wasPopped |
|---|
| 246 | { |
|---|
| 247 | // The user pressed Menu, removing us from the screen |
|---|
| 248 | |
|---|
| 249 | // always call super |
|---|
| 250 | [super wasPopped]; |
|---|
| 251 | } |
|---|
| 252 | |
|---|
| 253 | - (void) willBeBuried |
|---|
| 254 | { |
|---|
| 255 | // The user just chose an option, and we will be taken off the screen |
|---|
| 256 | |
|---|
| 257 | // always call super |
|---|
| 258 | [super willBeBuried]; |
|---|
| 259 | } |
|---|
| 260 | |
|---|
| 261 | - (void) wasBuriedByPushingController: (BRLayerController *) controller |
|---|
| 262 | { |
|---|
| 263 | // The user chose an option and this controller os no longer on screen |
|---|
| 264 | |
|---|
| 265 | // always call super |
|---|
| 266 | [super wasBuriedByPushingController: controller]; |
|---|
| 267 | } |
|---|
| 268 | |
|---|
| 269 | - (void) willBeExhumed |
|---|
| 270 | { |
|---|
| 271 | // the user pressed Menu, but we've not been revealed yet |
|---|
| 272 | |
|---|
| 273 | // always call super |
|---|
| 274 | [super willBeExhumed]; |
|---|
| 275 | [self setMenuFromSettings]; |
|---|
| 276 | [[self list] reload]; |
|---|
| 277 | [SapphireFrontRowCompat renderScene:[self scene]]; |
|---|
| 278 | } |
|---|
| 279 | |
|---|
| 280 | - (void) wasExhumedByPoppingController: (BRLayerController *) controller |
|---|
| 281 | { |
|---|
| 282 | // handle being revealed when the user presses Menu |
|---|
| 283 | |
|---|
| 284 | // always call super |
|---|
| 285 | [super wasExhumedByPoppingController: controller]; |
|---|
| 286 | } |
|---|
| 287 | |
|---|
| 288 | - (long) itemCount |
|---|
| 289 | { |
|---|
| 290 | // return the number of items in your menu list here |
|---|
| 291 | return ( [ names count]); |
|---|
| 292 | } |
|---|
| 293 | |
|---|
| 294 | - (id<BRMenuItemLayer>) itemForRow: (long) row |
|---|
| 295 | { |
|---|
| 296 | /* |
|---|
| 297 | // build a BRTextMenuItemLayer or a BRAdornedMenuItemLayer, etc. here |
|---|
| 298 | // return that object, it will be used to display the list item. |
|---|
| 299 | */ |
|---|
| 300 | if( row > [names count] ) return ( nil ) ; |
|---|
| 301 | |
|---|
| 302 | BRAdornedMenuItemLayer * result = nil ; |
|---|
| 303 | NSString *name = [names objectAtIndex:row]; |
|---|
| 304 | result = [SapphireFrontRowCompat textMenuItemForScene:[self scene] folder:YES]; |
|---|
| 305 | |
|---|
| 306 | SapphireTheme *theme = [SapphireTheme sharedTheme]; |
|---|
| 307 | if([name isEqual: ALL_IMPORT_MENU_ITEM]) [SapphireFrontRowCompat setLeftIcon:[theme gem:GEAR_GEM_KEY] forMenu:result]; |
|---|
| 308 | // else if([name isEqual: AGENT_IMPORT_MENU_ITEM]) [SapphireFrontRowCompat setLeftIcon:[theme gem:GEAR_GEM_KEY] forMenu:result]; |
|---|
| 309 | else if([name isEqual: SETTINGS_MENU_ITEM]) [SapphireFrontRowCompat setLeftIcon:[theme gem:GEAR_GEM_KEY] forMenu:result]; |
|---|
| 310 | else if([name isEqual: RESET_MENU_ITEM]) [SapphireFrontRowCompat setLeftIcon:[theme gem:CONE_GEM_KEY] forMenu:result]; |
|---|
| 311 | else [SapphireFrontRowCompat setLeftIcon:[SapphireApplianceController gemForPredicate:[SapphireApplianceController predicate]] forMenu:result]; |
|---|
| 312 | |
|---|
| 313 | // add text |
|---|
| 314 | [SapphireFrontRowCompat setTitle:name forMenu:result]; |
|---|
| 315 | |
|---|
| 316 | return ( result ) ; |
|---|
| 317 | } |
|---|
| 318 | |
|---|
| 319 | - (NSString *) titleForRow: (long) row |
|---|
| 320 | { |
|---|
| 321 | |
|---|
| 322 | if ( row > [ names count] ) return ( nil ); |
|---|
| 323 | |
|---|
| 324 | NSString *result = [ names objectAtIndex: row] ; |
|---|
| 325 | return ( result ) ; |
|---|
| 326 | /* |
|---|
| 327 | // return the title for the list item at the given index here |
|---|
| 328 | return ( @"Sapphire" ); |
|---|
| 329 | */ |
|---|
| 330 | } |
|---|
| 331 | |
|---|
| 332 | - (long) rowForTitle: (NSString *) title |
|---|
| 333 | { |
|---|
| 334 | long result = -1; |
|---|
| 335 | long i, count = [self itemCount]; |
|---|
| 336 | for ( i = 0; i < count; i++ ) |
|---|
| 337 | { |
|---|
| 338 | if ( [title isEqualToString: [self titleForRow: i]] ) |
|---|
| 339 | { |
|---|
| 340 | result = i; |
|---|
| 341 | break; |
|---|
| 342 | } |
|---|
| 343 | } |
|---|
| 344 | |
|---|
| 345 | return ( result ); |
|---|
| 346 | } |
|---|
| 347 | |
|---|
| 348 | - (void) itemSelected: (long) row |
|---|
| 349 | { |
|---|
| 350 | // This is called when the user presses play/pause on a list item |
|---|
| 351 | |
|---|
| 352 | if(row == [controllers count]) |
|---|
| 353 | [[NSApplication sharedApplication] terminate:self]; |
|---|
| 354 | id controller = [controllers objectAtIndex:row]; |
|---|
| 355 | [[self stack] pushController:controller]; |
|---|
| 356 | } |
|---|
| 357 | |
|---|
| 358 | - (id<BRMediaPreviewController>) previewControllerForItem: (long) item |
|---|
| 359 | { |
|---|
| 360 | // If subclassing BRMediaMenuController, this function is called when the selection cursor |
|---|
| 361 | // passes over an item. |
|---|
| 362 | return ( nil ); |
|---|
| 363 | } |
|---|
| 364 | |
|---|
| 365 | @end |
|---|