source: branches/CoreData/SapphireFrappliance/MetaData/Support/SapphireMetaDataSupport.m @ 570

Revision 570, 9.1 KB checked in by gbooker, 6 years ago (diff)

Upgrading metadata isn't done when finder starts anymore

Line 
1/*
2 * SapphireMetaDataSupport.m
3 * Sapphire
4 *
5 * Created by Graham Booker on Apr. 16, 2008.
6 * Copyright 2008 Sapphire Development Team and/or www.nanopi.net
7 * All rights reserved.
8 *
9 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU
10 * General Public License as published by the Free Software Foundation; either version 3 of the License,
11 * or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
14 * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
15 * Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along with this program; if not,
18 * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19 */
20
21#import "SapphireMetaDataSupport.h"
22#import "SapphireDirectoryMetaData.h"
23#import "SapphireFileMetaData.h"
24#import "SapphireJoinedFile.h"
25#import "SapphireCollectionDirectory.h"
26#import "SapphireMovie.h"
27#import "SapphireCast.h"
28#import "SapphireMovieCategories.h"
29#import "SapphireMovieTranslation.h"
30#import "SapphireMoviePoster.h"
31#import "SapphireTVTranslation.h"
32#import "SapphireTVShow.h"
33#import "SapphireMetaDataUpgrading.h"
34
35#define META_VERSION_KEY                        @"Version"
36
37/* Movie Translations */
38#define MOVIE_TRAN_VERSION_KEY                                  @"Version"
39#define MOVIE_TRAN_CURRENT_VERSION                              2
40/* Translation Keys */
41#define MOVIE_TRAN_TRANSLATIONS_KEY                             @"Translations"
42#define MOVIE_TRAN_IMDB_LINK_KEY                                @"IMDB Link"
43#define MOVIE_TRAN_IMP_LINK_KEY                                 @"IMP Link"
44#define MOVIE_TRAN_IMP_POSTERS_KEY                              @"IMP Posters"
45#define MOVIE_TRAN_SELECTED_POSTER_KEY                  @"Selected Poster"
46#define MOVIE_TRAN_AUTO_SELECT_POSTER_KEY               @"Default Poster"
47
48static NSSet *coverArtExtentions = nil;
49
50NSString *searchCoverArtExtForPath(NSString *path)
51{
52        NSFileManager *fm = [NSFileManager defaultManager];
53        NSString *directory = [path stringByDeletingLastPathComponent];
54        NSArray *files = [fm directoryContentsAtPath:directory];
55        NSString *lastComp = [path lastPathComponent];
56        /*Search all files*/
57        NSEnumerator *fileEnum = [files objectEnumerator];
58        NSString *file = nil;
59        while((file = [fileEnum nextObject]) != nil)
60        {
61                NSString *ext = [file pathExtension];
62                if([ext length] &&
63                   [coverArtExtentions containsObject:ext] &&
64                   [lastComp isEqualToString:[file stringByDeletingPathExtension]])
65                        return [directory stringByAppendingPathComponent:file];
66        }
67        /*Didn't find one*/
68        return nil;
69}
70
71@implementation SapphireMetaDataSupport
72
73+ (void)load
74{
75        coverArtExtentions = [[NSSet alloc] initWithObjects:
76                                                  @"jpg",
77                                                  @"jpeg",
78                                                  @"tif",
79                                                  @"tiff",
80                                                  @"png",
81                                                  @"gif",
82                                                  nil];
83}
84
85+ (SapphireMetaDataSupport *)sharedInstance
86{
87        static SapphireMetaDataSupport *shared = nil;
88       
89        if(shared == nil)
90                shared = [[SapphireMetaDataSupport alloc] init];
91       
92        return shared;
93}
94
95- (void)realWriteMetaData:(NSTimer *)timer
96{
97        writeTimer = nil;
98        NSManagedObjectContext *context = nil;
99        if([timer isKindOfClass:[NSManagedObjectContext class]])
100                context = (NSManagedObjectContext *)timer;
101        else
102                context = [timer userInfo];
103        NSError *error = nil;
104        [context save:&error];
105}
106
107- (void)save:(NSManagedObjectContext *)context;
108{
109/*      [writeTimer invalidate];
110        writeTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(realWriteMetaData:) userInfo:context repeats:NO];*/
111        [self realWriteMetaData:(NSTimer *)context];
112}
113
114+ (void)save:(NSManagedObjectContext *)context
115{
116        if(context == nil)
117                return;
118       
119        [[SapphireMetaDataSupport sharedInstance] save:context];
120}
121
122+ (void)importPlist:(NSString *)configDir intoContext:(NSManagedObjectContext *)context withDisplay:(SapphireMetaDataUpgrading *)display
123{
124        NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:[configDir stringByAppendingPathComponent:@"metaData.plist"]];
125        NSMutableDictionary *defer = [NSMutableDictionary dictionaryWithObjectsAndKeys:
126                                                                  [NSMutableDictionary dictionary], @"defer",
127                                                                  nil];
128        int version = [[dict objectForKey:META_VERSION_KEY] intValue];
129        SapphireDirectoryMetaData *newDir = nil;
130        if(version > 2)
131        {
132                NSDictionary *slash = [dict objectForKey:@"/"];
133                newDir = [SapphireDirectoryMetaData createDirectoryWithPath:@"/" parent:nil inContext:context];
134                [newDir insertDictionary:slash withDefer:defer andDisplay:display];
135        }
136        else
137        {
138                newDir = [SapphireDirectoryMetaData createDirectoryWithPath:[NSHomeDirectory() stringByAppendingPathComponent:@"Movies"] parent:nil inContext:context];
139                [newDir insertDictionary:dict withDefer:defer andDisplay:display];
140        }
141//      [display setCurrentFile:BRLocalizedString(@"Upgrading Joined Files", @"")];
142        NSDictionary *joinDict = [defer objectForKey:@"Join"];
143        if(joinDict != nil)
144        {
145                NSEnumerator *joinEunm = [joinDict keyEnumerator];
146                NSString *joinedPath;
147                while((joinedPath = [joinEunm nextObject]) != nil)
148                {
149                        SapphireJoinedFile *joinedFile = [SapphireJoinedFile joinedFileForPath:joinedPath inContext:context];
150                        NSArray *joinArray = [joinDict objectForKey:joinedPath];
151                        NSEnumerator *joinedEnum = [joinArray objectEnumerator];
152                        SapphireFileMetaData *joinFile;
153                        while((joinFile = [joinedEnum nextObject]) != nil)
154                                joinFile.joinedToFile = joinedFile;
155                }                       
156        }
157       
158//      [display setCurrentFile:BRLocalizedString(@"Upgrading Collection Prefs", @"")];
159        NSDictionary *options = [dict objectForKey:@"Options"];
160        NSMutableSet *collections = [NSMutableSet set];
161        NSArray *custom = [options objectForKey:@"Directories"];
162        if([custom count])
163                [collections unionSet:[NSSet setWithArray:custom]];
164        NSDictionary *hidden = [options objectForKey:@"Hide"];
165        NSArray *keyArray = [hidden allKeys];
166        if([keyArray count])
167                [collections unionSet:[NSSet setWithArray:keyArray]];
168        NSDictionary *skipped = [options objectForKey:@"Skip"];
169        keyArray = [skipped allKeys];
170        if([keyArray count])
171                [collections unionSet:[NSSet setWithArray:keyArray]];
172       
173        NSEnumerator *collectionEnum = [collections objectEnumerator];
174        NSString *collectionPath;
175        while((collectionPath = [collectionEnum nextObject]) != nil)
176        {
177                [SapphireCollectionDirectory collectionAtPath:collectionPath mount:([custom containsObject:collectionPath] == nil) skip:[[skipped objectForKey:collectionPath] boolValue] hidden:[[hidden objectForKey:collectionPath] boolValue] inContext:context];
178        }
179       
180//      NSArray *mountedCollections = [SapphireCollectionDirectory availableCollectionDirectoriesInContext:context];
181       
182//      [display setCurrentFile:BRLocalizedString(@"Upgrading Movie Translations", @"")];
183        NSDictionary *movieTranslations = [NSDictionary dictionaryWithContentsOfFile:[configDir stringByAppendingPathComponent:@"movieData.plist"]];
184        NSDictionary *translations = [movieTranslations objectForKey:MOVIE_TRAN_TRANSLATIONS_KEY];
185        NSEnumerator *movieEnum = [translations keyEnumerator];
186        NSString *movie = nil;
187        while((movie = [movieEnum nextObject]) != nil)
188        {
189                NSDictionary *movieDict = [translations objectForKey:movie];
190                SapphireMovieTranslation *trans = [SapphireMovieTranslation createMovieTranslationWithName:movie inContext:context];
191                trans.IMPLink = [movieDict objectForKey:MOVIE_TRAN_IMP_LINK_KEY];
192                NSString *IMDBLink = [movieDict objectForKey:MOVIE_TRAN_IMDB_LINK_KEY];
193                trans.IMDBLink = IMDBLink;
194               
195                int imdbNumber = [SapphireMovie imdbNumberFromString:IMDBLink];
196                if(imdbNumber != 0)
197                {
198                        SapphireMovie *thisMovie = [SapphireMovie movieWithIMDB:imdbNumber inContext:context];
199                        trans.movie = thisMovie;
200                }
201               
202                NSArray *posters = [movieDict objectForKey:MOVIE_TRAN_IMP_POSTERS_KEY];
203                NSSet *dupCheck = [NSSet setWithArray:posters];
204                posters = [dupCheck allObjects];
205               
206                NSString *selectedPoster = [movieDict objectForKey:MOVIE_TRAN_SELECTED_POSTER_KEY];
207                int i, count = [posters count];
208                for(i=0; i<count; i++)
209                {
210                        NSString *posterUrl = [posters objectAtIndex:i];
211                        if([posterUrl isEqualToString:selectedPoster])
212                                trans.selectedPosterIndexValue = i;
213                       
214                        [SapphireMoviePoster createPosterWithLink:posterUrl index:i translation:trans inContext:context];
215                }
216        }
217       
218//      [display setCurrentFile:BRLocalizedString(@"Upgrading TV Translations", @"")];
219        NSDictionary *tvTranslations = [NSDictionary dictionaryWithContentsOfFile:[configDir stringByAppendingPathComponent:@"tvdata.plist"]];
220        translations = [tvTranslations objectForKey:@"Translations"];
221        NSEnumerator *tvEnum = [translations keyEnumerator];
222        NSString *tvShow = nil;
223        while((tvShow = [tvEnum nextObject]) != nil)
224        {
225                NSString *showPath = [translations objectForKey:tvShow];
226                SapphireTVTranslation *trans = [SapphireTVTranslation createTVTranslationForName:tvShow withPath:showPath inContext:context];
227                SapphireTVShow *show = [SapphireTVShow showWithPath:showPath inContext:context];
228                trans.tvShow = show;
229        }
230       
231        NSError *error = nil;
232        NSManagedObject *obj;
233        NSEnumerator *objEnum = [[context registeredObjects] objectEnumerator];
234        while((obj = [objEnum nextObject]) != nil)
235        {
236                if(![obj validateForUpdate:&error])
237                        NSLog(@"%@", error);
238        }
239       
240        NSLog(@"Sapphire Import Complete");
241}
242
243+ (NSString *)collectionArtPath
244{
245        static NSString *path = nil;
246        if(path == nil)
247                path = [[NSHomeDirectory() stringByAppendingPathComponent:@"Library/Application Support/Sapphire/Collection Art"] retain];
248        return path;
249}
250
251@end
Note: See TracBrowser for help on using the repository browser.