...
 
Commits (3)
......@@ -29,6 +29,8 @@
@property (readonly) BOOL libraryNeedsUpgrade;
@property (nonatomic, strong) id delegate;
@property (nonatomic, strong) NSURL *persistentStoreURL;
@property (nonatomic, strong) NSPersistentStoreCoordinator *persistentStoreCoordinator;
+ (id)sharedMediaLibrary;
......
......@@ -223,7 +223,16 @@ NSString *kMLFileTypeAudio = @"audio";
if ([ret rangeOfString:documentFolderPath].location != NSNotFound)
return ret;
ret = [NSString stringWithFormat:@"%@/%@", documentFolderPath, [ret lastPathComponent]];
NSArray *pathComponents = [ret componentsSeparatedByString:@"/"];
NSUInteger componentCount = pathComponents.count;
if ([pathComponents[componentCount - 2] isEqualToString:@"Documents"])
ret = [NSString stringWithFormat:@"%@/%@", documentFolderPath, [ret lastPathComponent]];
else {
NSUInteger firstElement = [pathComponents indexOfObject:@"Documents"] + 1;
ret = documentFolderPath;
for (NSUInteger x = 0; x < componentCount - firstElement; x++)
ret = [ret stringByAppendingFormat:@"/%@", pathComponents[firstElement + x]];
}
APLog(@"returning modified URL! will return %@", ret);
return ret;
......
......@@ -173,23 +173,22 @@ static NSString *kDecrapifyTitles = @"MLDecrapifyTitles";
return _documentFolderPath;
}
- (NSManagedObjectContext *)managedObjectContext
{
if (_managedObjectContext)
return _managedObjectContext;
- (NSURL *)persistentStoreURL {
NSString *databaseFolderPath = [self databaseFolderPath];
NSString *path = [databaseFolderPath stringByAppendingPathComponent: @"MediaLibrary.sqlite"];
NSURL *url = [NSURL fileURLWithPath:path];
return [NSURL fileURLWithPath:path];
}
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator {
NSPersistentStoreCoordinator *coordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
NSNumber *yes = @YES;
NSDictionary *options = @{NSMigratePersistentStoresAutomaticallyOption : yes,
NSInferMappingModelAutomaticallyOption : yes};
NSInferMappingModelAutomaticallyOption : yes};
NSError *error;
NSPersistentStore *persistentStore = [coordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:url options:options error:&error];
NSPersistentStore *persistentStore = [coordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:self.persistentStoreURL options:options error:&error];
if (!persistentStore) {
#if! TARGET_OS_IPHONE
......@@ -197,11 +196,11 @@ static NSString *kDecrapifyTitles = @"MLDecrapifyTitles";
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];
[[NSFileManager defaultManager] removeItemAtPath:path error:nil];
[[NSFileManager defaultManager] removeItemAtPath:self.persistentStoreURL.path error:nil];
#else
[[NSFileManager defaultManager] removeItemAtPath:path error:nil];
[[NSFileManager defaultManager] removeItemAtPath:self.persistentStoreURL.path error:nil];
#endif
persistentStore = [coordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:url options:options error:&error];
persistentStore = [coordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:self.persistentStoreURL options:options error:&error];
if (!persistentStore) {
#if! TARGET_OS_IPHONE
NSRunInformationalAlertPanel(@"Corrupted Media Library", @"There is nothing we can apparently do about it...", @"OK", nil, nil);
......@@ -213,9 +212,16 @@ static NSString *kDecrapifyTitles = @"MLDecrapifyTitles";
return nil;
}
}
return coordinator;
}
- (NSManagedObjectContext *)managedObjectContext
{
if (_managedObjectContext)
return _managedObjectContext;
_managedObjectContext = [[NSManagedObjectContext alloc] init];
[_managedObjectContext setPersistentStoreCoordinator:coordinator];
[_managedObjectContext setPersistentStoreCoordinator:self.persistentStoreCoordinator];
[_managedObjectContext setUndoManager:nil];
[_managedObjectContext addObserver:self forKeyPath:@"hasChanges" options:NSKeyValueObservingOptionInitial context:nil];
return _managedObjectContext;
......@@ -659,22 +665,41 @@ static NSString *kDecrapifyTitles = @"MLDecrapifyTitles";
NSUInteger count = [filepaths count];
NSMutableArray *fetchPredicates = [NSMutableArray arrayWithCapacity:count];
NSMutableDictionary *urlToObject = [NSMutableDictionary dictionaryWithCapacity:count];
NSString *documentFolderPath = [[MLMediaLibrary sharedMediaLibrary] documentFolderPath];
// Prepare a fetch request for all items
NSArray *pathComponents;
NSUInteger componentCount;
for (NSString *path in filepaths) {
NSURL *url = [NSURL fileURLWithPath:path];
NSString *urlString = [url absoluteString];
#if TARGET_OS_IPHONE
/* check for the end of a path only, since parts of the path components may change
* while it is still the same file
* of course, this is only true for the flat file structure we use on iOS */
[fetchPredicates addObject:[NSPredicate predicateWithFormat:@"url ENDSWITH %@", [urlString lastPathComponent]]];
NSString *urlString;
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]];
}
}
/* 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];
#else
[fetchPredicates addObject:[NSPredicate predicateWithFormat:@"url == %@", urlString]];
[fetchPredicates addObject:[NSPredicate predicateWithFormat:@"url == %@", path]];
#endif
urlToObject[urlString] = path;
}
NSFetchRequest *request = [self fetchRequestForEntity:@"File"];
[request setPredicate:[NSCompoundPredicate orPredicateWithSubpredicates:fetchPredicates]];
......@@ -688,7 +713,7 @@ static NSString *kDecrapifyTitles = @"MLDecrapifyTitles";
// Remove objects that are already in db.
for (MLFile *dbResult in dbResults) {
NSString *urlString = dbResult.url;
[filePathsToAdd removeObject:urlToObject[urlString]];
[filePathsToAdd removeObject:[urlToObject objectForKey:urlString]];
}
// Add only the newly added items
......