source: trunk/SapphireFrappliance/MetaData/Support/SapphireMovieDirectory.m @ 966

Revision 966, 10.1 KB checked in by gbooker, 4 years ago (diff)

Made the virtual directory importer more global and at completed the custom virtual directory setup for tv directories.
Refs #313

Line 
1/*
2 * SapphireMovieDirectory.m
3 * Sapphire
4 *
5 * Created by Graham Booker on May 27, 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 <SapphireCompatClasses/SapphireFrontRowCompat.h>
22
23#import "SapphireMovieDirectory.h"
24#import "SapphireEntityDirectory.h"
25#import "SapphireFilteredFileDirectory.h"
26#import "CoreDataSupportFunctions.h"
27#import "SapphireBasicDirectoryFunctionsImports.h"
28#import "SapphireFileSorter.h"
29#import "SapphireCustomVirtualDirectoryImporter.h"
30#import "SapphireCustomVirtualDirectory.h"
31#import "SapphireMovie.h"
32#import "SapphireCast.h"
33#import "SapphireDirector.h"
34#import "SapphireGenre.h"
35
36NSArray *multiMovieEntityFetch(NSString *name, NSString *keyFromMovie, NSManagedObjectContext *moc, NSPredicate *filterPredicate)
37{
38        NSPredicate *entPred = nil;
39        if(filterPredicate != nil)
40        {
41                NSPredicate *fetchPredicate = [NSPredicate predicateWithFormat:@"movie != nil"];
42                NSPredicate *finalPred;
43                if(filterPredicate == nil)
44                        finalPred = fetchPredicate;
45                else
46                        finalPred = [NSCompoundPredicate andPredicateWithSubpredicates:[NSArray arrayWithObjects:filterPredicate, fetchPredicate, nil]];
47                NSArray *files = doFetchRequest(SapphireFileMetaDataName, moc, finalPred);
48                NSSet *movieIds = [NSSet setWithArray:[files valueForKeyPath:@"movie.objectID"]];
49               
50                if(![SapphireFrontRowCompat usingLeopard])
51                {
52                        /*      Damn you Apple for not making take 2 Leopard.  Not only does this make obj C 2 not available,
53                         but it also means that I have to content with a crippled and slower core data.  The else block here
54                         executes on Leopard at several times the speed, but on Tiger throws the exception:
55                         "to-many key not allowed here" even though it can be done through a JOIN in the SQL!!!!!*/
56                        NSPredicate *moviePred = [NSPredicate predicateWithFormat:@"SELF IN %@", movieIds];
57                        NSArray *movies = doFetchRequest(SapphireMovieName, moc, moviePred);
58                        NSArray *entSet = [movies valueForKeyPath:[NSString stringWithFormat:@"@distinctUnionOfSets.%@.objectID", keyFromMovie]];
59                       
60                        entPred = [NSPredicate predicateWithFormat:@"SELF IN %@", entSet];
61                }
62                else
63                        entPred = [NSPredicate predicateWithFormat:@"ANY movies IN %@", movieIds];
64        }
65        return doFetchRequest(name, moc, entPred);
66}
67
68NSArray *castEntityFetch(NSManagedObjectContext *moc, NSPredicate *filterPredicate)
69{
70        return multiMovieEntityFetch(SapphireCastName, @"cast", moc, filterPredicate);
71}
72
73NSArray *directorEntityFetch(NSManagedObjectContext *moc, NSPredicate *filterPredicate)
74{
75        return multiMovieEntityFetch(SapphireDirectorName, @"directors", moc, filterPredicate);
76}
77
78NSArray *genreEntityFetch(NSManagedObjectContext *moc, NSPredicate *filterPredicate)
79{
80        return multiMovieEntityFetch(SapphireGenreName, @"genres", moc, filterPredicate);
81}
82
83@implementation SapphireMovieDirectory
84
85- (id)initWithContext:(NSManagedObjectContext *)context
86{
87        self = [super init];
88        if(self == nil)
89                return self;
90       
91        moc = [context retain];
92       
93        /*Define the static virtual directories*/
94        NSPredicate *allPred = [NSPredicate predicateWithFormat:@"movie != nil"];
95        SapphireFilteredFileDirectory *all = [[SapphireFilteredFileDirectory alloc] initWithPredicate:allPred Context:moc];
96        SapphireEntityDirectory *cast = [[SapphireEntityDirectory alloc] initWithEntityFetch:castEntityFetch inContext:moc];
97        SapphireEntityDirectory *director = [[SapphireEntityDirectory alloc] initWithEntityFetch:directorEntityFetch inContext:moc];
98        SapphireEntityDirectory *genre = [[SapphireEntityDirectory alloc] initWithEntityFetch:genreEntityFetch inContext:moc];
99        NSPredicate *top250Pred = [NSPredicate predicateWithFormat:@"movie.imdbTop250Ranking != 0"];
100        SapphireFilteredFileDirectory *top250 = [[SapphireFilteredFileDirectory alloc] initWithPredicate:top250Pred Context:moc];
101        NSPredicate *oscarPred = [NSPredicate predicateWithFormat:@"movie.oscarsWon != 0"];
102        SapphireFilteredFileDirectory *oscar = [[SapphireFilteredFileDirectory alloc] initWithPredicate:oscarPred Context:moc];
103
104        originalSubDirs = [[NSArray alloc] initWithObjects:
105                                           all,
106                                           cast,
107                                           director,
108                                           genre,
109                                           top250,
110                                           oscar,
111                                           nil];
112       
113        originalNames = [[NSArray alloc] initWithObjects:
114                                         BRLocalizedString( @"All Movies", @"Select all movies" ),
115                                         BRLocalizedString( @"By Cast", @"Select movies based on cast members" ),
116                                         BRLocalizedString( @"By Director", @"Select movies based on director" ),
117                                         BRLocalizedString( @"By Genre", @"Select movies based on genre" ),
118                                         BRLocalizedString( @"IMDB Top 250", @"Show movies in IMDb Top 250 only" ),
119                                         BRLocalizedString( @"Academy Award Winning", @"Show Oscar winning movies only" ),
120                                         nil];
121                                         
122        SapphireFileSorter *titleSort = [SapphireMovieTitleSorter sharedInstance];
123        SapphireFileSorter *imdbRankSort = [SapphireMovieIMDBTop250RankSorter sharedInstance];
124        SapphireFileSorter *awardSort = [SapphireMovieAcademyAwardSorter sharedInstance];
125        SapphireFileSorter *dateSort = [SapphireDateSorter sharedInstance];
126        SapphireFileSorter *imdbRatingSort = [SapphireMovieIMDBRatingSorter sharedInstance];
127       
128        NSString *moviePath = [[NSBundle bundleForClass:[self class]] pathForResource:@"video_H" ofType:@"png"];
129        defaultSorters = [[NSArray alloc] initWithObjects:titleSort, dateSort, imdbRatingSort, nil];
130       
131        /*Finish the static directory setup*/
132        [all setPath:VIRTUAL_DIR_ALL_PATH];
133        [all setCoverArtPath:moviePath];
134        [all setFileSorters:[NSArray arrayWithObjects:titleSort, dateSort, imdbRatingSort, nil]];
135        [cast setPath:VIRTUAL_DIR_CAST_PATH];
136        [cast setCoverArtPath:moviePath];
137        [cast setMetaFileFetchPredicate:[NSPredicate predicateWithFormat:@"movie != nil AND ANY movie.#cast != nil"]];
138        [director setPath:VIRTUAL_DIR_DIRECTOR_PATH];
139        [director setCoverArtPath:moviePath];
140        [director setMetaFileFetchPredicate:[NSPredicate predicateWithFormat:@"movie != nil AND ANY movie.directors != nil"]];
141        [genre setPath:VIRTUAL_DIR_GENRE_PATH];
142        [genre setCoverArtPath:moviePath];
143        [genre setMetaFileFetchPredicate:[NSPredicate predicateWithFormat:@"movie != nil AND ANY movie.genres != nil"]];
144        [top250 setPath:VIRTUAL_DIR_TOP250_PATH];
145        [top250 setCoverArtPath:moviePath];
146        [top250 setFileSorters:[NSArray arrayWithObjects:imdbRankSort, titleSort, dateSort, imdbRatingSort, nil]];
147        [oscar setPath:VIRTUAL_DIR_OSCAR_PATH];
148        [oscar setCoverArtPath:[[NSBundle bundleForClass:[self class]] pathForResource:@"AMPAS_Oscar_H" ofType:@"png"]];
149        [oscar setFileSorters:[NSArray arrayWithObjects:awardSort, titleSort, dateSort, imdbRatingSort, nil]];
150        [subDirs makeObjectsPerformSelector:@selector(setNotificationName:) withObject:MOVIE_DID_CHANGE_PREDICATE_MATCHING];
151        Basic_Directory_Function_Inits
152
153        [all release];
154        [cast release];
155        [director release];
156        [genre release];
157        [top250 release];
158        [oscar release];
159        return self;
160}
161
162- (void) dealloc
163{
164        [originalSubDirs release];
165        [originalNames release];
166        [subDirs release];
167        [names release];
168        [virtualDirs release];
169        [defaultSorters release];
170        Basic_Directory_Function_Deallocs
171        [super dealloc];
172}
173
174- (NSArray *)files
175{
176        return [NSArray array];
177}
178
179- (NSArray *)directories
180{
181        if(filterPredicate == nil)
182                return names;
183       
184        NSMutableArray *ret = [NSMutableArray array];
185        int i, count = [names count];
186        for(i=0; i<count; i++)
187        {
188                if([[subDirs objectAtIndex:i] containsFileMatchingFilterPredicate:filterPredicate])
189                        [ret addObject:[names objectAtIndex:i]];
190        }
191        return ret;
192}
193
194- (NSArray *)metaFiles
195{
196        NSPredicate *allPred = [NSPredicate predicateWithFormat:@"movie != nil"];
197        return doFetchRequest(SapphireFileMetaDataName, moc, allPred);
198}
199
200- (NSPredicate *)metaFileFetchPredicate
201{
202        return nil;
203}
204
205- (SapphireFileMetaData *)metaDataForFile:(NSString *)file
206{
207        return nil;
208}
209
210- (id <SapphireDirectory>)metaDataForDirectory:(NSString *)directory
211{
212        int index = [names indexOfObject:directory];
213        if(index == NSNotFound)
214                return nil;
215        return [subDirs objectAtIndex:index];
216}
217
218- (void)reloadDirectoryContents
219{
220        /*Import any defined movie virtual directories*/
221        NSArray *newVirtualDirs = [[SapphireApplianceController customVirtualDirectoryImporter] movieVirtualDirectories];
222        if(![virtualDirs isEqualToArray:newVirtualDirs])
223        {
224                [virtualDirs release];
225                [names release];
226                [subDirs release];
227                virtualDirs = [newVirtualDirs retain];
228                names = [originalNames mutableCopy];
229                subDirs = [originalSubDirs mutableCopy];
230               
231                NSString *moviePath = [[NSBundle bundleForClass:[self class]] pathForResource:@"video_H" ofType:@"png"];
232                NSEnumerator *mvdEnum = [newVirtualDirs objectEnumerator];
233                SapphireCustomVirtualDirectory *virtualDir;
234                while((virtualDir = [mvdEnum nextObject]) != nil)
235                {
236                        SapphireFilteredFileDirectory *custom = [[SapphireFilteredFileDirectory alloc] initWithPredicate:[virtualDir predicate] Context:moc];
237                        [subDirs addObject:custom];
238                        [names addObject:[virtualDir title]];
239                        [custom setPath:[[VIRTUAL_DIR_ROOT_PATH stringByAppendingString:@"/"] stringByAppendingString:[virtualDir description]]];
240                        [custom setCoverArtPath:moviePath]; // Change this to be part of the XML?
241                        [custom setFileSorters:defaultSorters];
242                        [custom release];
243                }
244        }
245       
246        [subDirs makeObjectsPerformSelector:@selector(setFilterPredicate:) withObject:filterPredicate];
247        [delegate directoryContentsChanged];
248}
249
250- (NSManagedObjectContext *)managedObjectContext
251{
252        return moc;
253}
254
255- (BOOL)objectIsDeleted
256{
257        return NO;
258}
259
260- (NSString *)path
261{
262        return @"@MOVIES";
263}
264
265- (NSString *)coverArtPath
266{
267        return [[SapphireMetaDataSupport collectionArtPath] stringByAppendingPathComponent:@"@MOVIES"];
268}
269
270- (void)faultAllObjects
271{
272}
273
274- (id <SapphireDirectory>)parentDirectory
275{
276        return nil;
277}
278
279#include "SapphireBasicDirectoryFunctions.h"
280
281@end
Note: See TracBrowser for help on using the repository browser.