source: trunk/SapphireFrappliance/MetaData/Support/SapphireFileSorter.m @ 1291

Revision 1291, 10.7 KB checked in by gbooker, 4 years ago (diff)

Added sort by duration and sorting by filesize.

Line 
1/*
2 * SapphireFileSorter.h
3 * Sapphire
4 *
5 * Created by Graham Booker on Jun. 16, 2008.
6 * Copyright 2008 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 "SapphireFileSorter.h"
22#import "SapphireFileMetaData.h"
23#import "SapphireMovie.h"
24#import "SapphireEpisode.h"
25#import "SapphireFileSymLink.h"
26#import "NSString-Extensions.h"
27
28@interface SapphireFileMetaData (sorting)
29- (NSComparisonResult)movieIMDBRankCompare:(SapphireFileMetaData *)other;
30- (NSComparisonResult)movieOscarsWonCompare:(SapphireFileMetaData *)other;
31- (NSComparisonResult)movieIMDBRatingCompare:(SapphireFileMetaData *)other;
32- (NSComparisonResult)moviePremierDateCompare:(SapphireFileMetaData *)other;
33- (NSComparisonResult)episodeAirDateCompare:(SapphireFileMetaData *)other;
34@end
35
36SapphireFileMetaData *getFileFromFileOrLink(id file)
37{
38        if([file isKindOfClass:[SapphireFileMetaData class]])
39                return (SapphireFileMetaData *)file;
40        else
41                return ((SapphireFileSymLink *)file).file;     
42}
43
44@implementation SapphireFileSorter
45
46+ (SapphireFileSorter *)sharedInstance
47{
48        return nil;
49}
50
51+ (BOOL)sortFiles:(NSMutableArray *)files withSorter:(int)sorter inAllowedSorts:(NSArray *)allowed;
52{
53        if(![allowed count])
54                return NO;
55        SapphireFileSorter *sorterObj;
56        if(sorter == 0)
57                sorterObj = [allowed objectAtIndex:0];
58        else
59        {
60                NSEnumerator *sortEnum = [allowed objectEnumerator];
61                while((sorterObj = [sortEnum nextObject]) != nil)
62                {
63                        if([sorterObj sortNumber] == sorter)
64                                break;
65                }
66        }
67        BOOL ret = NO;
68        if(sorterObj == nil)
69        {
70                ret = YES;
71                sorterObj = [allowed objectAtIndex:0];
72        }
73        [sorterObj sortFiles:files];
74       
75        return ret;
76}
77
78- (NSString *)displayName
79{
80        return nil;
81}
82
83- (NSString *)displayDescription
84{
85        return nil;
86}
87
88- (int)sortNumber
89{
90        return -1;
91}
92
93- (void)sortFiles:(NSMutableArray *)files
94{
95}
96
97@end
98
99@implementation SapphireTVEpisodeSorter
100
101+ (SapphireFileSorter *)sharedInstance
102{
103        static SapphireTVEpisodeSorter *shared = nil;
104        if(shared == nil)
105                shared = [[SapphireTVEpisodeSorter alloc] init];
106        return shared;
107}
108
109- (NSString *)displayName
110{
111        return BRLocalizedString(@"By Episode", @"[Sort] By Episode number");
112}
113
114- (NSString *)displayDescription
115{
116        return BRLocalizedString(@"Sort by Episode Order.", @"Sort by Episode Order description");
117}
118
119- (int)sortNumber
120{
121        return 1;
122}
123
124- (void)sortFiles:(NSMutableArray *)files
125{
126        [files sortUsingSelector:@selector(episodeCompare:)];
127}
128
129@end
130
131@implementation SapphireMovieTitleSorter
132
133+ (SapphireFileSorter *)sharedInstance
134{
135        static SapphireMovieTitleSorter *shared = nil;
136        if(shared == nil)
137                shared = [[SapphireMovieTitleSorter alloc] init];
138        return shared;
139}
140
141- (NSString *)displayName
142{
143        return BRLocalizedString(@"By Movie Title", @"[Sort] By Movie Title");
144}
145
146- (NSString *)displayDescription
147{
148        return BRLocalizedString(@"Sort by Movie Title.", @"Sort by Movie Title description");
149}
150
151- (int)sortNumber
152{
153        return 2;
154}
155
156- (void)sortFiles:(NSMutableArray *)files
157{
158        [files sortUsingSelector:@selector(movieCompare:)];
159}
160
161@end
162
163@implementation SapphireMovieIMDBTop250RankSorter
164
165+ (SapphireFileSorter *)sharedInstance
166{
167        static SapphireMovieIMDBTop250RankSorter *shared = nil;
168        if(shared == nil)
169                shared = [[SapphireMovieIMDBTop250RankSorter alloc] init];
170        return shared;
171}
172
173- (NSString *)displayName
174{
175        return BRLocalizedString(@"By IMDB Rank", @"[Sort] By IMDB Rank");
176}
177
178- (NSString *)displayDescription
179{
180        return BRLocalizedString(@"Sort by IMDB Top 250 Rank.", @"Sort by IMDB Top 250 Rank description");
181}
182
183- (int)sortNumber
184{
185        return 3;
186}
187
188- (void)sortFiles:(NSMutableArray *)files
189{
190        [files sortUsingSelector:@selector(movieIMDBRankCompare:)];
191}
192
193@end
194
195@implementation SapphireMovieAcademyAwardSorter
196
197+ (SapphireFileSorter *)sharedInstance
198{
199        static SapphireMovieAcademyAwardSorter *shared = nil;
200        if(shared == nil)
201                shared = [[SapphireMovieAcademyAwardSorter alloc] init];
202        return shared;
203}
204
205- (NSString *)displayName
206{
207        return BRLocalizedString(@"By Awards Won", @"[Sort] By Awards Won");
208}
209
210- (NSString *)displayDescription
211{
212        return BRLocalizedString(@"Sort by Number of Awards Won.", @"Sort by Number of Awards Won description");
213}
214
215- (int)sortNumber
216{
217        return 4;
218}
219
220- (void)sortFiles:(NSMutableArray *)files
221{
222        [files sortUsingSelector:@selector(movieOscarsWonCompare:)];
223}
224
225@end
226
227@implementation SapphireDurationSorter
228
229+ (SapphireFileSorter *)sharedInstance
230{
231        static SapphireDurationSorter *shared = nil;
232        if(shared == nil)
233                shared = [[SapphireDurationSorter alloc] init];
234        return shared;
235}
236
237- (NSString *)displayName
238{
239        return BRLocalizedString(@"By Duration", @"[Sort] By Duration");
240}
241
242- (NSString *)displayDescription
243{
244        return BRLocalizedString(@"Sort by Duration.", @"Sort by Duration description");
245}
246
247- (int)sortNumber
248{
249        return 5;
250}
251
252NSComparisonResult fileAndLinkDurationCompare(id file1, id file2, void *context)
253{
254        SapphireFileMetaData *first = getFileFromFileOrLink(file1);
255        SapphireFileMetaData *second = getFileFromFileOrLink(file2);
256       
257        return [first.duration compare:second.duration];
258}
259
260- (void)sortFiles:(NSMutableArray *)files
261{
262        [files sortUsingFunction:fileAndLinkDurationCompare context:nil];
263}
264
265@end
266
267@implementation SapphireFileSizeSorter
268
269+ (SapphireFileSorter *)sharedInstance
270{
271        static SapphireFileSizeSorter *shared = nil;
272        if(shared == nil)
273                shared = [[SapphireFileSizeSorter alloc] init];
274        return shared;
275}
276
277- (NSString *)displayName
278{
279        return BRLocalizedString(@"By File Size", @"[Sort] By File Size");
280}
281
282- (NSString *)displayDescription
283{
284        return BRLocalizedString(@"Sort by File Size.", @"Sort by File Size description");
285}
286
287- (int)sortNumber
288{
289        return 6;
290}
291
292NSComparisonResult fileAndLinkSizeCompare(id file1, id file2, void *context)
293{
294        SapphireFileMetaData *first = getFileFromFileOrLink(file1);
295        SapphireFileMetaData *second = getFileFromFileOrLink(file2);
296       
297        return [first.size compare:second.size];
298}
299
300- (void)sortFiles:(NSMutableArray *)files
301{
302        [files sortUsingFunction:fileAndLinkSizeCompare context:nil];
303}
304
305@end
306
307@implementation SapphireDateSorter
308
309+ (SapphireFileSorter *)sharedInstance
310{
311        static SapphireDateSorter *shared = nil;
312        if(shared == nil)
313                shared = [[SapphireDateSorter alloc] init];
314        return shared;
315}
316
317- (NSString *)displayName
318{
319        return BRLocalizedString(@"By Date", @"[Sort] By Date");
320}
321
322- (NSString *)displayDescription
323{
324        return BRLocalizedString(@"Sort by Date.  With TV Episodes, this is Air Date.  With Movies, this is Premier Date.", @"Sort by Date description");
325}
326
327- (int)sortNumber
328{
329        return 7;
330}
331
332NSComparisonResult fileAndLinkDateCompare(id file1, id file2, void *context)
333{
334        /*Resolve link and try to sort by episodes*/
335        SapphireFileMetaData *first = getFileFromFileOrLink(file1);
336        SapphireFileMetaData *second = getFileFromFileOrLink(file2);
337       
338        NSComparisonResult result = [first episodeAirDateCompare:second];
339        if(result != NSOrderedSame)
340                return result;
341       
342        result = [first moviePremierDateCompare:second];
343        if(result != NSOrderedSame)
344                return result;
345       
346        /*Finally sort by path*/
347        return [[[file1 valueForKey:@"path"] lastPathComponent] nameCompare:[[file2 valueForKey:@"path"] lastPathComponent]];
348}
349
350- (void)sortFiles:(NSMutableArray *)files
351{
352        [files sortUsingFunction:fileAndLinkDateCompare context:nil];
353}
354
355@end
356
357@implementation SapphireMovieIMDBRatingSorter
358
359+ (SapphireFileSorter *)sharedInstance
360{
361        static SapphireMovieIMDBRatingSorter *shared = nil;
362        if(shared == nil)
363                shared = [[SapphireMovieIMDBRatingSorter alloc] init];
364        return shared;
365}
366
367- (NSString *)displayName
368{
369        return BRLocalizedString(@"By IMDB Rating", @"[Sort] By IMDB Rating");
370}
371
372- (NSString *)displayDescription
373{
374        return BRLocalizedString(@"Sort by IMDB User Rating.", @"Sort by IMDB User Rating description");
375}
376
377- (int)sortNumber
378{
379        return 8;
380}
381
382- (void)sortFiles:(NSMutableArray *)files
383{
384        [files sortUsingSelector:@selector(movieIMDBRatingCompare:)];
385}
386
387@end
388
389@implementation SapphireFileMetaData (sorting)
390
391- (NSComparisonResult)movieIMDBRankCompare:(SapphireFileMetaData *)other
392{
393        SapphireMovie *myMovie = self.movie;
394        SapphireMovie *theirMovie = other.movie;
395       
396        if(myMovie != nil)
397                if(theirMovie != nil)
398                {
399                        NSComparisonResult ret = [myMovie imdbTop250RankingCompare:theirMovie];
400                        if(ret == NSOrderedSame)
401                                ret = [self movieCompare:other];
402                        return ret;
403                }
404                else
405                        return NSOrderedAscending;
406                else if(theirMovie != nil)
407                        return NSOrderedDescending;
408       
409        return NSOrderedSame;
410}
411
412- (NSComparisonResult)movieOscarsWonCompare:(SapphireFileMetaData *)other
413{
414        SapphireMovie *myMovie = self.movie;
415        SapphireMovie *theirMovie = other.movie;
416       
417        if(myMovie != nil)
418                if(theirMovie != nil)
419                {
420                        NSComparisonResult ret = [theirMovie oscarsWonCompare:myMovie];
421                        if(ret == NSOrderedSame)
422                                ret = [self movieCompare:other];
423                        return ret;
424                }
425                else
426                        return NSOrderedAscending;
427                else if(theirMovie != nil)
428                        return NSOrderedDescending;
429       
430        return NSOrderedSame;
431}
432
433- (NSComparisonResult)movieIMDBRatingCompare:(SapphireFileMetaData *)other
434{
435        SapphireMovie *myMovie = self.movie;
436        SapphireMovie *theirMovie = other.movie;
437       
438        if(myMovie != nil)
439                if(theirMovie != nil)
440                {
441                        NSComparisonResult ret = [myMovie imdbRatingCompare:theirMovie];
442                        if(ret == NSOrderedSame)
443                                ret = [self movieCompare:other];
444                        return -ret;
445                }
446                else
447                        return NSOrderedAscending;
448                else if(theirMovie != nil)
449                        return NSOrderedDescending;
450       
451        return NSOrderedSame;
452}
453
454- (NSComparisonResult)moviePremierDateCompare:(SapphireFileMetaData *)other
455{
456        SapphireMovie *myMovie = self.movie;
457        SapphireMovie *theirMovie = other.movie;
458       
459        if(myMovie != nil)
460                if(theirMovie != nil)
461                {
462                        NSComparisonResult ret = [myMovie releaseDateCompare:theirMovie];
463                        if(ret == NSOrderedSame)
464                                ret = [self movieCompare:other];
465                        return ret;
466                }
467                else
468                        return NSOrderedAscending;
469                else if(theirMovie != nil)
470                        return NSOrderedDescending;
471       
472        return NSOrderedSame;
473}
474
475- (NSComparisonResult)episodeAirDateCompare:(SapphireFileMetaData *)other
476{
477        SapphireEpisode *myEp = self.tvEpisode;
478        SapphireEpisode *theirEp = other.tvEpisode;
479       
480        if(myEp != nil)
481        {
482                if(theirEp != nil)
483                {
484                        NSComparisonResult ret = [myEp airDateCompare:theirEp];
485                        if(ret == NSOrderedSame)
486                                ret = [self episodeCompare:other];
487                        return ret;
488                }
489                else
490                        return NSOrderedAscending;
491        }
492        else if (theirEp != nil)
493                return NSOrderedDescending;
494       
495        return NSOrderedSame;
496}
497
498@end
Note: See TracBrowser for help on using the repository browser.