diff --git a/MacPass/MPActionHelper.h b/MacPass/MPActionHelper.h index 0fc89a62..1114c0c5 100644 --- a/MacPass/MPActionHelper.h +++ b/MacPass/MPActionHelper.h @@ -20,6 +20,7 @@ typedef NS_ENUM(NSUInteger, MPActionType) { MPActionToggleInspector, MPActionLock, // show the lock screen MPActionEmptyTrash, // empties the trashcan, if there is one + MPActionEditPassword, // change the database password MPActionDatabaseSettings, // Show the settings for the database MPActionEditTemplateGroup }; diff --git a/MacPass/MPActionHelper.m b/MacPass/MPActionHelper.m index 9251751b..7d4dc1e2 100644 --- a/MacPass/MPActionHelper.m +++ b/MacPass/MPActionHelper.m @@ -21,6 +21,7 @@ @(MPActionCopyURL) : @"copyURL:", @(MPActionCopyUsername) : @"copyUsername:", @(MPActionDelete) : @"deleteNode:", + @(MPActionEditPassword) : @"editPassword:", @(MPActionOpenURL) : @"openURL:", @(MPActionToggleInspector) : @"toggleInspector:", @(MPActionLock) : @"lock:", diff --git a/MacPass/MPDocument.m b/MacPass/MPDocument.m index 16afc23a..baf76d29 100644 --- a/MacPass/MPDocument.m +++ b/MacPass/MPDocument.m @@ -509,14 +509,46 @@ typedef NS_ENUM(NSUInteger, MPAlertType) { return; } +- (BOOL)validateMenuItem:(NSMenuItem *)menuItem { + return [self validateUserInterfaceItem:menuItem]; +} + +- (BOOL)validateToolbarItem:(NSToolbarItem *)theItem { + return [self validateUserInterfaceItem:theItem]; +} + - (BOOL)validateUserInterfaceItem:(id)anItem { - if([anItem action] == [MPActionHelper actionOfType:MPActionEmptyTrash]) { - BOOL hasGroups = [self.trash.groups count] > 0; - BOOL hasEntries = [self.trash.entries count] > 0; - return (hasEntries || hasGroups); + if(self.encrypted || self.isReadOnly) { return NO; } + + BOOL valid = YES; + switch([MPActionHelper typeForAction:[anItem action]]) { + case MPActionAddGroup: + valid &= (nil != self.selectedGroup); + // fall-through + case MPActionAddEntry: + // fall-through + case MPActionDelete: { + valid &= (nil != self.selectedItem); + valid &= (self.trash != self.selectedItem); + valid &= ![self isItemTrashed:self.selectedItem]; + break; + } + case MPActionEmptyTrash: { + valid &= [self.trash.groups count] > 0; + valid &= [self.trash.entries count] > 0; + break; + } + case MPActionDatabaseSettings: + case MPActionEditPassword: + valid &= !self.encrypted; + break; + case MPActionLock: + valid &= self.compositeKey.hasPasswordOrKeyFile; + break; + default: + valid = YES; } - - return [super validateUserInterfaceItem:anItem]; + return valid; } - (void)_storeKeyURL:(NSURL *)keyURL { diff --git a/MacPass/MPDocumentWindowController.h b/MacPass/MPDocumentWindowController.h index 78a0501a..efbb4d16 100644 --- a/MacPass/MPDocumentWindowController.h +++ b/MacPass/MPDocumentWindowController.h @@ -22,13 +22,6 @@ @property (readonly, strong) MPOutlineViewController *outlineViewController; @property (readonly, strong) MPInspectorViewController *inspectorViewController; -/** - @param action The action that should be validatet - @param item The item that the action affects. Pass nil to fall back for default item - @returns YES if the action is valid, NO otherwise - */ -- (BOOL)validateAction:(SEL)action forItem:(id)item; - - (void)showEntries; - (void)showPasswordInput; - (void)performFindPanelAction:(id)sender; diff --git a/MacPass/MPDocumentWindowController.m b/MacPass/MPDocumentWindowController.m index e8c277e5..d19fb838 100644 --- a/MacPass/MPDocumentWindowController.m +++ b/MacPass/MPDocumentWindowController.m @@ -206,80 +206,6 @@ typedef NS_ENUM(NSUInteger, MPAlertContext) { [self.entryViewController showFilter:sender]; } -- (BOOL)validateMenuItem:(NSMenuItem *)menuItem { - MPDocument *document = [self document]; - SEL itemAction = [menuItem action]; - if(itemAction == @selector(showDatabaseSettings:) - || itemAction == @selector(editPassword:)) { - return !document.encrypted; - } - - BOOL enabled = YES; - if(itemAction == [MPActionHelper actionOfType:MPActionDelete]) { - enabled &= (nil != document.selectedItem) && (document.selectedItem != document.trash); - } - - enabled &= !( document.encrypted || document.isReadOnly ); - return enabled; -} - -- (BOOL)validateToolbarItem:(NSToolbarItem *)theItem { - MPDocument *document = [self document]; - if(document.encrypted || document.isReadOnly) { - return NO; - } - MPActionType actionType = [MPActionHelper typeForAction:[theItem action]]; - switch (actionType) { - case MPActionAddGroup: - case MPActionAddEntry: - return (nil != document.selectedGroup); - case MPActionDelete: { - BOOL valid = (nil != document.selectedItem); - valid &= (document.selectedItem != document.trash); - valid &= ![document isItemTrashed:document.selectedItem]; - return valid; - } - case MPActionLock: - return document.compositeKey.hasPasswordOrKeyFile; - - case MPActionToggleInspector: - return (nil != [_splitView superview]); - - default: - return YES; - } - return YES; -} - -- (BOOL)validateAction:(SEL)action forItem:(id)item { - MPDocument *document = [self document]; - if(document.encrypted || document.isReadOnly) { - return NO; - } - MPActionType actionType = [MPActionHelper typeForAction:action]; - switch (actionType) { - case MPActionAddGroup: - case MPActionAddEntry: - // test if Group is in trash - return (nil != document.selectedGroup); - case MPActionDelete: { - BOOL valid = (nil != document.selectedItem); - valid &= (document.selectedItem != document.trash); - valid &= ![document isItemTrashed:document.selectedItem]; - return valid; - } - case MPActionLock: - return document.compositeKey.hasPasswordOrKeyFile; - - case MPActionToggleInspector: - return (nil != [_splitView superview]); - - default: - return YES; - } - return YES; -} - - (void)showPasswordInput { if(!self.passwordInputController) { self.passwordInputController = [[MPPasswordInputController alloc] init]; diff --git a/MacPass/MPEntryViewController.m b/MacPass/MPEntryViewController.m index e77ef701..16dd5042 100644 --- a/MacPass/MPEntryViewController.m +++ b/MacPass/MPEntryViewController.m @@ -676,11 +676,6 @@ NSString *const _MPTAbleSecurCellView = @"PasswordCell"; // // //} -#pragma mark Validation -- (BOOL)validateMenuItem:(NSMenuItem *)menuItem { - return YES; -} - - (IBAction)_toggleFilterSpace:(id)sender { if(![sender isKindOfClass:[NSButton class]]) { return; // Wrong sender diff --git a/MacPass/MPOutlineViewController.m b/MacPass/MPOutlineViewController.m index 1f061a64..0cde82d4 100644 --- a/MacPass/MPOutlineViewController.m +++ b/MacPass/MPOutlineViewController.m @@ -158,29 +158,6 @@ NSString *const _MPOutlinveViewHeaderViewIdentifier = @"HeaderCell"; return [[self.outlineView itemAtRow:row] representedObject]; } -#pragma mark Validation - -- (BOOL)validateMenuItem:(NSMenuItem *)menuItem { - MPActionType actionType = [MPActionHelper typeForAction:[menuItem action]]; - switch(actionType) { - case MPActionAddEntry: - case MPActionAddGroup: - case MPActionDelete: { - MPDocument *document = [[self windowController] document]; - id selected = [self _clickedOrSelectedGroup]; - if(!selected) { - return NO; - } - if(selected == document.trash) { - return NO; - } - return ![document isItemTrashed:selected]; - } - default: - return YES; // We are only validated for three targets - } -} - #pragma mark - #pragma mark Actions