| 1 | // |
|---|
| 2 | // SapphireTVDirectory.m |
|---|
| 3 | // Sapphire |
|---|
| 4 | // |
|---|
| 5 | // Created by Graham Booker on 9/5/07. |
|---|
| 6 | // Copyright 2007 www.nanopi.net. All rights reserved. |
|---|
| 7 | // |
|---|
| 8 | |
|---|
| 9 | #import "SapphireTVDirectory.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 SapphireTVBaseDirectory |
|---|
| 17 | - (id)initWithParent:(SapphireTVBaseDirectory *)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 | - (id)init |
|---|
| 31 | { |
|---|
| 32 | return [self initWithParent:nil path:@"@TV"]; |
|---|
| 33 | } |
|---|
| 34 | |
|---|
| 35 | - (void) dealloc |
|---|
| 36 | { |
|---|
| 37 | [directory dealloc]; |
|---|
| 38 | [reloadTimer invalidate]; |
|---|
| 39 | [super dealloc]; |
|---|
| 40 | } |
|---|
| 41 | |
|---|
| 42 | - (void)reloadDirectoryContents |
|---|
| 43 | { |
|---|
| 44 | [files removeAllObjects]; |
|---|
| 45 | [directories removeAllObjects]; |
|---|
| 46 | [metaFiles removeAllObjects]; |
|---|
| 47 | [metaDirs removeAllObjects]; |
|---|
| 48 | [cachedMetaFiles removeAllObjects]; |
|---|
| 49 | [cachedMetaDirs removeAllObjects]; |
|---|
| 50 | [reloadTimer invalidate]; |
|---|
| 51 | reloadTimer = nil; |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | - (void)setReloadTimer |
|---|
| 55 | { |
|---|
| 56 | [reloadTimer invalidate]; |
|---|
| 57 | reloadTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(reloadDirectoryContents) userInfo:nil repeats:NO]; |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | - (void)processFile:(SapphireFileMetaData *)file |
|---|
| 61 | { |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | - (void)removeFile:(SapphireFileMetaData *)file |
|---|
| 65 | { |
|---|
| 66 | } |
|---|
| 67 | @end |
|---|
| 68 | |
|---|
| 69 | @implementation SapphireTVDirectory |
|---|
| 70 | - (id)initWithParent:(SapphireTVBaseDirectory *)myParent path:(NSString *)myPath |
|---|
| 71 | { |
|---|
| 72 | self = [super initWithParent:myParent path:myPath]; |
|---|
| 73 | if(self == nil) |
|---|
| 74 | return nil; |
|---|
| 75 | |
|---|
| 76 | [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(fileAdded:) name:META_DATA_FILE_ADDED_NOTIFICATION object:nil]; |
|---|
| 77 | [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(fileRemoved:) name:META_DATA_FILE_REMOVED_NOTIFICATION object:nil]; |
|---|
| 78 | [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(fileInfoHasChanged:) name:META_DATA_FILE_INFO_HAS_CHANGED_NOTIFICATION object:nil]; |
|---|
| 79 | [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(fileInfoWillChanged:) name:META_DATA_FILE_INFO_WILL_CHANGE_NOTIFICATION object:nil]; |
|---|
| 80 | |
|---|
| 81 | return self; |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | - (void)fileAdded:(NSNotification *)notification |
|---|
| 85 | { |
|---|
| 86 | SapphireFileMetaData *file = [notification object]; |
|---|
| 87 | [self processFile:file]; |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | - (void)fileRemoved:(NSNotification *)notification |
|---|
| 91 | { |
|---|
| 92 | SapphireFileMetaData *file = [notification object]; |
|---|
| 93 | [self removeFile:file]; |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | - (void)fileInfoHasChanged:(NSNotification *)notification |
|---|
| 97 | { |
|---|
| 98 | NSDictionary *info = [notification userInfo]; |
|---|
| 99 | if(![[info objectForKey:META_DATA_FILE_INFO_KIND] isEqualToString:META_TVRAGE_IMPORT_KEY]) |
|---|
| 100 | return; |
|---|
| 101 | SapphireFileMetaData *file = [notification object]; |
|---|
| 102 | [self processFile:file]; |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | - (void)fileInfoWillChanged:(NSNotification *)notification |
|---|
| 106 | { |
|---|
| 107 | NSDictionary *info = [notification userInfo]; |
|---|
| 108 | if(![[info objectForKey:META_DATA_FILE_INFO_KIND] isEqualToString:META_TVRAGE_IMPORT_KEY]) |
|---|
| 109 | return; |
|---|
| 110 | SapphireFileMetaData *file = [notification object]; |
|---|
| 111 | [self removeFile:file]; |
|---|
| 112 | } |
|---|
| 113 | |
|---|
| 114 | - (void)reloadDirectoryContents |
|---|
| 115 | { |
|---|
| 116 | [super reloadDirectoryContents]; |
|---|
| 117 | [directories addObjectsFromArray:[directory allKeys]]; |
|---|
| 118 | [directories sortUsingSelector:@selector(directoryNameCompare:)]; |
|---|
| 119 | [cachedMetaDirs addEntriesFromDictionary:directory]; |
|---|
| 120 | [metaDirs addEntriesFromDictionary:directory]; |
|---|
| 121 | } |
|---|
| 122 | |
|---|
| 123 | - (void)processFile:(SapphireFileMetaData *)file |
|---|
| 124 | { |
|---|
| 125 | NSString *show = [file showName]; |
|---|
| 126 | if(show == nil) |
|---|
| 127 | return; |
|---|
| 128 | SapphireShowDirectory *showInfo = [directory objectForKey:show]; |
|---|
| 129 | if(showInfo == nil) |
|---|
| 130 | { |
|---|
| 131 | showInfo = [[SapphireShowDirectory alloc] initWithParent:self path:[[self path] stringByAppendingPathComponent:show]]; |
|---|
| 132 | [directory setObject:showInfo forKey:show]; |
|---|
| 133 | [showInfo release]; |
|---|
| 134 | [self setReloadTimer]; |
|---|
| 135 | } |
|---|
| 136 | [showInfo processFile:file]; |
|---|
| 137 | } |
|---|
| 138 | |
|---|
| 139 | - (void)removeFile:(SapphireFileMetaData *)file |
|---|
| 140 | { |
|---|
| 141 | NSString *show = [file showName]; |
|---|
| 142 | if(show == nil) |
|---|
| 143 | return; |
|---|
| 144 | SapphireShowDirectory *showInfo = [directory objectForKey:show]; |
|---|
| 145 | if(showInfo != nil) |
|---|
| 146 | { |
|---|
| 147 | [showInfo removeFile:file]; |
|---|
| 148 | if([[showInfo directories] count] == 0) |
|---|
| 149 | { |
|---|
| 150 | [directory removeObjectForKey:show]; |
|---|
| 151 | [self setReloadTimer]; |
|---|
| 152 | } |
|---|
| 153 | } |
|---|
| 154 | } |
|---|
| 155 | @end |
|---|
| 156 | |
|---|
| 157 | @implementation SapphireShowDirectory |
|---|
| 158 | - (void)reloadDirectoryContents |
|---|
| 159 | { |
|---|
| 160 | [super reloadDirectoryContents]; |
|---|
| 161 | [directories addObjectsFromArray:[directory allKeys]]; |
|---|
| 162 | [directories sortUsingSelector:@selector(directoryNameCompare:)]; |
|---|
| 163 | [cachedMetaDirs addEntriesFromDictionary:directory]; |
|---|
| 164 | [metaDirs addEntriesFromDictionary:directory]; |
|---|
| 165 | } |
|---|
| 166 | |
|---|
| 167 | - (void)processFile:(SapphireFileMetaData *)file |
|---|
| 168 | { |
|---|
| 169 | int seasonNum = [file seasonNumber]; |
|---|
| 170 | if(seasonNum == 0) |
|---|
| 171 | return; |
|---|
| 172 | NSString *season = [NSString stringWithFormat:BRLocalizedString(@"Season %d", @"Season name"), seasonNum]; |
|---|
| 173 | SapphireSeasonDirectory *seasonInfo = [directory objectForKey:season]; |
|---|
| 174 | if(seasonInfo == nil) |
|---|
| 175 | { |
|---|
| 176 | seasonInfo = [[SapphireSeasonDirectory alloc] initWithParent:self path:[[self path] stringByAppendingPathComponent:season]]; |
|---|
| 177 | [directory setObject:seasonInfo forKey:season]; |
|---|
| 178 | [seasonInfo release]; |
|---|
| 179 | [self setReloadTimer]; |
|---|
| 180 | } |
|---|
| 181 | [seasonInfo processFile:file]; |
|---|
| 182 | } |
|---|
| 183 | |
|---|
| 184 | - (void)removeFile:(SapphireFileMetaData *)file |
|---|
| 185 | { |
|---|
| 186 | int seasonNum = [file seasonNumber]; |
|---|
| 187 | if(seasonNum == 0) |
|---|
| 188 | return; |
|---|
| 189 | NSString *season = [NSString stringWithFormat:BRLocalizedString(@"Season %d", @"Season name"), seasonNum]; |
|---|
| 190 | SapphireSeasonDirectory *seasonInfo = [directory objectForKey:season]; |
|---|
| 191 | if(seasonInfo == nil) |
|---|
| 192 | { |
|---|
| 193 | [seasonInfo removeFile:file]; |
|---|
| 194 | if([[seasonInfo directories] count] == 0) |
|---|
| 195 | { |
|---|
| 196 | [directory removeObjectForKey:season]; |
|---|
| 197 | [self setReloadTimer]; |
|---|
| 198 | } |
|---|
| 199 | } |
|---|
| 200 | } |
|---|
| 201 | @end |
|---|
| 202 | |
|---|
| 203 | @implementation SapphireSeasonDirectory |
|---|
| 204 | - (void)reloadDirectoryContents |
|---|
| 205 | { |
|---|
| 206 | [super reloadDirectoryContents]; |
|---|
| 207 | NSFileManager *fm = [NSFileManager defaultManager]; |
|---|
| 208 | NSMutableDictionary *mutDict = [[NSMutableDictionary alloc] initWithDictionary:directory]; |
|---|
| 209 | NSEnumerator *keyEnum = [directory keyEnumerator]; |
|---|
| 210 | NSString *key = nil; |
|---|
| 211 | while((key = [keyEnum nextObject]) != nil) |
|---|
| 212 | { |
|---|
| 213 | SapphireFileMetaData *file = [directory objectForKey:key]; |
|---|
| 214 | if(![fm fileExistsAtPath:[file path]]) |
|---|
| 215 | [mutDict removeObjectForKey:key]; |
|---|
| 216 | } |
|---|
| 217 | [files addObjectsFromArray:[mutDict allKeys]]; |
|---|
| 218 | [files sortUsingSelector:@selector(directoryNameCompare:)]; |
|---|
| 219 | [cachedMetaFiles addEntriesFromDictionary:mutDict]; |
|---|
| 220 | [metaFiles addEntriesFromDictionary:mutDict]; |
|---|
| 221 | [mutDict release]; |
|---|
| 222 | } |
|---|
| 223 | |
|---|
| 224 | - (void)processFile:(SapphireFileMetaData *)file |
|---|
| 225 | { |
|---|
| 226 | int epNum = [file episodeNumber]; |
|---|
| 227 | if(epNum == 0) |
|---|
| 228 | return; |
|---|
| 229 | NSString *ep = [NSString stringWithFormat:BRLocalizedString(@"Episode %d", @"Episode name"), epNum]; |
|---|
| 230 | [directory setObject:file forKey:ep]; |
|---|
| 231 | [self setReloadTimer]; |
|---|
| 232 | } |
|---|
| 233 | |
|---|
| 234 | - (void)removeFile:(SapphireFileMetaData *)file |
|---|
| 235 | { |
|---|
| 236 | int epNum = [file episodeNumber]; |
|---|
| 237 | if(epNum == 0) |
|---|
| 238 | return; |
|---|
| 239 | NSString *ep = [NSString stringWithFormat:BRLocalizedString(@"Episode %d", @"Episode name"), epNum]; |
|---|
| 240 | [directory removeObjectForKey:ep]; |
|---|
| 241 | [self setReloadTimer]; |
|---|
| 242 | } |
|---|
| 243 | @end |
|---|