diff --git a/Playlist/PlaylistController.h b/Playlist/PlaylistController.h index 8eb47be49..0976ef38e 100644 --- a/Playlist/PlaylistController.h +++ b/Playlist/PlaylistController.h @@ -19,10 +19,6 @@ @class SpotlightWindowController; @class PlaybackController; -@interface NSApplication (CoreDataStorageExtension) -- (NSPersistentContainer *_Nonnull)sharedPersistentContainer; -@end - typedef NS_ENUM(NSInteger, RepeatMode) { RepeatModeNoRepeat = 0, RepeatModeRepeatOne, @@ -69,7 +65,6 @@ typedef NS_ENUM(NSInteger, URLOrigin) { @property(retain) NSString *_Nullable totalTime; @property(retain) NSString *_Nullable currentStatus; -@property(strong, nonatomic, readonly) NSOperationQueue *_Nonnull persistentContainerQueue; @property(strong, nonatomic, readonly) NSPersistentContainer *_Nonnull persistentContainer; @property(strong, nonatomic, readonly) NSMutableDictionary *_Nonnull persistentArtStorage; @@ -139,6 +134,8 @@ typedef NS_ENUM(NSInteger, URLOrigin) { - (void)readQueueFromDataStore; - (void)readShuffleListFromDataStore; ++ (NSPersistentContainer *_Nonnull)sharedPersistentContainer; + // reload metadata of selection - (IBAction)reloadTags:(id _Nullable)sender; diff --git a/Playlist/PlaylistController.m b/Playlist/PlaylistController.m index 96a14955a..488c78ebf 100644 --- a/Playlist/PlaylistController.m +++ b/Playlist/PlaylistController.m @@ -27,13 +27,7 @@ extern BOOL kAppControllerShuttingDown; -NSPersistentContainer *__persistentContainer = nil; - -@implementation NSApplication (CoreDataStorageExtension) -- (NSPersistentContainer *_Nonnull)sharedPersistentContainer { - return __persistentContainer; -} -@end +NSPersistentContainer *kPersistentContainer = nil; @implementation PlaylistController @@ -43,7 +37,7 @@ NSPersistentContainer *__persistentContainer = nil; static NSArray *cellIdentifiers = nil; -NSMutableDictionary *__artworkDictionary = nil; +NSMutableDictionary *kArtworkDictionary = nil; static void *playlistControllerContext = &playlistControllerContext; @@ -126,16 +120,20 @@ static void *playlistControllerContext = &playlistControllerContext; } }]; - __persistentContainer = self.persistentContainer; + kPersistentContainer = self.persistentContainer; self.persistentContainer.viewContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy; _persistentArtStorage = [[NSMutableDictionary alloc] init]; - __artworkDictionary = self.persistentArtStorage; + kArtworkDictionary = self.persistentArtStorage; return self; } ++ (NSPersistentContainer *)sharedPersistentContainer { + return kPersistentContainer; +} + - (void)awakeFromNib { [super awakeFromNib]; diff --git a/Playlist/PlaylistEntry.m b/Playlist/PlaylistEntry.m index b96eba946..2d289a582 100644 --- a/Playlist/PlaylistEntry.m +++ b/Playlist/PlaylistEntry.m @@ -16,8 +16,8 @@ #import "SHA256Digest.h" #import "SecondsFormatter.h" -extern NSPersistentContainer *__persistentContainer; -extern NSMutableDictionary *__artworkDictionary; +extern NSPersistentContainer *kPersistentContainer; +extern NSMutableDictionary *kArtworkDictionary; @implementation PlaylistEntry (Extension) @@ -344,7 +344,7 @@ extern NSMutableDictionary *__artworkDictionary; @dynamic albumArtInternal; - (NSData *)albumArtInternal { NSString *imageCacheTag = self.artHash; - return [__artworkDictionary objectForKey:imageCacheTag].artData; + return [kArtworkDictionary objectForKey:imageCacheTag].artData; } - (void)setAlbumArtInternal:(NSData *)albumArtInternal { @@ -354,12 +354,12 @@ extern NSMutableDictionary *__artworkDictionary; self.artHash = imageCacheTag; - if(![__artworkDictionary objectForKey:imageCacheTag]) { - AlbumArtwork *art = [NSEntityDescription insertNewObjectForEntityForName:@"AlbumArtwork" inManagedObjectContext:__persistentContainer.viewContext]; + if(![kArtworkDictionary objectForKey:imageCacheTag]) { + AlbumArtwork *art = [NSEntityDescription insertNewObjectForEntityForName:@"AlbumArtwork" inManagedObjectContext:kPersistentContainer.viewContext]; art.artHash = imageCacheTag; art.artData = albumArtInternal; - [__artworkDictionary setObject:art forKey:imageCacheTag]; + [kArtworkDictionary setObject:art forKey:imageCacheTag]; } } @@ -505,7 +505,7 @@ NSURL *_Nullable urlForPath(NSString *_Nullable path) { request.predicate = predicate; NSError *error = nil; - NSArray *results = [__persistentContainer.viewContext executeFetchRequest:request error:&error]; + NSArray *results = [kPersistentContainer.viewContext executeFetchRequest:request error:&error]; if(!results || [results count] < 1) { NSPredicate *filenamePredicate = [NSPredicate predicateWithFormat:@"filename == %@", self.filename]; @@ -513,7 +513,7 @@ NSURL *_Nullable urlForPath(NSString *_Nullable path) { request = [NSFetchRequest fetchRequestWithEntityName:@"PlayCount"]; request.predicate = filenamePredicate; - results = [__persistentContainer.viewContext executeFetchRequest:request error:&error]; + results = [kPersistentContainer.viewContext executeFetchRequest:request error:&error]; } if(!results || [results count] < 1) return nil; diff --git a/Playlist/PlaylistLoader.m b/Playlist/PlaylistLoader.m index e9d6960e3..1a05b34da 100644 --- a/Playlist/PlaylistLoader.m +++ b/Playlist/PlaylistLoader.m @@ -41,7 +41,7 @@ @import Firebase; -extern NSMutableDictionary *__artworkDictionary; +extern NSMutableDictionary *kArtworkDictionary; @implementation PlaylistLoader @@ -839,7 +839,7 @@ NSURL *_Nullable urlForPath(NSString *_Nullable path); } for(AlbumArtwork *art in results) { - [__artworkDictionary setObject:art forKey:art.artHash]; + [kArtworkDictionary setObject:art forKey:art.artHash]; } request = [NSFetchRequest fetchRequestWithEntityName:@"PlaylistEntry"]; diff --git a/Preferences/Preferences/SandboxPathBehaviorController.m b/Preferences/Preferences/SandboxPathBehaviorController.m index 6eed712ad..eeb6c1eff 100644 --- a/Preferences/Preferences/SandboxPathBehaviorController.m +++ b/Preferences/Preferences/SandboxPathBehaviorController.m @@ -29,25 +29,23 @@ NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"path" ascending:YES]; [self setSortDescriptors:@[sortDescriptor]]; - if([NSApp respondsToSelector:@selector(sharedPersistentContainer)]) { - NSPersistentContainer *pc = [NSApp sharedPersistentContainer]; + NSPersistentContainer *pc = [NSClassFromString(@"PlaylistController") sharedPersistentContainer]; - NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"SandboxToken"]; + NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"SandboxToken"]; - NSError *error = nil; - NSArray *results = [pc.viewContext executeFetchRequest:request error:&error]; + NSError *error = nil; + NSArray *results = [pc.viewContext executeFetchRequest:request error:&error]; - if(results && [results count] > 0) { - for(SandboxToken *token in results) { - BOOL isStale = YES; - NSError *err = nil; - NSURL *bookmarkUrl = [NSURL URLByResolvingBookmarkData:token.bookmark options:NSURLBookmarkResolutionWithSecurityScope relativeToURL:nil bookmarkDataIsStale:&isStale error:&err]; - if(!bookmarkUrl) { - ALog(@"Stale bookmark for path: %@, with error: %@", token.path, [err localizedDescription]); - continue; - } - [self addObject:@{ @"path": token.path, @"valid": (isStale ? NSLocalizedPrefString(@"ValidNo") : NSLocalizedPrefString(@"ValidYes")) }]; + if(results && [results count] > 0) { + for(SandboxToken *token in results) { + BOOL isStale = YES; + NSError *err = nil; + NSURL *bookmarkUrl = [NSURL URLByResolvingBookmarkData:token.bookmark options:NSURLBookmarkResolutionWithSecurityScope relativeToURL:nil bookmarkDataIsStale:&isStale error:&err]; + if(!bookmarkUrl) { + ALog(@"Stale bookmark for path: %@, with error: %@", token.path, [err localizedDescription]); + continue; } + [self addObject:@{ @"path": token.path, @"valid": (isStale ? NSLocalizedPrefString(@"ValidNo") : NSLocalizedPrefString(@"ValidYes")) }]; } } } @@ -60,10 +58,7 @@ return; } - if(![NSApp respondsToSelector:@selector(sharedPersistentContainer)]) - return; - - NSPersistentContainer *pc = [NSApp sharedPersistentContainer]; + NSPersistentContainer *pc = [NSClassFromString(@"PlaylistController") sharedPersistentContainer]; SandboxToken *token = [NSEntityDescription insertNewObjectForEntityForName:@"SandboxToken" inManagedObjectContext:pc.viewContext]; @@ -81,10 +76,7 @@ } - (void)removePath:(NSString *)path { - if(![NSApp respondsToSelector:@selector(sharedPersistentContainer)]) - return; - - NSPersistentContainer *pc = [NSApp sharedPersistentContainer]; + NSPersistentContainer *pc = [NSClassFromString(@"PlaylistController") sharedPersistentContainer]; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"path == %@", path]; diff --git a/Spotlight/SpotlightPlaylistEntry.m b/Spotlight/SpotlightPlaylistEntry.m index d58a4386c..253d5da09 100644 --- a/Spotlight/SpotlightPlaylistEntry.m +++ b/Spotlight/SpotlightPlaylistEntry.m @@ -13,7 +13,7 @@ // with format (entryKey, transformerName) static NSDictionary *importKeys; -extern NSPersistentContainer *__persistentContainer; +extern NSPersistentContainer *kPersistentContainer; @implementation SpotlightPlaylistEntry @@ -41,7 +41,7 @@ extern NSPersistentContainer *__persistentContainer; } + (PlaylistEntry *)playlistEntryWithMetadataItem:(NSMetadataItem *)metadataItem { - PlaylistEntry *entry = [NSEntityDescription insertNewObjectForEntityForName:@"PlaylistEntry" inManagedObjectContext:__persistentContainer.viewContext]; + PlaylistEntry *entry = [NSEntityDescription insertNewObjectForEntityForName:@"PlaylistEntry" inManagedObjectContext:kPersistentContainer.viewContext]; entry.deLeted = YES; diff --git a/Utils/SQLiteStore.m b/Utils/SQLiteStore.m index 70234d146..764263ebf 100644 --- a/Utils/SQLiteStore.m +++ b/Utils/SQLiteStore.m @@ -14,7 +14,7 @@ #import "SHA256Digest.h" -extern NSPersistentContainer *__persistentContainer; +extern NSPersistentContainer *kPersistentContainer; NSString *getDatabasePath(void) { NSArray *paths = NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory, NSUserDomainMask, YES); @@ -1443,7 +1443,7 @@ static SQLiteStore *g_sharedStore = nil; #endif - (PlaylistEntry *_Nonnull)getTrack:(int64_t)trackId { - PlaylistEntry *entry = [NSEntityDescription insertNewObjectForEntityForName:@"PlaylistEntry" inManagedObjectContext:__persistentContainer.viewContext]; + PlaylistEntry *entry = [NSEntityDescription insertNewObjectForEntityForName:@"PlaylistEntry" inManagedObjectContext:kPersistentContainer.viewContext]; if(trackId < 0) { entry.error = YES; @@ -1863,7 +1863,7 @@ static SQLiteStore *g_sharedStore = nil; entry.index = index; entry.entryId = entryId; } else { - [__persistentContainer.viewContext deleteObject:entry]; + [kPersistentContainer.viewContext deleteObject:entry]; entry = nil; } } diff --git a/Utils/SandboxBroker.m b/Utils/SandboxBroker.m index fe50ba6d7..094518451 100644 --- a/Utils/SandboxBroker.m +++ b/Utils/SandboxBroker.m @@ -17,17 +17,7 @@ #import "PlaylistController.h" -static SandboxBroker *__sharedSandboxBroker = nil; - -@interface NSApplication (SandboxBrokerExtension) -- (SandboxBroker *)sharedSandboxBroker; -@end - -@implementation NSApplication (SandboxBrokerExtension) -- (SandboxBroker *)sharedSandboxBroker { - return __sharedSandboxBroker; -} -@end +static SandboxBroker *kSharedSandboxBroker = nil; static NSURL *urlWithoutFragment(NSURL *u) { if(![u isFileURL]) return u; @@ -111,9 +101,13 @@ static NSURL *urlWithoutFragment(NSURL *u) { + (id)sharedSandboxBroker { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ - __sharedSandboxBroker = [[self alloc] init]; + kSharedSandboxBroker = [[self alloc] init]; }); - return [NSApp sharedSandboxBroker]; + return kSharedSandboxBroker; +} + ++ (NSPersistentContainer *)sharedPersistentContainer { + return [NSClassFromString(@"PlaylistController") sharedPersistentContainer]; } - (id)init { @@ -156,7 +150,7 @@ static NSURL *urlWithoutFragment(NSURL *u) { } } - NSPersistentContainer *pc = [NSApp sharedPersistentContainer]; + NSPersistentContainer *pc = [SandboxBroker sharedPersistentContainer]; NSSortDescriptor *sortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"path.length" ascending:NO]; @@ -196,7 +190,6 @@ static NSURL *urlWithoutFragment(NSURL *u) { - (const void *)beginFolderAccess:(NSURL *)fileUrl { NSURL *folderUrl = [urlWithoutFragment(fileUrl) URLByDeletingLastPathComponent]; if(![folderUrl isFileURL]) return NULL; - if(![NSApp respondsToSelector:@selector(sharedPersistentContainer)]) return NULL; SandboxEntry *entry;