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

Revision 609, 5.9 KB checked in by gbooker, 6 years ago (diff)

When sorter is in place, still sort the directories the normal way (until such a time a better mechanism is in place)

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