From e1d2164267e1dc433989d358d0db1fa94dc4d4bf Mon Sep 17 00:00:00 2001 From: michael starke Date: Tue, 21 Oct 2014 19:02:51 +0200 Subject: [PATCH] Moving actions centralized to MPDocumentWindowController --- MacPass.xcodeproj/project.pbxproj | 2 + MacPass/MPAppDelegate.m | 1 + MacPass/MPDocument.m | 24 +++++++-- MacPass/MPEntryInspectorViewController.m | 4 +- MacPass/MPEntryViewController.h | 3 +- MacPass/MPEntryViewController.m | 66 ++++++++--------------- MacPass/MPTargetItemResolving.h | 4 ++ MacPass/en.lproj/Localizable.strings | Bin 10806 -> 11040 bytes 8 files changed, 56 insertions(+), 48 deletions(-) diff --git a/MacPass.xcodeproj/project.pbxproj b/MacPass.xcodeproj/project.pbxproj index 6ea366a6..9f132586 100644 --- a/MacPass.xcodeproj/project.pbxproj +++ b/MacPass.xcodeproj/project.pbxproj @@ -510,6 +510,7 @@ 4C26C34A18D8D5A300CF1A1C /* PreviewView.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = PreviewView.xib; sourceTree = ""; }; 4C2724D51778FF1A00FD8456 /* NSUUID+KeePassKit.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSUUID+KeePassKit.h"; sourceTree = ""; }; 4C2724D61778FF1A00FD8456 /* NSUUID+KeePassKit.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSUUID+KeePassKit.m"; sourceTree = ""; }; + 4C2B0B7419F66F6400E48913 /* MPTargetItemResolving.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MPTargetItemResolving.h; sourceTree = ""; }; 4C2C8B331787500E009649F3 /* UnprotectedWarningView.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = UnprotectedWarningView.xib; sourceTree = ""; }; 4C2E381A16D11FF900037A9D /* 03_ServerTemplate.pdf */ = {isa = PBXFileReference; lastKnownFileType = image.pdf; path = 03_ServerTemplate.pdf; sourceTree = ""; }; 4C2E381B16D11FF900037A9D /* 04_KlipperTemplate.pdf */ = {isa = PBXFileReference; lastKnownFileType = image.pdf; path = 04_KlipperTemplate.pdf; sourceTree = ""; }; @@ -1682,6 +1683,7 @@ children = ( 4CF6C715176F5183007A811D /* MPServerRequestHandling.h */, 4CA0B30D15BCB6FD00654E32 /* MPSettingsTab.h */, + 4C2B0B7419F66F6400E48913 /* MPTargetItemResolving.h */, ); name = Protocolls; sourceTree = ""; diff --git a/MacPass/MPAppDelegate.m b/MacPass/MPAppDelegate.m index 1223235d..2e062896 100644 --- a/MacPass/MPAppDelegate.m +++ b/MacPass/MPAppDelegate.m @@ -184,6 +184,7 @@ NSString *const MPDidChangeStoredKeyFilesSettings = @"com.hicknhack.macpass.MPDi self.passwordCreatorController.closeTarget = self.passwordCreatorWindow; NSView *creatorView = [_passwordCreatorController view]; [self.passwordCreatorWindow setContentView:creatorView]; + [self.passwordCreatorController updateResponderChain]; } [self.passwordCreatorController reset]; [self.passwordCreatorWindow makeKeyAndOrderFront:self.passwordCreatorWindow]; diff --git a/MacPass/MPDocument.m b/MacPass/MPDocument.m index 335db0a9..86f00418 100644 --- a/MacPass/MPDocument.m +++ b/MacPass/MPDocument.m @@ -32,6 +32,7 @@ #import "MPConstants.h" #import "MPSavePanelAccessoryViewController.h" #import "MPTreeDelegate.h" +#import "MPTargetItemResolving.h" #import "DDXMLNode.h" @@ -285,7 +286,7 @@ NSString *const MPDocumentGroupKey = @"MPDocumentGroupKey self.unlockCount += 1; [[NSNotificationCenter defaultCenter] postNotificationName:MPDocumentDidUnlockDatabaseNotification object:self]; /* Make sure to only store */ - MPAppDelegate *delegate = [NSApp delegate]; + MPAppDelegate *delegate = (MPAppDelegate *)[NSApp delegate]; if(self.compositeKey.hasKeyFile && self.compositeKey.hasPassword && delegate.isAllowedToStoreKeyFile) { [self _storeKeyURL:keyFileURL]; } @@ -315,7 +316,7 @@ NSString *const MPDocumentGroupKey = @"MPDocumentGroupKey } - (NSURL *)suggestedKeyURL { - MPAppDelegate *delegate = [NSApp delegate]; + MPAppDelegate *delegate = (MPAppDelegate *)[NSApp delegate]; if(!delegate.isAllowedToStoreKeyFile) { return nil; } @@ -611,6 +612,11 @@ NSString *const MPDocumentGroupKey = @"MPDocumentGroupKey } - (BOOL)validateUserInterfaceItem:(id)anItem { + id target = [NSApp targetForAction:@selector(targetItemForAction)]; + KPKNode *targetNode = [target targetItemForAction]; + KPKEntry *targetEntry = [targetNode asEntry]; + KPKGroup *targetGroup = [targetNode asGroup]; + if(self.encrypted || self.isReadOnly) { return NO; } BOOL valid = self.selectedItem ? self.selectedItem.isEditable : YES; @@ -647,6 +653,18 @@ NSString *const MPDocumentGroupKey = @"MPDocumentGroupKey break; case MPActionShowHistory: valid &= (self.selectedEntry && (self.selectedItem == (id)self.selectedEntry)); + break; + /* Entry View Actions */ + case MPActionCopyUsername: + valid &= (nil != targetEntry) && ([targetEntry.username length] > 0); + break; + case MPActionCopyPassword: + valid &= (nil != targetEntry ) && ([targetEntry.password length] > 0); + break; + case MPActionCopyURL: + case MPActionOpenURL: + valid &= (nil != targetEntry ) && ([targetEntry.url length] > 0); + break; default: break; } @@ -657,7 +675,7 @@ NSString *const MPDocumentGroupKey = @"MPDocumentGroupKey if(nil == keyURL) { return; // no URL to store in the first place } - MPAppDelegate *delegate = [NSApp delegate]; + MPAppDelegate *delegate = (MPAppDelegate *)[NSApp delegate]; NSAssert(delegate.isAllowedToStoreKeyFile, @"We can only store if we are allowed to do so!"); NSMutableDictionary *keysForFiles = [[[NSUserDefaults standardUserDefaults] dictionaryForKey:kMPSettingsKeyRememeberdKeysForDatabases] mutableCopy]; if(nil == keysForFiles) { diff --git a/MacPass/MPEntryInspectorViewController.m b/MacPass/MPEntryInspectorViewController.m index fcf33d53..60111a87 100644 --- a/MacPass/MPEntryInspectorViewController.m +++ b/MacPass/MPEntryInspectorViewController.m @@ -271,7 +271,9 @@ typedef NS_ENUM(NSUInteger, MPEntryTab) { - (IBAction)_popUpPasswordGenerator:(id)sender { [self.generatePasswordButton setEnabled:NO]; - [self _showPopopver:[[MPPasswordCreatorViewController alloc] init] atView:self.passwordTextField onEdge:NSMinYEdge]; + MPPasswordCreatorViewController *viewController = [[MPPasswordCreatorViewController alloc] init]; + viewController.allowsEntryDefaults = YES; + [self _showPopopver:viewController atView:self.passwordTextField onEdge:NSMinYEdge]; } - (void)_showPopopver:(NSViewController *)viewController atView:(NSView *)view onEdge:(NSRectEdge)edge { diff --git a/MacPass/MPEntryViewController.h b/MacPass/MPEntryViewController.h index a87446d6..0304047f 100644 --- a/MacPass/MPEntryViewController.h +++ b/MacPass/MPEntryViewController.h @@ -8,6 +8,7 @@ #import "MPViewController.h" #import "MPContextBarViewController.h" +#import "MPTargetItemResolving.h" APPKIT_EXTERN NSString *const MPEntryTableUserNameColumnIdentifier; APPKIT_EXTERN NSString *const MPEntryTableTitleColumnIdentifier; @@ -31,7 +32,7 @@ typedef NS_ENUM( NSUInteger, MPCopyContentTypeTag) { @class MPDocumentWindowController; @class MPDocument; -@interface MPEntryViewController : MPViewController +@interface MPEntryViewController : MPViewController @property (weak,readonly) NSTableView *entryTable; @property (readonly, strong) NSArrayController *entryArrayController; diff --git a/MacPass/MPEntryViewController.m b/MacPass/MPEntryViewController.m index 3036e7c0..17741dfc 100644 --- a/MacPass/MPEntryViewController.m +++ b/MacPass/MPEntryViewController.m @@ -70,6 +70,7 @@ NSString *const _MPTableStringCellView = @"StringCell"; NSString *const _MPTableSecurCellView = @"PasswordCell"; @interface MPEntryViewController () { + /* TODO unify delegation */ MPEntryContextMenuDelegate *_menuDelegate; BOOL _isDisplayingContextBar; BOOL _didUnlock; @@ -170,7 +171,7 @@ NSString *const _MPTableSecurCellView = @"PasswordCell"; [self.entryTable setAutosaveTableColumns:YES]; NSString *parentNameKeyPath = [[NSString alloc] initWithFormat:@"%@.%@", NSStringFromSelector(@selector(parent)), NSStringFromSelector(@selector(name))]; - NSSortDescriptor *titleColumSortDescriptor = [NSSortDescriptor sortDescriptorWithKey:NSStringFromSelector(@selector(title))ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)]; + NSSortDescriptor *titleColumSortDescriptor = [NSSortDescriptor sortDescriptorWithKey:NSStringFromSelector(@selector(title))ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)]; NSSortDescriptor *userNameSortDescriptor = [NSSortDescriptor sortDescriptorWithKey:NSStringFromSelector(@selector(username)) ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)]; NSSortDescriptor *urlSortDescriptor = [NSSortDescriptor sortDescriptorWithKey:NSStringFromSelector(@selector(url)) ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)]; NSSortDescriptor *groupnameSortDescriptor = [NSSortDescriptor sortDescriptorWithKey:parentNameKeyPath ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)]; @@ -338,6 +339,19 @@ NSString *const _MPTableSecurCellView = @"PasswordCell"; } } +#pragma mark MPTargetItemResolving +- (KPKNode *)targetItemForAction { + NSInteger activeRow = [self.entryTable clickedRow]; + /* Fallback to selection e.g. for toolbar actions */ + if(activeRow < 0 ) { + activeRow = [self.entryTable selectedRow]; + } + if(activeRow >= 0 && activeRow <= [[self.entryArrayController arrangedObjects] count]) { + return [self.entryArrayController arrangedObjects][activeRow]; + } + return nil; +} + #pragma mark MPDocument Notifications - (void)_didChangeCurrentItem:(NSNotification *)notification { MPDocument *document = [notification object]; @@ -535,28 +549,9 @@ NSString *const _MPTableSecurCellView = @"PasswordCell"; #pragma mark Validation - (BOOL)validateMenuItem:(NSMenuItem *)menuItem { + /* Validation is soley handeld in the document */ MPDocument *document = [[self windowController] document]; - if(![document validateMenuItem:menuItem]) { - return NO; - } - - KPKEntry *targetEntry = [self _clickedOrSelectedEntry]; - MPActionType actionType = [MPActionHelper typeForAction:[menuItem action]]; - - switch (actionType) { - case MPActionCopyUsername: - return [targetEntry.username length] > 0; - - case MPActionCopyPassword: - return [targetEntry.password length] > 0; - - case MPActionCopyURL: - case MPActionOpenURL: - return [targetEntry.url length] > 0; - - default: - return YES; - } + return [document validateMenuItem:menuItem]; } #pragma mark ContextMenu @@ -600,38 +595,23 @@ NSString *const _MPTableSecurCellView = @"PasswordCell"; [[self.entryTable headerView] setMenu:headerMenu]; } - -#pragma mark Action Helper - -- (KPKEntry *)_clickedOrSelectedEntry { - NSInteger activeRow = [self.entryTable clickedRow]; - /* Fallback to selection e.g. for toolbar actions */ - if(activeRow < 0 ) { - activeRow = [self.entryTable selectedRow]; - } - if(activeRow >= 0 && activeRow <= [[self.entryArrayController arrangedObjects] count]) { - return [self.entryArrayController arrangedObjects][activeRow]; - } - return nil; -} - #pragma mark Actions - (void)copyPassword:(id)sender { - KPKEntry *selectedEntry = [self _clickedOrSelectedEntry]; + KPKEntry *selectedEntry = [[self targetItemForAction] asEntry]; if(selectedEntry) { [self _copyToPasteboard:[selectedEntry.password finalValueForEntry:selectedEntry] overlayInfo:MPOverlayInfoPassword name:nil]; } } - (void)copyUsername:(id)sender { - KPKEntry *selectedEntry = [self _clickedOrSelectedEntry]; + KPKEntry *selectedEntry = [[self targetItemForAction] asEntry]; if(selectedEntry) { [self _copyToPasteboard:[selectedEntry.username finalValueForEntry:selectedEntry] overlayInfo:MPOverlayInfoUsername name:nil]; } } - (void)copyCustomAttribute:(id)sender { - KPKEntry *selectedEntry = [self _clickedOrSelectedEntry]; + KPKEntry *selectedEntry = [[self targetItemForAction] asEntry]; if(selectedEntry && [selectedEntry isKindOfClass:[KPKEntry class]]) { NSUInteger index = [sender tag]; NSAssert((index >= 0) && (index < [selectedEntry.customAttributes count]), @"Index for custom field needs to be valid"); @@ -641,14 +621,14 @@ NSString *const _MPTableSecurCellView = @"PasswordCell"; } - (void)copyURL:(id)sender { - KPKEntry *selectedEntry = [self _clickedOrSelectedEntry]; + KPKEntry *selectedEntry = [[self targetItemForAction] asEntry]; if(selectedEntry) { [self _copyToPasteboard:[selectedEntry.url finalValueForEntry:selectedEntry] overlayInfo:MPOverlayInfoURL name:nil]; } } - (void)openURL:(id)sender { - KPKEntry *selectedEntry = [self _clickedOrSelectedEntry]; + KPKEntry *selectedEntry = [[self targetItemForAction] asEntry]; if(selectedEntry && [selectedEntry.url length] > 0) { NSURL *webURL = [NSURL URLWithString:[selectedEntry.url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; NSString *scheme = [webURL scheme]; @@ -670,7 +650,7 @@ NSString *const _MPTableSecurCellView = @"PasswordCell"; } - (void)delete:(id)sender { - KPKEntry *entry =[self _clickedOrSelectedEntry]; + KPKEntry *entry = [[self targetItemForAction] asEntry]; if(!entry) { return; } diff --git a/MacPass/MPTargetItemResolving.h b/MacPass/MPTargetItemResolving.h index 6d68df0a..8a6f9733 100644 --- a/MacPass/MPTargetItemResolving.h +++ b/MacPass/MPTargetItemResolving.h @@ -7,7 +7,11 @@ // #import +@class KPKNode; @protocol MPTargetItemResolving +@required +- (KPKNode *)targetItemForAction; + @end diff --git a/MacPass/en.lproj/Localizable.strings b/MacPass/en.lproj/Localizable.strings index 6d7f7e5d1a2ece28e7a5f78dacd819b025c40bbb..64e4106f94f01e4767efdd9bdbdf736abb6a3f47 100644 GIT binary patch delta 176 zcmdlMvLI~3E>2k&23H0*21kZa1|NnHhIj^dAm0y&gMeZ_lQ*h}x(0#eTp5%Y6c}uQ zScxH+A(f#7$Z}`MVaR7lVn_ti3Jfkl(KH~t6v!(9s4CF3!zMxM~ytCtn3|