source: branches/CoreData/SapphireFrappliance/MetaData/Support/SapphireEntityDirectory.m @ 575

Last change on this file since 575 was 575, checked in by gbooker, 10 years ago

Several optimizations, fixes, etc...
This is getting usable, but I'm still not happy with the speed of the movies

File size: 4.2 KB
Line 
1/*
2 * SapphireEntityDirectory.h
3 * Sapphire
4 *
5 * Created by Graham Booker on May 26, 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 "SapphireEntityDirectory.h"
22#import "SapphireFileMetaData.h"
23#import "SapphireDirectoryMetaData.h"
24#import "SapphireMetaDataSupport.h"
25#import "CoreDataSupportFunctions.h"
26
27@implementation SapphireEntityDirectory
28
29- (id)initWithEntityName:(NSString *)entityName inContext:(NSManagedObjectContext *)context
30{
31        self = [super init];
32        if(self == nil)
33                return self;
34       
35        name = [entityName retain];
36        moc = [context retain];
37        nameKey = [@"name" retain];
38        isFiles = NO;
39       
40        return self;
41}
42
43- (void) dealloc
44{
45        [name release];
46        [moc release];
47        [entities release];
48        [nameKey release];
49        [fileKey release];
50        [metaKey release];
51        [path release];
52        [parent release];
53        Basic_Directory_Function_Deallocs
54        [super dealloc];
55}
56
57- (void)setNameKey:(NSString *)key
58{
59        [nameKey release];
60        nameKey = [key retain];
61}
62
63- (void)setFinalFileKey:(NSString *)key
64{
65        [fileKey release];
66        fileKey = [key retain];
67}
68
69- (void)setMetaKey:(NSString *)key
70{
71        [metaKey release];
72        metaKey = [key retain];
73}
74
75- (NSArray *)files
76{
77        if(!isFiles)
78                return [NSArray array];
79        return [[entities allKeys] sortedArrayUsingSelector:@selector(compare:)];
80}
81
82- (NSArray *)directories
83{
84        if(isFiles)
85                return [NSArray array];
86        return [[entities allKeys] sortedArrayUsingSelector:@selector(compare:)];
87}
88
89- (NSArray *)metaFiles
90{
91        if(!isFiles)
92                return [NSArray array];
93        return [entities allValues];
94}
95
96- (NSPredicate *)metaFileFetchPredicate
97{
98        return nil;
99}
100
101- (SapphireFileMetaData *)metaDataForFile:(NSString *)file
102{
103        if(!isFiles)
104                return nil;
105        return [entities objectForKey:file];
106}
107
108- (id <SapphireDirectory>)metaDataForDirectory:(NSString *)directory
109{
110        if(isFiles)
111                return nil;
112        return [entities objectForKey:directory];
113}
114
115- (void)reloadDirectoryContents
116{
117        NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:nameKey ascending:YES];
118        NSArray *objects = nil;
119        if(fileKey != nil)
120        {
121                objects = doFetchRequest(name, moc, nil);
122                objects = [objects valueForKeyPath:[@"@unionOfSets." stringByAppendingString:fileKey]];
123        }
124        else
125                objects = doSortedFetchRequest(name, moc, nil, sort);
126        [sort release];
127        if(filterPredicate != nil)
128                objects = [objects filteredArrayUsingPredicate:filterPredicate];
129        int i, count = [objects count];
130        NSMutableDictionary *newData = [[NSMutableDictionary alloc] init];
131        if(count)
132                isFiles = ![[objects objectAtIndex:0] conformsToProtocol:@protocol(SapphireDirectory)];
133        for(i=0; i<count; i++)
134        {
135                id obj = [objects objectAtIndex:i];
136                NSString *key = [obj valueForKey:nameKey];
137                if(metaKey != nil)
138                {
139                        obj = [obj valueForKeyPath:metaKey];
140                        if([obj isKindOfClass:[NSSet class]])
141                                obj = [obj anyObject];
142                }
143                if(obj != nil)
144                        [newData setObject:obj forKey:key];
145        }
146        [entities release];
147        entities = [[NSDictionary alloc] initWithDictionary:newData];
148        [newData release];
149        [delegate directoryContentsChanged];
150}
151
152- (void)setPath:(NSString *)newPath
153{
154        [path release];
155        path = [newPath retain];
156}
157
158- (NSString *)path
159{
160        return path;
161}
162
163- (NSManagedObjectContext *)managedObjectContext
164{
165        return moc;
166}
167
168- (NSString *)coverArtPath
169{
170        return nil;
171}
172
173- (void)refreshAllObjects
174{
175        NSEnumerator *objEnum;
176        NSManagedObject *obj;
177       
178        objEnum = [entities objectEnumerator];
179        while((obj = [objEnum nextObject]) != nil)
180        {
181                [moc refreshObject:obj mergeChanges:YES];
182        }
183}
184
185- (id <SapphireDirectory>)parentDirectory
186{
187        return nil;
188}
189
190#include "SapphireBasicDirectoryFunctions.h"
191
192@end
Note: See TracBrowser for help on using the repository browser.