source: branches/CoreData/SapphireFrappliance/MetaData/SapphireMObjects/SapphireCategoryDirectory.m @ 622

Revision 622, 6.4 KB checked in by gbooker, 6 years ago (diff)

Last of the mergeChanges change
Massive optimizations of tv show directories

Line 
1#import "SapphireCategoryDirectory.h"
2#import "SapphireBasicDirectoryFunctionsImports.h"
3#import "SapphireFileSorter.h"
4#import "CoreDataSupportFunctions.h"
5
6@implementation SapphireCategoryDirectory
7
8- (id)initWithEntity:(NSEntityDescription *)entity insertIntoManagedObjectContext:(NSManagedObjectContext *)context
9{
10        self = [super initWithEntity:entity insertIntoManagedObjectContext:context];
11        if(self == nil)
12                return self;
13       
14        cachedLookup = [[NSMutableDictionary alloc] init];
15        cachedFiles = [[NSMutableArray alloc] init];
16        cachedDirs = [[NSMutableArray alloc] init];
17        cachedMetaFiles = [[NSMutableArray alloc] init];
18        Basic_Directory_Function_Inits
19
20        return self;
21}
22
23- (void) dealloc
24{
25        [cachedLookup release];
26        [cachedFiles release];
27        [cachedDirs release];
28        [cachedMetaFiles release];
29        Basic_Directory_Function_Deallocs
30        [super dealloc];
31}
32
33- (NSString *)dirsValueFromFiles
34{
35        return nil;
36}
37
38- (NSString *)fileNameValue
39{
40        return @"path";
41}
42
43- (NSString *)dirNameValue
44{
45        return nil;
46}
47
48- (NSString *)metaFilesValue;
49{
50        return nil;
51}
52
53- (NSPredicate *)metaFileFetchPredicate
54{
55        return nil;
56}
57
58- (NSArray *)fileSorters
59{
60        return nil;
61}
62
63- (void)defaultDirectorySort:(NSMutableArray *)dirs
64{
65        [dirs sortUsingSelector:@selector(nameCompare:)];
66}
67
68- (NSMutableArray *)internalFileFetch
69{
70        NSString *metaFilesKey = [self metaFilesValue];
71        if(metaFilesKey != nil)
72        {
73                //This is the slow method
74                NSMutableArray *files = [[[self valueForKeyPath:metaFilesKey] allObjects] mutableCopy];
75                if(filterPredicate != nil)
76                        [files filterUsingPredicate:filterPredicate];
77               
78                [cachedMetaFiles setArray:files];
79                return [files autorelease];
80        }
81        NSPredicate *fetchPredicate = [self metaFileFetchPredicate];
82        if(fetchPredicate == nil)
83                return nil;
84       
85        NSPredicate *combPredicate;
86        if(filterPredicate != nil)
87                combPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:[NSArray arrayWithObjects:fetchPredicate, filterPredicate, nil]];
88        else
89                combPredicate = fetchPredicate;
90       
91        NSMutableArray *files = [doFetchRequest(SapphireFileMetaDataName, [self managedObjectContext], combPredicate) mutableCopy];
92       
93        [cachedMetaFiles setArray:files];
94        return [files autorelease];
95}
96
97- (NSArray *)files
98{
99        return cachedFiles;
100}
101
102- (NSMutableArray *)internalDirectoryFetchFromFiles:(NSArray *)files
103{
104        return nil;
105}
106
107- (void)prefetch:(NSArray *)files
108{
109}
110
111- (NSArray *)directories
112{
113        return cachedDirs;
114}
115
116- (SapphireFileMetaData *)metaDataForFile:(NSString *)file
117{
118        return [cachedLookup objectForKey:file];
119}
120
121- (id <SapphireDirectory>)metaDataForDirectory:(NSString *)directory
122{
123        return [cachedLookup objectForKey:directory];
124}
125
126- (void)reloadDirectoryContents
127{
128        [[self managedObjectContext] refreshObject:self mergeChanges:NO];
129        [cachedLookup removeAllObjects];
130        [cachedFiles removeAllObjects];
131        [cachedDirs removeAllObjects];
132       
133        int sortValue = self.sortMethodValue;
134        NSMutableArray *files = [self internalFileFetch];
135        [self prefetch:files];
136        if([files count] != 0)
137        {
138                NSString *fileNameKey = [self fileNameValue];
139                if([SapphireFileSorter sortFiles:files withSorter:sortValue inAllowedSorts:[self fileSorters]])
140                {
141                        self.sortMethodValue = 0;
142                }
143               
144                NSEnumerator *fileEnum = [files objectEnumerator];
145                SapphireFileMetaData *file;
146                while((file = [fileEnum nextObject]) != nil)
147                {
148                        NSString *name = [file valueForKeyPath:fileNameKey];
149                        [cachedLookup setObject:file forKey:name];
150                        [cachedFiles addObject:name];
151                }               
152        }
153       
154        NSMutableArray *dirs = [self internalDirectoryFetchFromFiles:files];
155        if([dirs count] != 0)
156        {
157                NSString *dirNameKey = [self dirNameValue];
158                [self defaultDirectorySort:dirs];
159               
160                NSEnumerator *dirEnum = [dirs objectEnumerator];
161                SapphireDirectoryMetaData *dir;
162                while((dir = [dirEnum nextObject]) != nil)
163                {
164                        NSString *name = [dir valueForKeyPath:dirNameKey];
165                        [dir setFilterPredicate:filterPredicate];
166                        [cachedLookup setObject:dir forKey:name];
167                        [cachedDirs addObject:name];
168                }               
169        }
170       
171        [delegate directoryContentsChanged];
172}
173
174- (NSString *)path
175{
176        return [@"@MOVIES" stringByAppendingPathComponent:[[self entity] name]];
177}
178
179- (NSString *)coverArtSearch:(NSString *)path PathUpToParents:(int)parents
180{
181        NSString *ret = searchCoverArtExtForPath([path stringByAppendingPathComponent:@"cover"]);
182        if(ret != nil)
183                return ret;
184       
185        if(parents != 0)
186                return [self coverArtSearch:[path stringByDeletingLastPathComponent] PathUpToParents:parents-1];
187        return nil;
188}
189
190- (NSString *)coverArtPath
191{
192        NSString *path = [self path];
193        int count = [[path pathComponents] count];
194        return [self coverArtSearch:[[SapphireMetaDataSupport collectionArtPath] stringByAppendingPathComponent:path] PathUpToParents:count];
195}
196
197- (void)refreshAllObjects
198{
199        NSManagedObjectContext *moc = [self managedObjectContext];
200        NSEnumerator *objEnum;
201        NSManagedObject *obj;
202       
203        objEnum = [cachedLookup objectEnumerator];
204        while((obj = [objEnum nextObject]) != nil)
205        {
206                [moc refreshObject:obj mergeChanges:NO];
207        }
208
209        [moc refreshObject:self mergeChanges:NO];
210}
211
212- (id <SapphireDirectory>)parentDirectory
213{
214        return nil;
215}
216
217- (void)invokeOnAllFiles:(NSInvocation *)fileInv
218{
219        NSArray *files = cachedMetaFiles;
220        if(![files count])
221                files = [self internalFileFetch];
222        if([files count])
223        {
224                SapphireFileMetaData *file;
225                NSEnumerator *fileEnum = [files objectEnumerator];
226                while((file = [fileEnum nextObject]) != nil)
227                {
228                        [fileInv invokeWithTarget:file];
229                }
230        }
231}
232
233- (BOOL)checkPredicate:(NSPredicate *)pred
234{
235        NSPredicate *fetchPred = [self metaFileFetchPredicate];
236        if(fetchPred != nil)
237        {
238                NSPredicate *final;
239                if(pred == nil)
240                        final = [NSCompoundPredicate andPredicateWithSubpredicates:[NSArray arrayWithObjects:fetchPred, filterPredicate, nil]];
241                else
242                        final = [NSCompoundPredicate andPredicateWithSubpredicates:[NSArray arrayWithObjects:pred, fetchPred, filterPredicate, nil]];
243                return entityExists(SapphireFileMetaDataName, [self managedObjectContext], final);     
244        }
245       
246        NSArray *files = cachedMetaFiles;
247        if(![files count])
248                files = [self internalFileFetch];
249        int i, count = [files count];
250        for(i=0; i<count; i++)
251                if([pred evaluateWithObject:[files objectAtIndex:i]])
252                        return YES;
253       
254        return NO;
255}
256
257- (void)getSubFileMetasWithDelegate:(id <SapphireMetaDataScannerDelegate>)subDelegate skipDirectories:(NSMutableSet *)skip
258{
259}
260
261- (void)scanForNewFilesWithDelegate:(id <SapphireMetaDataScannerDelegate>)subDelegate skipDirectories:(NSMutableSet *)skip
262{
263}
264
265- (void)cancelImport
266{
267}
268
269- (void)resumeImport
270{
271}
272
273#define RECURSIVE_FUNCTIONS_ALREADY_DEFINED
274
275#include "SapphireBasicDirectoryFunctions.h"
276
277@end
Note: See TracBrowser for help on using the repository browser.