| 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:VIRTUAL_DIR_ROOT_KEY]; |
|---|
| 30 | if(self == nil) |
|---|
| 31 | return nil; |
|---|
| 32 | |
|---|
| 33 | collection = myCollection; |
|---|
| 34 | |
|---|
| 35 | SapphireMovieCategoryDirectory *allMovies; |
|---|
| 36 | SapphireMovieCastDirectory *cast; |
|---|
| 37 | SapphireMovieDirectorDirectory *directors; |
|---|
| 38 | SapphireMovieGenreDirectory *genres; |
|---|
| 39 | SapphireMovieOscarDirectory *oscars; |
|---|
| 40 | SapphireMovieTop250Directory *imdbtop250; |
|---|
| 41 | |
|---|
| 42 | allMovies = [[SapphireMovieCategoryDirectory alloc] initWithParent:self path:[[self path] stringByAppendingPathComponent:VIRTUAL_DIR_ALL_KEY]]; |
|---|
| 43 | cast = [[SapphireMovieCastDirectory alloc] initWithParent:self path:[[self path] stringByAppendingPathComponent:VIRTUAL_DIR_CAST_KEY]]; |
|---|
| 44 | directors = [[SapphireMovieDirectorDirectory alloc] initWithParent:self path:[[self path] stringByAppendingPathComponent:VIRTUAL_DIR_DIRECTOR_KEY]]; |
|---|
| 45 | genres = [[SapphireMovieGenreDirectory alloc] initWithParent:self path:[[self path] stringByAppendingPathComponent:VIRTUAL_DIR_GENRE_KEY]]; |
|---|
| 46 | imdbtop250 = [[SapphireMovieTop250Directory alloc] initWithParent:self path:[[self path] stringByAppendingPathComponent:VIRTUAL_DIR_TOP250_KEY]]; |
|---|
| 47 | oscars = [[SapphireMovieOscarDirectory alloc] initWithParent:self path:[[self path] stringByAppendingPathComponent:VIRTUAL_DIR_OSCAR_KEY]]; |
|---|
| 48 | |
|---|
| 49 | |
|---|
| 50 | [directory setObject:allMovies forKey:VIRTUAL_DIR_ALL_KEY]; |
|---|
| 51 | [directory setObject:cast forKey:@"By Cast"]; |
|---|
| 52 | [directory setObject:directors forKey:@"By Director"]; |
|---|
| 53 | [directory setObject:genres forKey:@"By Genre"]; |
|---|
| 54 | [directory setObject:imdbtop250 forKey:@"IMDB Top 250"]; |
|---|
| 55 | [directory setObject:oscars forKey:@"Academy Award Winning"]; |
|---|
| 56 | |
|---|
| 57 | keyOrder = [[NSArray alloc] initWithObjects: |
|---|
| 58 | VIRTUAL_DIR_ALL_KEY, |
|---|
| 59 | VIRTUAL_DIR_GENRE_KEY, |
|---|
| 60 | VIRTUAL_DIR_CAST_KEY, |
|---|
| 61 | VIRTUAL_DIR_DIRECTOR_KEY, |
|---|
| 62 | VIRTUAL_DIR_TOP250_KEY, |
|---|
| 63 | VIRTUAL_DIR_OSCAR_KEY, |
|---|
| 64 | nil]; |
|---|
| 65 | |
|---|
| 66 | return self; |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | - (void) dealloc |
|---|
| 70 | { |
|---|
| 71 | [keyOrder release]; |
|---|
| 72 | [super dealloc]; |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | - (void)writeMetaData |
|---|
| 76 | { |
|---|
| 77 | [collection writeMetaData]; |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | - (void)reloadDirectoryContents |
|---|
| 81 | { |
|---|
| 82 | [super reloadDirectoryContents]; |
|---|
| 83 | [directories setArray:keyOrder]; |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | - (void)fileAdded:(NSNotification *)notification |
|---|
| 87 | { |
|---|
| 88 | SapphireFileMetaData *file = [notification object]; |
|---|
| 89 | [self processFile:file]; |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | - (void)fileRemoved:(NSNotification *)notification |
|---|
| 93 | { |
|---|
| 94 | SapphireFileMetaData *file = [notification object]; |
|---|
| 95 | [self removeFile:file]; |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | - (void)fileInfoHasChanged:(NSNotification *)notification |
|---|
| 99 | { |
|---|
| 100 | NSDictionary *info = [notification userInfo]; |
|---|
| 101 | if(![[info objectForKey:META_DATA_FILE_INFO_KIND] isEqualToString:META_IMDB_IMPORT_KEY]) |
|---|
| 102 | return; |
|---|
| 103 | SapphireFileMetaData *file = [notification object]; |
|---|
| 104 | [self processFile:file]; |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | - (void)fileInfoWillChanged:(NSNotification *)notification |
|---|
| 108 | { |
|---|
| 109 | NSDictionary *info = [notification userInfo]; |
|---|
| 110 | if(![[info objectForKey:META_DATA_FILE_INFO_KIND] isEqualToString:META_IMDB_IMPORT_KEY]) |
|---|
| 111 | return; |
|---|
| 112 | SapphireFileMetaData *file = [notification object]; |
|---|
| 113 | [self removeFile:file]; |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | - (void)processFile:(SapphireFileMetaData *)file |
|---|
| 117 | { |
|---|
| 118 | [[directory allValues] makeObjectsPerformSelector:@selector(processFile:) withObject:file]; |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | - (void)removeFile:(SapphireFileMetaData *)file |
|---|
| 122 | { |
|---|
| 123 | [[directory allValues] makeObjectsPerformSelector:@selector(removeFile:) withObject:file]; |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | @end |
|---|
| 127 | |
|---|
| 128 | @implementation SapphireMovieCastDirectory |
|---|
| 129 | - (void)processFile:(SapphireFileMetaData *)file |
|---|
| 130 | { |
|---|
| 131 | NSArray * cast=[file movieCast]; |
|---|
| 132 | NSEnumerator *castEnum = [cast objectEnumerator]; |
|---|
| 133 | NSString *actor = nil; |
|---|
| 134 | int i=0 ; |
|---|
| 135 | while((actor = [castEnum nextObject]) != nil) |
|---|
| 136 | { |
|---|
| 137 | /* Limit the cast depth to 10 actors */ |
|---|
| 138 | if(i>10)break ; |
|---|
| 139 | BOOL added=[self addFile:file toKey:actor withChildClass:[SapphireMovieCategoryDirectory class]]; |
|---|
| 140 | if(added==YES) |
|---|
| 141 | i++; |
|---|
| 142 | } |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | - (void)removeFile:(SapphireFileMetaData *)file |
|---|
| 146 | { |
|---|
| 147 | NSArray * cast=[file movieCast]; |
|---|
| 148 | NSEnumerator *castEnum = [cast objectEnumerator]; |
|---|
| 149 | NSString *actor = nil; |
|---|
| 150 | while((actor = [castEnum nextObject]) != nil) |
|---|
| 151 | [self removeFile:file fromKey:actor]; |
|---|
| 152 | |
|---|
| 153 | } |
|---|
| 154 | @end |
|---|
| 155 | |
|---|
| 156 | @implementation SapphireMovieDirectorDirectory |
|---|
| 157 | - (void)processFile:(SapphireFileMetaData *)file |
|---|
| 158 | { |
|---|
| 159 | NSArray * directors=[file movieDirectors]; |
|---|
| 160 | NSEnumerator *directorsEnum = [directors objectEnumerator]; |
|---|
| 161 | NSString *director = nil; |
|---|
| 162 | |
|---|
| 163 | while((director = [directorsEnum nextObject]) != nil) |
|---|
| 164 | [self addFile:file toKey:director withChildClass:[SapphireMovieCategoryDirectory class]]; |
|---|
| 165 | } |
|---|
| 166 | |
|---|
| 167 | - (void)removeFile:(SapphireFileMetaData *)file |
|---|
| 168 | { |
|---|
| 169 | NSArray * directors=[file movieDirectors]; |
|---|
| 170 | NSEnumerator *directorsEnum = [directors objectEnumerator]; |
|---|
| 171 | NSString *director = nil; |
|---|
| 172 | while((director = [directorsEnum nextObject]) != nil) |
|---|
| 173 | [self removeFile:file fromKey:director]; |
|---|
| 174 | |
|---|
| 175 | } |
|---|
| 176 | @end |
|---|
| 177 | |
|---|
| 178 | @implementation SapphireMovieGenreDirectory |
|---|
| 179 | |
|---|
| 180 | - (void)processFile:(SapphireFileMetaData *)file |
|---|
| 181 | { |
|---|
| 182 | NSArray * genres=[file movieGenres]; |
|---|
| 183 | NSEnumerator *genresEnum = [genres objectEnumerator]; |
|---|
| 184 | NSString *genre = nil; |
|---|
| 185 | |
|---|
| 186 | while((genre = [genresEnum nextObject]) != nil) |
|---|
| 187 | [self addFile:file toKey:genre withChildClass:[SapphireMovieCategoryDirectory class]]; |
|---|
| 188 | } |
|---|
| 189 | |
|---|
| 190 | - (void)removeFile:(SapphireFileMetaData *)file |
|---|
| 191 | { |
|---|
| 192 | NSArray * genres=[file movieGenres]; |
|---|
| 193 | NSEnumerator *genresEnum = [genres objectEnumerator]; |
|---|
| 194 | NSString *genre = nil; |
|---|
| 195 | while((genre = [genresEnum nextObject]) != nil) |
|---|
| 196 | [self removeFile:file fromKey:genre]; |
|---|
| 197 | |
|---|
| 198 | } |
|---|
| 199 | @end |
|---|
| 200 | |
|---|
| 201 | @implementation SapphireMovieCategoryDirectory |
|---|
| 202 | - (void)reloadDirectoryContents |
|---|
| 203 | { |
|---|
| 204 | [super reloadDirectoryContents]; |
|---|
| 205 | NSFileManager *fm = [NSFileManager defaultManager]; |
|---|
| 206 | NSMutableDictionary *mutDict = [[NSMutableDictionary alloc] init]; |
|---|
| 207 | NSEnumerator *keyEnum = [directory keyEnumerator]; |
|---|
| 208 | NSString *key = nil; |
|---|
| 209 | while((key = [keyEnum nextObject]) != nil) |
|---|
| 210 | { |
|---|
| 211 | SapphireFileMetaData *file = [directory objectForKey:key]; |
|---|
| 212 | if([fm fileExistsAtPath:[file path]]) |
|---|
| 213 | { |
|---|
| 214 | if([file fileClass]==FILE_CLASS_MOVIE) |
|---|
| 215 | { |
|---|
| 216 | NSString * title=[file movieTitle]; |
|---|
| 217 | [mutDict setObject:file forKey:title]; |
|---|
| 218 | } |
|---|
| 219 | else |
|---|
| 220 | continue; |
|---|
| 221 | } |
|---|
| 222 | } |
|---|
| 223 | [files addObjectsFromArray:[mutDict allKeys]]; |
|---|
| 224 | [files sortUsingSelector:@selector(directoryNameCompare:)]; |
|---|
| 225 | [cachedMetaFiles addEntriesFromDictionary:mutDict]; |
|---|
| 226 | [metaFiles addEntriesFromDictionary:mutDict]; |
|---|
| 227 | [mutDict release]; |
|---|
| 228 | [(SapphireVirtualDirectory *)parent childDisplayChanged]; |
|---|
| 229 | } |
|---|
| 230 | |
|---|
| 231 | - (void)processFile:(SapphireFileMetaData *)file |
|---|
| 232 | { |
|---|
| 233 | [directory setObject:file forKey:[file path]]; |
|---|
| 234 | [self setReloadTimer]; |
|---|
| 235 | } |
|---|
| 236 | |
|---|
| 237 | - (void)removeFile:(SapphireFileMetaData *)file |
|---|
| 238 | { |
|---|
| 239 | [directory removeObjectForKey:[file path]]; |
|---|
| 240 | [self setReloadTimer]; |
|---|
| 241 | } |
|---|
| 242 | @end |
|---|
| 243 | |
|---|
| 244 | @implementation SapphireMovieTop250Directory |
|---|
| 245 | |
|---|
| 246 | - (void)processFile:(SapphireFileMetaData *)file |
|---|
| 247 | { |
|---|
| 248 | if([file imdbTop250]>0) |
|---|
| 249 | [directory setObject:file forKey:[file path]]; |
|---|
| 250 | [self setReloadTimer]; |
|---|
| 251 | |
|---|
| 252 | } |
|---|
| 253 | @end |
|---|
| 254 | |
|---|
| 255 | @implementation SapphireMovieOscarDirectory |
|---|
| 256 | |
|---|
| 257 | - (void)processFile:(SapphireFileMetaData *)file |
|---|
| 258 | { |
|---|
| 259 | if([file oscarsWon]>0) |
|---|
| 260 | [directory setObject:file forKey:[file path]]; |
|---|
| 261 | [self setReloadTimer]; |
|---|
| 262 | |
|---|
| 263 | } |
|---|
| 264 | @end |
|---|
| 265 | |
|---|