source: trunk/SapphireBrowser.m @ 16

Revision 16, 7.0 KB checked in by gbooker, 8 years ago (diff)

Set the icons and titles for subdirs
Added a divider between directories and files.

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