source: trunk/SapphireFrappliance/main_debug.m @ 1261

Revision 1261, 12.0 KB checked in by gbooker, 5 years ago (diff)

Added ability to calculate a show's sort path based on where files are currently located.

Line 
1/*
2 * main_debug.c
3 * Sapphire
4 *
5 * Created by Graham Booker on Aug. 2, 2008.
6 * Copyright 2007 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 "SapphireImportHelper.h"
22#import <CoreData/CoreData.h>
23
24#include "../SapphireCompatibilityClasses/Sapphire_Prefix.pch"
25
26//Debug Imports
27#import "SapphireCollectionDirectory.h"
28#import "SapphireDirectoryMetaData.h"
29#import "SapphireApplianceController.h"
30#import "SapphireMetaDataUpgrading.h"
31#import "SapphireMovie.h"
32#import "CoreDataSupportFunctions.h"
33#import "SapphireFileMetaData.h"
34#import "SapphireTVShowImporter.h"
35#import "SapphireMovieImporter.h"
36#import "SapphireMetaDataSupport.h"
37#import "SapphireAllImporter.h"
38#import "SapphireTVShow.h"
39#import "SapphireGenre.h"
40#import "SapphireCast.h"
41#import "SapphireDirector.h"
42#import "SapphireXMLData.h"
43#import "SapphireMovieDirectory.h"
44
45//#define TESTING_UPGRADE
46//#define LISTING_MOVIES
47//#define TESTING_XML_IMPORT
48//#define TESTING_FILE_SCANNING
49//#define TESTING_UPDATED_VALUES
50//#define TESTING_DIRECTORY_RESCAN
51//#define TESTING_AUTO_PRUNING
52//#define TESTING_MOVIE_IMPORT
53//#define TESTING_TV_SHOW_IMPORT
54//#define TESTING_MULTIPLE_AND_SINGLE_TV_SHOW_IMPORT
55//#define TESTING_MOVIE_VIRTUAL_DIRS_IN_XML
56//#define TESTING_TV_IMPORT_THROUGH_XML
57#define TESTING_AUTO_SORT_PATH
58
59void overrideApplicationSupportdir(NSString *override);
60
61@interface TestFileScanning : NSObject <SapphireMetaDataScannerDelegate>
62{
63        int i;
64        NSArray *collections;
65        NSMutableSet *skip;
66}
67- (id)initWithCollections:(NSArray *)col;
68@end
69
70@implementation TestFileScanning
71
72- (id)initWithCollections:(NSArray *)col
73{
74        self = [super init];
75       
76        collections = [col retain];
77        skip = [[NSMutableSet alloc] init];
78        i=-1;
79       
80        return self;
81}
82
83- (void) dealloc
84{
85        [collections release];
86        [skip release];
87        [super dealloc];
88}
89
90- (void)gotSubFiles:(NSArray *)subs
91{
92        i++;
93        if(i==[collections count])
94                NSLog(@"DONE!!!");
95        else
96        {
97                [[(SapphireCollectionDirectory *)[collections objectAtIndex:i] directory] getSubFileMetasWithDelegate:self skipDirectories:skip];
98        }
99}
100
101- (void)scanningDir:(NSString *)dir
102{
103}
104
105- (BOOL)getSubFilesCanceled
106{
107        return NO;
108}
109
110@end
111
112@interface SapphireMetaDataUpgrading (debug)
113- (void)doUpgrade:(id)obj;
114@end
115
116@interface SapphireMetaDataSupport (debug)
117+ (void)deletePendingObjects;
118@end
119
120static BOOL completedImports = YES;
121
122@interface TestImportManager : NSObject <SapphireImporterDelegate>
123{
124        NSMutableArray          *waitingImports;
125}
126
127- (void)importer:(id <SapphireImporter>)importer importMetaData:(SapphireFileMetaData *)metaData path:(NSString *)path;
128@end
129
130@implementation TestImportManager
131
132- (id)init
133{
134        self = [super init];
135        if (self != nil) {
136                waitingImports = [[NSMutableArray alloc] init];
137        }
138        return self;
139}
140
141- (void) dealloc
142{
143        [waitingImports release];
144        [super dealloc];
145}
146
147- (void)importer:(id <SapphireImporter>)importer importMetaData:(SapphireFileMetaData *)metaData path:(NSString *)path;
148{
149        [importer setDelegate:self];
150        ImportState state = [importer importMetaData:metaData path:path];
151        if(state == ImportStateBackground || state == ImportStateMultipleSuspend)
152        {
153                [waitingImports addObject:importer];
154                completedImports = NO;
155        }
156}
157
158- (void)backgroundImporter:(id <SapphireImporter>)importer completedImportOnPath:(NSString *)path withState:(ImportState)state
159{
160        [waitingImports removeObject:importer];
161        if(![waitingImports count])
162                completedImports = YES;
163}
164
165- (BOOL)canDisplayChooser
166{
167        return YES;
168}
169
170- (id)chooserScene
171{
172        return nil;
173}
174
175- (void)displayChooser:(BRLayerController <SapphireChooser> *)chooser forImporter:(id <SapphireImporter>)importer withContext:(id)context
176{
177        [chooser itemSelected:1];
178        [importer exhumedChooser:chooser withContext:context];
179}
180
181@end
182
183
184
185int main(int argc, char *argv[])
186{
187        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
188       
189        {
190                NSString *path = [[NSFileManager defaultManager] stringWithFileSystemRepresentation:argv[0] length:strlen(argv[0])];
191               
192                path = [[path stringByDeletingLastPathComponent] stringByAppendingPathComponent:@"Sapphire.frappliance"];
193               
194                NSBundle *bundle = [NSBundle bundleWithPath:path];
195                [bundle load];         
196        }
197       
198        overrideApplicationSupportdir([NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Frontrow"]);
199#ifdef TESTING_UPGRADE
200        {
201                SapphireMetaDataUpgrading *upgrade = [[SapphireMetaDataUpgrading alloc] init];
202                [upgrade doUpgrade:nil];
203                [upgrade release];
204        }
205#endif
206       
207        NSString *storeFile = [applicationSupportDir() stringByAppendingPathComponent:@"metaData.sapphireDataV3"];
208        BOOL exists = [[NSFileManager defaultManager] fileExistsAtPath:storeFile];
209        if(!exists)
210                return 0;
211       
212        NSManagedObjectContext *moc = [SapphireApplianceController newManagedObjectContextForFile:storeFile withOptions:nil];
213        [SapphireMetaDataSupport setMainContext:moc];
214        SapphireAllImporter *allImporter = [[SapphireAllImporter alloc] init];
215       
216        TestImportManager *importManager = [[TestImportManager alloc] init];
217       
218        //Debug code goes here:
219#ifdef LISTING_MOVIES
220        {
221                NSArray *allMovies = doFetchRequest(SapphireMovieName, moc, nil);
222                NSEnumerator *movieEnum = [allMovies objectEnumerator];
223                SapphireMovie *movie;
224                while((movie = [movieEnum nextObject]) != nil)
225                {
226                        NSLog(@"Looking at movie %@ with xml: %d", [movie title], [[movie xmlSet] count]);
227                        NSLog(@"Cast is %@", [movie valueForKeyPath:@"cast.name"]);
228                        NSLog(@"Directors is %@", [movie valueForKeyPath:@"directors.name"]);
229                        NSLog(@"Genres is %@", [movie valueForKeyPath:@"genres.name"]);
230                        NSLog(@"Plot is %@", [movie plot]);
231                }               
232        }
233#endif
234#ifdef TESTING_XML_IMPORT
235        {
236                NSString *path = @"/Users/gbooker/Movies/MovieTests/Little Eistiens: Our Big Huge Adventure (2005).avi";
237                SapphireFileMetaData *meta = [SapphireFileMetaData fileWithPath:path inContext:moc];
238                [meta clearMetaData];
239                [SapphireMetaDataSupport save:moc];
240                SapphireXMLFileDataImporter *importer = [[SapphireXMLFileDataImporter alloc] init];
241                [importManager importer:importer importMetaData:meta path:[meta path]];
242                [importer release];             
243        }
244#endif
245#ifdef TESTING_FILE_SCANNING
246        {
247                NSMutableArray *collections = [[SapphireCollectionDirectory allCollectionsInContext:moc] mutableCopy];
248                //remove / and ~/Movies
249                [collections removeObjectAtIndex:0];
250                [collections removeObjectAtIndex:0];
251                TestFileScanning *debug = [[TestFileScanning alloc] initWithCollections:collections];
252                [NSTimer scheduledTimerWithTimeInterval:0 target:debug selector:@selector(gotSubFiles:) userInfo:nil repeats:NO];
253                [debug release];
254               
255                NSRunLoop *currentRL = [NSRunLoop currentRunLoop];
256                while([currentRL runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]])
257                        ;               
258        }
259#endif
260#ifdef TESTING_UPDATED_VALUES
261        {
262                NSString *path = @"/Users/gbooker/Movies/Little Einsteins.avi";
263                SapphireFileMetaData *meta = [SapphireFileMetaData fileWithPath:path inContext:moc];
264                [meta clearMetaData];
265                [importManager importer:allImporter importMetaData:meta path:[meta path]];
266               
267                NSDictionary *changes = [SapphireMetaDataSupport changesDictionaryForContext:moc];
268                [moc reset];
269                [SapphireMetaDataSupport applyChanges:changes toContext:moc];
270                [allImporter release];
271        }
272#endif
273#ifdef TESTING_DIRECTORY_RESCAN
274        {
275                SapphireMovie *movie = [SapphireMovie movieWithTitle:@"Little Eistiens: Our Big Huge Adventure" inContext:moc];
276                SapphireFileMetaData *file = [SapphireFileMetaData fileWithPath:@"/Users/gbooker/Movies/Little Einsteins.avi" inContext:moc];
277                [moc deleteObject:file];
278                [moc processPendingChanges];
279                SapphireDirectoryMetaData *dir = [SapphireDirectoryMetaData directoryWithPath:@"/Users/gbooker/Movies" inContext:moc];
280                [dir reloadDirectoryContents];
281        }
282#endif
283#ifdef TESTING_AUTO_PRUNING
284        {
285                SapphireFileMetaData *file = [SapphireFileMetaData fileWithPath:@"/Users/gbooker/Movies/MovieTests/Little Einsteins.avi" inContext:moc];
286                SapphireXMLData *xml = [file xmlData];
287                [moc deleteObject:xml];
288                [SapphireMetaDataSupport deletePendingObjects];
289                SapphireDirectoryMetaData *dir = [SapphireDirectoryMetaData directoryWithPath:@"/Users/gbooker/Movies/MovieTests" inContext:moc];
290                [moc deleteObject:dir];
291                [SapphireMetaDataSupport deletePendingObjects];
292                dir = [SapphireDirectoryMetaData directoryWithPath:@"/Users/gbooker/Movies/TVShowsTests" inContext:moc];
293                [moc deleteObject:dir];
294                [SapphireMetaDataSupport deletePendingObjects];
295                NSArray *allMovies = doFetchRequest(SapphireMovieName, moc, nil);
296                NSArray *allShows = doFetchRequest(SapphireTVShowName, moc, nil);
297                NSArray *allGenres = doFetchRequest(SapphireGenreName, moc, nil);
298                NSArray *allCast = doFetchRequest(SapphireCastName, moc, nil);
299                NSArray *allDirectors = doFetchRequest(SapphireDirectorName, moc, nil);
300               
301                NSLog(@"Movies: %@\nShows: %@\nCast: %@\nGenres: %@\nDirectors: %@", allMovies, allShows, allCast, allGenres, allDirectors);
302        }
303#endif
304#ifdef TESTING_MOVIE_IMPORT
305        {
306                SapphireFileMetaData *file = [SapphireFileMetaData createFileWithPath:@"/Users/gbooker/Movies/MovieTests/FIFTH_ELEMENT.mov" inContext:moc];
307                SapphireMovieImporter *import = [[SapphireMovieImporter alloc] init];
308                [file setToReimportFromMaskValue:IMPORT_TYPE_MOVIE_MASK];
309                [file setToResetImportDecisions];
310                [importManager importer:import importMetaData:file path:[file path]];
311                [import release];
312        }
313#endif
314#ifdef TESTING_TV_SHOW_IMPORT
315        {
316                SapphireFileMetaData *file = [SapphireFileMetaData createFileWithPath:@"/Users/gbooker/Movies/TVShowsTests/Doctor Who (2005) S03ES1 Voyage of the Damned.avi" inContext:moc];
317                SapphireTVShowImporter *import = [[SapphireTVShowImporter alloc] init];
318                [file setToReimportFromMaskValue:IMPORT_TYPE_TVSHOW_MASK];
319                [importManager importer:import importMetaData:file path:[file path]];
320                [import release];
321        }
322#endif
323#ifdef TESTING_MULTIPLE_AND_SINGLE_TV_SHOW_IMPORT
324        {
325                SapphireFileMetaData *file = [SapphireFileMetaData createFileWithPath:@"/Users/gbooker/Movies/TVShowsTests/Stargate Atlantis S01E01-E02.avi" inContext:moc];
326                SapphireTVShowImporter *import = [[SapphireTVShowImporter alloc] init];
327                [file setToReimportFromMaskValue:IMPORT_TYPE_TVSHOW_MASK];
328                [importManager importer:import importMetaData:file path:[file path]];
329                [import release];
330               
331                import = [[SapphireTVShowImporter alloc] init];
332                file = [SapphireFileMetaData createFileWithPath:@"/Users/gbooker/Movies/TVShowsTests/Stargate Atlantis S01E02.avi" inContext:moc];
333//              [importManager importer:import importMetaData:file path:[file path]];
334                [import release];
335        }
336#endif
337#ifdef TESTING_MOVIE_VIRTUAL_DIRS_IN_XML
338        {
339                SapphireMovieDirectory *movieDir = [[SapphireMovieDirectory alloc] init];
340                [movieDir reloadDirectoryContents];
341                [movieDir reloadDirectoryContents];
342                [movieDir release];
343        }
344#endif
345#ifdef TESTING_TV_IMPORT_THROUGH_XML
346        {
347                SapphireFileMetaData *file = [SapphireFileMetaData createFileWithPath:@"/Users/gbooker/Movies/TVShowsTests/life on mars.avi" inContext:moc];
348                [importManager importer:allImporter importMetaData:file path:[file path]];
349        }
350#endif
351#ifdef TESTING_AUTO_SORT_PATH
352        {
353                NSArray *shows = doFetchRequest(SapphireTVShowName, moc, nil);
354                NSEnumerator *showEnum = [shows objectEnumerator];
355                SapphireTVShow *show;
356                while((show = [showEnum nextObject]) != nil)
357                {
358                        NSString *autoPath = [show calculateAutoSortPath];
359                        NSLog(@"Sort path for %@ is %@", [show name], autoPath);
360                }
361        }
362#endif
363       
364        [allImporter release];
365       
366        [moc release];
367        [pool drain];
368       
369        pool = [[NSAutoreleasePool alloc] init];
370        NSRunLoop *currentRL = [NSRunLoop currentRunLoop];
371        while(!completedImports && [currentRL runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]])
372                ;
373       
374        [importManager release];
375        [pool drain];
376        return 0;
377}
Note: See TracBrowser for help on using the repository browser.