source: trunk/SapphireMovieDirectory.m @ 299

Revision 299, 5.8 KB checked in by pmerrill, 7 years ago (diff)
  • Added a few more icons.
  • Updated current icon set to a standard resolution multiple for better icon placement.
  • Updated ListTitles? to accommodate the icon proportion changes.
  • Updated MenuItems? to accommodate the icon proportion changes.
  • Added some testing code to dump the virtual directories to a plist file.
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@implementation SapphireMovieDirectory
13- (id)initWithParent:(SapphireVirtualDirectory *)myParent path:(NSString *)myPath
14{
15        self = [super initWithParent:myParent path:myPath];
16        if(self == nil)
17                return nil;
18       
19        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(fileAdded:) name:META_DATA_FILE_ADDED_NOTIFICATION object:nil];
20        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(fileRemoved:) name:META_DATA_FILE_REMOVED_NOTIFICATION object:nil];
21        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(fileInfoHasChanged:) name:META_DATA_FILE_INFO_HAS_CHANGED_NOTIFICATION object:nil];
22        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(fileInfoWillChanged:) name:META_DATA_FILE_INFO_WILL_CHANGE_NOTIFICATION object:nil];
23       
24        return self;
25}
26
27- (id)initWithCollection:(SapphireMetaDataCollection *)myCollection
28{
29        self = [self initWithParent:nil path:@"@MOVIES"];
30        if(self == nil)
31                return nil;
32       
33        collection = myCollection;
34       
35        return self;
36}
37
38- (void)writeMetaData
39{
40        [collection writeMetaData];
41}
42
43- (void)fileAdded:(NSNotification *)notification
44{
45        SapphireFileMetaData *file = [notification object];
46        [self processFile:file];
47}
48
49- (void)fileRemoved:(NSNotification *)notification
50{
51        SapphireFileMetaData *file = [notification object];
52        [self removeFile:file];
53}
54
55- (void)fileInfoHasChanged:(NSNotification *)notification
56{
57        NSDictionary *info = [notification userInfo];
58        if(![[info objectForKey:META_DATA_FILE_INFO_KIND] isEqualToString:META_IMDB_IMPORT_KEY])
59                return;
60        SapphireFileMetaData *file = [notification object];
61        [self processFile:file];
62}
63
64- (void)fileInfoWillChanged:(NSNotification *)notification
65{
66        NSDictionary *info = [notification userInfo];
67        if(![[info objectForKey:META_DATA_FILE_INFO_KIND] isEqualToString:META_IMDB_IMPORT_KEY])
68                return;
69        SapphireFileMetaData *file = [notification object];
70        [self removeFile:file];
71}
72
73- (void)reloadDirectoryContents
74{
75        [super reloadDirectoryContents];
76        NSMutableDictionary *mutDict = [[NSMutableDictionary alloc] init];
77        NSEnumerator *keyEnum = [directory keyEnumerator];
78        NSString *key = nil;
79        while((key = [keyEnum nextObject]) != nil)
80        {
81                SapphireMovieCategoryDirectory *dir = [directory objectForKey:key];
82                if(![dir isDisplayEmpty])
83                        [mutDict setObject:dir forKey:key];
84        }
85        [directories addObjectsFromArray:[mutDict allKeys]];
86        [directories sortUsingSelector:@selector(directoryNameCompare:)];
87        [cachedMetaDirs addEntriesFromDictionary:mutDict];
88        [metaDirs addEntriesFromDictionary:mutDict];
89        [mutDict release];
90        [(SapphireVirtualDirectory *)parent childDisplayChanged];
91}
92
93- (void)processFile:(SapphireFileMetaData *)file
94{
95        NSArray * genres=[file movieGenres];
96        NSEnumerator *genresEnum = [genres objectEnumerator];
97        NSString *genre = nil;
98        SapphireMovieCategoryDirectory *allCategory=[directory objectForKey:@"All Movies"];
99        if(allCategory==nil)
100        {
101                allCategory=[[SapphireMovieCategoryDirectory alloc] initWithParent:self path:[[self path] stringByAppendingString:@"/All Movies/"]];
102                [directory setObject:allCategory forKey:@"All Movies"];
103                [allCategory release];
104        }
105        [allCategory processFile:file];
106                                         
107        while((genre = [genresEnum nextObject]) != nil)
108        {
109                BOOL added=NO ;
110                SapphireMovieCategoryDirectory *genreInfo=[directory objectForKey:genre];
111                if(genreInfo==nil)
112                {
113                        /* Testing path structure */
114//                      NSString * aPath=@"/Test/1/2/3/";
115//                      genreInfo=[[SapphireMovieCategoryDirectory alloc] initWithParent:self path:aPath];
116                        genreInfo=[[SapphireMovieCategoryDirectory alloc] initWithParent:self path:[[self path] stringByAppendingPathComponent:[NSString stringWithFormat:@"/MOVIES/By Genre/%@",genre]]];
117                        [directory setObject:genreInfo forKey:genre];
118                        [genreInfo release];
119                        added=YES;
120                }
121                [genreInfo processFile:file];
122                if(added==YES)
123                {
124                        if([genreInfo isEmpty])
125                                [directory removeObjectForKey:genre];
126                        [self writeToFile:[NSHomeDirectory() stringByAppendingPathComponent:@"Library/Application Support/Sapphire/virtualMovieDir.plist"]];
127                }
128        }
129        [self setReloadTimer];
130}
131
132- (void)removeFile:(SapphireFileMetaData *)file
133{
134        NSArray * genres=[file movieGenres];
135        NSEnumerator *genresEnum = [genres objectEnumerator];
136        NSString *genre = nil;
137        while((genre = [genresEnum nextObject]) != nil)
138        {
139                SapphireMovieCategoryDirectory *genreInfo = [directory objectForKey:genre];
140                if(genreInfo != nil)
141                {
142                        [genreInfo removeFile:file];
143                        if([genreInfo isEmpty])
144                                [directory removeObjectForKey:genre];
145                }
146        }
147        [self setReloadTimer];
148}
149@end
150
151
152
153@implementation SapphireMovieCategoryDirectory
154- (void)reloadDirectoryContents
155{
156        [super reloadDirectoryContents];
157        NSFileManager *fm = [NSFileManager defaultManager];
158        NSMutableDictionary *mutDict = [[NSMutableDictionary alloc] init];
159        NSEnumerator *keyEnum = [directory keyEnumerator];
160        NSString *key = nil;
161        while((key = [keyEnum nextObject]) != nil)
162        {
163                SapphireFileMetaData *file = [directory objectForKey:key];
164                if([fm fileExistsAtPath:[file path]])
165                {
166                        if([file fileClass]==FILE_CLASS_MOVIE)
167                        {
168                                NSString * title=[file movieTitle];
169                                [mutDict setObject:file forKey:title];
170                        }
171                        else
172                                continue;
173                }
174        }
175        [files addObjectsFromArray:[mutDict allKeys]];
176        [files sortUsingSelector:@selector(directoryNameCompare:)];
177        [cachedMetaFiles addEntriesFromDictionary:mutDict];
178        [metaFiles addEntriesFromDictionary:mutDict];
179        [mutDict release];
180        [(SapphireVirtualDirectory *)parent childDisplayChanged];
181}
182
183- (void)processFile:(SapphireFileMetaData *)file
184{
185        [directory setObject:file forKey:[file path]];
186        [self setReloadTimer];
187}
188
189- (void)removeFile:(SapphireFileMetaData *)file
190{
191        [directory removeObjectForKey:[file path]];
192        [self setReloadTimer];
193}
194@end
Note: See TracBrowser for help on using the repository browser.