source: trunk/SapphireApplianceController.m @ 299

Revision 299, 11.9 KB checked in by pmerrill, 7 years ago (diff)
  • Added a few more icons.
  • Updated current icon set to a standard resolution multiple for better icon placement.
  • Updated ListTitles? to accommodate the icon proportion changes.
  • Updated MenuItems? to accommodate the icon proportion changes.
  • Added some testing code to dump the virtual directories to a plist file.
Line 
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
38static 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        [tvDir writeToFile:[NSHomeDirectory() stringByAppendingPathComponent:@"Library/Application Support/Sapphire/virtualTVDir.plist"]];
167        [tvBrowser release];
168       
169        SapphireMovieDirectory *movieDir = [[SapphireMovieDirectory alloc] initWithCollection:metaCollection];
170        SapphireBrowser *movieBrowser = [[SapphireBrowser alloc] initWithScene:[self scene] metaData:movieDir];
171        [movieBrowser setListTitle:BRLocalizedString(@" Movies", nil)];
172        [movieBrowser setListIcon:predicateGem];
173        [mutableMasterNames addObject:BRLocalizedString(@"  Movies", nil)];
174        [mutableMasterControllers addObject:movieBrowser];
175        [movieDir writeToFile:[NSHomeDirectory() stringByAppendingPathComponent:@"Library/Application Support/Sapphire/virtualMovieDir.plist"]];
176        [movieBrowser release];
177       
178        NSEnumerator *browserPointsEnum = [[metaCollection collectionDirectories] objectEnumerator];
179        NSString *browserPoint = nil;
180        while((browserPoint = [browserPointsEnum nextObject]) != nil)
181        {
182                if(![metaCollection skipCollection:browserPoint])
183                        [[metaCollection directoryForPath:browserPoint] loadMetaData];
184                if([metaCollection hideCollection:browserPoint])
185                        continue;
186                SapphireBrowser *browser = [[SapphireBrowser alloc] initWithScene:[self scene] metaData:[metaCollection directoryForPath:browserPoint]];
187                [browser setListTitle:[NSString stringWithFormat:@" %@",[browserPoint lastPathComponent]]];
188                [browser setListIcon:predicateGem];
189                [mutableMasterNames addObject:[NSString stringWithFormat:@"  %@", browserPoint]];
190                [mutableMasterControllers addObject:browser];
191                [browser release];
192        }
193        [mutableMasterNames addObjectsFromArray:[NSArray arrayWithObjects:
194                ALL_IMPORT_MENU_ITEM,
195                SETTINGS_MENU_ITEM,
196                RESET_MENU_ITEM,
197                nil]];
198        [mutableMasterControllers addObjectsFromArray:[NSArray arrayWithObjects:
199                allImporter,
200                settings,
201                nil]];
202        masterNames = [[NSArray alloc] initWithArray:mutableMasterNames];
203        masterControllers = [[NSArray alloc] initWithArray:mutableMasterControllers];
204        [mutableMasterNames release];
205        [mutableMasterControllers release];
206       
207        names = [[NSMutableArray alloc] init];
208        controllers = [[NSMutableArray alloc] init];
209        [self setMenuFromSettings];
210}
211
212- (void)setMenuFromSettings
213{
214        [names removeAllObjects];
215        [controllers removeAllObjects];
216        [names addObjectsFromArray:masterNames];
217        [controllers addObjectsFromArray:masterControllers];
218        if([settings disableUIQuit])
219                [names removeLastObject];
220}
221
222- (void) willBePushed
223{
224    // We're about to be placed on screen, but we're not yet there
225    [self recreateMenu];
226        [[self list] reload];
227    // always call super
228    [super willBePushed];
229}
230
231- (void) wasPushed
232{
233    // We've just been put on screen, the user can see this controller's content now
234   
235    // always call super
236    [super wasPushed];
237}
238
239- (void) willBePopped
240{
241    // The user pressed Menu, but we've not been removed from the screen yet
242   
243    // always call super
244    [super willBePopped];
245}
246
247- (void) wasPopped
248{
249    // The user pressed Menu, removing us from the screen
250   
251    // always call super
252    [super wasPopped];
253}
254
255- (void) willBeBuried
256{
257    // The user just chose an option, and we will be taken off the screen
258   
259    // always call super
260    [super willBeBuried];
261}
262
263- (void) wasBuriedByPushingController: (BRLayerController *) controller
264{
265    // The user chose an option and this controller os no longer on screen
266   
267    // always call super
268    [super wasBuriedByPushingController: controller];
269}
270
271- (void) willBeExhumed
272{
273    // the user pressed Menu, but we've not been revealed yet
274   
275    // always call super
276    [super willBeExhumed];
277        [self setMenuFromSettings];
278        [[self list] reload];
279        [SapphireFrontRowCompat renderScene:[self scene]];
280}
281
282- (void) wasExhumedByPoppingController: (BRLayerController *) controller
283{
284    // handle being revealed when the user presses Menu
285   
286    // always call super
287    [super wasExhumedByPoppingController: controller];
288}
289
290- (long) itemCount
291{
292    // return the number of items in your menu list here
293        return ( [ names count]);
294}
295
296- (id<BRMenuItemLayer>) itemForRow: (long) row
297{
298/*
299    // build a BRTextMenuItemLayer or a BRAdornedMenuItemLayer, etc. here
300    // return that object, it will be used to display the list item.
301*/
302        if( row > [names count] ) return ( nil ) ;
303       
304        BRAdornedMenuItemLayer * result = nil ;
305        NSString *name = [names objectAtIndex:row];
306        result = [SapphireFrontRowCompat textMenuItemForScene:[self scene] folder:YES];
307       
308        SapphireTheme *theme = [SapphireTheme sharedTheme];
309        if([name isEqual: ALL_IMPORT_MENU_ITEM]) [SapphireFrontRowCompat setLeftIcon:[theme gem:GEAR_GEM_KEY] forMenu:result];
310        //      else if([name isEqual: AGENT_IMPORT_MENU_ITEM]) [SapphireFrontRowCompat setLeftIcon:[theme gem:GEAR_GEM_KEY] forMenu:result];
311        else if([name isEqual: SETTINGS_MENU_ITEM]) [SapphireFrontRowCompat setLeftIcon:[theme gem:GEAR_GEM_KEY] forMenu:result];
312        else if([name isEqual: RESET_MENU_ITEM]) [SapphireFrontRowCompat setLeftIcon:[theme gem:FRONTROW_GEM_KEY] forMenu:result];
313        else [SapphireFrontRowCompat setLeftIcon:[SapphireApplianceController gemForPredicate:[SapphireApplianceController predicate]] forMenu:result];
314       
315        // add text
316        [SapphireFrontRowCompat setTitle:name forMenu:result];
317                               
318        return ( result ) ;
319}
320
321- (NSString *) titleForRow: (long) row
322{
323
324        if ( row > [ names count] ) return ( nil );
325       
326        NSString *result = [ names objectAtIndex: row] ;
327        return ( result ) ;
328/*
329    // return the title for the list item at the given index here
330    return ( @"Sapphire" );
331*/
332}
333
334- (long) rowForTitle: (NSString *) title
335{
336    long result = -1;
337    long i, count = [self itemCount];
338    for ( i = 0; i < count; i++ )
339    {
340        if ( [title isEqualToString: [self titleForRow: i]] )
341        {
342            result = i;
343            break;
344        }
345    }
346   
347    return ( result );
348}
349
350- (void) itemSelected: (long) row
351{
352    // This is called when the user presses play/pause on a list item
353       
354        if(row == [controllers count])
355                [[NSApplication sharedApplication] terminate:self];
356        id controller = [controllers objectAtIndex:row];
357        [[self stack] pushController:controller];
358}
359
360- (id<BRMediaPreviewController>) previewControllerForItem: (long) item
361{
362    // If subclassing BRMediaMenuController, this function is called when the selection cursor
363    // passes over an item.
364    return ( nil );
365}
366
367@end
Note: See TracBrowser for help on using the repository browser.