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>"; };
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>"; };
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>"; };
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>"; };
@@ -1682,6 +1683,7 @@
children = (
4CF6C715176F5183007A811D /* MPServerRequestHandling.h */,
4CA0B30D15BCB6FD00654E32 /* MPSettingsTab.h */,
4C2B0B7419F66F6400E48913 /* MPTargetItemResolving.h */,
);
name = Protocolls;
sourceTree = "<group>";

View File

@@ -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];

View File

@@ -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<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; }
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) {

View File

@@ -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 {

View File

@@ -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 <NSTableViewDelegate>
@interface MPEntryViewController : MPViewController <NSTableViewDelegate, MPTargetItemResolving>
@property (weak,readonly) NSTableView *entryTable;
@property (readonly, strong) NSArrayController *entryArrayController;

View File

@@ -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;
}

View File

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

Binary file not shown.