// // SapphireApplianceController.m // Sapphire // // Created by pnmerrill on 6/20/07. // Copyright 2007 www.nanopi.net. All rights reserved. // #import "SapphireApplianceController.h" #import #include #import "SapphireBrowser.h" #import "SapphireMetaData.h" #import "SapphirePredicates.h" #import "SapphireSettings.h" #import "SapphireTheme.h" #import "SapphireTVDirectory.h" #import "SapphireMovieDirectory.h" #import "SapphireImporterDataMenu.h" #import "SapphireFileDataImporter.h" #import "SapphireTVShowImporter.h" #import "SapphireMovieImporter.h" #import "SapphireAllImporter.h" #import "SapphireFrontRowCompat.h" #import "SapphireVirtualDirectoryLoading.h" #define BROWSER_MENU_ITEM BRLocalizedString(@" Browse", @"Browser Menu Item") #define ALL_IMPORT_MENU_ITEM BRLocalizedString(@" Import All Data", @"All Importer Menu Item") #define SETTINGS_MENU_ITEM BRLocalizedString(@" Settings", @"Settings Menu Item") #define RESET_MENU_ITEM BRLocalizedString(@" Reset the thing already", @"UI Quit") @interface SapphireApplianceController (private) - (void)setMenuFromSettings; - (void)recreateMenu; @end @implementation SapphireApplianceController static NSArray *predicates = nil; + (void)initialize { predicates = [[NSArray alloc] initWithObjects:[[SapphireUnwatchedPredicate alloc] init], [[SapphireFavoritePredicate alloc] init], [[SapphireTopShowPredicate alloc] init], nil]; [predicates makeObjectsPerformSelector:@selector(release)]; if([SapphireFrontRowCompat usingFrontRow]) { NSString *compatPath = [[[NSBundle bundleForClass:[self class]] bundlePath] stringByAppendingString:@"/Contents/Frameworks/CompatClasses.framework"]; NSBundle *compat = [NSBundle bundleWithPath:compatPath]; [compat load]; } } + (SapphirePredicate *)predicate { SapphireSettings *settings = [SapphireSettings sharedSettings]; int index = [settings indexOfLastPredicate]; if(index == NSNotFound) return nil; return [predicates objectAtIndex:index]; } + (SapphirePredicate *)nextPredicate { SapphireSettings *settings = [SapphireSettings sharedSettings]; int index = [settings indexOfLastPredicate]; int newIndex; switch(index) { case NSNotFound: newIndex = 0; if([settings displayUnwatched]) break; case 0: newIndex = 1; if([settings displayFavorites]) break; case 1: newIndex = 2; if([settings displayTopShows]) break; default: newIndex = NSNotFound; } [settings setIndexOfLastPredicate:newIndex]; if(newIndex == NSNotFound) return nil; return [predicates objectAtIndex:newIndex]; } + (BRTexture *)gemForPredicate:(SapphirePredicate *)predicate { SapphireTheme *theme = [SapphireTheme sharedTheme]; if(predicate == nil) return [theme gem:RED_GEM_KEY]; if([predicate isKindOfClass:[SapphireUnwatchedPredicate class]]) return [theme gem:BLUE_GEM_KEY]; if([predicate isKindOfClass:[SapphireFavoritePredicate class]]) return [theme gem:YELLOW_GEM_KEY]; if([predicate isKindOfClass:[SapphireTopShowPredicate class]]) return [theme gem:GREEN_GEM_KEY]; return nil; } + (void)logException:(NSException *)e { NSMutableString *ret = [NSMutableString stringWithFormat:@"Exception:"]; if([e respondsToSelector:@selector(backtrace)]) { [ret appendFormat:@"%@\n%@", e, [(BRBacktracingException *)e backtrace]]; Dl_info info; if(dladdr(&predicates, &info)) [ret appendFormat:@"Sapphire is at 0x%X", info.dli_fbase]; } else { NSArray *addrs = [SapphireFrontRowCompat callStackReturnAddressesForException:e]; NSLog(@"Got addrs: %@", addrs); int i, count = [addrs count]; NSMutableDictionary *mapping = [NSMutableDictionary dictionary]; for(i=0; i) itemForRow: (long) row { /* // build a BRTextMenuItemLayer or a BRAdornedMenuItemLayer, etc. here // return that object, it will be used to display the list item. */ if( row > [names count] ) return ( nil ) ; BRAdornedMenuItemLayer * result = nil ; NSString *name = [names objectAtIndex:row]; result = [SapphireFrontRowCompat textMenuItemForScene:[self scene] folder:YES]; SapphireTheme *theme = [SapphireTheme sharedTheme]; if([name isEqual: ALL_IMPORT_MENU_ITEM]) [SapphireFrontRowCompat setLeftIcon:[theme gem:IMPORT_GEM_KEY] forMenu:result]; else if([name isEqual: SETTINGS_MENU_ITEM]) [SapphireFrontRowCompat setLeftIcon:[theme gem:GEAR_GEM_KEY] forMenu:result]; else if([name isEqual: RESET_MENU_ITEM]) [SapphireFrontRowCompat setLeftIcon:[theme gem:FRONTROW_GEM_KEY] forMenu:result]; else if([name isEqual: @" TV Shows"]) [SapphireFrontRowCompat setLeftIcon:[theme gem:TV_GEM_KEY] forMenu:result]; else if([name isEqual: @" Movies"]) [SapphireFrontRowCompat setLeftIcon:[theme gem:MOV_GEM_KEY] forMenu:result]; else [SapphireFrontRowCompat setLeftIcon:[SapphireApplianceController gemForPredicate:[SapphireApplianceController predicate]] forMenu:result]; // add text [SapphireFrontRowCompat setTitle:name forMenu:result]; return ( result ) ; } - (NSString *) titleForRow: (long) row { if ( row > [ names count] ) return ( nil ); NSString *result = [ names objectAtIndex: row] ; return ( result ) ; /* // return the title for the list item at the given index here return ( @"Sapphire" ); */ } - (long) rowForTitle: (NSString *) title { long result = -1; long i, count = [self itemCount]; for ( i = 0; i < count; i++ ) { if ( [title isEqualToString: [self titleForRow: i]] ) { result = i; break; } } return ( result ); } - (void) itemSelected: (long) row { // This is called when the user presses play/pause on a list item if(row == [controllers count]) [[NSApplication sharedApplication] terminate:self]; id controller = [controllers objectAtIndex:row]; // This if statement needs to be done in a much more elegant way if([controller isKindOfClass:[SapphireBrowser class]]) { SapphireDirectoryMetaData *meta = [(SapphireBrowser *)controller metaData]; if([meta isKindOfClass:[SapphireVirtualDirectory class]]) { if(![(SapphireVirtualDirectory *)meta isLoaded]) { SapphireVirtualDirectoryLoading *loader = [[SapphireVirtualDirectoryLoading alloc] initWithScene:[self scene] title:BRLocalizedString(@"Loading", @"Loading") text:BRLocalizedString(@"Virtual directory is still loading.\n You may go back or wait for it to finish.", nil) showBack:YES]; [loader setDirectory:(SapphireVirtualDirectory *)meta]; [loader setBrowser:(SapphireBrowser *)controller]; controller = loader; } } } [[self stack] pushController:controller]; } - (id) previewControllerForItem: (long) item { // If subclassing BRMediaMenuController, this function is called when the selection cursor // passes over an item. return ( nil ); } @end