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)

RevLine 
[540]1#import "SapphireCategoryDirectory.h"
[585]2#import "SapphireBasicDirectoryFunctionsImports.h"
[599]3#import "SapphireFileSorter.h"
[540]4
5@implementation SapphireCategoryDirectory
6
[578]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];
[588]17        Basic_Directory_Function_Inits
18
[578]19        return self;
20}
21
[540]22- (void) dealloc
23{
[578]24        [cachedLookup release];
25        [cachedFiles release];
26        [cachedDirs release];
27        [cachedMetaFiles release];
[554]28        Basic_Directory_Function_Deallocs
[540]29        [super dealloc];
30}
31
32
[578]33- (NSString *)metaFilesValue;
[540]34{
35        return nil;
36}
37
[598]38- (NSString *)dirsValueFromFiles
[540]39{
40        return nil;
41}
42
43- (NSString *)fileNameValue
44{
[598]45        return @"path";
[540]46}
47
48- (NSString *)dirNameValue
49{
50        return nil;
51}
52
[573]53- (NSPredicate *)metaFileFetchPredicate
54{
55        return nil;
56}
57
[599]58- (NSArray *)fileSorters
[540]59{
[599]60        return nil;
[540]61}
62
[578]63- (void)defaultDirectorySort:(NSMutableArray *)dirs
[540]64{
[598]65        [dirs sortUsingSelector:@selector(nameCompare:)];
[540]66}
67
[578]68- (NSMutableArray *)internalFileFetch
[540]69{
[578]70        NSString *metaFilesKey = [self metaFilesValue];
71        if(metaFilesKey == nil)
72                return nil;
[540]73       
[578]74        NSMutableArray *files = [[[self valueForKeyPath:metaFilesKey] allObjects] mutableCopy];
[540]75        if(filterPredicate != nil)
[578]76                [files filterUsingPredicate:filterPredicate];
[540]77       
[598]78        [cachedMetaFiles setArray:files];
[578]79        return [files autorelease];
[540]80}
81
82- (NSArray *)files
83{
[578]84        return cachedFiles;
[540]85}
86
[578]87- (NSMutableArray *)internalDirectoryFetch
[540]88{
[598]89        NSString *dirsKey = [self dirsValueFromFiles];
[540]90        if(dirsKey == nil)
[578]91                return nil;
[540]92       
[598]93        NSArray *dirs = [[cachedMetaFiles valueForKeyPath:dirsKey] mutableCopy];
[578]94        return [dirs autorelease];
[540]95}
96
97- (NSArray *)directories
98{
[578]99        return cachedDirs;
[540]100}
101
102- (SapphireFileMetaData *)metaDataForFile:(NSString *)file
103{
[578]104        return [cachedLookup objectForKey:file];
[540]105}
106
107- (id <SapphireDirectory>)metaDataForDirectory:(NSString *)directory
108{
[578]109        return [cachedLookup objectForKey:directory];
[540]110}
111
112- (void)reloadDirectoryContents
113{
[557]114        [[self managedObjectContext] refreshObject:self mergeChanges:YES];
[578]115        [cachedLookup removeAllObjects];
116        [cachedFiles removeAllObjects];
117        [cachedDirs removeAllObjects];
118       
119        int sortValue = self.sortMethodValue;
[598]120        NSMutableArray *files = [self internalFileFetch];
121        if([files count] != 0)
[578]122        {
[598]123                NSString *fileNameKey = [self fileNameValue];
[599]124                if([SapphireFileSorter sortFiles:files withSorter:sortValue inAllowedSorts:[self fileSorters]])
125                {
126                        self.sortMethodValue = 0;
127                }
[598]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                }               
[578]137        }
138       
139        NSMutableArray *dirs = [self internalDirectoryFetch];
[598]140        if([dirs count] != 0)
[578]141        {
[598]142                NSString *dirNameKey = [self dirNameValue];
[609]143                [self defaultDirectorySort:dirs];
[598]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                }               
[578]154        }
155       
[551]156        [delegate directoryContentsChanged];
[540]157}
158
159- (NSString *)path
160{
161        return [@"@MOVIES" stringByAppendingPathComponent:[[self entity] name]];
162}
163
[551]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
[568]182- (void)refreshAllObjects
183{
184        NSManagedObjectContext *moc = [self managedObjectContext];
185        NSEnumerator *objEnum;
186        NSManagedObject *obj;
187       
[578]188        objEnum = [cachedLookup objectEnumerator];
[568]189        while((obj = [objEnum nextObject]) != nil)
190        {
191                [moc refreshObject:obj mergeChanges:YES];
192        }
193
194        [moc refreshObject:self mergeChanges:YES];
195}
196
[575]197- (id <SapphireDirectory>)parentDirectory
198{
199        return nil;
200}
201
[588]202- (void)invokeOnAllFiles:(NSInvocation *)fileInv
[578]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
[585]218- (BOOL)checkPredicate:(NSPredicate *)pred
[578]219{
220        NSPredicate *fetchPred = [self metaFileFetchPredicate];
221        if(fetchPred != nil)
222        {
[590]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]];
[578]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
[540]260#include "SapphireBasicDirectoryFunctions.h"
261
262@end
Note: See TracBrowser for help on using the repository browser.