From 10556d6c0816633a0fe7e5bc215c63d09975d653 Mon Sep 17 00:00:00 2001 From: michael starke Date: Thu, 18 May 2017 12:10:49 +0200 Subject: [PATCH] extracted merge to function. Added action to merge arbitrary files --- MacPass/MPDocument.h | 1 + MacPass/MPDocument.m | 28 ++++++++++++++++++++++------ MacPass/MPDocumentWindowController.h | 1 + MacPass/MPDocumentWindowController.m | 14 ++++++++++++++ 4 files changed, 38 insertions(+), 6 deletions(-) diff --git a/MacPass/MPDocument.h b/MacPass/MPDocument.h index 37f4c18e..de4d36c0 100644 --- a/MacPass/MPDocument.h +++ b/MacPass/MPDocument.h @@ -150,6 +150,7 @@ FOUNDATION_EXPORT NSString *const MPDocumentGroupKey; - (void)writeXMLToURL:(NSURL *)url; - (void)readXMLfromURL:(NSURL *)url; +- (void)mergeWithContentsFromURL:(NSURL *)url; /* Undoable Intiialization of elements */ - (KPKGroup *)createGroup:(KPKGroup *)parent; diff --git a/MacPass/MPDocument.m b/MacPass/MPDocument.m index d779f5d0..7ccaedca 100644 --- a/MacPass/MPDocument.m +++ b/MacPass/MPDocument.m @@ -283,6 +283,7 @@ NSString *const MPDocumentGroupKey = @"MPDocumentGrou self.fileChangeDialogOpen = YES; /* Dispatch the alert to the main queue */ + __weak MPDocument *welf = self; dispatch_async(dispatch_get_main_queue(), ^{ NSAlert *alert = [[NSAlert alloc] init]; @@ -292,18 +293,16 @@ NSString *const MPDocumentGroupKey = @"MPDocumentGrou [alert addButtonWithTitle:NSLocalizedString(@"KEEP_MINE", @"Ignore the changes to an open file!")]; [alert addButtonWithTitle:NSLocalizedString(@"LOAD_CHANGES", @"Reopen the file!")]; [alert addButtonWithTitle:NSLocalizedString(@"MERGE_CHANGES", @"Merge changes into file!")]; - [alert beginSheetModalForWindow:self.windowForSheet completionHandler:^(NSModalResponse returnCode) { + [alert beginSheetModalForWindow:welf.windowForSheet completionHandler:^(NSModalResponse returnCode) { - self.fileChangeDialogOpen = NO; + welf.fileChangeDialogOpen = NO; switch(returnCode) { case NSAlertSecondButtonReturn: - [self revertToContentsOfURL:self.fileURL ofType:self.fileType error:nil]; + [welf revertToContentsOfURL:welf.fileURL ofType:welf.fileType error:nil]; break; case NSAlertThirdButtonReturn: { - KPKTree *otherTree = [[KPKTree alloc] initWithContentsOfUrl:self.fileURL key:self.compositeKey error:nil]; - [self.tree syncronizeWithTree:otherTree options:KPKSynchronizationSynchronizeOption]; - break; + [welf mergeWithContentsFromURL:self.fileURL]; } default: break; @@ -327,6 +326,23 @@ NSString *const MPDocumentGroupKey = @"MPDocumentGrou self.encryptedData = nil; } +- (void)mergeWithContentsFromURL:(NSURL *)url { + /* TODO read file to check what format to use */ + NSError *error; + KPKTree *otherTree = [[KPKTree alloc] initWithContentsOfUrl:url key:self.compositeKey error:&error]; + if(!otherTree) { + if(error.code == KPKErrorPasswordAndOrKeyfileWrong) { + [self presentError:error]; + } + else { + [self presentError:error]; + } + } + else { + [self.tree syncronizeWithTree:otherTree options:KPKSynchronizationSynchronizeOption]; + } +} + #pragma mark Lock/Unlock/Decrypt - (void)lockDatabase:(id)sender { diff --git a/MacPass/MPDocumentWindowController.h b/MacPass/MPDocumentWindowController.h index 2596a5c6..ab469986 100644 --- a/MacPass/MPDocumentWindowController.h +++ b/MacPass/MPDocumentWindowController.h @@ -43,6 +43,7 @@ - (IBAction)editTrashGroup:(id)sender; - (IBAction)exportAsXML:(id)sender; +- (IBAction)mergeWithOther:(id)sender; - (IBAction)importFromXML:(id)sender; - (IBAction)lock:(id)sender; diff --git a/MacPass/MPDocumentWindowController.m b/MacPass/MPDocumentWindowController.m index 8b509ce3..05dbc999 100644 --- a/MacPass/MPDocumentWindowController.m +++ b/MacPass/MPDocumentWindowController.m @@ -296,6 +296,20 @@ typedef void (^MPPasswordChangedBlock)(BOOL didChangePassword); }]; } +- (void)mergeWithOther:(id)sender { + NSOpenPanel *openPanel = [NSOpenPanel openPanel]; + MPDocument *document = self.document; + openPanel.allowsMultipleSelection = NO; + openPanel.canChooseDirectories = NO; + openPanel.canChooseFiles = YES; + //openPanel.allowedFileTypes = @[(id)kUTTypeXML]; + [openPanel beginSheetModalForWindow:self.window completionHandler:^(NSInteger result) { + if(result == NSFileHandlingPanelOKButton) { + [document mergeWithContentsFromURL:openPanel.URL]; + } + }]; +} + - (void)fixAutotype:(id)sender { if(!self.fixAutotypeWindowController) { self.fixAutotypeWindowController = [[MPFixAutotypeWindowController alloc] init];