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

Revision 1261, 5.3 KB checked in by gbooker, 5 years ago (diff)

Added ability to calculate a show's sort path based on where files are currently located.

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