source: trunk/SapphireMovieDirectory.m @ 256

Revision 256, 8.4 KB checked in by pmerrill, 6 years ago (diff)
  • Added a dynamic moviebrowser allowing the user to traverse movies by genre.
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        //              else
183        //                      [self setReloadTimer];
184                }
185        }
186        [self setReloadTimer];
187}
188
189- (void)removeFile:(SapphireFileMetaData *)file
190{
191        NSArray * genres=[file movieGenres];
192        NSEnumerator *genresEnum = [genres objectEnumerator];
193        NSString *genre = nil;
194        while((genre = [genresEnum nextObject]) != nil)
195        {
196                SapphireMovieGenreDirectory *genreInfo = [directory objectForKey:genre];
197                if(genreInfo != nil)
198                {
199                        [genreInfo removeFile:file];
200                        if([genreInfo isEmpty])
201                        {
202                                [directory removeObjectForKey:genre];
203                        //      [self setReloadTimer];
204                        }
205                }
206        }
207        [self setReloadTimer];
208}
209@end
210/*
211@implementation SapphireMovieGenreDirectory
212- (void)reloadDirectoryContents
213{
214        [super reloadDirectoryContents];
215        NSMutableDictionary *mutDict = [[NSMutableDictionary alloc] init];
216        NSEnumerator *keyEnum = [directory keyEnumerator];
217        NSString *key = nil;
218        while((key = [keyEnum nextObject]) != nil)
219        {
220                SapphireSeasonDirectory *dir = [directory objectForKey:key];
221                if(![dir isDisplayEmpty])
222                        [mutDict setObject:dir forKey:key];
223        }
224        [directories addObjectsFromArray:[mutDict allKeys]];
225        [directories sortUsingSelector:@selector(directoryNameCompare:)];
226        [cachedMetaDirs addEntriesFromDictionary:mutDict];
227        [metaDirs addEntriesFromDictionary:mutDict];
228        [mutDict release];
229        [(SapphireTVBaseDirectory *)parent childDisplayChanged];
230}
231
232- (void)processFile:(SapphireFileMetaData *)file
233{
234        int seasonNum = [file seasonNumber];
235        if(seasonNum == 0)
236                return;
237        BOOL added = NO;
238        NSString *season = [NSString stringWithFormat:BRLocalizedString(@"Season %d", @"Season name"), seasonNum];
239        SapphireSeasonDirectory *seasonInfo = [directory objectForKey:season];
240        if(seasonInfo == nil)
241        {
242                seasonInfo = [[SapphireSeasonDirectory alloc] initWithParent:self path:[[self path] stringByAppendingPathComponent:season]];
243                [directory setObject:seasonInfo forKey:season];
244                [seasonInfo release];
245                added = YES;
246        }
247        [seasonInfo processFile:file];
248        if(added == YES)
249        {
250                if([seasonInfo isEmpty])
251                        [directory removeObjectForKey:season];
252                else
253                        [self setReloadTimer];
254        }
255}
256
257- (void)removeFile:(SapphireFileMetaData *)file
258{
259        int seasonNum = [file seasonNumber];
260        if(seasonNum == 0)
261                return;
262        NSString *season = [NSString stringWithFormat:BRLocalizedString(@"Season %d", @"Season name"), seasonNum];
263        SapphireSeasonDirectory *seasonInfo = [directory objectForKey:season];
264        if(seasonInfo == nil)
265        {
266                [seasonInfo removeFile:file];
267                if([seasonInfo isEmpty])
268                {
269                        [directory removeObjectForKey:season];
270                        [self setReloadTimer];
271                }
272        }
273}
274@end
275*/
276
277@implementation SapphireMovieGenreDirectory
278- (void)reloadDirectoryContents
279{
280        [super reloadDirectoryContents];
281        NSFileManager *fm = [NSFileManager defaultManager];
282        NSMutableDictionary *mutDict = [[NSMutableDictionary alloc] init];
283        NSEnumerator *keyEnum = [directory keyEnumerator];
284        NSString *key = nil;
285        while((key = [keyEnum nextObject]) != nil)
286        {
287                SapphireFileMetaData *file = [directory objectForKey:key];
288                if([fm fileExistsAtPath:[file path]])
289                {
290                        if([file fileClass]==FILE_CLASS_MOVIE)
291                        {
292                                NSString * title=[file movieTitle];
293                                [mutDict setObject:file forKey:title];
294                        }
295                        else
296                                continue;
297                }
298        }
299        [files addObjectsFromArray:[mutDict allKeys]];
300        [files sortUsingSelector:@selector(directoryNameCompare:)];
301        [cachedMetaFiles addEntriesFromDictionary:mutDict];
302        [metaFiles addEntriesFromDictionary:mutDict];
303        [mutDict release];
304        [(SapphireMovieBaseDirectory *)parent childDisplayChanged];
305}
306
307- (void)processFile:(SapphireFileMetaData *)file
308{
309        [directory setObject:file forKey:[file path]];
310        [self setReloadTimer];
311}
312
313- (void)removeFile:(SapphireFileMetaData *)file
314{
315        [directory removeObjectForKey:[file path]];
316        [self setReloadTimer];
317}
318@end
Note: See TracBrowser for help on using the repository browser.