From 56767216c607fbcb51e4572049fa079f4128bf5f Mon Sep 17 00:00:00 2001 From: michael starke Date: Fri, 28 Jun 2013 02:45:48 +0200 Subject: [PATCH] Undo/Redo for add/remove of Stringfields --- MacPass/MPDocument.h | 11 ++++--- MacPass/MPDocument.m | 48 +++++++++++++++++++++++------ MacPass/MPInspectorViewController.m | 10 +++--- MacPass/MacPass-Info.plist | 2 +- 4 files changed, 52 insertions(+), 19 deletions(-) diff --git a/MacPass/MPDocument.h b/MacPass/MPDocument.h index 671302f2..28139e86 100644 --- a/MacPass/MPDocument.h +++ b/MacPass/MPDocument.h @@ -24,9 +24,11 @@ APPKIT_EXTERN NSString *const MPDocumentGroupKey; @class KdbTree; @class Kdb4Tree; @class Kdb3Tree; +@class Kdb4Entry; @class UUID; @class Binary; @class BinaryRef; +@class StringField; @class MPRootAdapter; @interface MPDocument : NSDocument @@ -65,6 +67,7 @@ APPKIT_EXTERN NSString *const MPDocumentGroupKey; /* Undoable Intiialization of elements */ - (KdbGroup *)createGroup:(KdbGroup *)parent; - (KdbEntry *)createEntry:(KdbGroup *)parent; +- (StringField *)createStringField:(KdbEntry *)entry; /* All non-setter undoable actions @@ -74,12 +77,12 @@ APPKIT_EXTERN NSString *const MPDocumentGroupKey; - (BOOL)group:(KdbGroup *)group isMoveableToGroup:(KdbGroup *)target; - (void)moveEntry:(KdbEntry *)entry toGroup:(KdbGroup *)target index:(NSInteger)index; -- (void)group:(KdbGroup *)group addEntry:(KdbEntry *)entry; -- (void)group:(KdbGroup *)group addGroup:(KdbGroup *)aGroup; +- (void)group:(KdbGroup *)group addEntry:(KdbEntry *)entry atIndex:(NSUInteger)index; +- (void)group:(KdbGroup *)group addGroup:(KdbGroup *)aGroup atIndex:(NSUInteger)index; - (void)group:(KdbGroup *)group removeEntry:(KdbEntry *)entry; - (void)group:(KdbGroup *)group removeGroup:(KdbGroup *)aGroup; - - +- (void)entry:(Kdb4Entry *)entry addStringField:(StringField *)field atIndex:(NSUInteger)index; +- (void)entry:(Kdb4Entry *)entry removeStringField:(StringField *)field; @end diff --git a/MacPass/MPDocument.m b/MacPass/MPDocument.m index dd2a78b0..80aa327a 100644 --- a/MacPass/MPDocument.m +++ b/MacPass/MPDocument.m @@ -18,6 +18,7 @@ #import "KdbGroup+Undo.h" #import "KdbGroup+KVOAdditions.h" +#import "Kdb4Entry+KVOAdditions.h" #import "KdbGroup+MPTreeTools.h" #import "KdbEntry+Undo.h" #import "Kdb3Tree+NewTree.h" @@ -256,7 +257,7 @@ NSString *const MPDocumentGroupKey = @"MPDocumentGroupKey"; if(self.treeV4 && ([self.treeV4.defaultUserName length] > 0)) { newEntry.title = self.treeV4.defaultUserName; } - [self group:parent addEntry:newEntry]; + [self group:parent addEntry:newEntry atIndex:[parent.entries count]]; NSDictionary *userInfo = @{ MPDocumentEntryKey : newEntry }; [[NSNotificationCenter defaultCenter] postNotificationName:MPDocumentDidAddEntryNotification object:self userInfo:userInfo]; return newEntry; @@ -265,12 +266,24 @@ NSString *const MPDocumentGroupKey = @"MPDocumentGroupKey"; - (KdbGroup *)createGroup:(KdbGroup *)parent { KdbGroup *newGroup = [self.tree createGroup:parent]; newGroup.name = NSLocalizedString(@"DEFAULT_GROUP_NAME", @"Title for a newly created group"); - [self group:parent addGroup:newGroup]; + [self group:parent addGroup:newGroup atIndex:[parent.groups count]]; NSDictionary *userInfo = @{ MPDocumentGroupKey : newGroup }; [[NSNotificationCenter defaultCenter] postNotificationName:MPDocumentDidAddGroupNotification object:self userInfo:userInfo]; return newGroup; } +- (StringField *)createStringField:(KdbEntry *)entry { + // TODO: Localize! + if(![entry isKindOfClass:[Kdb4Entry class]]) { + return nil; + } + Kdb4Entry *entryV4 = (Kdb4Entry *)entry; + StringField *newStringField = [StringField stringFieldWithKey:@"Title" andValue:@"Value"]; + [self entry:entryV4 addStringField:newStringField atIndex:[entryV4.stringFields count]]; + return newStringField; +} + + - (void)moveGroup:(KdbGroup *)group toGroup:(KdbGroup *)target index:(NSInteger)index { NSInteger oldIndex = [group.parent.groups indexOfObject:group]; if(group.parent == target && oldIndex == index) { @@ -316,16 +329,16 @@ NSString *const MPDocumentGroupKey = @"MPDocumentGroupKey"; [target insertObject:entry inEntriesAtIndex:index]; } -- (void)group:(KdbGroup *)group addEntry:(KdbEntry *)entry { +- (void)group:(KdbGroup *)group addEntry:(KdbEntry *)entry atIndex:(NSUInteger)index { [[[self undoManager] prepareWithInvocationTarget:self] group:group removeEntry:entry]; [[self undoManager] setActionName:NSLocalizedString(@"UNDO_ADD_ENTRY", "Undo adding of entry")]; - [group insertObject:entry inEntriesAtIndex:[group.entries count]]; + [group insertObject:entry inEntriesAtIndex:index]; } -- (void)group:(KdbGroup *)group addGroup:(KdbGroup *)aGroup { +- (void)group:(KdbGroup *)group addGroup:(KdbGroup *)aGroup atIndex:(NSUInteger)index { [[[self undoManager] prepareWithInvocationTarget:self] group:group removeGroup:aGroup]; [[self undoManager] setActionName:NSLocalizedString(@"UNDO_ADD_GROUP", @"Create Group Undo")]; - [group insertObject:aGroup inGroupsAtIndex:[group.groups count]]; + [group insertObject:aGroup inGroupsAtIndex:index]; } - (void)group:(KdbGroup *)group removeEntry:(KdbEntry *)entry { @@ -333,7 +346,7 @@ NSString *const MPDocumentGroupKey = @"MPDocumentGroupKey"; if(NSNotFound == index) { return; // No object found; } - [[[self undoManager] prepareWithInvocationTarget:self] group:group addEntry:entry]; + [[[self undoManager] prepareWithInvocationTarget:self] group:group addEntry:entry atIndex:index]; [[self undoManager] setActionName:NSLocalizedString(@"UNDO_DELETE_ENTRY", "Undo deleting of entry")]; [group removeObjectFromEntriesAtIndex:index]; } @@ -343,11 +356,28 @@ NSString *const MPDocumentGroupKey = @"MPDocumentGroupKey"; if(NSNotFound == index) { return; // No object found } - [[[self undoManager] prepareWithInvocationTarget:self] group:group addGroup:aGroup]; - [[self undoManager] setActionName:NSLocalizedString(@"UNDO_DELETE_GROUP", @"Create Group Undo")]; + [[[self undoManager] prepareWithInvocationTarget:self] group:group addGroup:aGroup atIndex:index]; + [[self undoManager] setActionName:NSLocalizedString(@"UNDO_DELETE_GROUP", @"Delete Group Undo")]; [group removeObjectFromGroupsAtIndex:index]; } +- (void)entry:(Kdb4Entry *)entry addStringField:(StringField *)field atIndex:(NSUInteger)index { + [[[self undoManager] prepareWithInvocationTarget:self] entry:entry removeStringField:field]; + [[self undoManager] setActionName:NSLocalizedString(@"UNDO_ADD_STRING_FIELD", @"Add Stringfield Undo")]; + [entry insertObject:field inStringFieldsAtIndex:index]; +} + +- (void)entry:(Kdb4Entry *)entry removeStringField:(StringField *)field { + NSInteger index = [entry.stringFields indexOfObject:field]; + if(NSNotFound == index) { + return; // Nothing found to be removed + } + [[[self undoManager] prepareWithInvocationTarget:self] entry:entry addStringField:field atIndex:index]; + [[self undoManager] setActionName:NSLocalizedString(@"UNDO_DELETE_STRING_FIELD", @"Delte Stringfield undo")]; + [entry removeObjectFromStringFieldsAtIndex:index]; +} + + #pragma mark Private - (void)_cleanupLock { if(_didLockFile) { diff --git a/MacPass/MPInspectorViewController.m b/MacPass/MPInspectorViewController.m index 03e36d2a..8e82f4f2 100644 --- a/MacPass/MPInspectorViewController.m +++ b/MacPass/MPInspectorViewController.m @@ -303,14 +303,14 @@ enum { #pragma mark Actions - (IBAction)addCustomField:(id)sender { - Kdb4Entry *entry = (Kdb4Entry *)self.selectedEntry; - StringField *stringField = [StringField stringFieldWithKey:@"Key" andValue:@"Value"]; - [entry insertObject:stringField inStringFieldsAtIndex:[entry.stringFields count]]; + MPDocument *document = [[self windowController] document]; + [document createStringField:self.selectedEntry]; } - (IBAction)removeCustomField:(id)sender { - NSButton *button = sender; + MPDocument *document = [[self windowController] document]; + NSUInteger index = [sender tag]; Kdb4Entry *entry = (Kdb4Entry *)self.selectedEntry; - [entry removeObjectFromStringFieldsAtIndex:[button tag]]; + [document entry:entry removeStringField:[entry.stringFields objectAtIndex:index]]; } #pragma mark Notificiations diff --git a/MacPass/MacPass-Info.plist b/MacPass/MacPass-Info.plist index fab8894b..d909baef 100644 --- a/MacPass/MacPass-Info.plist +++ b/MacPass/MacPass-Info.plist @@ -48,7 +48,7 @@ CFBundleSignature ???? CFBundleVersion - 1407 + 1419 LSMinimumSystemVersion ${MACOSX_DEPLOYMENT_TARGET} NSHumanReadableCopyright