From a7a28063f0806ee5c979ee025edc930de58a2d30 Mon Sep 17 00:00:00 2001 From: michael starke Date: Fri, 26 Aug 2016 16:23:08 +0200 Subject: [PATCH] Changes on entries now get pushed to the history --- MacPass/MPDocument.h | 8 ---- MacPass/MPDocument.m | 34 ++++----------- MacPass/MPInspectorViewController.h | 3 -- MacPass/MPInspectorViewController.m | 64 ++++++++++++++--------------- MacPass/MPViewController.h | 3 ++ MacPass/MPViewController.m | 6 ++- 6 files changed, 48 insertions(+), 70 deletions(-) diff --git a/MacPass/MPDocument.h b/MacPass/MPDocument.h index 3dee9ac9..6cc32388 100644 --- a/MacPass/MPDocument.h +++ b/MacPass/MPDocument.h @@ -167,14 +167,6 @@ APPKIT_EXTERN NSString *const MPDocumentGroupKey; - (IBAction)duplicateEntryWithOptions:(id)sender; - -#pragma mark - -#pragma mark Editing - -- (void)willChangeEntry:(KPKEntry *)entry; -- (void)commitChangesToEntry:(KPKEntry *)entry; -- (void)discardChangesToEntry:(KPKEntry *)entry; - @end @interface MPDocument (Attachments) diff --git a/MacPass/MPDocument.m b/MacPass/MPDocument.m index 2f76d89a..d3a5666b 100644 --- a/MacPass/MPDocument.m +++ b/MacPass/MPDocument.m @@ -39,18 +39,18 @@ #import "NSString+MPPasswordCreation.h" #import "NSString+MPHash.h" -NSString *const MPDocumentDidAddGroupNotification = @"com.hicknhack.macpass.MPDocumentDidAddGroupNotification"; -NSString *const MPDocumentDidAddEntryNotification = @"com.hicknhack.macpass.MPDocumentDidAddEntryNotification"; +NSString *const MPDocumentDidAddGroupNotification = @"com.hicknhack.macpass.MPDocumentDidAddGroupNotification"; +NSString *const MPDocumentDidAddEntryNotification = @"com.hicknhack.macpass.MPDocumentDidAddEntryNotification"; -NSString *const MPDocumentDidRevertNotifiation = @"com.hicknhack.macpass.MPDocumentDidRevertNotifiation"; +NSString *const MPDocumentDidRevertNotifiation = @"com.hicknhack.macpass.MPDocumentDidRevertNotifiation"; -NSString *const MPDocumentDidLockDatabaseNotification = @"com.hicknhack.macpass.MPDocumentDidLockDatabaseNotification"; -NSString *const MPDocumentDidUnlockDatabaseNotification = @"com.hicknhack.macpass.MPDocumentDidUnlockDatabaseNotification"; +NSString *const MPDocumentDidLockDatabaseNotification = @"com.hicknhack.macpass.MPDocumentDidLockDatabaseNotification"; +NSString *const MPDocumentDidUnlockDatabaseNotification = @"com.hicknhack.macpass.MPDocumentDidUnlockDatabaseNotification"; -NSString *const MPDocumentCurrentItemChangedNotification = @"com.hicknhack.macpass.MPDocumentCurrentItemChangedNotification"; +NSString *const MPDocumentCurrentItemChangedNotification = @"com.hicknhack.macpass.MPDocumentCurrentItemChangedNotification"; -NSString *const MPDocumentEntryKey = @"MPDocumentEntryKey"; -NSString *const MPDocumentGroupKey = @"MPDocumentGroupKey"; +NSString *const MPDocumentEntryKey = @"MPDocumentEntryKey"; +NSString *const MPDocumentGroupKey = @"MPDocumentGroupKey"; @interface MPDocument () { @private @@ -77,8 +77,6 @@ NSString *const MPDocumentGroupKey = @"MPDocumentGroupKey @property (assign) BOOL fileChangeDialogOpen; -@property (strong) NSMutableDictionary *modifiedEntries; - @end @implementation MPDocument @@ -771,22 +769,6 @@ NSString *const MPDocumentGroupKey = @"MPDocumentGroupKey [self.trash clear]; } -- (void)willChangeEntry:(KPKEntry *)entry { - /* we store a copy of the entry */ - NSAssert(nil == self.modifiedEntries[entry.uuid], @"Inconsistent state, pending changes present for entry!"); - self.modifiedEntries[entry.uuid] = [entry copy]; -} - -- (void)discardChangesToEntry:(KPKEntry *)entry { - self.modifiedEntries[entry.uuid] = nil; - /* TODO KeePassKit copy entry with info from old entry */ -} - -- (void)commitChangesToEntry:(KPKEntry *)entry { - /* TODO KeePassKit entry pushHistory:self.modifiedEntries[entry.uuid] */ - [self discardChangesToEntry:entry]; -} - #pragma mark - #pragma mark MPTargetNodeResolving diff --git a/MacPass/MPInspectorViewController.h b/MacPass/MPInspectorViewController.h index 80757f28..10b00a0d 100644 --- a/MacPass/MPInspectorViewController.h +++ b/MacPass/MPInspectorViewController.h @@ -24,9 +24,6 @@ - (IBAction)pickIcon:(id)sender; - (IBAction)pickExpiryDate:(id)sender; -- (IBAction)saveChanges:(id)sender; -- (IBAction)discardChanges:(id)sender; - /* Separate call to ensure all registered objects are in place */ - (void)registerNotificationsForDocument:(NSDocument *)document; diff --git a/MacPass/MPInspectorViewController.m b/MacPass/MPInspectorViewController.m index fe154ca8..9fbe372c 100644 --- a/MacPass/MPInspectorViewController.m +++ b/MacPass/MPInspectorViewController.m @@ -46,6 +46,8 @@ typedef NS_ENUM(NSUInteger, MPContentTab) { @property (weak) IBOutlet NSSplitView *splitView; @property (unsafe_unretained) IBOutlet NSTextView *notesTextView; +@property BOOL didPushHistory; + @end @implementation MPInspectorViewController @@ -60,6 +62,16 @@ typedef NS_ENUM(NSUInteger, MPContentTab) { self.activeTab = MPEmptyTab; self.entryViewController = [[MPEntryInspectorViewController alloc] init]; self.groupViewController = [[MPGroupInspectorViewController alloc] init]; + self.didPushHistory = NO; + /* subviewcontrollers will notify us about a change so we can handle the history pushing */ + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(_willChangeValueForRepresentedObjectNotification:) + name:MPViewControllerWillChangeValueForRepresentedObjectKeyPathNotification + object:self.entryViewController]; + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(_willChangeValueForRepresentedObjectNotification:) + name:MPViewControllerWillChangeValueForRepresentedObjectKeyPathNotification + object:self.groupViewController]; } return self; } @@ -185,18 +197,6 @@ typedef NS_ENUM(NSUInteger, MPContentTab) { } -- (void)saveChanges:(id)sender { - MPDocument *document = self.windowController.document; - KPKEntry *entry = self.representedObject; - [document commitChangesToEntry:entry]; -} - -- (void)discardChanges:(id)sender { - MPDocument *document = self.windowController.document; - KPKEntry *entry = self.representedObject; - [document discardChangesToEntry:entry]; -} - #pragma mark - #pragma mark NSPopover Delegate @@ -207,31 +207,28 @@ typedef NS_ENUM(NSUInteger, MPContentTab) { #pragma mark - #pragma mark Bindings -- (void)_establishBindings { - -// [self.itemImageView bind:NSValueBinding -// toObject:self -// withKeyPath:[NSString stringWithFormat:@"%@.%@", NSStringFromSelector(@selector(representedObject)), NSStringFromSelector(@selector(iconImage))] -// options:nil]; -// [self.notesTextView bind:NSValueBinding -// toObject:self -// withKeyPath:[NSString stringWithFormat:@"%@.%@", NSStringFromSelector(@selector(representedObject)), NSStringFromSelector(@selector(notes))] -// options:@{ NSNullPlaceholderBindingOption: NSLocalizedString(@"NONE", "")}]; -// [self.itemNameTextField bind:NSValueBinding -// toObject:self -// withKeyPath:[NSString stringWithFormat:@"%@.%@", NSStringFromSelector(@selector(representedObject)), NSStringFromSelector(@selector(title))] -// options:@{NSNullPlaceholderBindingOption: NSLocalizedString(@"NONE", "")}]; -} - (void)willChangeValueForRepresentedObjectKeyPath:(NSString *)keyPath { - MPDocument *document = self.windowController.document; - KPKEntry *entry = [self.representedObject asEntry]; - if(entry) { - [document willChangeEntry:entry]; - } + [self _recordChangesForCurrentNode]; } +#pragma mark - +#pragma mark MPViewController Notifications +- (void)_willChangeValueForRepresentedObjectNotification:(NSNotification *)notification { + [self _recordChangesForCurrentNode]; +} +- (void)_recordChangesForCurrentNode { + /* TODO use uuids for pushed item? */ + if(self.didPushHistory) { + return; + } + KPKEntry *entry = [self.representedObject asEntry]; + if( entry ) { + [entry pushHistory]; + self.didPushHistory = YES; + } +} #pragma mark - #pragma mark MPDocument Notifications @@ -248,9 +245,12 @@ typedef NS_ENUM(NSUInteger, MPContentTab) { else { self.activeTab = MPEmptyTab; } + self.didPushHistory = NO; + self.representedObject = node; self.entryViewController.representedObject = node.asEntry; self.groupViewController.representedObject = node.asGroup; + } @end diff --git a/MacPass/MPViewController.h b/MacPass/MPViewController.h index 4a2eda74..ec896236 100644 --- a/MacPass/MPViewController.h +++ b/MacPass/MPViewController.h @@ -10,6 +10,9 @@ @interface MPViewController : NSViewController +APPKIT_EXTERN NSString *const MPViewControllerWillChangeValueForRepresentedObjectKeyPathNotification; +APPKIT_EXTERN NSString *const MPViewControllerDidChangeValueForRepresentedObjectKeyPathNotification; + @property (nonatomic, readonly) NSWindowController *windowController; - (void)didLoadView; diff --git a/MacPass/MPViewController.m b/MacPass/MPViewController.m index df15aa35..0a9af771 100644 --- a/MacPass/MPViewController.m +++ b/MacPass/MPViewController.m @@ -7,7 +7,9 @@ // #import "MPViewController.h" -#import "MPDocument.h" + +NSString *const MPViewControllerWillChangeValueForRepresentedObjectKeyPathNotification = @"com.hicknhack.macpass.MPViewControllerWillChangeValueForRepresentedObjectKeyPathNotification"; +NSString *const MPViewControllerDidChangeValueForRepresentedObjectKeyPathNotification = @"comt.hicknhack.macpass.MPViewControllerDidChangeValueForRepresentedObjectKeyPathNotification"; @implementation MPViewController @@ -41,8 +43,10 @@ #pragma mark Binding observation - (void)setValue:(id)value forKeyPath:(NSString *)keyPath { if([keyPath hasPrefix:@"representedObject."]) { + [[NSNotificationCenter defaultCenter] postNotificationName:MPViewControllerWillChangeValueForRepresentedObjectKeyPathNotification object:self]; [self willChangeValueForRepresentedObjectKeyPath:keyPath]; [super setValue:value forKeyPath:keyPath]; + [[NSNotificationCenter defaultCenter] postNotificationName:MPViewControllerDidChangeValueForRepresentedObjectKeyPathNotification object:self]; [self didChangeValueForRepresentedObjectKeyPath:keyPath]; } else {