source: trunk/SapphireApplianceController.m @ 1

Revision 1, 6.3 KB checked in by pmerrill, 8 years ago (diff)

Initial import

Line 
1//
2//  SapphireApplianceController.m
3//  Sapphire
4//
5//  Created by pnmerrill on 6/20/07.
6//  Copyright 2007 __MyCompanyName__. All rights reserved.
7//
8
9#import "SapphireApplianceController.h"
10#import <BackRow/BackRow.h>
11
12@implementation NSString (episodeSorting)
13
14// Custom TV Episode handler
15- (NSComparisonResult) episodeCompare:(NSString *)other
16{
17        return [self compare:other options:NSCaseInsensitiveSearch | NSNumericSearch];
18}
19
20@end
21
22@implementation SapphireApplianceController
23
24// Static set of file extensions to filter
25static NSArray *extensions = nil;
26
27+(void)load
28{
29        extensions = [[NSArray alloc] initWithObjects:@"avi", @"mov", @"mpg", @"wmv", nil];
30}
31
32+ (NSString *) rootMenuLabel
33{
34        return (@"net.pmerrill.recursivemenu.root" );
35}
36
37//
38- (id) initWithScene: (BRRenderScene *) scene
39{
40/*
41    if ( [super initWithScene: scene] == nil )
42        return ( nil );
43   
44    // initialize your resources here
45   
46    return ( self );
47*/
48        return ( [self initWithScene: scene directory:[NSHomeDirectory() stringByAppendingPathComponent:@"Movies"]] );
49        [[NSFileManager defaultManager] subpathsAtPath:[NSHomeDirectory() stringByAppendingPathComponent:@"Movies"]];
50}
51
52- (id) initWithScene: (BRRenderScene *) scene directory:(NSString *)dir
53{
54        if ( [super initWithScene: scene] == nil ) return ( nil );
55        _dir = [dir retain];
56               
57        _names = [NSMutableArray new];
58       
59        NSMutableArray *files = [NSMutableArray array];
60       
61        NSArray *names = [[[NSFileManager defaultManager] directoryContentsAtPath:dir] retain];
62       
63        NSEnumerator *nameEnum = [names objectEnumerator];
64        NSString *name = nil;
65        // Display Menu Items
66        while((name = [nameEnum nextObject]) != nil)
67        {
68                if([name hasPrefix:@"."])
69                        continue;
70                //Only accept if it is a directory or right extension
71                NSString *extension = [name pathExtension];
72                if([extensions containsObject:extension])
73                        [files addObject:name];
74                else if([self isDirectory:[_dir stringByAppendingPathComponent:name]])
75                        [_names addObject:name];
76        }
77        [_names sortUsingSelector:@selector(episodeCompare:)];
78        [files sortUsingSelector:@selector(episodeCompare:)];
79        [_names addObjectsFromArray:files];
80       
81        // set the datasource *after* you've setup your array
82        [[self list] setDatasource: self] ;
83               
84        return ( self );
85}
86
87- (void) dealloc
88{
89    // always remember to deallocate your resources
90        [_dir release];
91        [_names release] ;
92    [super dealloc];
93}
94
95//Check to see if the path is a directory
96- (BOOL)isDirectory:(NSString *)path
97{
98        BOOL isDir = NO;
99        return [[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&isDir] && isDir;
100}
101
102- (void) willBePushed
103{
104    // We're about to be placed on screen, but we're not yet there
105   
106    // always call super
107    [super willBePushed];
108}
109
110- (void) wasPushed
111{
112    // We've just been put on screen, the user can see this controller's content now
113   
114    // always call super
115    [super wasPushed];
116}
117
118- (void) willBePopped
119{
120    // The user pressed Menu, but we've not been removed from the screen yet
121   
122    // always call super
123    [super willBePopped];
124}
125
126- (void) wasPopped
127{
128    // The user pressed Menu, removing us from the screen
129   
130    // always call super
131    [super wasPopped];
132}
133
134- (void) willBeBuried
135{
136    // The user just chose an option, and we will be taken off the screen
137   
138    // always call super
139    [super willBeBuried];
140}
141
142- (void) wasBuriedByPushingController: (BRLayerController *) controller
143{
144    // The user chose an option and this controller os no longer on screen
145   
146    // always call super
147    [super wasBuriedByPushingController: controller];
148}
149
150- (void) willBeExhumed
151{
152    // the user pressed Menu, but we've not been revealed yet
153   
154    // always call super
155    [super willBeExhumed];
156}
157
158- (void) wasExhumedByPoppingController: (BRLayerController *) controller
159{
160    // handle being revealed when the user presses Menu
161   
162    // always call super
163    [super wasExhumedByPoppingController: controller];
164}
165
166- (long) itemCount
167{
168    // return the number of items in your menu list here
169        return ( [ _names count]);
170}
171
172- (id<BRMenuItemLayer>) itemForRow: (long) row
173{
174/*
175    // build a BRTextMenuItemLayer or a BRAdornedMenuItemLayer, etc. here
176    // return that object, it will be used to display the list item.
177    return ( nil );
178*/
179        if( row > [_names count] ) return ( nil ) ;
180       
181        BRAdornedMenuItemLayer * result = nil ;
182        NSString *name = [_names objectAtIndex:row];
183        if([self isDirectory:[_dir stringByAppendingPathComponent:name]])
184                result = [BRAdornedMenuItemLayer adornedFolderMenuItemWithScene: [self scene]] ;
185        else
186                        result = [BRAdornedMenuItemLayer adornedMenuItemWithScene: [self scene]] ;
187                       
188        // add text
189        [[result textItem] setTitle: name] ;
190                               
191        return ( result ) ;
192}
193
194- (NSString *) titleForRow: (long) row
195{
196
197        if ( row > [ _names count] ) return ( nil );
198       
199        NSString *result = [ _names objectAtIndex: row] ;
200        return ( result ) ;
201/*
202    // return the title for the list item at the given index here
203    return ( @"Sapphire" );
204*/
205}
206
207- (long) rowForTitle: (NSString *) title
208{
209    long result = -1;
210    long i, count = [self itemCount];
211    for ( i = 0; i < count; i++ )
212    {
213        if ( [title isEqualToString: [self titleForRow: i]] )
214        {
215            result = i;
216            break;
217        }
218    }
219   
220    return ( result );
221}
222
223- (void) itemSelected: (long) row
224{
225    // This is called when the user presses play/pause on a list item
226       
227        NSString *name = [_names objectAtIndex:row];
228       
229        if([self isDirectory:[_dir stringByAppendingPathComponent:name]])
230        {
231                id controller = [[SapphireApplianceController alloc] initWithScene:[self scene] directory:[_dir stringByAppendingPathComponent:name]];
232                [[self stack] pushController:controller];
233                [controller release];
234        }
235        else
236        {
237                BRVideoPlayerController *controller = [[BRVideoPlayerController alloc] initWithScene:[self scene]];
238                BRQTKitVideoPlayer *player = [[BRQTKitVideoPlayer alloc] init];
239                NSError *error = nil;
240               
241                NSURL *url = [NSURL fileURLWithPath:[_dir stringByAppendingPathComponent:name]];
242                BRSimpleMediaAsset *asset  =[[BRSimpleMediaAsset alloc] initWithMediaURL:url];
243                [player setMedia:asset error:&error];
244               
245                [controller setVideoPlayer:player];
246                [[self stack] pushController:controller];
247
248                [asset release];
249                [player release];
250                [controller release];
251        }
252}
253
254- (id<BRMediaPreviewController>) previewControllerForItem: (long) item
255{
256    // If subclassing BRMediaMenuController, this function is called when the selection cursor
257    // passes over an item.
258    return ( nil );
259}
260
261@end
Note: See TracBrowser for help on using the repository browser.