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