MLMediaLibrary.m 38.9 KB
Newer Older
1 2 3 4 5
/*****************************************************************************
 * MLMediaLibrary.m
 * MobileMediaLibraryKit
 *****************************************************************************
 * Copyright (C) 2010 Pierre d'Herbemont
6
 * Copyright (C) 2010-2015 VLC authors and VideoLAN
7 8 9 10 11
 * $Id$
 *
 * Authors: Pierre d'Herbemont <pdherbemont # videolan.org>
 *          Felix Paul Kühne <fkuehne # videolan.org>
 *
12 13 14
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
15 16 17 18
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 20
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
21
 *
22 23 24
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25
 *****************************************************************************/
Pierre's avatar
Pierre committed
26 27

#import "MLMediaLibrary.h"
Pierre's avatar
Pierre committed
28 29
#import "MLTitleDecrapifier.h"
#import "MLMovieInfoGrabber.h"
Pierre's avatar
Pierre committed
30 31
#import "MLTVShowInfoGrabber.h"
#import "MLTVShowEpisodesInfoGrabber.h"
Pierre's avatar
Pierre committed
32 33 34 35
#import "MLFile.h"
#import "MLLabel.h"
#import "MLShowEpisode.h"
#import "MLShow.h"
36 37 38
#import "MLThumbnailerQueue.h"
#import "MLAlbumTrack.h"
#import "MLAlbum.h"
39
#import "MLFileParserQueue.h"
40
#import "MLCrashPreventer.h"
Pierre's avatar
Pierre committed
41

42 43
@interface MLMediaLibrary ()
{
44 45 46 47 48
    NSManagedObjectContext *_managedObjectContext;
    NSManagedObjectModel   *_managedObjectModel;

    BOOL _allowNetworkAccess;

49 50
    NSString *_thumbnailFolderPath;
    NSString *_databaseFolderPath;
51
    NSString *_documentFolderPath;
52
    NSString *_libraryBasePath;
53 54 55
}
@end

Pierre's avatar
Pierre committed
56
#define DEBUG 1
57 58
// To debug
#define DELETE_LIBRARY_ON_EACH_LAUNCH 0
Pierre's avatar
Pierre committed
59

Pierre's avatar
Pierre committed
60 61
// Pref key
static NSString *kLastTVDBUpdateServerTime = @"MLLastTVDBUpdateServerTime";
62
static NSString *kUpdatedToTheGreatSharkHuntDatabaseFormat = @"upgradedToDatabaseFormat 2.3";
63
static NSString *kDecrapifyTitles = @"MLDecrapifyTitles";
Pierre's avatar
Pierre committed
64

Pierre's avatar
Pierre committed
65
#if HAVE_BLOCK
Pierre's avatar
Pierre committed
66
@interface MLMediaLibrary ()
Pierre's avatar
Pierre committed
67 68 69
#else
@interface MLMediaLibrary () <MLMovieInfoGrabberDelegate, MLTVShowEpisodesInfoGrabberDelegate, MLTVShowInfoGrabberDelegate>
#endif
Pierre's avatar
Pierre committed
70
- (NSManagedObjectContext *)managedObjectContext;
Pierre's avatar
Pierre committed
71
- (NSString *)databaseFolderPath;
Pierre's avatar
Pierre committed
72 73
@end

Pierre's avatar
Pierre committed
74
@implementation MLMediaLibrary
75 76
+ (void)initialize
{
77
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
78
    [defaults registerDefaults:@{kUpdatedToTheGreatSharkHuntDatabaseFormat : @NO, kDecrapifyTitles : @YES}];
79 80
}

Pierre's avatar
Pierre committed
81 82 83
+ (id)sharedMediaLibrary
{
    static id sharedMediaLibrary = nil;
Pierre's avatar
Pierre committed
84
    if (!sharedMediaLibrary) {
Pierre's avatar
Pierre committed
85
        sharedMediaLibrary = [[[self class] alloc] init];
86
        APLog(@"Initializing db in %@", [sharedMediaLibrary databaseFolderPath]);
87 88 89 90

        // Also force to init the crash preventer
        // Because it will correctly set up the parser and thumbnail queue
        [MLCrashPreventer sharedPreventer];
Pierre's avatar
Pierre committed
91
    }
Pierre's avatar
Pierre committed
92 93 94
    return sharedMediaLibrary;
}

95 96 97 98 99 100
- (void)dealloc
{
    if (_managedObjectContext)
        [_managedObjectContext removeObserver:self forKeyPath:@"hasChanges"];
}

Pierre's avatar
Pierre committed
101 102 103 104
- (NSFetchRequest *)fetchRequestForEntity:(NSString *)entity
{
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    NSManagedObjectContext *moc = [self managedObjectContext];
105 106 107
    if (!moc || moc.persistentStoreCoordinator == nil)
        return nil;

Pierre's avatar
Pierre committed
108
    NSEntityDescription *entityDescription = [NSEntityDescription entityForName:entity inManagedObjectContext:moc];
Pierre's avatar
Pierre committed
109
    NSAssert(entityDescription, @"No entity");
Pierre's avatar
Pierre committed
110
    [request setEntity:entityDescription];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
111
    return request;
Pierre's avatar
Pierre committed
112 113 114 115 116
}

- (id)createObjectForEntity:(NSString *)entity
{
    NSManagedObjectContext *moc = [self managedObjectContext];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
117
    if (!moc || moc.persistentStoreCoordinator == nil)
118 119
        return nil;

Pierre's avatar
Pierre committed
120 121 122
    return [NSEntityDescription insertNewObjectForEntityForName:entity inManagedObjectContext:moc];
}

123 124
- (void)removeObject:(NSManagedObject *)object
{
125 126 127 128
    NSManagedObjectContext *moc = [self managedObjectContext];

    if (moc)
        [[self managedObjectContext] deleteObject:object];
129 130
}

Pierre's avatar
Pierre committed
131 132 133 134 135 136
#pragma mark -
#pragma mark Media Library
- (NSManagedObjectModel *)managedObjectModel
{
    if (_managedObjectModel)
        return _managedObjectModel;
137

138
    NSString *path = [[NSBundle mainBundle] pathForResource:@"MediaLibrary" ofType:@"momd"];
139 140 141
    NSURL *momURL = [NSURL fileURLWithPath:path];
    _managedObjectModel = [[NSManagedObjectModel alloc] initWithContentsOfURL:momURL];

Pierre's avatar
Pierre committed
142 143 144
    return _managedObjectModel;
}

