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