Moving actions centralized to MPDocumentWindowController

This commit is contained in:
michael starke
2014-10-21 19:02:51 +02:00
parent c2def5653e
commit e1d2164267
8 changed files with 56 additions and 48 deletions

View File

@@ -510,6 +510,7 @@
4C26C34A18D8D5A300CF1A1C /* PreviewView.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = PreviewView.xib; sourceTree = "<group>"; }; 4C26C34A18D8D5A300CF1A1C /* PreviewView.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = PreviewView.xib; sourceTree = "<group>"; };
4C2724D51778FF1A00FD8456 /* NSUUID+KeePassKit.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSUUID+KeePassKit.h"; sourceTree = "<group>"; }; 4C2724D51778FF1A00FD8456 /* NSUUID+KeePassKit.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "NSUUID+KeePassKit.h"; sourceTree = "<group>"; };
4C2724D61778FF1A00FD8456 /* NSUUID+KeePassKit.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSUUID+KeePassKit.m"; sourceTree = "<group>"; }; 4C2724D61778FF1A00FD8456 /* NSUUID+KeePassKit.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "NSUUID+KeePassKit.m"; sourceTree = "<group>"; };
4C2B0B7419F66F6400E48913 /* MPTargetItemResolving.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MPTargetItemResolving.h; sourceTree = "<group>"; };
4C2C8B331787500E009649F3 /* UnprotectedWarningView.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = UnprotectedWarningView.xib; sourceTree = "<group>"; }; 4C2C8B331787500E009649F3 /* UnprotectedWarningView.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = UnprotectedWarningView.xib; sourceTree = "<group>"; };
4C2E381A16D11FF900037A9D /* 03_ServerTemplate.pdf */ = {isa = PBXFileReference; lastKnownFileType = image.pdf; path = 03_ServerTemplate.pdf; sourceTree = "<group>"; }; 4C2E381A16D11FF900037A9D /* 03_ServerTemplate.pdf */ = {isa = PBXFileReference; lastKnownFileType = image.pdf; path = 03_ServerTemplate.pdf; sourceTree = "<group>"; };
4C2E381B16D11FF900037A9D /* 04_KlipperTemplate.pdf */ = {isa = PBXFileReference; lastKnownFileType = image.pdf; path = 04_KlipperTemplate.pdf; sourceTree = "<group>"; }; 4C2E381B16D11FF900037A9D /* 04_KlipperTemplate.pdf */ = {isa = PBXFileReference; lastKnownFileType = image.pdf; path = 04_KlipperTemplate.pdf; sourceTree = "<group>"; };
@@ -1682,6 +1683,7 @@
children = ( children = (
4CF6C715176F5183007A811D /* MPServerRequestHandling.h */, 4CF6C715176F5183007A811D /* MPServerRequestHandling.h */,
4CA0B30D15BCB6FD00654E32 /* MPSettingsTab.h */, 4CA0B30D15BCB6FD00654E32 /* MPSettingsTab.h */,
4C2B0B7419F66F6400E48913 /* MPTargetItemResolving.h */,
); );
name = Protocolls; name = Protocolls;
sourceTree = "<group>"; sourceTree = "<group>";

View File

@@ -184,6 +184,7 @@ NSString *const MPDidChangeStoredKeyFilesSettings = @"com.hicknhack.macpass.MPDi
self.passwordCreatorController.closeTarget = self.passwordCreatorWindow; self.passwordCreatorController.closeTarget = self.passwordCreatorWindow;
NSView *creatorView = [_passwordCreatorController view]; NSView *creatorView = [_passwordCreatorController view];
[self.passwordCreatorWindow setContentView:creatorView]; [self.passwordCreatorWindow setContentView:creatorView];
[self.passwordCreatorController updateResponderChain];
} }
[self.passwordCreatorController reset]; [self.passwordCreatorController reset];
[self.passwordCreatorWindow makeKeyAndOrderFront:self.passwordCreatorWindow]; [self.passwordCreatorWindow makeKeyAndOrderFront:self.passwordCreatorWindow];

View File

