mirror of
https://github.com/MacPass/MacPass.git
synced 2026-01-30 15:38:15 +00:00
centralize all the UI validation in MPDocument
This commit is contained in:
@@ -509,12 +509,40 @@ typedef NS_ENUM(NSUInteger, MPAlertType) {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
- (BOOL)validateMenuItem:(NSMenuItem *)menuItem {
|
||||||
|
return [self validateUserInterfaceItem:menuItem];
|
||||||
|
}
|
||||||
|
|
||||||
|
- (BOOL)validateToolbarItem:(NSToolbarItem *)theItem {
|
||||||
|
return [self validateUserInterfaceItem:theItem];
|
||||||
|
}
|
||||||
|
|
||||||
- (BOOL)validateUserInterfaceItem:(id<NSValidatedUserInterfaceItem>)anItem {
|
- (BOOL)validateUserInterfaceItem:(id<NSValidatedUserInterfaceItem>)anItem {
|
||||||
if([anItem action] == [MPActionHelper actionOfType:MPActionEmptyTrash]) {
|
if(self.encrypted || self.isReadOnly) { return NO; }
|
||||||
|
|
||||||
|
switch([MPActionHelper typeForAction:[anItem action]]) {
|
||||||
|
case MPActionDelete: {
|
||||||
|
BOOL valid = (nil != self.selectedItem);
|
||||||
|
valid &= (self.selectedItem != self.trash);
|
||||||
|
valid &= ![self isItemTrashed:self.selectedItem];
|
||||||
|
return valid;
|
||||||
|
}
|
||||||
|
case MPActionEmptyTrash: {
|
||||||
BOOL hasGroups = [self.trash.groups count] > 0;
|
BOOL hasGroups = [self.trash.groups count] > 0;
|
||||||
BOOL hasEntries = [self.trash.entries count] > 0;
|
BOOL hasEntries = [self.trash.entries count] > 0;
|
||||||
return (hasEntries || hasGroups);
|
return (hasEntries || hasGroups);
|
||||||
}
|
}
|
||||||
|
case MPActionDatabaseSettings:
|
||||||
|
case MPActionEditPassword:
|
||||||
|
return !self.encrypted;
|
||||||
|
case MPActionAddGroup:
|
||||||
|
case MPActionAddEntry:
|
||||||
|
return (nil != self.selectedGroup);
|
||||||
|
case MPActionLock:
|
||||||
|
return self.compositeKey.hasPasswordOrKeyFile;
|
||||||
|
default:
|
||||||
|
return YES;
|
||||||
|
}
|
||||||
|
|
||||||
return [super validateUserInterfaceItem:anItem];
|
return [super validateUserInterfaceItem:anItem];
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,13 +22,6 @@
|
|||||||
@property (readonly, strong) MPOutlineViewController *outlineViewController;
|
@property (readonly, strong) MPOutlineViewController *outlineViewController;
|
||||||
@property (readonly, strong) MPInspectorViewController *inspectorViewController;
|
@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)showEntries;
|
||||||
- (void)showPasswordInput;
|
- (void)showPasswordInput;
|
||||||
- (void)performFindPanelAction:(id)sender;
|
- (void)performFindPanelAction:(id)sender;
|
||||||
|
|||||||
@@ -206,80 +206,6 @@ typedef NS_ENUM(NSUInteger, MPAlertContext) {
|
|||||||
[self.entryViewController showFilter:sender];
|
[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 {
|
- (void)showPasswordInput {
|
||||||
if(!self.passwordInputController) {
|
if(!self.passwordInputController) {
|
||||||
self.passwordInputController = [[MPPasswordInputController alloc] init];
|
self.passwordInputController = [[MPPasswordInputController alloc] init];
|
||||||
|
|||||||
Reference in New Issue
Block a user