145
- (NSString *)libraryBasePath
Pierre's avatar
Pierre committed
146
{
147 148 149 150
    if (_libraryBasePath.length == 0) {
        int directory = NSLibraryDirectory;
        NSArray *paths = NSSearchPathForDirectoriesInDomains(directory, NSUserDomainMask, YES);
        NSString *directoryPath = paths.firstObject;
151
#if DELETE_LIBRARY_ON_EACH_LAUNCH
152
        [[NSFileManager defaultManager] removeItemAtPath:directoryPath error:nil];
153
#endif
154 155 156 157 158
        _libraryBasePath = directoryPath;
    }
    return _libraryBasePath;
}

159 160 161 162 163 164 165 166
- (void)setLibraryBasePath:(NSString *)libraryBasePath
{
    _libraryBasePath = [libraryBasePath copy];
    _databaseFolderPath = nil;
    _thumbnailFolderPath = nil;
    _persistentStoreURL = nil;
}

167 168 169 170 171
- (NSString *)databaseFolderPath
{
    if (_databaseFolderPath.length == 0) {
        _databaseFolderPath = self.libraryBasePath;
    }
172
    return _databaseFolderPath;
Pierre's avatar
Pierre committed
173 174
}

Pierre's avatar
Pierre committed
175 176
- (NSString *)thumbnailFolderPath
{
177 178
    if (_thumbnailFolderPath.length == 0) {
        _thumbnailFolderPath = [self.libraryBasePath stringByAppendingPathComponent:@"Thumbnails"];
179 180
    }
    return _thumbnailFolderPath;
Pierre's avatar
Pierre committed
181 182
}

183 184 185 186 187 188 189 190
- (NSString *)documentFolderPath
{
    if (_documentFolderPath) {
        if (_documentFolderPath.length > 0)
            return _documentFolderPath;
    }
    int directory = NSDocumentDirectory;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(directory, NSUserDomainMask, YES);
191
    _documentFolderPath = [NSString stringWithFormat:@"file://%@", paths[0]];
192 193 194
    return _documentFolderPath;
}

195 196 197 198 199 200 201 202
- (NSURL *)persistentStoreURL
{
    if (_persistentStoreURL == nil) {
        NSString *databaseFolderPath = [self databaseFolderPath];
        NSString *path = [databaseFolderPath stringByAppendingPathComponent: @"MediaLibrary.sqlite"];
        _persistentStoreURL = [NSURL fileURLWithPath:path];
    }
    return _persistentStoreURL;
203 204
}

205 206
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
207

Pierre's avatar
Pierre committed
208 209
    NSPersistentStoreCoordinator *coordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];

210
    NSNumber *yes = @YES;
211
    NSDictionary *options = @{NSMigratePersistentStoresAutomaticallyOption : yes,
212
                              NSInferMappingModelAutomaticallyOption : yes};
213 214 215 216 217
    if (self.additionalPersitentStoreOptions.count > 0) {
        NSMutableDictionary *mutableOptions = options.mutableCopy;
        [mutableOptions addEntriesFromDictionary:self.additionalPersitentStoreOptions];
        options = mutableOptions;
    }
Pierre's avatar
Pierre committed
218

219 220 221 222 223 224 225
    if ([[self.additionalPersitentStoreOptions objectForKey:NSReadOnlyPersistentStoreOption] boolValue] == YES) {
        if (![[NSFileManager defaultManager] fileExistsAtPath:self.persistentStoreURL.path]) {
            APLog(@"no library was found in read-only mode, hence no functionality will be available in this session");
            return nil;
        }
    }

Pierre's avatar
Pierre committed
226
    NSError *error;
227
    NSPersistentStore *persistentStore = [coordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:self.persistentStoreURL options:options error:&error];
Pierre's avatar
Pierre committed
228 229

    if (!persistentStore) {
Pierre's avatar
Pierre committed
230
#if! TARGET_OS_IPHONE
Pierre's avatar
Pierre committed
231 232 233 234
        // FIXME: Deal with versioning
        NSInteger ret = NSRunAlertPanel(@"Error", @"The Media Library you have on your disk is not compatible with the one Lunettes can read. Do you want to create a new one?", @"No", @"Yes", nil);
        if (ret == NSOKButton)
            [NSApp terminate:nil];
235
        [[NSFileManager defaultManager] removeItemAtPath:self.persistentStoreURL.path error:nil];
Pierre's avatar
Pierre committed
236
#else
237
        [[NSFileManager defaultManager] removeItemAtPath:self.persistentStoreURL.path error:nil];
Pierre's avatar
Pierre committed
238
#endif
239
        persistentStore = [coordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:self.persistentStoreURL options:options error:&error];
Pierre's avatar
Pierre committed
240 241 242 243 244 245 246 247 248 249
        if (!persistentStore) {
#if! TARGET_OS_IPHONE
            NSRunInformationalAlertPanel(@"Corrupted Media Library", @"There is nothing we can apparently do about it...", @"OK", nil, nil);
#else
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Corrupted Media Library" message:@"There is nothing we can apparently do about it..." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
            [alert show];
#endif
            // Probably assert instead.
            return nil;
        }
Pierre's avatar
Pierre committed
250
    }
251 252 253 254 255 256 257
    return coordinator;
}

- (NSManagedObjectContext *)managedObjectContext
{
    if (_managedObjectContext)
        return _managedObjectContext;
Pierre's avatar
Pierre committed
258 259

    _managedObjectContext = [[NSManagedObjectContext alloc] init];
260
    [_managedObjectContext setPersistentStoreCoordinator:self.persistentStoreCoordinator];
261 262
    if (_managedObjectContext.persistentStoreCoordinator == nil)
        return nil;
Pierre's avatar
Pierre committed
263 264 265 266 267 268 269 270
    [_managedObjectContext setUndoManager:nil];
    [_managedObjectContext addObserver:self forKeyPath:@"hasChanges" options:NSKeyValueObservingOptionInitial context:nil];
    return _managedObjectContext;
}

