source: branches/CoreData/SapphireFrappliance/MetaData/Support/SapphireFileSorter.m @ 872

Revision 872, 9.1 KB checked in by gbooker, 5 years ago (diff)

Correct ordering of IMDB sorting
Fixes #293

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