source: trunk/SapphireTVDirectory.m @ 205

Revision 205, 4.5 KB checked in by gbooker, 7 years ago (diff)

Exclude non-existing files (for instance, on missing mounts).

Line 
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@end
64
65@implementation SapphireTVDirectory
66- (id)initWithParent:(SapphireTVBaseDirectory *)myParent path:(NSString *)myPath
67{
68        self = [super initWithParent:myParent path:myPath];
69        if(self == nil)
70                return nil;
71       
72        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(fileAdded:) name:META_DATA_FILE_ADDED_NOTIFICATION object:nil];
73       
74        return self;
75}
76
77- (void)fileAdded:(NSNotification *)notification
78{
79        SapphireFileMetaData *file = [notification object];
80        [self processFile:file];
81}
82
83- (void)reloadDirectoryContents
84{
85        [super reloadDirectoryContents];
86        [directories addObjectsFromArray:[directory allKeys]];
87        [directories sortUsingSelector:@selector(directoryNameCompare:)];
88        [cachedMetaDirs addEntriesFromDictionary:directory];
89        [metaDirs addEntriesFromDictionary:directory];
90}
91
92- (void)processFile:(SapphireFileMetaData *)file
93{
94        NSString *show = [file showName];
95        if(show == nil)
96                return;
97        SapphireShowDirectory *showInfo = [directory objectForKey:show];
98        if(showInfo == nil)
99        {
100                showInfo = [[SapphireShowDirectory alloc] initWithParent:self path:[[self path] stringByAppendingPathComponent:show]];
101                [directory setObject:showInfo forKey:show];
102                [showInfo release];
103                [self setReloadTimer];
104        }
105        [showInfo processFile:file];
106}
107@end
108
109@implementation SapphireShowDirectory
110- (void)reloadDirectoryContents
111{
112        [super reloadDirectoryContents];
113        [directories addObjectsFromArray:[directory allKeys]];
114        [directories sortUsingSelector:@selector(directoryNameCompare:)];
115        [cachedMetaDirs addEntriesFromDictionary:directory];
116        [metaDirs addEntriesFromDictionary:directory];
117}
118
119- (void)processFile:(SapphireFileMetaData *)file
120{
121        int seasonNum = [file seasonNumber];
122        if(seasonNum == 0)
123                return;
124        NSString *season = [NSString stringWithFormat:BRLocalizedString(@"Season %d", @"Season name"), seasonNum];
125        SapphireSeasonDirectory *seasonInfo = [directory objectForKey:season];
126        if(seasonInfo == nil)
127        {
128                seasonInfo = [[SapphireSeasonDirectory alloc] initWithParent:self path:[[self path] stringByAppendingPathComponent:season]];
129                [directory setObject:seasonInfo forKey:season];
130                [seasonInfo release];
131                [self setReloadTimer];
132        }
133        [seasonInfo processFile:file];
134}
135@end
136
137@implementation SapphireSeasonDirectory
138- (void)reloadDirectoryContents
139{
140        [super reloadDirectoryContents];
141        NSFileManager *fm = [NSFileManager defaultManager];
142        NSMutableDictionary *mutDict = [[NSMutableDictionary alloc] initWithDictionary:directory];
143        NSEnumerator *keyEnum = [directory keyEnumerator];
144        NSString *key = nil;
145        while((key = [keyEnum nextObject]) != nil)
146        {
147                SapphireFileMetaData *file = [directory objectForKey:key];
148                if(![fm fileExistsAtPath:[file path]])
149                        [mutDict removeObjectForKey:key];
150        }
151        [files addObjectsFromArray:[mutDict allKeys]];
152        [files sortUsingSelector:@selector(directoryNameCompare:)];
153        [cachedMetaFiles addEntriesFromDictionary:mutDict];
154        [metaFiles addEntriesFromDictionary:mutDict];
155        [mutDict release];
156}
157
158- (void)processFile:(SapphireFileMetaData *)file
159{
160        int epNum = [file episodeNumber];
161        if(epNum == 0)
162                return;
163        NSString *ep = [NSString stringWithFormat:BRLocalizedString(@"Episode %d", @"Episode name"), epNum];
164        [directory setObject:file forKey:ep];
165        [self setReloadTimer];
166}
167@end
Note: See TracBrowser for help on using the repository browser.