- (void)savePendingChanges
{
    [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(savePendingChanges) object:nil];
Pierre's avatar
Pierre committed
271
    NSError *error = nil;
272 273 274 275
    NSManagedObjectContext *moc = [self managedObjectContext];
    if (!moc)
        return;

Pierre's avatar
Pierre committed
276 277
    BOOL success = [[self managedObjectContext] save:&error];
    NSAssert1(success, @"Can't save: %@", error);
Pierre's avatar
Pierre committed
278
#if !TARGET_OS_IPHONE && MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_5
Pierre's avatar
Pierre committed
279 280 281 282 283 284
    NSProcessInfo *process = [NSProcessInfo processInfo];
    if ([process respondsToSelector:@selector(enableSuddenTermination)])
        [process enableSuddenTermination];
#endif
}

285 286 287
- (void)save
{
    NSError *error = nil;
288 289 290 291 292
    NSManagedObjectContext *moc = [self managedObjectContext];
    if (!moc)
        return;

    BOOL success = [moc save:&error];
293 294 295
    NSAssert1(success, @"Can't save: %@", error);
}

Pierre's avatar
Pierre committed
296
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
Pierre's avatar
Pierre committed
297 298
{
    if ([keyPath isEqualToString:@"hasChanges"] && object == _managedObjectContext) {
Pierre's avatar
Pierre committed
299
#if !TARGET_OS_IPHONE && MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_5
Pierre's avatar
Pierre committed
300 301 302 303 304
        NSProcessInfo *process = [NSProcessInfo processInfo];
        if ([process respondsToSelector:@selector(disableSuddenTermination)])
            [process disableSuddenTermination];
#endif

Pierre's avatar
Pierre committed
305 306 307 308
        if ([[self managedObjectContext] hasChanges]) {
            [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(savePendingChanges) object:nil];
            [self performSelector:@selector(savePendingChanges) withObject:nil afterDelay:1.];
        }
Pierre's avatar
Pierre committed
309 310 311 312 313
        return;
    }
    [super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}

Pierre's avatar
Pierre committed
314 315
#pragma mark -
#pragma mark No meta data fallbacks
Pierre's avatar
Pierre committed
316

Pierre's avatar
Pierre committed
317
- (void)computeThumbnailForFile:(MLFile *)file
Pierre's avatar
Pierre committed
318
{
Pierre's avatar
Pierre committed
319
    if (!file.computedThumbnail) {
320
        APLog(@"Computing thumbnail for %@", file.title);
Pierre's avatar
Pierre committed
321 322
        [[MLThumbnailerQueue sharedThumbnailerQueue] addFile:file];
    }
Pierre's avatar
Pierre committed
323
}
Pierre's avatar
Pierre committed
324
- (void)errorWhenFetchingMetaDataForFile:(MLFile *)file
Pierre's avatar
Pierre committed
325
{
326
    APLog(@"Error when fetching for '%@'", file.title);
Pierre's avatar
Pierre committed
327

Pierre's avatar
Pierre committed
328 329
    [self computeThumbnailForFile:file];
}
Pierre's avatar
Pierre committed
330

Pierre's avatar
Pierre committed
331 332 333 334 335 336 337
- (void)errorWhenFetchingMetaDataForShow:(MLShow *)show
{
    for (MLShowEpisode *episode in show.episodes) {
        for (MLFile *file in episode.files)
            [self errorWhenFetchingMetaDataForFile:file];
    }
}
Pierre's avatar
Pierre committed
338

Pierre's avatar
Pierre committed
339 340
- (void)noMetaDataInRemoteDBForFile:(MLFile *)file
{
341
    file.noOnlineMetaData = @YES;
Pierre's avatar
Pierre committed
342
    [self computeThumbnailForFile:file];
Pierre's avatar
Pierre committed
343 344
}

Pierre's avatar
Pierre committed
345
- (void)noMetaDataInRemoteDBForShow:(MLShow *)show
Pierre's avatar
Pierre committed
346
{
Pierre's avatar
Pierre committed
347 348 349
    for (MLShowEpisode *episode in show.episodes) {
        for (MLFile *file in episode.files)
            [self noMetaDataInRemoteDBForFile:file];
Pierre's avatar
Pierre committed
350 351 352
    }
}

Pierre's avatar
Pierre committed
353 354 355 356
#pragma mark -
#pragma mark Getter

- (void)addNewLabelWithName:(NSString *)name
Pierre's avatar
Pierre committed
357
{
Pierre's avatar
Pierre committed
358 359
    MLLabel *label = [self createObjectForEntity:@"Label"];
    label.name = name;
Pierre's avatar
Pierre committed
360 361
}

Pierre's avatar
Pierre committed
362 363 364 365 366 367 368
/**
 * TV MLShow Episodes
 */

#pragma mark -
#pragma mark Online meta data grabbing

Pierre's avatar
Pierre committed
369 370 371
#if !HAVE_BLOCK
- (void)tvShowEpisodesInfoGrabberDidFinishGrabbing:(MLTVShowEpisodesInfoGrabber *)grabber
{
Pierre's avatar
Pierre committed
372
    MLShow *show = grabber.userData;
Pierre's avatar
Pierre committed
373 374

    NSArray *results = grabber.episodesResults;
375
    [show setValue:(grabber.results)[@"serieArtworkURL"] forKey:@"artworkURL"];
Pierre's avatar
Pierre committed
376
    for (id result in results) {
377
        if ([result[@"serie"] boolValue]) {
Pierre's avatar
Pierre committed
378 379
            continue;
        }
380 381 382 383 384
        MLShowEpisode *showEpisode = [MLShowEpisode episodeWithShow:show episodeNumber:result[@"episodeNumber"] seasonNumber:result[@"seasonNumber"] createIfNeeded:YES];
        showEpisode.name = result[@"title"];
        showEpisode.theTVDBID = result[@"id"];
        showEpisode.shortSummary = result[@"shortSummary"];
        showEpisode.artworkURL = result[@"artworkURL"];
Pierre's avatar
Pierre committed
385 386 387 388 389
        if (!showEpisode.artworkURL) {
            for (MLFile *file in showEpisode.files)
                [self computeThumbnailForFile:file];
        }

Pierre's avatar
Pierre committed
390 391 392 393 394
        showEpisode.lastSyncDate = [MLTVShowInfoGrabber serverTime];
    }
    show.lastSyncDate = [MLTVShowInfoGrabber serverTime];
}

Pierre's avatar
Pierre committed
395 396 397 398 399 400
- (void)tvShowEpisodesInfoGrabber:(MLTVShowEpisodesInfoGrabber *)grabber didFailWithError:(NSError *)error
{
    MLShow *show = grabber.userData;
    [self errorWhenFetchingMetaDataForShow:show];
}

Pierre's avatar
Pierre committed
401 402
- (void)tvShowInfoGrabberDidFinishGrabbing:(MLTVShowInfoGrabber *)grabber
{
Pierre's avatar
Pierre committed
403
    MLShow *show = grabber.userData;
Pierre's avatar
Pierre committed
404 405
    NSArray *results = grabber.results;
    if ([results count] > 0) {
406 407
        NSDictionary *result = results[0];
        NSString *showId = result[@"id"];
Pierre's avatar
Pierre committed
408 409

        show.theTVDBID = showId;
410 411 412
        show.name = result[@"title"];
        show.shortSummary = result[@"shortSummary"];
        show.releaseYear = result[@"releaseYear"];
Pierre's avatar
Pierre committed
413 414

        // Fetch episodes info
Felix Paul Kühne's avatar
Felix Paul Kühne committed
415
        MLTVShowEpisodesInfoGrabber *grabber = [[MLTVShowEpisodesInfoGrabber alloc] init];
Pierre's avatar
Pierre committed
416 417 418 419 420 421
        grabber.delegate = self;
        grabber.userData = show;
        [grabber lookUpForShowID:showId];
    }
    else {
        // Not found.
Pierre's avatar
Pierre committed
422
        [self noMetaDataInRemoteDBForShow:show];
Pierre's avatar
Pierre committed
423 424 425 426
        show.lastSyncDate = [MLTVShowInfoGrabber serverTime];
    }
}

Pierre's avatar
Pierre committed
427 428 429 430 431 432
- (void)tvShowInfoGrabber:(MLTVShowInfoGrabber *)grabber didFailWithError:(NSError *)error
{
    MLShow *show = grabber.userData;
    [self errorWhenFetchingMetaDataForShow:show];
}

Pierre's avatar
Pierre committed
433 434
- (void)tvShowInfoGrabberDidFetchServerTime:(MLTVShowInfoGrabber *)grabber
{
Pierre's avatar
Pierre committed
435
    MLShow *show = grabber.userData;
Pierre's avatar
Pierre committed
436 437 438

    [[NSUserDefaults standardUserDefaults] setInteger:[[MLTVShowInfoGrabber serverTime] integerValue] forKey:kLastTVDBUpdateServerTime];

Pierre's avatar
Pierre committed
439
    // First fetch the MLShow ID
Felix Paul Kühne's avatar
Felix Paul Kühne committed
440
    MLTVShowInfoGrabber *showInfoGrabber = [[MLTVShowInfoGrabber alloc] init];
Pierre's avatar
Pierre committed
441 442 443
    showInfoGrabber.delegate = self;
    showInfoGrabber.userData = show;

444
    APLog(@"Fetching show information on %@", show.name);
Pierre's avatar
Pierre committed
445

Pierre's avatar
Pierre committed
446 447 448 449
    [showInfoGrabber lookUpForTitle:show.name];
}
#endif

Pierre's avatar
Pierre committed
450
- (void)fetchMetaDataForShow:(MLShow *)show
Pierre's avatar
Pierre committed
451
{
Pierre's avatar
Pierre committed
452 453
    if (!_allowNetworkAccess)
        return;
454
    APLog(@"Fetching show server time");
Pierre's avatar
Pierre committed
455

Pierre's avatar
Pierre committed
456
    // First fetch the serverTime, so that we can update each entry.
Pierre's avatar
Pierre committed
457 458
#if HAVE_BLOCK
    [MLTVShowInfoGrabber fetchServerTimeAndExecuteBlock:^(NSNumber *serverDate) {
Pierre's avatar
Pierre committed
459 460 461

        [[NSUserDefaults standardUserDefaults] setInteger:[serverDate integerValue] forKey:kLastTVDBUpdateServerTime];

462
        APLog(@"Fetching show information on %@", show.name);
Pierre's avatar
Pierre committed
463 464 465

        // First fetch the MLShow ID
        MLTVShowInfoGrabber *grabber = [[[MLTVShowInfoGrabber alloc] init] autorelease];
Pierre's avatar
Pierre committed
466 467 468 469 470 471 472 473 474 475 476
        [grabber lookUpForTitle:show.name andExecuteBlock:^{
            NSArray *results = grabber.results;
            if ([results count] > 0) {
                NSDictionary *result = [results objectAtIndex:0];
                NSString *showId = [result objectForKey:@"id"];

                show.theTVDBID = showId;
                show.name = [result objectForKey:@"title"];
                show.shortSummary = [result objectForKey:@"shortSummary"];
                show.releaseYear = [result objectForKey:@"releaseYear"];

477
                APLog(@"Fetching show episode information on %@", showId);
Pierre's avatar
Pierre committed
478

Pierre's avatar
Pierre committed
479
                // Fetch episode info
Pierre's avatar
Pierre committed
480
                MLTVShowEpisodesInfoGrabber *grabber = [[[MLTVShowEpisodesInfoGrabber alloc] init] autorelease];
Pierre's avatar
Pierre committed
481 482 483 484 485 486 487
                [grabber lookUpForShowID:showId andExecuteBlock:^{
                    NSArray *results = grabber.episodesResults;
                    [show setValue:[grabber.results objectForKey:@"serieArtworkURL"] forKey:@"artworkURL"];
                    for (id result in results) {
                        if ([[result objectForKey:@"serie"] boolValue]) {
                            continue;
                        }
488
                        MLShowEpisode *showEpisode = [MLShowEpisode episodeWithShow:show episodeNumber:[result objectForKey:@"episodeNumber"] seasonNumber:[result objectForKey:@"seasonNumber"] createIfNeeded:YES];
Pierre's avatar
Pierre committed
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
                        showEpisode.name = [result objectForKey:@"title"];
                        showEpisode.theTVDBID = [result objectForKey:@"id"];
                        showEpisode.shortSummary = [result objectForKey:@"shortSummary"];
                        showEpisode.artworkURL = [result objectForKey:@"artworkURL"];
                        showEpisode.lastSyncDate = serverDate;
                    }
                    show.lastSyncDate = serverDate;
                }];
            }
            else {
                // Not found.
                show.lastSyncDate = serverDate;
            }

        }];
    }];
Pierre's avatar
Pierre committed
505
#else
Felix Paul Kühne's avatar
Felix Paul Kühne committed
506
    MLTVShowInfoGrabber *grabber = [[MLTVShowInfoGrabber alloc] init];
Pierre's avatar
Pierre committed
507 508 509
    grabber.delegate = self;
    grabber.userData = show;
    [grabber fetchServerTime];
Pierre's avatar
Pierre committed
510
#endif
Pierre's avatar
Pierre committed
511 512
}

Pierre's avatar
Pierre committed
513
- (void)addTVShowEpisodeWithInfo:(NSDictionary *)tvShowEpisodeInfo andFile:(MLFile *)file
Pierre's avatar
Pierre committed
514
{
Pierre's avatar
Pierre committed
515
    file.type = kMLFileTypeTVShowEpisode;
Pierre's avatar
Pierre committed
516

517 518 519
    NSNumber *seasonNumber = tvShowEpisodeInfo[@"season"];
    NSNumber *episodeNumber = tvShowEpisodeInfo[@"episode"];
    NSString *tvShowName = tvShowEpisodeInfo[@"tvShowName"];
520
    NSString *tvEpisodeName = tvShowEpisodeInfo[@"tvEpisodeName"];
Pierre's avatar
Pierre committed
521 522
    BOOL hasNoTvShow = NO;
    if (!tvShowName) {
523
        tvShowName = @"";
Pierre's avatar
Pierre committed
524 525 526
        hasNoTvShow = YES;
    }
    BOOL wasInserted = NO;
Pierre's avatar
Pierre committed
527 528
    MLShow *show = nil;
    MLShowEpisode *episode = [MLShowEpisode episodeWithShowName:tvShowName episodeNumber:episodeNumber seasonNumber:seasonNumber createIfNeeded:YES wasCreated:&wasInserted];
529 530

    if (episode) {
Pierre's avatar
Pierre committed
531
        show = episode.show;
532 533
        [show addEpisode:episode];
    }
Pierre's avatar
Pierre committed
534 535 536 537
    if (wasInserted && !hasNoTvShow) {
        show.name = tvShowName;
        [self fetchMetaDataForShow:show];
    }
538
    episode.name = tvEpisodeName;
Pierre's avatar
Pierre committed
539

Felix Paul Kühne's avatar
Felix Paul Kühne committed
540
    if (episode.name.length < 1)
Pierre's avatar
Pierre committed
541 542 543
        episode.name = file.title;
    file.seasonNumber = seasonNumber;
    file.episodeNumber = episodeNumber;
544
    episode.shouldBeDisplayed = @YES;
Pierre's avatar
Pierre committed
545 546

    [episode addFilesObject:file];
Pierre's avatar
Pierre committed
547
    file.showEpisode = episode;
Pierre's avatar
Pierre committed
548

Pierre's avatar
Pierre committed
549
    // The rest of the meta data will be fetched using the MLShow
550
    file.hasFetchedInfo = @YES;
Pierre's avatar
Pierre committed
551 552
}

553 554 555 556
- (void)addAudioContentWithInfo:(NSDictionary *)audioContentInfo andFile:(MLFile *)file
{
    file.type = kMLFileTypeAudio;

557 558 559
    file.title = audioContentInfo[VLCMetaInformationTitle];

    /* all further meta data is set by the FileParserQueue */
560 561 562 563

    file.hasFetchedInfo = @YES;
}

Pierre's avatar
Pierre committed
564
/**
Pierre's avatar
Pierre committed
565
 * MLFile auto detection
Pierre's avatar
Pierre committed
566 567
 */

Pierre's avatar
Pierre committed
568
#if !HAVE_BLOCK
Pierre's avatar
Pierre committed
569 570 571 572 573 574
- (void)movieInfoGrabber:(MLMovieInfoGrabber *)grabber didFailWithError:(NSError *)error
{
    MLFile *file = grabber.userData;
    [self errorWhenFetchingMetaDataForFile:file];
}

Pierre's avatar
Pierre committed
575 576
- (void)movieInfoGrabberDidFinishGrabbing:(MLMovieInfoGrabber *)grabber
{
577
    NSNumber *yes = @YES;
Pierre's avatar
Pierre committed
578 579

    NSArray *results = grabber.results;
Pierre's avatar
Pierre committed
580
    MLFile *file = grabber.userData;
Pierre's avatar
Pierre committed
581
    if ([results count] > 0) {
582 583 584 585 586
        NSDictionary *result = results[0];
        file.artworkURL = result[@"artworkURL"];
        file.title = result[@"title"];
        file.shortSummary = result[@"shortSummary"];
        file.releaseYear = result[@"releaseYear"];
Pierre's avatar
Pierre committed
587
    }
Pierre's avatar
Pierre committed
588 589 590 591
    else {
        [self noMetaDataInRemoteDBForFile:file];
    }

Pierre's avatar
Pierre committed
592 593 594 595
    file.hasFetchedInfo = yes;
}
#endif

Pierre's avatar
Pierre committed
596
- (void)fetchMetaDataForFile:(MLFile *)file
Pierre's avatar
Pierre committed
597
{
598
    APLog(@"Fetching meta data for %@", file.title);
Pierre's avatar
Pierre committed
599

600
    [[MLFileParserQueue sharedFileParserQueue] addFile:file];
601

Pierre's avatar
Pierre committed
602 603 604 605 606
    if (!_allowNetworkAccess) {
        // Automatically compute the thumbnail
        [self computeThumbnailForFile:file];
    }

Pierre's avatar
Pierre committed
607
    NSDictionary *tvShowEpisodeInfo = [MLTitleDecrapifier tvShowEpisodeInfoFromString:file.title];
Pierre's avatar
Pierre committed
608 609 610 611 612
    if (tvShowEpisodeInfo) {
        [self addTVShowEpisodeWithInfo:tvShowEpisodeInfo andFile:file];
        return;
    }

613 614 615 616 617 618 619 620
    if ([file isSupportedAudioFile]) {
        NSDictionary *audioContentInfo = [MLTitleDecrapifier audioContentInfoFromFile:file];
        if (audioContentInfo && ![file videoTrack]) {
            [self addAudioContentWithInfo:audioContentInfo andFile:file];
            return;
        }
    }

Pierre's avatar
Pierre committed
621 622 623
    if (!_allowNetworkAccess)
        return;

Pierre's avatar
Pierre committed
624
    // Go online and fetch info.
Pierre's avatar
Pierre committed
625 626 627

    // We don't care about keeping a reference to track the item during its life span
    // because we are a singleton
Felix Paul Kühne's avatar
Felix Paul Kühne committed
628
    MLMovieInfoGrabber *grabber = [[MLMovieInfoGrabber alloc] init];
Pierre's avatar
Pierre committed
629

630
    APLog(@"Looking up for Movie '%@'", file.title);
Pierre's avatar
Pierre committed
631

Pierre's avatar
Pierre committed
632
#if HAVE_BLOCK
Pierre's avatar
Pierre committed
633
    [grabber lookUpForTitle:file.title andExecuteBlock:^(NSError *err){
Pierre's avatar
Pierre committed
634 635
        if (err) {
            [self errorWhenFetchingMetaDataForFile:file];
Pierre's avatar
Pierre committed
636
            return;
Pierre's avatar
Pierre committed
637
        }
Pierre's avatar
Pierre committed
638 639 640 641 642

        NSArray *results = grabber.results;
        if ([results count] > 0) {
            NSDictionary *result = [results objectAtIndex:0];
            file.artworkURL = [result objectForKey:@"artworkURL"];
Pierre's avatar
Pierre committed
643 644
            if (!file.artworkURL)
                [self computeThumbnailForFile:file];
Pierre's avatar
Pierre committed
645 646 647
            file.title = [result objectForKey:@"title"];
            file.shortSummary = [result objectForKey:@"shortSummary"];
            file.releaseYear = [result objectForKey:@"releaseYear"];
Pierre's avatar
Pierre committed
648 649 650
        } else
            [self noMetaDataInRemoteDBForFile:file];
        file.hasFetchedInfo = [NSNumber numberWithBool:YES];
Pierre's avatar
Pierre committed
651
    }];
Pierre's avatar
Pierre committed
652
#else
Pierre's avatar
Pierre committed
653 654 655
    grabber.userData = file;
    grabber.delegate = self;
    [grabber lookUpForTitle:file.title];
Pierre's avatar
Pierre committed
656
#endif
Pierre's avatar
Pierre committed
657 658
}

Pierre's avatar
Pierre committed
659 660 661
#pragma mark -
#pragma mark Adding file to the DB

Pierre's avatar
Pierre committed
662
- (void)addFilePath:(NSString *)filePath
Pierre's avatar
Pierre committed
663
{
664
    APLog(@"Adding Path %@", filePath);
Pierre's avatar
Pierre committed
665

Pierre's avatar
Pierre committed
666
    NSURL *url = [NSURL fileURLWithPath:filePath];
Pierre's avatar
Pierre committed
667 668
    NSDictionary *attributes = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil];
    NSString *title = [filePath lastPathComponent];
Pierre's avatar
Pierre committed
669
#if !TARGET_OS_IPHONE
Pierre's avatar
Pierre committed
670 671
    NSDate *openedDate = nil; // FIXME kMDItemLastUsedDate
    NSDate *modifiedDate = nil; // FIXME [result valueForAttribute:@"kMDItemFSContentChangeDate"];
Pierre's avatar
Pierre committed
672
#endif
673
    NSNumber *size = attributes[NSFileSize]; // FIXME [result valueForAttribute:@"kMDItemFSSize"];
Pierre's avatar
Pierre committed
674

Pierre's avatar
Pierre committed
675
    MLFile *file = [self createObjectForEntity:@"File"];
Pierre's avatar
Pierre committed
676
    file.url = [url absoluteString];
Pierre's avatar
Pierre committed
677 678 679 680

    // Yes, this is a negative number. VLCTime nicely display negative time
    // with "XX minutes remaining". And we are using this facility.

681 682
    NSNumber *no = @NO;
    NSNumber *yes = @YES;
Pierre's avatar
Pierre committed
683 684

    file.currentlyWatching = no;
685 686
    file.lastPosition = @0.0;
    file.remainingTime = @0.0;
Pierre's avatar
Pierre committed
687 688
    file.unread = yes;

Pierre's avatar
Pierre committed
689
#if !TARGET_OS_IPHONE
Pierre's avatar
Pierre committed
690 691 692 693
    if ([openedDate isGreaterThan:modifiedDate]) {
        file.playCount = [NSNumber numberWithDouble:1];
        file.unread = no;
    }
Pierre's avatar
Pierre committed
694 695
#endif

696 697 698 699
    if ([[[NSUserDefaults standardUserDefaults] objectForKey:kDecrapifyTitles] boolValue] == YES)
        file.title = [MLTitleDecrapifier decrapify:[title stringByDeletingPathExtension]];
    else
        file.title = [title stringByDeletingPathExtension];
Pierre's avatar
Pierre committed
700 701

    if ([size longLongValue] < 150000000) /* 150 MB */
Pierre's avatar
Pierre committed
702
        file.type = kMLFileTypeClip;
Pierre's avatar
Pierre committed
703
    else
Pierre's avatar
Pierre committed
704
        file.type = kMLFileTypeMovie;
Pierre's avatar
Pierre committed
705 706 707 708

    [self fetchMetaDataForFile:file];
}

