source: trunk/SapphireMarkMenu.m @ 387

Revision 387, 10.1 KB checked in by gbooker, 7 years ago (diff)

Better mark menu for joining

Line 
1//
2//  SapphireMarkMenu.m
3//  Sapphire
4//
5//  Created by Graham Booker on 6/25/07.
6//  Copyright 2007 www.nanopi.net. All rights reserved.
7//
8
9#import "SapphireMarkMenu.h"
10#import "SapphireMetaData.h"
11#import "SapphireFrontRowCompat.h"
12#import <QTKit/QTKit.h>
13
14@implementation SapphireMarkMenu
15
16typedef enum {
17        COMMAND_TOGGLE_WATCHED,
18        COMMAND_TOGGLE_FAVORITE,
19        COMMAND_MARK_TO_REFETCH_TV,
20        COMMAND_MARK_TO_REFETCH_MOVIE,
21        COMMAND_MARK_TO_DELETE_METADATA,
22        COMMAND_MARK_TO_JOIN,
23        COMMAND_MARK_AND_JOIN,
24        COMMAND_CLEAR_JOIN_MARK,
25        COMMAND_JOIN,
26} MarkCommand;
27
28static NSMutableArray *joinList;
29
30+ (void)initialize
31{
32        joinList = [[NSMutableArray alloc] init];
33}
34
35- (id) initWithScene: (BRRenderScene *) scene metaData: (SapphireMetaData *)meta
36{
37        self = [super initWithScene:scene];
38        if(!self)
39                return nil;
40       
41        /*Check to see if it is directory or file*/
42        isDir = [meta isKindOfClass:[SapphireDirectoryMetaData class]];
43        metaData = [meta retain];
44        commands = nil;
45        /*Create the menu*/
46        if(isDir)
47                names = [[NSMutableArray alloc] initWithObjects:
48                        BRLocalizedString(@"Mark All as Watched", @"Mark whole directory as watched"),
49                        BRLocalizedString(@"Mark All as Unwatched", @"Mark whole directory as unwatched"),
50                        BRLocalizedString(@"Mark All as Favorite", @"Mark whole directory as favorite"),
51                        BRLocalizedString(@"Mark All as Not Favorite", @"Mark whole directory as not favorite"),
52                        BRLocalizedString(@"Mark All to Refetch TV Data", @"Mark whole directory to re-fetch its tv data"),
53                        BRLocalizedString(@"Mark All to Refetch Movie Data", @"Mark whole directory to re-fetch its movie data"),
54                        BRLocalizedString(@"Mark All to Clear Metadata", @"Mark whole directory to delete the metadata"),
55                        nil];
56        else if([meta isKindOfClass:[SapphireFileMetaData class]])
57        {
58                SapphireFileMetaData *fileMeta = (SapphireFileMetaData *)metaData;
59                NSString *watched = nil;
60                NSString *favorite = nil;
61               
62                if([fileMeta watched])
63                        watched = BRLocalizedString(@"Mark as Unwatched", @"Mark file as unwatched");
64                else
65                        watched = BRLocalizedString(@"Mark as Watched", @"Mark file as watched");
66
67                if([fileMeta favorite])
68                        favorite = BRLocalizedString(@"Mark as Not Favorite", @"Mark file as a favorite");
69                else
70                        favorite = BRLocalizedString(@"Mark as Favorite", @"Mark file as not a favorite");
71                names = [[NSMutableArray alloc] initWithObjects:watched, favorite, nil];
72                commands = [[NSMutableArray alloc] initWithObjects: [NSNumber numberWithInt:COMMAND_TOGGLE_WATCHED], [NSNumber numberWithInt:COMMAND_TOGGLE_FAVORITE], nil];
73                if([fileMeta importedTimeFromSource:META_TVRAGE_IMPORT_KEY])
74                {
75                        [names addObject:BRLocalizedString(@"Mark to Refetch TV Data", @"Mark file to re-fetch its tv data")];
76                        [commands addObject:[NSNumber numberWithInt:COMMAND_MARK_TO_REFETCH_TV]];
77                }
78                if([fileMeta importedTimeFromSource:META_IMDB_IMPORT_KEY])
79                {
80                        [names addObject:BRLocalizedString(@"Mark to Refetch Movie Data", @"Mark file to re-fetch its movie data")];
81                        [commands addObject:[NSNumber numberWithInt:COMMAND_MARK_TO_REFETCH_MOVIE]];
82                }
83                if([fileMeta fileClass] != FILE_CLASS_UNKNOWN)
84                {
85                        [names addObject:BRLocalizedString(@"Mark to Clear Metadata", @"Mark a file to delete the metadata")];
86                        [commands addObject:[NSNumber numberWithInt:COMMAND_MARK_TO_DELETE_METADATA]];
87                }
88                if(![joinList containsObject:fileMeta])
89                {
90                        [names addObject:BRLocalizedString(@"Mark This File to Be Joined", @"Mark This File to Be Joined")];
91                        [commands addObject:[NSNumber numberWithInt:COMMAND_MARK_TO_JOIN]];
92                        [names addObject:BRLocalizedString(@"Mark This File and Join Group", @"Mark This File and Join Group")];
93                        [commands addObject:[NSNumber numberWithInt:COMMAND_MARK_AND_JOIN]];
94                }
95                if([joinList count])
96                {
97                        [names addObject:BRLocalizedString(@"Join Marked Files", @"Join Marked Files")];
98                        [commands addObject:[NSNumber numberWithInt:COMMAND_JOIN]];
99                        [names addObject:BRLocalizedString(@"Clear the Join List", @"Clear the Join List")];
100                        [commands addObject:[NSNumber numberWithInt:COMMAND_CLEAR_JOIN_MARK]];
101                }
102        }
103        else
104        {
105                /*Neither, so just return nil*/
106                [self autorelease];
107                return nil;
108        }
109        [[self list] setDatasource:self];
110       
111        return self;
112}
113
114- (void) dealloc
115{
116        [metaData release];
117        [names release];
118        [predicate release];
119        [super dealloc];
120}
121
122- (void)setPredicate:(SapphirePredicate *)newPredicate
123{
124        predicate = [newPredicate retain];
125}
126
127- (void)doJoin
128{
129        if(![joinList count])
130                return;
131       
132        QTMovie *resultingMovie = [[QTMovie alloc] init];
133        [resultingMovie setAttribute:[NSNumber numberWithBool:YES] forKey:QTMovieEditableAttribute];
134       
135        NSString *savePath = [[joinList objectAtIndex:0] path];
136        BOOL hasmovExt = [[savePath pathExtension] isEqualToString:@"mov"];
137        NSString *base = [savePath stringByDeletingPathExtension];
138        if([[base lowercaseString] hasSuffix:@" part 1"])
139                base = [base substringToIndex:[base length] - 7];
140        if(hasmovExt)
141                savePath = [[base stringByAppendingString:@" Joined"] stringByAppendingPathExtension:@"mov"];
142        else
143                savePath = [base stringByAppendingPathExtension:@"mov"];
144
145        int i, count=[joinList count];
146        for(i=0;i<count;i++)
147        {
148                SapphireFileMetaData *meta = [joinList objectAtIndex:i];
149                NSError *error = nil;
150                NSDictionary *openAttr = [NSDictionary dictionaryWithObjectsAndKeys:
151                                                                  [meta path], QTMovieFileNameAttribute,
152                                                                  [NSNumber numberWithBool:NO], QTMovieOpenAsyncOKAttribute,
153                                                                  nil];
154                QTMovie *current = [[QTMovie alloc] initWithAttributes:openAttr error:&error];
155                if(error == nil)
156                {
157                        QTTimeRange range;
158                        range.time = [current selectionStart];
159                        range.duration = [current duration];
160                        [current setSelection:range];
161                        [resultingMovie appendSelectionFromMovie:current];
162                        [meta setJoinedFile:savePath];
163                }
164                [current release];
165        }
166
167        [resultingMovie writeToFile:savePath withAttributes:[NSDictionary dictionary]];
168        [resultingMovie release];
169        [joinList removeAllObjects];
170        [metaData writeMetaData];
171}
172
173- (void) willBePushed
174{
175    // We're about to be placed on screen, but we're not yet there
176   
177    // always call super
178    [super willBePushed];
179}
180
181- (void) wasPushed
182{
183    // We've just been put on screen, the user can see this controller's content now
184   
185    // always call super
186    [super wasPushed];
187}
188
189- (void) willBePopped
190{
191    // The user pressed Menu, but we've not been removed from the screen yet
192   
193    // always call super
194    [super willBePopped];
195}
196
197- (void) wasPopped
198{
199    // The user pressed Menu, removing us from the screen
200   
201    // always call super
202    [super wasPopped];
203}
204
205- (void) willBeBuried
206{
207    // The user just chose an option, and we will be taken off the screen
208   
209    // always call super
210    [super willBeBuried];
211}
212
213- (void) wasBuriedByPushingController: (BRLayerController *) controller
214{
215    // The user chose an option and this controller os no longer on screen
216   
217    // always call super
218    [super wasBuriedByPushingController: controller];
219}
220
221- (void) willBeExhumed
222{
223    // the user pressed Menu, but we've not been revealed yet
224   
225    // always call super
226    [super willBeExhumed];
227}
228
229- (void) wasExhumedByPoppingController: (BRLayerController *) controller
230{
231    // handle being revealed when the user presses Menu
232   
233    // always call super
234    [super wasExhumedByPoppingController: controller];
235}
236
237- (long) itemCount
238{
239    // return the number of items in your menu list here
240        return ( [ names count]);
241}
242
243- (id<BRMenuItemLayer>) itemForRow: (long) row
244{
245        /*
246         // build a BRTextMenuItemLayer or a BRAdornedMenuItemLayer, etc. here
247         // return that object, it will be used to display the list item.
248         return ( nil );
249         */
250        if( row >= [names count] ) return ( nil ) ;
251       
252        BRAdornedMenuItemLayer * result = nil ;
253        NSString *name = [names objectAtIndex:row];
254        result = [SapphireFrontRowCompat textMenuItemForScene:[self scene] folder:NO];
255       
256        // add text
257        [SapphireFrontRowCompat setTitle:name forMenu:result];
258                               
259        return ( result ) ;
260}
261
262- (NSString *) titleForRow: (long) row
263{
264       
265        if ( row >= [ names count] ) return ( nil );
266       
267        NSString *result = [ names objectAtIndex: row] ;
268        return ( result ) ;
269}
270
271- (long) rowForTitle: (NSString *) title
272{
273    long result = -1;
274    long i, count = [self itemCount];
275    for ( i = 0; i < count; i++ )
276    {
277        if ( [title isEqualToString: [self titleForRow: i]] )
278        {
279            result = i;
280            break;
281        }
282    }
283   
284    return ( result );
285}
286
287- (void) itemSelected: (long) row
288{
289    // This is called when the user presses play/pause on a list item
290        if(row >= [names count])
291                return;
292       
293        /*Do action on dir or file*/
294        if(isDir)
295        {
296                SapphireDirectoryMetaData *dirMeta = (SapphireDirectoryMetaData *)metaData;
297                switch(row)
298                {
299                        case 0:
300                                [dirMeta setWatched:YES forPredicate:predicate];
301                                break;
302                        case 1:
303                                [dirMeta setWatched:NO forPredicate:predicate];
304                                break;
305                        case 2:
306                                [dirMeta setFavorite:YES forPredicate:predicate];
307                                break;
308                        case 3:
309                                [dirMeta setFavorite:NO forPredicate:predicate];
310                                break;
311                        case 4:
312                                [dirMeta setToImportFromSource:META_TVRAGE_IMPORT_KEY forPredicate:predicate];
313                        case 5:
314                                [dirMeta setToImportFromSource:META_IMDB_IMPORT_KEY forPredicate:predicate];
315                                break;
316                }
317        }
318        else
319        {
320                SapphireFileMetaData *fileMeta = (SapphireFileMetaData *)metaData;
321                switch([[commands objectAtIndex:row] intValue])
322                {
323                        case COMMAND_TOGGLE_WATCHED:
324                                [fileMeta setWatched:![fileMeta watched]];
325                                break;
326                        case COMMAND_TOGGLE_FAVORITE:
327                                [fileMeta setFavorite:![fileMeta favorite]];
328                                break;
329                        case COMMAND_MARK_TO_REFETCH_TV:
330                                [fileMeta setToImportFromSource:META_TVRAGE_IMPORT_KEY];
331                                break;
332                        case COMMAND_MARK_TO_REFETCH_MOVIE:
333                                [fileMeta setToImportFromSource:META_IMDB_IMPORT_KEY];
334                                break;
335                        case COMMAND_MARK_TO_DELETE_METADATA:
336                                [fileMeta setFileClass:FILE_CLASS_MOVIE];
337                                break;
338                        case COMMAND_MARK_TO_JOIN:
339                                [joinList addObject:fileMeta];
340                                break;
341                        case COMMAND_CLEAR_JOIN_MARK:
342                                [joinList removeAllObjects];
343                                break;
344                        case COMMAND_MARK_AND_JOIN:
345                                [joinList addObject:fileMeta];
346                        case COMMAND_JOIN:
347                                [self doJoin];
348                                break;
349                }
350        }
351        /*Save and exit*/
352        [metaData writeMetaData];
353        [[self stack] popController];
354}
355
356- (id<BRMediaPreviewController>) previewControllerForItem: (long) item
357{
358    // If subclassing BRMediaMenuController, this function is called when the selection cursor
359    // passes over an item.
360    return ( nil );
361}
362
363@end
Note: See TracBrowser for help on using the repository browser.