source: trunk/SapphireFrappliance/MetaData/SapphireMObjects/SapphireTVShow.m @ 1291

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

Added sort by duration and sorting by filesize.

Line 
1#import "SapphireTVShow.h"
2#import "SapphireSeason.h"
3#import "SapphireEpisode.h"
4#import "SapphireSubEpisode.h"
5#import "CoreDataSupportFunctions.h"
6#import "SapphireMetaDataSupport.h"
7#import "NSString-Extensions.h"
8#import "SapphireFileSorter.h"
9#import "SapphireTVTranslation.h"
10#import "SapphireFileMetaData.h"
11#import "SapphireOtherInformation.h"
12
13NSString *autoSortPathKey = @"autoSortPath";
14
15@implementation SapphireTVShow
16
17static NSArray *allowedSorts = nil;
18
19+ (void)load
20{
21        allowedSorts = [[NSArray alloc] initWithObjects:[SapphireTVEpisodeSorter sharedInstance], [SapphireDurationSorter sharedInstance], [SapphireFileSizeSorter sharedInstance], [SapphireDateSorter sharedInstance], nil];
22}
23
24+ (SapphireTVShow *)show:(NSString *)show inContext:(NSManagedObjectContext *)moc
25{
26        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"name == %@", show];
27        SapphireTVShow *ret = (SapphireTVShow *)doSingleFetchRequest(SapphireTVShowName, moc, predicate);
28        if(ret != nil)
29                return ret;
30       
31        ret = [NSEntityDescription insertNewObjectForEntityForName:SapphireTVShowName inManagedObjectContext:moc];
32        ret.name = show;
33        return ret;
34}
35
36+ (void)upgradeShowsVersion:(int)version fromContext:(NSManagedObjectContext *)oldMoc toContext:(NSManagedObjectContext *)newMoc
37{
38        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
39        NSArray *oldShows = doFetchRequest(SapphireTVShowName, oldMoc, nil);
40        NSEnumerator *showEnum = [oldShows objectEnumerator];
41        NSManagedObject *oldShow;
42        while((oldShow = [showEnum nextObject]) != nil)
43        {
44                SapphireTVShow *newShow = [NSEntityDescription insertNewObjectForEntityForName:SapphireTVShowName inManagedObjectContext:newMoc];
45                newShow.name = [oldShow valueForKey:@"name"];
46                newShow.showDescription = [oldShow valueForKey:@"showDescription"];
47               
48                NSEnumerator *translationEnum = [[oldShow valueForKey:@"translations"] objectEnumerator];
49                NSManagedObject *translation;
50                while((translation = [translationEnum nextObject]) != nil)
51                {
52                        [SapphireTVTranslation upgradeTVTranslationVersion:version from:translation toShow:newShow];
53                }
54               
55                NSEnumerator *seasonEnum = [[oldShow valueForKey:@"seasons"] objectEnumerator];
56                NSManagedObject *season;
57                while((season = [seasonEnum nextObject]) != nil)
58                {
59                        [SapphireSeason upgradeSeasonVersion:version from:season toShow:newShow];
60                }
61        }
62        [SapphireTVTranslation upgradeShowLessTVTranslationsVersion:version fromContext:oldMoc toContext:newMoc];
63        [pool drain];
64}
65
66+ (NSArray *)sortMethods
67{
68        return allowedSorts;
69}
70
71- (NSComparisonResult)compare:(SapphireTVShow *)other
72{
73        return [self.name nameCompare:other.name];
74}
75
76- (NSString *)dirNameValue
77{
78        return @"seasonName";
79}
80
81- (NSPredicate *)metaFileFetchPredicate
82{
83        return [NSPredicate predicateWithFormat:@"tvEpisode.tvShow == %@", self];
84}
85
86- (NSArray *)fileSorters
87{
88        return allowedSorts;
89}
90
91- (void)defaultDirectorySort:(NSMutableArray *)dirs
92{
93        [dirs sortUsingSelector:@selector(compare:)];
94}
95
96- (NSString *)path
97{
98        return [NSString stringWithFormat:@"@TV/%@", self.name];
99}
100
101- (NSString *)classDefaultCoverPath
102{
103        return [[NSBundle bundleForClass:[self class]] pathForResource:@"TV" ofType:@"png"];
104}
105
106static inline NSArray *getEpsFromFiles(NSManagedObjectContext *moc, NSArray *files)
107{
108        NSSet *epIds = [NSSet setWithArray:[files valueForKeyPath:@"tvEpisode.objectID"]];
109       
110        NSPredicate *epPredicate = [NSPredicate predicateWithFormat:@"SELF IN %@", epIds];
111        return doFetchRequest(SapphireEpisodeName, moc, epPredicate);
112}
113
114- (NSMutableArray *)internalDirectoryFetchFromFiles:(NSArray *)files
115{
116        NSManagedObjectContext *moc = [self managedObjectContext];
117        NSArray *eps = getEpsFromFiles(moc, files);
118       
119        NSSet *seasonIds = [NSSet setWithArray:[eps valueForKeyPath:@"season.objectID"]];
120        NSPredicate *seasonPred = [NSPredicate predicateWithFormat:@"SELF IN %@", seasonIds];
121        NSMutableArray *ret = [doFetchRequest(SapphireSeasonName, moc, seasonPred) mutableCopy];
122        return [ret autorelease];
123}
124
125- (void)prefetch:(NSArray *)files
126{
127        NSManagedObjectContext *moc = [self managedObjectContext];
128        NSArray *eps = getEpsFromFiles(moc, files);
129       
130        NSPredicate *subEpsPred = [NSPredicate predicateWithFormat:@"episode IN %@", eps];
131        doFetchRequest(SapphireSubEpisodeName, moc, subEpsPred);
132}
133
134- (BOOL)shouldDelete
135{
136        return [self.episodesSet count] == 0;
137}
138
139-(void)removeEpisodes:(NSSet*)removedEpisodes
140{
141        [super removeEpisodes:removedEpisodes];
142        if([self.episodesSet count] == 0)
143                [SapphireMetaDataSupport setObjectForPendingDelete:self];
144}
145
146- (void)removeEpisodesObject:(SapphireEpisode*)removedEpisode
147{
148        [super removeEpisodesObject:removedEpisode];
149        if([self.episodesSet count] == 0)
150                [SapphireMetaDataSupport setObjectForPendingDelete:self];
151}
152
153- (NSString *)calculateAutoSortPath
154{
155        NSArray *files = doFetchRequest(SapphireFileMetaDataName, [self managedObjectContext], [self metaFileFetchPredicate]);
156        if([files count] == 0)
157                return nil;
158       
159        NSString *sortPath = [[(SapphireFileMetaData *)[files objectAtIndex:0] path] stringByDeletingLastPathComponent];
160        BOOL cropTwoDirs = NO;
161        NSEnumerator *fileEnum = [files objectEnumerator];
162        SapphireFileMetaData *file;
163        while((file = [fileEnum nextObject]) != nil)
164        {
165                NSString *dirPath = [[file path] stringByDeletingLastPathComponent];
166                if(cropTwoDirs)
167                        dirPath = [dirPath stringByDeletingLastPathComponent];
168               
169                if([dirPath isEqualToString:sortPath])
170                        continue;
171               
172                if(!cropTwoDirs)
173                {
174                        sortPath = [sortPath stringByDeletingLastPathComponent];
175                        dirPath = [dirPath stringByDeletingLastPathComponent];
176                        cropTwoDirs = YES;
177                        if([dirPath isEqualToString:sortPath])
178                                continue;
179                }
180               
181                return nil;
182        }
183       
184        if(!cropTwoDirs)
185        {
186                NSString *lastPathComponent = [sortPath lastPathComponent];
187                if([[lastPathComponent lowercaseString] hasPrefix:@"season"])
188                        sortPath = [sortPath stringByDeletingLastPathComponent];
189        }
190       
191        return sortPath;
192}
193
194- (NSString *)autoSortPath
195{
196        return [self otherInformationForKey:autoSortPathKey];
197}
198
199- (void)setAutoSortPath:(NSString *)path
200{
201        [self setOtherObject:path forKey:autoSortPathKey];
202}
203
204@end
Note: See TracBrowser for help on using the repository browser.