Pierre's avatar
Pierre committed
709
- (void)addFilePaths:(NSArray *)filepaths
Pierre's avatar
Pierre committed
710
{
Pierre's avatar
Pierre committed
711
    NSUInteger count = [filepaths count];
Pierre's avatar
Pierre committed
712 713
    NSMutableArray *fetchPredicates = [NSMutableArray arrayWithCapacity:count];
    NSMutableDictionary *urlToObject = [NSMutableDictionary dictionaryWithCapacity:count];
714
    NSString *documentFolderPath = [[MLMediaLibrary sharedMediaLibrary] documentFolderPath];
Pierre's avatar
Pierre committed
715 716

    // Prepare a fetch request for all items
717 718 719
    NSArray *pathComponents;
    NSUInteger componentCount;

Pierre's avatar
Pierre committed
720
    for (NSString *path in filepaths) {
721
#if TARGET_OS_IPHONE
722
        NSString *urlString;
723 724 725 726 727 728 729 730 731 732 733 734 735 736 737
        NSString *componentString = @"";

        pathComponents = [path componentsSeparatedByString:@"/"];
        componentCount = pathComponents.count;
        if ([pathComponents[componentCount - 2] isEqualToString:@"Documents"])
            componentString = [path lastPathComponent];
        else {
            NSUInteger firstElement = [pathComponents indexOfObject:@"Documents"] + 1;
            for (NSUInteger x = 0; x < componentCount - firstElement; x++) {
                if (x == 0)
                    componentString = [componentString stringByAppendingFormat:@"%@", pathComponents[firstElement + x]];
                else
                    componentString = [componentString stringByAppendingFormat:@"/%@", pathComponents[firstElement + x]];
            }
        }
738

739 740 741 742 743 744
        /* compose and escape string */
        urlString = [[NSString stringWithFormat:@"%@/%@", documentFolderPath, componentString] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

        /* check for the end of the paths */
        [fetchPredicates addObject:[NSPredicate predicateWithFormat:@"url CONTAINS %@", [urlString lastPathComponent]]];
        [urlToObject setObject:path forKey:urlString];
745
#else
746
        [fetchPredicates addObject:[NSPredicate predicateWithFormat:@"url == %@", path]];
747
#endif
Pierre's avatar
Pierre committed
748 749 750 751 752
    }
    NSFetchRequest *request = [self fetchRequestForEntity:@"File"];

    [request setPredicate:[NSCompoundPredicate orPredicateWithSubpredicates:fetchPredicates]];

753
    APLog(@"Fetching");
754 755 756 757
    NSManagedObjectContext *moc = [self managedObjectContext];
    if (!moc)
        return;
    NSArray *dbResults = [moc executeFetchRequest:request error:nil];
758
    APLog(@"Done");
Pierre's avatar
Pierre committed
759

Pierre's avatar
Pierre committed
760
    NSMutableArray *filePathsToAdd = [NSMutableArray arrayWithArray:filepaths];
Pierre's avatar
Pierre committed
761 762

    // Remove objects that are already in db.
Pierre's avatar
Pierre committed
763 764
    for (MLFile *dbResult in dbResults) {
        NSString *urlString = dbResult.url;
765
        [filePathsToAdd removeObject:[urlToObject objectForKey:urlString]];
Pierre's avatar
Pierre committed
766 767 768
    }

    // Add only the newly added items
Pierre's avatar
Pierre committed
769 770
    for (NSString* path in filePathsToAdd)
        [self addFilePath:path];
Pierre's avatar
Pierre committed
771 772
}

Pierre's avatar
Pierre committed
773 774 775 776

#pragma mark -
#pragma mark DB Updates

Pierre's avatar
Pierre committed
777 778 779 780 781 782
#if !HAVE_BLOCK
- (void)tvShowInfoGrabber:(MLTVShowInfoGrabber *)grabber didFetchUpdates:(NSArray *)updates
{
    NSFetchRequest *request = [self fetchRequestForEntity:@"Show"];
    [request setPredicate:[NSComparisonPredicate predicateWithLeftExpression:[NSExpression expressionForKeyPath:@"theTVDBID"] rightExpression:[NSExpression expressionForConstantValue:updates] modifier:NSDirectPredicateModifier type:NSInPredicateOperatorType options:0]];
    NSArray *results = [[self managedObjectContext] executeFetchRequest:request error:nil];
Pierre's avatar
Pierre committed
783
    for (MLShow *show in results)
Pierre's avatar
Pierre committed
784 785 786 787
        [self fetchMetaDataForShow:show];
}
#endif

788 789
- (BOOL)libraryNeedsUpgrade
{
790
    if (![[[NSUserDefaults standardUserDefaults] objectForKey:kUpdatedToTheGreatSharkHuntDatabaseFormat] boolValue])
791 792 793 794 795
        return YES;
    return NO;
}

- (void)upgradeLibrary
Felix Paul Kühne's avatar
Felix Paul Kühne committed
796 797 798 799 800 801
{
    if (![[[NSUserDefaults standardUserDefaults] objectForKey:kUpdatedToTheGreatSharkHuntDatabaseFormat] boolValue])
        [self _upgradeLibraryToGreatSharkHuntDatabaseFormat];
}

- (void)_upgradeLibraryToGreatSharkHuntDatabaseFormat
802
{
Felix Paul Kühne's avatar
Felix Paul Kühne committed
803
    [self libraryDidDisappear];
804 805 806
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSFileManager *fileManager = [NSFileManager defaultManager];

807
    /* remove potential empty albums left over by previous releases */
808 809 810 811 812 813 814 815 816 817
    NSArray *collection = [MLAlbum allAlbums];
    NSUInteger count = collection.count;
    MLAlbum *album;
    MLAlbumTrack *track;
    NSArray *secondaryCollection;
    NSURL *fileURL;
    NSUInteger secondaryCount = 0;
    NSArray *tertiaryCollection;
    NSUInteger tertiaryCount = 0;
    NSUInteger emptyAlbumCounter = 0;
818 819 820 821 822 823 824
    NSManagedObjectContext *moc = [self managedObjectContext];
    if (!moc) {
        [self libraryDidAppear];
        if ([self.delegate respondsToSelector:@selector(libraryUpgradeComplete)])
            [self.delegate libraryUpgradeComplete];
        return;
    }
825 826 827
    for (NSUInteger x = 0; x < count; x++) {
        album = collection[x];
        if (album.tracks.count < 1)
828
            [moc deleteObject:album];
829 830 831 832 833 834 835 836 837 838 839 840 841
        else {
            secondaryCollection = album.tracks.allObjects;
            secondaryCount = secondaryCollection.count;
            emptyAlbumCounter = 0;
            for (NSUInteger y = 0; y < secondaryCount; y++) {
                track = secondaryCollection[y];
                tertiaryCollection = track.files.allObjects;
                tertiaryCount = tertiaryCollection.count;
                for (NSUInteger z = 0; z < tertiaryCount; z++) {
                    fileURL = [NSURL URLWithString:[(MLFile *)tertiaryCollection[z] url]];
                    BOOL exists = [fileManager fileExistsAtPath:[fileURL path]];
                    if (exists)
                        emptyAlbumCounter++;
842 843
                    else
                        [album removeTrack:track];
844 845 846
                }
            }
            if (emptyAlbumCounter == 0)
847
                [moc deleteObject:album];
848 849 850
        }
    }
    album = nil;
851 852

    /* remove potential empty shows left over by previous releases */
853 854 855 856 857 858 859
    collection = [MLShow allShows];
    MLShow *show;
    MLShowEpisode *showEpisode;
    count = collection.count;
    for (NSUInteger x = 0; x < count; x++) {
        show = collection[x];
        if (show.episodes.count < 1)
860
            [moc deleteObject:show];
861 862 863 864 865 866 867 868 869 870 871 872 873
        else {
            secondaryCollection = show.episodes.allObjects;
            secondaryCount = secondaryCollection.count;
            emptyAlbumCounter = 0;
            for (NSUInteger y = 0; y < secondaryCount; y++) {
                showEpisode = secondaryCollection[y];
                tertiaryCollection = showEpisode.files.allObjects;
                tertiaryCount = tertiaryCollection.count;
                for (NSUInteger z = 0; z < tertiaryCount; z++) {
                    fileURL = [NSURL URLWithString:[(MLFile *)tertiaryCollection[z] url]];
                    BOOL exists = [fileManager fileExistsAtPath:[fileURL path]];
                    if (exists)
                        emptyAlbumCounter++;
874 875
                    else
                        [show removeEpisode:showEpisode];
876 877 878
                }
            }
            if (emptyAlbumCounter == 0)
879
                [moc deleteObject:show];
880 881 882
        }
    }

883 884 885 886 887
    /* remove duplicates */
    NSArray *allFiles = [MLFile allFiles];
    NSUInteger allFilesCount = allFiles.count;
    NSMutableArray *seenFiles = [[NSMutableArray alloc] initWithCapacity:allFilesCount];
    MLFile *currentFile;
Felix Paul Kühne's avatar
Felix Paul Kühne committed
888 889
    NSString *currentFilePath;
    for (NSUInteger x = 0; x < allFilesCount; x++) {
890
        currentFile = allFiles[x];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
891 892
        currentFilePath = [currentFile.url stringByReplacingOccurrencesOfString:@"/localhost/" withString:@"//"];
        if ([seenFiles containsObject:currentFilePath])
893
            [moc deleteObject:currentFile];
894
        else
Felix Paul Kühne's avatar
Felix Paul Kühne committed
895
            [seenFiles addObject:currentFilePath];
896 897
    }

898
    [defaults setBool:YES forKey:kUpdatedToTheGreatSharkHuntDatabaseFormat];
Felix Paul Kühne's avatar
Felix Paul Kühne committed
899 900 901
    [defaults synchronize];

    [self libraryDidAppear];
902 903 904 905
    if ([self.delegate respondsToSelector:@selector(libraryUpgradeComplete)])
        [self.delegate libraryUpgradeComplete];
}

906
- (void)updateMediaDatabase
Pierre's avatar
Pierre committed
907
{
Felix Paul Kühne's avatar
Felix Paul Kühne committed
908
    [self libraryDidDisappear];
Pierre's avatar
Pierre committed
909
    // Remove no more present files
Pierre's avatar
Pierre committed
910
    NSFetchRequest *request = [self fetchRequestForEntity:@"File"];
911 912 913 914
    NSManagedObjectContext *moc = [self managedObjectContext];
    if (!moc)
        return;
    NSArray *results = [moc executeFetchRequest:request error:nil];
Pierre's avatar
Pierre committed
915
    NSFileManager *fileManager = [NSFileManager defaultManager];
916

Felix Paul Kühne's avatar
Felix Paul Kühne committed
917
    unsigned int count = (unsigned int)results.count;
918 919
    for (unsigned int x = 0; x < count; x++) {
        MLFile *file = results[x];
Pierre's avatar
Pierre committed
920 921
        NSString *urlString = [file url];
        NSURL *fileURL = [NSURL URLWithString:urlString];
922
        BOOL exists = [fileManager fileExistsAtPath:[fileURL path]];
923
        if (!exists) {
924
            APLog(@"Marking - %@", [fileURL absoluteString]);
925
            file.isSafe = YES; // It doesn't exist, it's safe.
926 927
            if (file.isAlbumTrack) {
                MLAlbum *album = file.albumTrack.album;
928 929
                if (album.tracks.count <= 1) {
                    @try {
930
                        [moc deleteObject:album];
931 932 933 934
                    }
                    @catch (NSException *exception) {
                        APLog(@"failed to nuke object because it disappeared in front of us");
                    }
935
                } else
936
                    [album removeTrack:file.albumTrack];
937 938 939
            }
            if (file.isShowEpisode) {
                MLShow *show = file.showEpisode.show;
940 941
                if (show.episodes.count <= 1) {
                    @try {
942
                        [moc deleteObject:show];
943 944 945 946
                    }
                    @catch (NSException *exception) {
                        APLog(@"failed to nuke object because it disappeared in front of us");
                    }
947 948
                } else
                    [show removeEpisode:file.showEpisode];
949
            }
950
#if TARGET_OS_IPHONE
951
            NSString *thumbPath = [file thumbnailPath];
952 953
            bool thumbExists = [fileManager fileExistsAtPath:thumbPath];
            if (thumbExists)
954
                [fileManager removeItemAtPath:thumbPath error:nil];
955
            [moc deleteObject:file];
956
#endif
957
        }
958 959 960
#if !TARGET_OS_IPHONE
    file.isOnDisk = @(exists);
#endif
Pierre's avatar
Pierre committed
961
    }
Felix Paul Kühne's avatar
Felix Paul Kühne committed
962
    [self libraryDidAppear];
Pierre's avatar
Pierre committed
963

964 965 966
    // Get the file to parse
    request = [self fetchRequestForEntity:@"File"];
    [request setPredicate:[NSPredicate predicateWithFormat:@"isOnDisk == YES && tracks.@count == 0"]];
967
    results = [moc executeFetchRequest:request error:nil];
968 969 970
    for (MLFile *file in results)
        [[MLFileParserQueue sharedFileParserQueue] addFile:file];

Pierre's avatar
Pierre committed
971 972 973
    if (!_allowNetworkAccess) {
        // Always attempt to fetch
        request = [self fetchRequestForEntity:@"File"];