source: branches/CoreData/SapphireFrappliance/Browser/SapphireDisplayMenu.m @ 739

Revision 739, 8.2 KB checked in by gbooker, 5 years ago (diff)

Ran a static analysis. Corrected many leaks, unused assignments, and missing assignments.

Line 
1/*
2 * SapphireDisplayMenu.m
3 * Sapphire
4 *
5 * Created by Graham Booker on Jun. 17, 2008.
6 * Copyright 2007 Sapphire Development Team and/or www.nanopi.net
7 * All rights reserved.
8 *
9 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU
10 * General Public License as published by the Free Software Foundation; either version 3 of the License,
11 * or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
14 * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
15 * Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along with this program; if not,
18 * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19 */
20
21#import "SapphireDisplayMenu.h"
22#import "SapphireMetaData.h"
23#import "SapphireDirectory.h"
24#import "SapphireSettings.h"
25#import "SapphireFileSorter.h"
26#import "SapphireApplianceController.h"
27#import "SapphireFileMetaData.h"
28#import "SapphireMediaPreview.h"
29#import "SapphireTheme.h"
30#import <SapphireCompatClasses/SapphireFrontRowCompat.h>
31
32typedef enum {
33        COMMAND_PREDICATE_NONE,
34        COMMAND_PREDICATE_UNWATCHED,
35        COMMAND_PREDICATE_FAVORITE,
36        COMMAND_SORT_NUM_BASE
37} DisplayCommand;
38
39@implementation SapphireDisplayMenu
40
41- (id) initWithScene:(BRRenderScene *)scene directory:(id <SapphireDirectory>)directory
42{
43        self = [super initWithScene:scene];
44        if(!self)
45                return nil;
46
47        dir = [directory retain];
48        names = [[NSMutableArray alloc] init];
49        dispDescriptions = [[NSMutableArray alloc] init];
50        commands = [[NSMutableArray alloc] init];
51       
52        SapphireSettings *settings = [SapphireSettings sharedSettings];
53        BOOL dispUnwatched = [settings displayUnwatched];
54        BOOL dispFavorite = [settings displayFavorites];
55        if(dispUnwatched || dispFavorite)
56        {
57                [names addObject:BRLocalizedString(@"Display All Files", @"Display All Files (no fitering)")];
58                [commands addObject:[NSNumber numberWithInt:COMMAND_PREDICATE_NONE]];
59                [dispDescriptions addObject:BRLocalizedString(@"Sapphire will not filter files, but instead display all files", @"No filtering description")];
60                if(dispUnwatched)
61                {
62                        [names addObject:BRLocalizedString(@"  Unwatched Files", @"Display only Unwatched Files")];
63                        [commands addObject:[NSNumber numberWithInt:COMMAND_PREDICATE_UNWATCHED]];
64                        [dispDescriptions addObject:BRLocalizedString(@"Sapphire will filter files to display only unwatched files", @"Unwatched filtering description")];
65                }
66                if(dispFavorite)
67                {
68                        [names addObject:BRLocalizedString(@"  Favorite Files", @"Display only Favorite Files")];
69                        [commands addObject:[NSNumber numberWithInt:COMMAND_PREDICATE_FAVORITE]];
70                        [dispDescriptions addObject:BRLocalizedString(@"Sapphire will filter files to display only favorite files", @"Favorite filtering description")];
71                }
72        }
73       
74        if([dir conformsToProtocol:@protocol(SapphireSortableDirectory)])
75        {
76                id <SapphireSortableDirectory> sortable = (id <SapphireSortableDirectory>)dir;
77                NSArray *sortMechanisms = [sortable fileSorters];
78                NSEnumerator *sortEnum = [sortMechanisms objectEnumerator];
79                SapphireFileSorter *sorter;
80                BOOL first = YES;
81                while((sorter = [sortEnum nextObject]) != nil)
82                {
83                        NSString *dispName = [sorter displayName];
84                        if(first)
85                        {
86                                dispName = [@"Sort " stringByAppendingString:dispName];
87                                [commands addObject:[NSNumber numberWithInt:COMMAND_SORT_NUM_BASE]];
88                                first = NO;
89                        }
90                        else
91                        {
92                                dispName = [@"  " stringByAppendingString:dispName];
93                                [commands addObject:[NSNumber numberWithInt:COMMAND_SORT_NUM_BASE + [sorter sortNumber]]];
94                        }
95                        [names addObject:dispName];
96                        [dispDescriptions addObject:[sorter displayDescription]];
97                }
98        }
99       
100        [[self list] setDatasource:self];
101       
102        return self;
103}
104
105- (void) dealloc
106{
107        [names release];
108        [commands release];
109        [dispDescriptions release];
110        [dir release];
111        [super dealloc];
112}
113
114- (long) itemCount
115{
116    // return the number of items in your menu list here
117        return ( [ names count]);
118}
119
120- (id<BRMenuItemLayer>) itemForRow: (long) row
121{
122        /*
123         // build a BRTextMenuItemLayer or a BRAdornedMenuItemLayer, etc. here
124         // return that object, it will be used to display the list item.
125         return ( nil );
126         */
127        if( row >= [names count] ) return ( nil ) ;
128       
129        BRAdornedMenuItemLayer * result = nil ;
130        NSString *name = [names objectAtIndex:row];
131        result = [SapphireFrontRowCompat textMenuItemForScene:[self scene] folder:NO];
132       
133        // add text
134        [SapphireFrontRowCompat setTitle:name forMenu:result];
135       
136        DisplayCommand command = [[commands objectAtIndex:row] intValue];
137        switch (command) {
138                case COMMAND_PREDICATE_NONE:
139                        if([SapphireApplianceController predicateType] == PREDICATE_TYPE_NONE)
140                                [SapphireFrontRowCompat setLeftIcon:[SapphireFrontRowCompat selectedSettingImageForScene:[self scene]] forMenu:result];
141                        [SapphireFrontRowCompat setRightIcon:[[SapphireTheme sharedTheme] gem:RED_GEM_KEY] forMenu:result];
142                        break;
143                case COMMAND_PREDICATE_UNWATCHED:
144                        if([SapphireApplianceController predicateType] == PREDICATE_TYPE_UNWATCHED)
145                                [SapphireFrontRowCompat setLeftIcon:[SapphireFrontRowCompat selectedSettingImageForScene:[self scene]] forMenu:result];
146                        [SapphireFrontRowCompat setRightIcon:[[SapphireTheme sharedTheme] gem:BLUE_GEM_KEY] forMenu:result];
147                        break;
148                case COMMAND_PREDICATE_FAVORITE:
149                        if([SapphireApplianceController predicateType] == PREDICATE_TYPE_FAVORITE)
150                                [SapphireFrontRowCompat setLeftIcon:[SapphireFrontRowCompat selectedSettingImageForScene:[self scene]] forMenu:result];
151                        [SapphireFrontRowCompat setRightIcon:[[SapphireTheme sharedTheme] gem:YELLOW_GEM_KEY] forMenu:result];
152                        break;
153                default:
154                        if(command - COMMAND_SORT_NUM_BASE == [(id <SapphireSortableDirectory>)dir sortMethodValue])
155                                [SapphireFrontRowCompat setLeftIcon:[SapphireFrontRowCompat selectedSettingImageForScene:[self scene]] forMenu:result];
156                        break;
157        }
158       
159        return ( result ) ;
160}
161
162- (NSString *) titleForRow: (long) row
163{
164       
165        if ( row >= [ names count] ) return ( nil );
166       
167        NSString *result = [ names objectAtIndex: row] ;
168        return ( result ) ;
169}
170
171- (long) rowForTitle: (NSString *) title
172{
173    long result = -1;
174    long i, count = [self itemCount];
175    for ( i = 0; i < count; i++ )
176    {
177        if ( [title isEqualToString: [self titleForRow: i]] )
178        {
179            result = i;
180            break;
181        }
182    }
183   
184    return ( result );
185}
186
187- (void) itemSelected: (long) row
188{
189    // This is called when the user presses play/pause on a list item
190        if(row >= [names count])
191                return;
192       
193        DisplayCommand command = [[commands objectAtIndex:row] intValue];
194        switch (command) {
195                case COMMAND_PREDICATE_NONE:
196                        [SapphireApplianceController setPredicateType:PREDICATE_TYPE_NONE];
197                        break;
198                case COMMAND_PREDICATE_UNWATCHED:
199                        [SapphireApplianceController setPredicateType:PREDICATE_TYPE_UNWATCHED];
200                        break;
201                case COMMAND_PREDICATE_FAVORITE:
202                        [SapphireApplianceController setPredicateType:PREDICATE_TYPE_FAVORITE];
203                        break;
204                default:
205                        [(id <SapphireSortableDirectory>)dir setSortMethodValue:command - COMMAND_SORT_NUM_BASE];
206                        [dir reloadDirectoryContents];
207                        break;
208        }
209       
210        [[self stack] popController];
211}
212
213- (id<BRMediaPreviewController>) previewControlForItem:(long)item
214{
215        return [self previewControllerForItem:item];
216}
217       
218- (id<BRMediaPreviewController>) previewControllerForItem: (long) item
219{
220    // If subclassing BRMediaMenuController, this function is called when the selection cursor
221    // passes over an item.
222        if(item >= [names count])
223                return nil;
224        else
225        {
226                /* Get setting name & kill cushion  */
227                NSString *markName =[NSString stringWithFormat:@"%@ for \"%@\"",[names objectAtIndex:item],(NSString *)[self listTitle]];
228                NSString *markDescription=[dispDescriptions objectAtIndex:item];
229                /* Construct a gerneric metadata asset for display */
230                NSMutableDictionary *markMeta=[[NSMutableDictionary alloc] init];
231                [markMeta setObject:markName forKey:META_TITLE_KEY];
232                [markMeta setObject:[NSNumber numberWithInt:FILE_CLASS_UTILITY] forKey:FILE_CLASS_KEY];
233                [markMeta setObject:markDescription forKey:META_DESCRIPTION_KEY];
234                SapphireMediaPreview *preview = [[SapphireMediaPreview alloc] initWithScene:[self scene]];
235                [preview setUtilityData:markMeta];
236                [markMeta release];
237                [preview setShowsMetadataImmediately:YES];
238                /*And go*/
239                return [preview autorelease];
240        }
241    return ( nil );
242}
243
244@end
Note: See TracBrowser for help on using the repository browser.