@@ -32,6 +32,7 @@
#import "MPConstants.h" #import "MPConstants.h"
#import "MPSavePanelAccessoryViewController.h" #import "MPSavePanelAccessoryViewController.h"
#import "MPTreeDelegate.h" #import "MPTreeDelegate.h"
#import "MPTargetItemResolving.h"
#import "DDXMLNode.h" #import "DDXMLNode.h"
@@ -285,7 +286,7 @@ NSString *const MPDocumentGroupKey = @"MPDocumentGroupKey
self.unlockCount += 1; self.unlockCount += 1;
[[NSNotificationCenter defaultCenter] postNotificationName:MPDocumentDidUnlockDatabaseNotification object:self]; [[NSNotificationCenter defaultCenter] postNotificationName:MPDocumentDidUnlockDatabaseNotification object:self];
/* Make sure to only store */ /* Make sure to only store */
MPAppDelegate *delegate = [NSApp delegate]; MPAppDelegate *delegate = (MPAppDelegate *)[NSApp delegate];
if(self.compositeKey.hasKeyFile && self.compositeKey.hasPassword && delegate.isAllowedToStoreKeyFile) { if(self.compositeKey.hasKeyFile && self.compositeKey.hasPassword && delegate.isAllowedToStoreKeyFile) {
[self _storeKeyURL:keyFileURL]; [self _storeKeyURL:keyFileURL];
} }
@@ -315,7 +316,7 @@ NSString *const MPDocumentGroupKey = @"MPDocumentGroupKey
} }
- (NSURL *)suggestedKeyURL { - (NSURL *)suggestedKeyURL {
MPAppDelegate *delegate = [NSApp delegate]; MPAppDelegate *delegate = (MPAppDelegate *)[NSApp delegate];
if(!delegate.isAllowedToStoreKeyFile) { if(!delegate.isAllowedToStoreKeyFile) {
return nil; return nil;
} }
@@ -611,6 +612,11 @@ NSString *const MPDocumentGroupKey = @"MPDocumentGroupKey
} }
- (BOOL)validateUserInterfaceItem:(id<NSValidatedUserInterfaceItem>)anItem { - (BOOL)validateUserInterfaceItem:(id<NSValidatedUserInterfaceItem>)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; } if(self.encrypted || self.isReadOnly) { return NO; }
BOOL valid = self.selectedItem ? self.selectedItem.isEditable : YES; BOOL valid = self.selectedItem ? self.selectedItem.isEditable : YES;
@@ -647,6 +653,18 @@ NSString *const MPDocumentGroupKey = @"MPDocumentGroupKey
break; break;
case MPActionShowHistory: case MPActionShowHistory:
valid &= (self.selectedEntry && (self.selectedItem == (id)self.selectedEntry)); 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: default:
break; break;
} }
@@ -657,7 +675,7 @@ NSString *const MPDocumentGroupKey = @"MPDocumentGroupKey
if(nil == keyURL) { if(nil == keyURL) {
return; // no URL to store in the first place 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!"); NSAssert(delegate.isAllowedToStoreKeyFile, @"We can only store if we are allowed to do so!");
NSMutableDictionary *keysForFiles = [[[NSUserDefaults standardUserDefaults] dictionaryForKey:kMPSettingsKeyRememeberdKeysForDatabases] mutableCopy]; NSMutableDictionary *keysForFiles = [[[NSUserDefaults standardUserDefaults] dictionaryForKey:kMPSettingsKeyRememeberdKeysForDatabases] mutableCopy];
if(nil == keysForFiles) { if(nil == keysForFiles) {

View File

@@ -271,7 +271,9 @@ typedef NS_ENUM(NSUInteger, MPEntryTab) {
- (IBAction)_popUpPasswordGenerator:(id)sender { - (IBAction)_popUpPasswordGenerator:(id)sender {
[self.generatePasswordButton setEnabled:NO]; [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 { - (void)_showPopopver:(NSViewController *)viewController atView:(NSView *)view onEdge:(NSRectEdge)edge {

View File

@@ -8,6 +8,7 @@
#import "MPViewController.h" #import "MPViewController.h"
#import "MPContextBarViewController.h" #import "MPContextBarViewController.h"
#import "MPTargetItemResolving.h"
APPKIT_EXTERN NSString *const MPEntryTableUserNameColumnIdentifier; APPKIT_EXTERN NSString *const MPEntryTableUserNameColumnIdentifier;
APPKIT_EXTERN NSString *const MPEntryTableTitleColumnIdentifier; APPKIT_EXTERN NSString *const MPEntryTableTitleColumnIdentifier;
@@ -31,7 +32,7 @@ typedef NS_ENUM( NSUInteger, MPCopyContentTypeTag) {
@class MPDocumentWindowController; @class MPDocumentWindowController;
@class MPDocument; @class MPDocument;
@interface MPEntryViewController : MPViewController <NSTableViewDelegate> @interface MPEntryViewController : MPViewController <NSTableViewDelegate, MPTargetItemResolving>
@property (weak,readonly) NSTableView *entryTable; @property (weak,readonly) NSTableView *entryTable;
@property (readonly, strong) NSArrayController *entryArrayController; @property (readonly, strong) NSArrayController *entryArrayController;

View File

@@ -70,6 +70,7 @@ NSString *const _MPTableStringCellView = @"StringCell";
NSString *const _MPTableSecurCellView = @"PasswordCell"; NSString *const _MPTableSecurCellView = @"PasswordCell";
@interface MPEntryViewController () { @interface MPEntryViewController () {
/* TODO unify delegation */
MPEntryContextMenuDelegate *_menuDelegate; MPEntryContextMenuDelegate *_menuDelegate;
BOOL _isDisplayingContextBar; BOOL _isDisplayingContextBar;
BOOL _didUnlock; BOOL _didUnlock;
@@ -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 #pragma mark MPDocument Notifications
- (void)_didChangeCurrentItem:(NSNotification *)notification { - (void)_didChangeCurrentItem:(NSNotification *)notification {
MPDocument *document = [notification object]; MPDocument *document = [notification object];
@@ -535,28 +549,9 @@ NSString *const _MPTableSecurCellView = @"PasswordCell";
#pragma mark Validation #pragma mark Validation
- (BOOL)validateMenuItem:(NSMenuItem *)menuItem { - (BOOL)validateMenuItem:(NSMenuItem *)menuItem {
/* Validation is soley handeld in the document */
MPDocument *document = [[self windowController] document]; MPDocument *document = [[self windowController] document];
if(![document validateMenuItem:menuItem]) { return [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;
}
} }
#pragma mark ContextMenu #pragma mark ContextMenu
@@ -600,38 +595,23 @@ NSString *const _MPTableSecurCellView = @"PasswordCell";
[[self.entryTable headerView] setMenu:headerMenu]; [[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 #pragma mark Actions
- (void)copyPassword:(id)sender { - (void)copyPassword:(id)sender {
KPKEntry *selectedEntry = [self _clickedOrSelectedEntry]; KPKEntry *selectedEntry = [[self targetItemForAction] asEntry];
if(selectedEntry) { if(selectedEntry) {
[self _copyToPasteboard:[selectedEntry.password finalValueForEntry:selectedEntry] overlayInfo:MPOverlayInfoPassword name:nil]; [self _copyToPasteboard:[selectedEntry.password finalValueForEntry:selectedEntry] overlayInfo:MPOverlayInfoPassword name:nil];
} }
} }
- (void)copyUsername:(id)sender { - (void)copyUsername:(id)sender {
KPKEntry *selectedEntry = [self _clickedOrSelectedEntry]; KPKEntry *selectedEntry = [[self targetItemForAction] asEntry];
if(selectedEntry) { if(selectedEntry) {
[self _copyToPasteboard:[selectedEntry.username finalValueForEntry:selectedEntry] overlayInfo:MPOverlayInfoUsername name:nil]; [self _copyToPasteboard:[selectedEntry.username finalValueForEntry:selectedEntry] overlayInfo:MPOverlayInfoUsername name:nil];
} }
} }
- (void)copyCustomAttribute:(id)sender { - (void)copyCustomAttribute:(id)sender {
KPKEntry *selectedEntry = [self _clickedOrSelectedEntry]; KPKEntry *selectedEntry = [[self targetItemForAction] asEntry];
if(selectedEntry && [selectedEntry isKindOfClass:[KPKEntry class]]) { if(selectedEntry && [selectedEntry isKindOfClass:[KPKEntry class]]) {
NSUInteger index = [sender tag]; NSUInteger index = [sender tag];
NSAssert((index >= 0) && (index < [selectedEntry.customAttributes count]), @"Index for custom field needs to be valid"); 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 { - (void)copyURL:(id)sender {
KPKEntry *selectedEntry = [self _clickedOrSelectedEntry]; KPKEntry *selectedEntry = [[self targetItemForAction] asEntry];
if(selectedEntry) { if(selectedEntry) {
[self _copyToPasteboard:[selectedEntry.url finalValueForEntry:selectedEntry] overlayInfo:MPOverlayInfoURL name:nil]; [self _copyToPasteboard:[selectedEntry.url finalValueForEntry:selectedEntry] overlayInfo:MPOverlayInfoURL name:nil];
} }
} }
- (void)openURL:(id)sender { - (void)openURL:(id)sender {
KPKEntry *selectedEntry = [self _clickedOrSelectedEntry]; KPKEntry *selectedEntry = [[self targetItemForAction] asEntry];
if(selectedEntry && [selectedEntry.url length] > 0) { if(selectedEntry && [selectedEntry.url length] > 0) {
NSURL *webURL = [NSURL URLWithString:[selectedEntry.url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; NSURL *webURL = [NSURL URLWithString:[selectedEntry.url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSString *scheme = [webURL scheme]; NSString *scheme = [webURL scheme];
@@ -670,7 +650,7 @@ NSString *const _MPTableSecurCellView = @"PasswordCell";
} }
- (void)delete:(id)sender { - (void)delete:(id)sender {
KPKEntry *entry =[self _clickedOrSelectedEntry]; KPKEntry *entry = [[self targetItemForAction] asEntry];
if(!entry) { if(!entry) {
return; return;
} }

View File

@@ -7,7 +7,11 @@
// //
#import <Foundation/Foundation.h> #import <Foundation/Foundation.h>
@class KPKNode;
@protocol MPTargetItemResolving <NSObject> @protocol MPTargetItemResolving <NSObject>
@required
- (KPKNode *)targetItemForAction;
@end @end

Binary file not shown.