source: trunk/SapphireBrowser.m @ 6

Revision 6, 6.2 KB checked in by gbooker, 7 years ago (diff)

A new main-menu and the browser is now a different class

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