source: trunk/SapphireTVDirectory.m @ 208

Revision 208, 6.7 KB checked in by gbooker, 7 years ago (diff)

Fix a case where duplicate information exists but one may point to a file which doesn't exist. Prefer the existing file

RevLine 
[203]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
[204]12@interface SapphireDirectoryMetaData (privateFunctions)
13- (id)initWithDictionary:(NSDictionary *)dict parent:(SapphireMetaData *)myParent path:(NSString *)myPath;
14@end
15
[203]16@implementation SapphireTVBaseDirectory
[204]17- (id)initWithParent:(SapphireTVBaseDirectory *)myParent path:(NSString *)myPath
[203]18{
[204]19        self = [super initWithDictionary:nil parent:myParent path:myPath];
[203]20        if(self == nil)
21                return nil;
22       
23        directory = [[NSMutableDictionary alloc] init];
24        reloadTimer = nil;
[204]25        scannedDirectory = YES;
[203]26       
27        return self;
28}
29
[204]30- (id)init
31{
32        return [self initWithParent:nil path:@"@TV"];
33}
34
[203]35- (void) dealloc
36{
37        [directory dealloc];
38        [reloadTimer invalidate];
39        [super dealloc];
40}
41
42- (void)reloadDirectoryContents
43{
[204]44        [files removeAllObjects];
45        [directories removeAllObjects];
46        [metaFiles removeAllObjects];
47        [metaDirs removeAllObjects];
48        [cachedMetaFiles removeAllObjects];
49        [cachedMetaDirs removeAllObjects];
[203]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}
[206]63
64- (void)removeFile:(SapphireFileMetaData *)file
65{
66}
[203]67@end
68
69@implementation SapphireTVDirectory
[204]70- (id)initWithParent:(SapphireTVBaseDirectory *)myParent path:(NSString *)myPath
[203]71{
[204]72        self = [super initWithParent:myParent path:myPath];
[203]73        if(self == nil)
74                return nil;
75       
76        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(fileAdded:) name:META_DATA_FILE_ADDED_NOTIFICATION object:nil];
[206]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];
[203]80       
81        return self;
82}
83
84- (void)fileAdded:(NSNotification *)notification
85{
86        SapphireFileMetaData *file = [notification object];
87        [self processFile:file];
88}
89
[206]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
[203]114- (void)reloadDirectoryContents
115{
[204]116        [super reloadDirectoryContents];
[203]117        [directories addObjectsFromArray:[directory allKeys]];
118        [directories sortUsingSelector:@selector(directoryNameCompare:)];
[204]119        [cachedMetaDirs addEntriesFromDictionary:directory];
120        [metaDirs addEntriesFromDictionary:directory];
[203]121}
122
123- (void)processFile:(SapphireFileMetaData *)file
124{
125        NSString *show = [file showName];
[204]126        if(show == nil)
127                return;
[203]128        SapphireShowDirectory *showInfo = [directory objectForKey:show];
129        if(showInfo == nil)
130        {
[204]131                showInfo = [[SapphireShowDirectory alloc] initWithParent:self path:[[self path] stringByAppendingPathComponent:show]];
[203]132                [directory setObject:showInfo forKey:show];
133                [showInfo release];
134                [self setReloadTimer];
135        }
136        [showInfo processFile:file];
137}
[206]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}
[203]155@end
156
157@implementation SapphireShowDirectory
158- (void)reloadDirectoryContents
159{
[204]160        [super reloadDirectoryContents];
[203]161        [directories addObjectsFromArray:[directory allKeys]];
162        [directories sortUsingSelector:@selector(directoryNameCompare:)];
[204]163        [cachedMetaDirs addEntriesFromDictionary:directory];
164        [metaDirs addEntriesFromDictionary:directory];
[203]165}
166
167- (void)processFile:(SapphireFileMetaData *)file
168{
[204]169        int seasonNum = [file seasonNumber];
170        if(seasonNum == 0)
171                return;
172        NSString *season = [NSString stringWithFormat:BRLocalizedString(@"Season %d", @"Season name"), seasonNum];
[203]173        SapphireSeasonDirectory *seasonInfo = [directory objectForKey:season];
174        if(seasonInfo == nil)
175        {
[204]176                seasonInfo = [[SapphireSeasonDirectory alloc] initWithParent:self path:[[self path] stringByAppendingPathComponent:season]];
[203]177                [directory setObject:seasonInfo forKey:season];
178                [seasonInfo release];
179                [self setReloadTimer];
180        }
181        [seasonInfo processFile:file];
182}
[206]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}
[203]201@end
202
203@implementation SapphireSeasonDirectory
204- (void)reloadDirectoryContents
205{
206        [super reloadDirectoryContents];
[205]207        NSFileManager *fm = [NSFileManager defaultManager];
[208]208        NSMutableDictionary *mutDict = [[NSMutableDictionary alloc] init];
[205]209        NSEnumerator *keyEnum = [directory keyEnumerator];
210        NSString *key = nil;
211        while((key = [keyEnum nextObject]) != nil)
212        {
213                SapphireFileMetaData *file = [directory objectForKey:key];
[208]214                if([fm fileExistsAtPath:[file path]])
215                {
216                        NSString *ep = [NSString stringWithFormat:BRLocalizedString(@"Episode %d", @"Episode name"), [file episodeNumber]];
217                        [mutDict setObject:file forKey:ep];
218                }
[205]219        }
220        [files addObjectsFromArray:[mutDict allKeys]];
[204]221        [files sortUsingSelector:@selector(directoryNameCompare:)];
[205]222        [cachedMetaFiles addEntriesFromDictionary:mutDict];
223        [metaFiles addEntriesFromDictionary:mutDict];
224        [mutDict release];
[203]225}
226
227- (void)processFile:(SapphireFileMetaData *)file
228{
[204]229        int epNum = [file episodeNumber];
230        if(epNum == 0)
231                return;
[208]232        [directory setObject:file forKey:[file path]];
[203]233        [self setReloadTimer];
234}
[206]235
236- (void)removeFile:(SapphireFileMetaData *)file
237{
238        int epNum = [file episodeNumber];
239        if(epNum == 0)
240                return;
[208]241        [directory removeObjectForKey:[file path]];
[206]242        [self setReloadTimer];
243}
[203]244@end
Note: See TracBrowser for help on using the repository browser.