mirror of
https://github.com/MacPass/MacPass.git
synced 2025-12-14 14:02:28 +00:00
Using NSFileCoordinator to manage file access in presentedItemDidChange
This commit is contained in:
@@ -39,6 +39,7 @@
|
||||
#import "NSError+Messages.h"
|
||||
#import "NSString+MPPasswordCreation.h"
|
||||
#import "NSString+MPHash.h"
|
||||
#import "NSApplication+MPAdditions.h"
|
||||
|
||||
NSString *const MPDocumentDidAddGroupNotification = @"com.hicknhack.macpass.MPDocumentDidAddGroupNotification";
|
||||
NSString *const MPDocumentDidAddEntryNotification = @"com.hicknhack.macpass.MPDocumentDidAddEntryNotification";
|
||||
@@ -264,16 +265,20 @@ NSString *const MPDocumentGroupKey = @"MPDocumentGrou
|
||||
}
|
||||
|
||||
- (void)presentedItemDidChange {
|
||||
[super presentedItemDidChange];
|
||||
/* If we are locked we have the data written back to file - just revert */
|
||||
if(self.encrypted) {
|
||||
[self revertDocumentToSaved:nil];
|
||||
return;
|
||||
NSFileCoordinator *coordinator = [[NSFileCoordinator alloc] initWithFilePresenter:self];
|
||||
|
||||
[coordinator coordinateReadingItemAtURL:self.fileURL options:NSFileCoordinatorReadingWithoutChanges error:nil byAccessor:^(NSURL * _Nonnull newURL) {
|
||||
NSDictionary *attributes = [NSFileManager.defaultManager attributesOfItemAtPath:newURL.path error:nil];
|
||||
if(NSOrderedSame == [attributes.fileModificationDate compare:self.fileModificationDate]) {
|
||||
return; // no content change
|
||||
}
|
||||
NSDictionary *attributes = [[NSFileManager defaultManager] attributesOfItemAtPath:self.fileURL.path error:nil];
|
||||
NSDate *modificationDate = attributes[NSFileModificationDate];
|
||||
if(NSOrderedSame == [self.fileModificationDate compare:modificationDate]) {
|
||||
return; // Just metadata has changed
|
||||
|
||||
if(self.encrypted) {
|
||||
__weak MPDocument *welf = self;
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
[welf _handleFileChangeWithStrategy:MPFileChangeStrategyUseOther setAsDefault:NO];
|
||||
});
|
||||
return; // Done!
|
||||
}
|
||||
|
||||
if(self.lockedForFileChange) {
|
||||
@@ -283,8 +288,6 @@ NSString *const MPDocumentGroupKey = @"MPDocumentGrou
|
||||
/* Set the flag in this call! */
|
||||
self.lockedForFileChange = YES;
|
||||
|
||||
/* TODO read file to check if changes took place! */
|
||||
|
||||
/* Dispatch the alert to the main queue */
|
||||
__weak MPDocument *welf = self;
|
||||
dispatch_async(dispatch_get_main_queue(), ^{
|
||||
@@ -321,9 +324,11 @@ NSString *const MPDocumentGroupKey = @"MPDocumentGrou
|
||||
welf.lockedForFileChange = NO;
|
||||
}];
|
||||
});
|
||||
}];
|
||||
}
|
||||
|
||||
- (void)_handleFileChangeWithStrategy:(MPFileChangeStrategy)strategy setAsDefault:(BOOL)setAsDefault {
|
||||
NSAssert(NSThread.currentThread.isMainThread, @"Invoke only on main thread!");
|
||||
if(setAsDefault) {
|
||||
[NSUserDefaults.standardUserDefaults setInteger:strategy forKey:kMPSettingsKeyFileChangeStrategy];
|
||||
}
|
||||
@@ -366,6 +371,12 @@ NSString *const MPDocumentGroupKey = @"MPDocumentGrou
|
||||
}
|
||||
else {
|
||||
[self.tree syncronizeWithTree:otherTree options:KPKSynchronizationSynchronizeOption];
|
||||
NSUserNotification *notification = [[NSUserNotification alloc] init];
|
||||
notification.title = NSApp.applicationName;
|
||||
notification.informativeText = NSLocalizedString(@"AUTO_MERGE_NOTIFICATION_TEXT", @"");
|
||||
notification.deliveryDate = NSDate.date;
|
||||
[NSUserNotificationCenter.defaultUserNotificationCenter scheduleNotification:notification];
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -403,7 +414,7 @@ NSString *const MPDocumentGroupKey = @"MPDocumentGrou
|
||||
}
|
||||
self.tree = nil;
|
||||
self.compositeKey = nil;
|
||||
[[NSNotificationCenter defaultCenter] postNotificationName:MPDocumentDidLockDatabaseNotification object:self];
|
||||
[NSNotificationCenter.defaultCenter postNotificationName:MPDocumentDidLockDatabaseNotification object:self];
|
||||
}
|
||||
|
||||
|
||||
@@ -417,7 +428,7 @@ NSString *const MPDocumentGroupKey = @"MPDocumentGrou
|
||||
/* only clear the data if we actually do not need it anymore */
|
||||
self.encryptedData = nil;
|
||||
self.unlockCount += 1;
|
||||
[[NSNotificationCenter defaultCenter] postNotificationName:MPDocumentDidUnlockDatabaseNotification object:self];
|
||||
[NSNotificationCenter.defaultCenter postNotificationName:MPDocumentDidUnlockDatabaseNotification object:self];
|
||||
[self _storeKeyURL:keyFileURL];
|
||||
}
|
||||
else {
|
||||
@@ -452,7 +463,7 @@ NSString *const MPDocumentGroupKey = @"MPDocumentGrou
|
||||
if(!delegate.isAllowedToStoreKeyFile) {
|
||||
return nil;
|
||||
}
|
||||
NSDictionary *keysForFiles = [[NSUserDefaults standardUserDefaults] dictionaryForKey:kMPSettingsKeyRememeberdKeysForDatabases];
|
||||
NSDictionary *keysForFiles = [NSUserDefaults.standardUserDefaults dictionaryForKey:kMPSettingsKeyRememeberdKeysForDatabases];
|
||||
NSString *keyPath = keysForFiles[self.fileURL.path.sha1HexDigest];
|
||||
if(!keyPath) {
|
||||
return nil;
|
||||
|
||||
Reference in New Issue
Block a user