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

Revision 588, 5.8 KB checked in by gbooker, 7 years ago (diff)

Changed the filtering method and the check for unwatched/favorite. This new system is far more powerful, and should actually be faster too. Implemented a bit more caching to help speed as well.

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