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