| 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 | while((genre = [genresEnum nextObject]) != nil) |
|---|
| 99 | { |
|---|
| 100 | BOOL added=NO ; |
|---|
| 101 | SapphireMovieCategoryDirectory *genreInfo=[directory objectForKey:genre]; |
|---|
| 102 | if(genreInfo==nil) |
|---|
| 103 | { |
|---|
| 104 | genreInfo=[[SapphireMovieCategoryDirectory alloc] initWithParent:self path:[[self path] stringByAppendingString:[NSString stringWithFormat:@"/MOVIES/By Genre/%@",genre]]]; |
|---|
| 105 | [directory setObject:genreInfo forKey:genre]; |
|---|
| 106 | [genreInfo release]; |
|---|
| 107 | added=YES; |
|---|
| 108 | } |
|---|
| 109 | [genreInfo processFile:file]; |
|---|
| 110 | if(added==YES) |
|---|
| 111 | { |
|---|
| 112 | if([genreInfo isEmpty]) |
|---|
| 113 | [directory removeObjectForKey:genre]; |
|---|
| 114 | } |
|---|
| 115 | } |
|---|
| 116 | [self setReloadTimer]; |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 119 | - (void)removeFile:(SapphireFileMetaData *)file |
|---|
| 120 | { |
|---|
| 121 | NSArray * genres=[file movieGenres]; |
|---|
| 122 | NSEnumerator *genresEnum = [genres objectEnumerator]; |
|---|
| 123 | NSString *genre = nil; |
|---|
| 124 | while((genre = [genresEnum nextObject]) != nil) |
|---|
| 125 | { |
|---|
| 126 | SapphireMovieCategoryDirectory *genreInfo = [directory objectForKey:genre]; |
|---|
| 127 | if(genreInfo != nil) |
|---|
| 128 | { |
|---|
| 129 | [genreInfo removeFile:file]; |
|---|
| 130 | if([genreInfo isEmpty]) |
|---|
| 131 | [directory removeObjectForKey:genre]; |
|---|
| 132 | } |
|---|
| 133 | } |
|---|
| 134 | [self setReloadTimer]; |
|---|
| 135 | } |
|---|
| 136 | @end |
|---|
| 137 | |
|---|
| 138 | |
|---|
| 139 | |
|---|
| 140 | @implementation SapphireMovieCategoryDirectory |
|---|
| 141 | - (void)reloadDirectoryContents |
|---|
| 142 | { |
|---|
| 143 | [super reloadDirectoryContents]; |
|---|
| 144 | NSFileManager *fm = [NSFileManager defaultManager]; |
|---|
| 145 | NSMutableDictionary *mutDict = [[NSMutableDictionary alloc] init]; |
|---|
| 146 | NSEnumerator *keyEnum = [directory keyEnumerator]; |
|---|
| 147 | NSString *key = nil; |
|---|
| 148 | while((key = [keyEnum nextObject]) != nil) |
|---|
| 149 | { |
|---|
| 150 | SapphireFileMetaData *file = [directory objectForKey:key]; |
|---|
| 151 | if([fm fileExistsAtPath:[file path]]) |
|---|
| 152 | { |
|---|
| 153 | if([file fileClass]==FILE_CLASS_MOVIE) |
|---|
| 154 | { |
|---|
| 155 | NSString * title=[file movieTitle]; |
|---|
| 156 | [mutDict setObject:file forKey:title]; |
|---|
| 157 | } |
|---|
| 158 | else |
|---|
| 159 | continue; |
|---|
| 160 | } |
|---|
| 161 | } |
|---|
| 162 | [files addObjectsFromArray:[mutDict allKeys]]; |
|---|
| 163 | [files sortUsingSelector:@selector(directoryNameCompare:)]; |
|---|
| 164 | [cachedMetaFiles addEntriesFromDictionary:mutDict]; |
|---|
| 165 | [metaFiles addEntriesFromDictionary:mutDict]; |
|---|
| 166 | [mutDict release]; |
|---|
| 167 | [(SapphireVirtualDirectory *)parent childDisplayChanged]; |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | - (void)processFile:(SapphireFileMetaData *)file |
|---|
| 171 | { |
|---|
| 172 | [directory setObject:file forKey:[file path]]; |
|---|
| 173 | [self setReloadTimer]; |
|---|
| 174 | } |
|---|
| 175 | |
|---|
| 176 | - (void)removeFile:(SapphireFileMetaData *)file |
|---|
| 177 | { |
|---|
| 178 | [directory removeObjectForKey:[file path]]; |
|---|
| 179 | [self setReloadTimer]; |
|---|
| 180 | } |
|---|
| 181 | @end |
|---|