source: trunk/SapphireBrowser.m @ 8

Revision 8, 6.7 KB checked in by gbooker, 7 years ago (diff)

Predicate matching.

Line 
1//
2//  SapphireBrowser.m
3//  Sapphire
4//
5//  Created by pnmerrill on 6/20/07.
6//  Copyright 2007 __MyCompanyName__. All rights reserved.
7//
8
9#import "SapphireBrowser.h"
10#import <BackRow/BackRow.h>
11#import "SapphireMetaData.h"
12
13@interface SapphireBrowser (private)
14- (void)reloadDirectoryContents;
15- (void)processFiles:(NSArray *)files;
16- (void)filesProcessed:(NSDictionary *)files;
17- (NSMutableDictionary *)metaDataForPath:(NSString *)path;
18@end
19
20@implementation SapphireBrowser
21
22- (id) initWithScene: (BRRenderScene *) scene metaData: (SapphireDirectoryMetaData *)meta
23{
24        return [self initWithScene:scene metaData:meta predicate:NULL];
25}
26- (id) initWithScene: (BRRenderScene *) scene metaData: (SapphireDirectoryMetaData *)meta predicate:(metaDataPredicate)newPredicate;
27{
28        if ( [super initWithScene: scene] == nil ) return ( nil );
29               
30        _names = [NSMutableArray new];
31        metaData = [meta retain];
32        [metaData setDelegate:self];
33        predicate = newPredicate;
34
35        [self reloadDirectoryContents];
36       
37        // set the datasource *after* you've setup your array
38        [[self list] setDatasource: self] ;
39               
40        return ( self );
41}
42
43- (void)reloadDirectoryContents
44{
45        [metaData reloadDirectoryContents];
46        [_names removeAllObjects];
47        if(predicate == NULL)
48        {
49                [_names addObjectsFromArray:[metaData directories]];
50                [_names addObjectsFromArray:[metaData files]];
51        }
52        else
53        {
54                [_names addObjectsFromArray:[metaData predicatedDirectories:predicate]];
55                [_names addObjectsFromArray:[metaData predicatedFiles:predicate]];
56        }
57
58        BRListControl *list = [self list];
59        long selection = [list selection];
60        [list reload];
61        [list setSelection:selection]; 
62}
63
64- (void) dealloc
65{
66    // always remember to deallocate your resources
67        [_names release];
68        [metaData release];
69    [super dealloc];
70}
71
72- (NSString *)sizeStringForMetaData:(SapphireFileMetaData *)meta
73{
74        float size = [meta size];
75        char letter = ' ';
76        if(size >= 1024000)
77        {
78                if(size >= 1024*1024000)
79                {
80                        size /= 1024 * 1024 * 1024;
81                        letter = 'G';
82                }
83                else
84                {
85                        size /= 1024 * 1024;
86                        letter = 'M';
87                }
88        }
89        else if (size >= 1000)
90        {
91                size /= 1024;
92                letter = 'K';
93        }
94        return [NSString stringWithFormat:@"%.1f\n%cB", size, letter]; 
95}
96
97- (void) willBePushed
98{
99    // We're about to be placed on screen, but we're not yet there
100   
101    // always call super
102    [super willBePushed];
103}
104
105- (void) wasPushed
106{
107    // We've just been put on screen, the user can see this controller's content now
108   
109    // always call super
110    [super wasPushed];
111        [metaData resumeImport];
112}
113
114- (void) willBePopped
115{
116    // The user pressed Menu, but we've not been removed from the screen yet
117   
118    // always call super
119    [super willBePopped];
120        [metaData cancelImport];
121        [metaData setDelegate:nil];
122}
123
124- (void) wasPopped
125{
126    // The user pressed Menu, removing us from the screen
127   
128    // always call super
129    [super wasPopped];
130}
131
132- (void) willBeBuried
133{
134    // The user just chose an option, and we will be taken off the screen
135   
136    // always call super
137        [metaData cancelImport];
138    [super willBeBuried];
139}
140
141- (void) wasBuriedByPushingController: (BRLayerController *) controller
142{
143    // The user chose an option and this controller os no longer on screen
144   
145    // always call super
146    [super wasBuriedByPushingController: controller];
147}
148
149- (void) willBeExhumed
150{
151    // the user pressed Menu, but we've not been revealed yet
152   
153    // always call super
154        [self reloadDirectoryContents];
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        [metaData resumeImport];
165}
166
167- (long) itemCount
168{
169    // return the number of items in your menu list here
170        return ( [ _names count]);
171}
172
173- (id<BRMenuItemLayer>) itemForRow: (long) row
174{
175/*
176    // build a BRTextMenuItemLayer or a BRAdornedMenuItemLayer, etc. here
177    // return that object, it will be used to display the list item.
178    return ( nil );
179*/
180        if( row > [_names count] ) return ( nil ) ;
181       
182        BRAdornedMenuItemLayer * result = nil ;
183        NSString *name = [_names objectAtIndex:row];
184        if([[metaData directories] containsObject:name])
185                result = [BRAdornedMenuItemLayer adornedFolderMenuItemWithScene: [self scene]] ;
186        else
187        {
188                result = [BRAdornedMenuItemLayer adornedMenuItemWithScene: [self scene]] ;
189                BOOL watched = NO;
190                SapphireFileMetaData *meta = [metaData metaDataForFile:name];
191                if(meta != nil)
192                {
193                        [[result textItem] setRightJustifiedText:[self sizeStringForMetaData:meta]];
194                        watched = [meta watched];
195                }
196                if(!watched)
197                        [result setLeftIcon:[[BRThemeInfo sharedTheme] unplayedPodcastImageForScene:[self scene]]];
198        }
199                       
200        // add text
201        [[result textItem] setTitle: name] ;
202                               
203        return ( result ) ;
204}
205
206- (NSString *) titleForRow: (long) row
207{
208
209        if ( row > [ _names count] ) return ( nil );
210       
211        NSString *result = [ _names objectAtIndex: row] ;
212        return ( result ) ;
213/*
214    // return the title for the list item at the given index here
215    return ( @"Sapphire" );
216*/
217}
218
219- (long) rowForTitle: (NSString *) title
220{
221    long result = -1;
222    long i, count = [self itemCount];
223    for ( i = 0; i < count; i++ )
224    {
225        if ( [title isEqualToString: [self titleForRow: i]] )
226        {
227            result = i;
228            break;
229        }
230    }
231   
232    return ( result );
233}
234
235- (void) itemSelected: (long) row
236{
237    // This is called when the user presses play/pause on a list item
238       
239        NSString *name = [_names objectAtIndex:row];
240        NSString *dir = [metaData path];
241       
242        if([[metaData directories] containsObject:name])
243        {
244                id controller = [[SapphireBrowser alloc] initWithScene:[self scene] metaData:[metaData metaDataForDirectory:name] predicate:predicate];
245                [[self stack] pushController:controller];
246                [controller release];
247        }
248        else
249        {
250                BRVideoPlayerController *controller = [[BRVideoPlayerController alloc] initWithScene:[self scene]];
251                BRQTKitVideoPlayer *player = [[BRQTKitVideoPlayer alloc] init];
252                NSError *error = nil;
253               
254                NSURL *url = [NSURL fileURLWithPath:[dir stringByAppendingPathComponent:name]];
255                BRSimpleMediaAsset *asset  =[[BRSimpleMediaAsset alloc] initWithMediaURL:url];
256                [player setMedia:asset error:&error];
257               
258                [controller setVideoPlayer:player];
259                SapphireFileMetaData *meta = [metaData metaDataForFile:name];
260                [meta setWatched];
261                [meta writeMetaData];
262                [[self stack] pushController:controller];
263
264                [asset release];
265                [player release];
266                [controller release];
267        }
268}
269
270- (id<BRMediaPreviewController>) previewControllerForItem: (long) item
271{
272    // If subclassing BRMediaMenuController, this function is called when the selection cursor
273    // passes over an item.
274    return ( nil );
275}
276
277- (void)updateComplete
278{
279        BRListControl *list = [self list];
280        long selection = [list selection];
281        [list reload];
282        [list setSelection:selection]; 
283}
284
285@end
Note: See TracBrowser for help on using the repository browser.