source: trunk/SapphireMovieDirectory.m @ 257

Revision 257, 8.3 KB checked in by pmerrill, 7 years ago (diff)
Line 
1//
2//  SapphireMovieDirectory.m
3//  Sapphire
4//
5//  Created by Patrick Merrill on 10/22/07.
6//  Copyright 2007 www.nanopi.net. All rights reserved.
7//
8
9#import "SapphireMovieDirectory.h"
10#import "SapphireMetaData.h"
11
12@interface SapphireDirectoryMetaData (privateFunctions)
13- (id)initWithDictionary:(NSDictionary *)dict parent:(SapphireMetaData *)myParent path:(NSString *)myPath;
14@end
15
16@implementation SapphireMovieBaseDirectory
17- (id)initWithParent:(SapphireMovieBaseDirectory *)myParent path:(NSString *)myPath
18{
19        self = [super initWithDictionary:nil parent:myParent path:myPath];
20        if(self == nil)
21                return nil;
22       
23        directory = [[NSMutableDictionary alloc] init];
24        reloadTimer = nil;
25        scannedDirectory = YES;
26       
27        return self;
28}
29
30- (void) dealloc
31{
32        [directory release];
33        [reloadTimer invalidate];
34        [super dealloc];
35}
36
37- (void)reloadDirectoryContents
38{
39        [files removeAllObjects];
40        [directories removeAllObjects];
41        [metaFiles removeAllObjects];
42        [metaDirs removeAllObjects];
43        [cachedMetaFiles removeAllObjects];
44        [cachedMetaDirs removeAllObjects];
45        [reloadTimer invalidate];
46        reloadTimer = nil;
47}
48
49- (void)setReloadTimer
50{
51        [reloadTimer invalidate];
52        reloadTimer = [NSTimer scheduledTimerWithTimeInterval:0 target:self selector:@selector(reloadDirectoryContents) userInfo:nil repeats:NO];
53}
54
55- (void)processFile:(SapphireFileMetaData *)file
56{
57}
58
59- (void)removeFile:(SapphireFileMetaData *)file
60{
61}
62
63- (void)childDisplayChanged
64{
65        [self setReloadTimer];
66}
67
68- (BOOL)isDisplayEmpty
69{
70        return [files count] == [directories count];
71}
72
73- (BOOL)isEmpty
74{
75        return [directory count] == 0;
76}
77
78@end
79
80@implementation SapphireMovieDirectory
81- (id)initWithParent:(SapphireMovieBaseDirectory *)myParent path:(NSString *)myPath
82{
83        self = [super initWithParent:myParent path:myPath];
84        if(self == nil)
85                return nil;
86       
87        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(fileAdded:) name:META_DATA_FILE_ADDED_NOTIFICATION object:nil];
88        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(fileRemoved:) name:META_DATA_FILE_REMOVED_NOTIFICATION object:nil];
89        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(fileInfoHasChanged:) name:META_DATA_FILE_INFO_HAS_CHANGED_NOTIFICATION object:nil];
90        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(fileInfoWillChanged:) name:META_DATA_FILE_INFO_WILL_CHANGE_NOTIFICATION object:nil];
91       
92        return self;
93}
94
95- (id)initWithCollection:(SapphireMetaDataCollection *)myCollection
96{
97        self = [self initWithParent:nil path:@"@MOVIES"];
98        if(self == nil)
99                return nil;
100       
101        collection = myCollection;
102       
103        return self;
104}
105
106- (void)writeMetaData
107{
108        [collection writeMetaData];
109}
110
111- (void)fileAdded:(NSNotification *)notification
112{
113        SapphireFileMetaData *file = [notification object];
114        [self processFile:file];
115}
116
117- (void)fileRemoved:(NSNotification *)notification
118{
119        SapphireFileMetaData *file = [notification object];
120        [self removeFile:file];
121}
122
123- (void)fileInfoHasChanged:(NSNotification *)notification
124{
125        NSDictionary *info = [notification userInfo];
126        if(![[info objectForKey:META_DATA_FILE_INFO_KIND] isEqualToString:META_IMDB_IMPORT_KEY])
127                return;
128        SapphireFileMetaData *file = [notification object];
129        [self processFile:file];
130}
131
132- (void)fileInfoWillChanged:(NSNotification *)notification
133{
134        NSDictionary *info = [notification userInfo];
135        if(![[info objectForKey:META_DATA_FILE_INFO_KIND] isEqualToString:META_IMDB_IMPORT_KEY])
136                return;
137        SapphireFileMetaData *file = [notification object];
138        [self removeFile:file];
139}
140
141- (void)reloadDirectoryContents
142{
143        [super reloadDirectoryContents];
144        NSMutableDictionary *mutDict = [[NSMutableDictionary alloc] init];
145        NSEnumerator *keyEnum = [directory keyEnumerator];
146        NSString *key = nil;
147        while((key = [keyEnum nextObject]) != nil)
148        {
149                SapphireMovieGenreDirectory *dir = [directory objectForKey:key];
150                if(![dir isDisplayEmpty])
151                        [mutDict setObject:dir forKey:key];
152        }
153        [directories addObjectsFromArray:[mutDict allKeys]];
154        [directories sortUsingSelector:@selector(directoryNameCompare:)];
155        [cachedMetaDirs addEntriesFromDictionary:mutDict];
156        [metaDirs addEntriesFromDictionary:mutDict];
157        [mutDict release];
158        [(SapphireMovieBaseDirectory *)parent childDisplayChanged];
159}
160
161- (void)processFile:(SapphireFileMetaData *)file
162{
163        NSArray * genres=[file movieGenres];
164        NSEnumerator *genresEnum = [genres objectEnumerator];
165        NSString *genre = nil;
166        while((genre = [genresEnum nextObject]) != nil)
167        {
168                BOOL added=NO ;
169                SapphireMovieGenreDirectory *genreInfo=[directory objectForKey:genre];
170                if(genreInfo==nil)
171                {
172                        genreInfo=[[SapphireMovieGenreDirectory alloc] initWithParent:self path:[[self path] stringByAppendingString:genre]];
173                        [directory setObject:genreInfo forKey:genre];
174                        [genreInfo release];
175                        added=YES;
176                }
177                [genreInfo processFile:file];
178                if(added==YES)
179                {
180                        if([genreInfo isEmpty])
181                                [directory removeObjectForKey:genre];
182                }
183        }
184        [self setReloadTimer];
185}
186
187- (void)removeFile:(SapphireFileMetaData *)file
188{
189        NSArray * genres=[file movieGenres];
190        NSEnumerator *genresEnum = [genres objectEnumerator];
191        NSString *genre = nil;
192        while((genre = [genresEnum nextObject]) != nil)
193        {
194                SapphireMovieGenreDirectory *genreInfo = [directory objectForKey:genre];
195                if(genreInfo != nil)
196                {
197                        [genreInfo removeFile:file];
198                        if([genreInfo isEmpty])
199                                [directory removeObjectForKey:genre];
200                }
201        }
202        [self setReloadTimer];
203}
204@end
205/*
206@implementation SapphireMovieGenreDirectory
207- (void)reloadDirectoryContents
208{
209        [super reloadDirectoryContents];
210        NSMutableDictionary *mutDict = [[NSMutableDictionary alloc] init];
211        NSEnumerator *keyEnum = [directory keyEnumerator];
212        NSString *key = nil;
213        while((key = [keyEnum nextObject]) != nil)
214        {
215                SapphireSeasonDirectory *dir = [directory objectForKey:key];
216                if(![dir isDisplayEmpty])
217                        [mutDict setObject:dir forKey:key];
218        }
219        [directories addObjectsFromArray:[mutDict allKeys]];
220        [directories sortUsingSelector:@selector(directoryNameCompare:)];
221        [cachedMetaDirs addEntriesFromDictionary:mutDict];
222        [metaDirs addEntriesFromDictionary:mutDict];
223        [mutDict release];
224        [(SapphireTVBaseDirectory *)parent childDisplayChanged];
225}
226
227- (void)processFile:(SapphireFileMetaData *)file
228{
229        int seasonNum = [file seasonNumber];
230        if(seasonNum == 0)
231                return;
232        BOOL added = NO;
233        NSString *season = [NSString stringWithFormat:BRLocalizedString(@"Season %d", @"Season name"), seasonNum];
234        SapphireSeasonDirectory *seasonInfo = [directory objectForKey:season];
235        if(seasonInfo == nil)
236        {
237                seasonInfo = [[SapphireSeasonDirectory alloc] initWithParent:self path:[[self path] stringByAppendingPathComponent:season]];
238                [directory setObject:seasonInfo forKey:season];
239                [seasonInfo release];
240                added = YES;
241        }
242        [seasonInfo processFile:file];
243        if(added == YES)
244        {
245                if([seasonInfo isEmpty])
246                        [directory removeObjectForKey:season];
247                else
248                        [self setReloadTimer];
249        }
250}
251
252- (void)removeFile:(SapphireFileMetaData *)file
253{
254        int seasonNum = [file seasonNumber];
255        if(seasonNum == 0)
256                return;
257        NSString *season = [NSString stringWithFormat:BRLocalizedString(@"Season %d", @"Season name"), seasonNum];
258        SapphireSeasonDirectory *seasonInfo = [directory objectForKey:season];
259        if(seasonInfo == nil)
260        {
261                [seasonInfo removeFile:file];
262                if([seasonInfo isEmpty])
263                {
264                        [directory removeObjectForKey:season];
265                        [self setReloadTimer];
266                }
267        }
268}
269@end
270*/
271
272@implementation SapphireMovieGenreDirectory
273- (void)reloadDirectoryContents
274{
275        [super reloadDirectoryContents];
276        NSFileManager *fm = [NSFileManager defaultManager];
277        NSMutableDictionary *mutDict = [[NSMutableDictionary alloc] init];
278        NSEnumerator *keyEnum = [directory keyEnumerator];
279        NSString *key = nil;
280        while((key = [keyEnum nextObject]) != nil)
281        {
282                SapphireFileMetaData *file = [directory objectForKey:key];
283                if([fm fileExistsAtPath:[file path]])
284                {
285                        if([file fileClass]==FILE_CLASS_MOVIE)
286                        {
287                                NSString * title=[file movieTitle];
288                                [mutDict setObject:file forKey:title];
289                        }
290                        else
291                                continue;
292                }
293        }
294        [files addObjectsFromArray:[mutDict allKeys]];
295        [files sortUsingSelector:@selector(directoryNameCompare:)];
296        [cachedMetaFiles addEntriesFromDictionary:mutDict];
297        [metaFiles addEntriesFromDictionary:mutDict];
298        [mutDict release];
299        [(SapphireMovieBaseDirectory *)parent childDisplayChanged];
300}
301
302- (void)processFile:(SapphireFileMetaData *)file
303{
304        [directory setObject:file forKey:[file path]];
305        [self setReloadTimer];
306}
307
308- (void)removeFile:(SapphireFileMetaData *)file
309{
310        [directory removeObjectForKey:[file path]];
311        [self setReloadTimer];
312}
313@end
Note: See TracBrowser for help on using the repository browser.