From 3a2dff5e07d55390a6234b961c4a8db71dfd4702 Mon Sep 17 00:00:00 2001 From: michael starke Date: Fri, 24 Oct 2014 16:18:53 +0200 Subject: [PATCH] Centralized actions inside MPDocumetnWindowController --- MacPass.xcodeproj/project.pbxproj | 4 +- MacPass/Base.lproj/GroupInspectorView.xib | 20 ++----- MacPass/Base.lproj/InspectorView.xib | 4 +- MacPass/MPDocument.h | 4 +- MacPass/MPDocument.m | 42 +++++++++++--- MacPass/MPDocumentWindowController.h | 3 +- MacPass/MPDocumentWindowController.m | 19 +++++-- MacPass/MPEntryViewController.h | 4 +- MacPass/MPEntryViewController.m | 23 +++++--- MacPass/MPInspectorViewController.m | 3 +- MacPass/MPOutlineViewController.h | 6 +- MacPass/MPOutlineViewController.m | 57 +++++++------------ ...temResolving.h => MPTargetNodeResolving.h} | 10 +++- 13 files changed, 112 insertions(+), 87 deletions(-) rename MacPass/{MPTargetItemResolving.h => MPTargetNodeResolving.h} (54%) diff --git a/MacPass.xcodeproj/project.pbxproj b/MacPass.xcodeproj/project.pbxproj index 9f132586..b0c1ce7a 100644 --- a/MacPass.xcodeproj/project.pbxproj +++ b/MacPass.xcodeproj/project.pbxproj @@ -510,7 +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 = ""; }; + 4C2B0B7419F66F6400E48913 /* MPTargetNodeResolving.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MPTargetNodeResolving.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 = ""; }; @@ -1683,7 +1683,7 @@ children = ( 4CF6C715176F5183007A811D /* MPServerRequestHandling.h */, 4CA0B30D15BCB6FD00654E32 /* MPSettingsTab.h */, - 4C2B0B7419F66F6400E48913 /* MPTargetItemResolving.h */, + 4C2B0B7419F66F6400E48913 /* MPTargetNodeResolving.h */, ); name = Protocolls; sourceTree = ""; diff --git a/MacPass/Base.lproj/GroupInspectorView.xib b/MacPass/Base.lproj/GroupInspectorView.xib index bfb220eb..4b8d7eeb 100644 --- a/MacPass/Base.lproj/GroupInspectorView.xib +++ b/MacPass/Base.lproj/GroupInspectorView.xib @@ -1,8 +1,8 @@ - + - - + + @@ -18,21 +18,18 @@ - + - - - @@ -41,7 +38,6 @@ - @@ -53,7 +49,6 @@ - @@ -102,7 +95,6 @@ - @@ -120,7 +112,6 @@ - @@ -129,7 +120,6 @@ - @@ -138,7 +128,6 @@ - @@ -150,7 +139,6 @@ - diff --git a/MacPass/Base.lproj/InspectorView.xib b/MacPass/Base.lproj/InspectorView.xib index bbffec7c..63425332 100644 --- a/MacPass/Base.lproj/InspectorView.xib +++ b/MacPass/Base.lproj/InspectorView.xib @@ -73,9 +73,9 @@ - + - + diff --git a/MacPass/MPDocument.h b/MacPass/MPDocument.h index 26a66bcd..f67be43c 100644 --- a/MacPass/MPDocument.h +++ b/MacPass/MPDocument.h @@ -23,6 +23,7 @@ #import #import "KPKVersion.h" #import "MPEntrySearchContext.h" +#import "MPTargetNodeResolving.h" /** * Posted when a new group was added to the document. @@ -59,7 +60,7 @@ APPKIT_EXTERN NSString *const MPDocumentGroupKey; @class KPKNode; @class MPEditSession; -@interface MPDocument : NSDocument +@interface MPDocument : NSDocument @property (nonatomic, readonly, assign) BOOL encrypted; @@ -164,6 +165,7 @@ APPKIT_EXTERN NSString *const MPDocumentGroupKey; - (KPKEntry *)createEntry:(KPKGroup *)parent; - (KPKAttribute *)createCustomAttribute:(KPKEntry *)entry; +- (void)deleteNode:(KPKNode *)node; - (void)deleteGroup:(KPKGroup *)group; - (void)deleteEntry:(KPKEntry *)entry; diff --git a/MacPass/MPDocument.m b/MacPass/MPDocument.m index 143f735f..7dfe3402 100644 --- a/MacPass/MPDocument.m +++ b/MacPass/MPDocument.m @@ -32,7 +32,7 @@ #import "MPConstants.h" #import "MPSavePanelAccessoryViewController.h" #import "MPTreeDelegate.h" -#import "MPTargetItemResolving.h" +#import "MPTargetNodeResolving.h" #import "DDXMLNode.h" @@ -512,7 +512,19 @@ NSString *const MPDocumentGroupKey = @"MPDocumentGroupKey return newAttribute; } +- (void)deleteNode:(KPKNode *)node { + if([node asGroup]) { + [self deleteGroup:[node asGroup]]; + } + else if([node asEntry]) { + [self deleteEntry:[node asEntry]]; + } +} + - (void)deleteEntry:(KPKEntry *)entry { + if(!entry) { + return; // Nothing to do; + } if(self.useTrash) { if([self isItemTrashed:entry]) { return; // Entry is already trashed @@ -615,11 +627,13 @@ 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]; - + id entryResolver = [NSApp targetForAction:@selector(currentTargetEntry)]; + id groupResolver = [NSApp targetForAction:@selector(currentTargetGroup)]; + id nodeResolver = [NSApp targetForAction:@selector(currentTargetNode)]; + KPKNode *targetNode = [nodeResolver currentTargetNode]; + KPKEntry *targetEntry = [entryResolver currentTargetEntry]; + KPKGroup *targetGroup = [groupResolver currentTargetGroup]; + if(self.encrypted || self.isReadOnly) { return NO; } BOOL valid = self.selectedItem ? self.selectedItem.isEditable : YES; @@ -640,9 +654,7 @@ NSString *const MPDocumentGroupKey = @"MPDocumentGroupKey valid &= ![self isItemTrashed:self.selectedItem]; break; case MPActionCloneEntry: - //case MPActionCloneEntryWithOptions: - valid &= (nil != self.selectedItem); - valid &= self.selectedEntry == self.selectedItem; + valid &= (nil != targetEntry); break; case MPActionEmptyTrash: valid &= ([self.trash.groups count] + [self.trash.entries count]) > 0; @@ -720,6 +732,18 @@ NSString *const MPDocumentGroupKey = @"MPDocumentGroupKey [self.trash clear]; } +#pragma mark - +#pragma mark MPTargetNodeResolving + +- (KPKEntry *)currentTargetEntry { + return self.selectedEntry; +} + +- (KPKGroup *)currentTargetGroup { + return self.selectedGroup; +} + + # pragma mark File Watching - (void) _watchForFileChanges:(BOOL)watch { } diff --git a/MacPass/MPDocumentWindowController.h b/MacPass/MPDocumentWindowController.h index 024d0ea3..fb4de2b9 100644 --- a/MacPass/MPDocumentWindowController.h +++ b/MacPass/MPDocumentWindowController.h @@ -45,8 +45,9 @@ - (IBAction)lock:(id)sender; - (IBAction)createGroup:(id)sender; +- (IBAction)createEntry:(id)sender; +- (IBAction)delete:(id)sender; -- (IBAction)pickIcon:(id)sender; - (IBAction)pickExpiryDate:(id)sender; #pragma mark Helper diff --git a/MacPass/MPDocumentWindowController.m b/MacPass/MPDocumentWindowController.m index b78ba1b6..54352246 100644 --- a/MacPass/MPDocumentWindowController.m +++ b/MacPass/MPDocumentWindowController.m @@ -24,6 +24,7 @@ #import "MPContextToolbarButton.h" #import "KPKTree.h" +#import "KPKEntry.h" #import "KPKCompositeKey.h" typedef NS_ENUM(NSUInteger, MPAlertContext) { @@ -326,15 +327,25 @@ typedef void (^MPPasswordChangedBlock)(BOOL didChangePassword); } - (void)createGroup:(id)sender { - [self.outlineViewController createGroup:nil]; + id target = [NSApp targetForAction:@selector(currentTargetGroup)]; + KPKGroup *group = [target currentTargetGroup]; + MPDocument *document = self.document; + if(!group) { + group = document.root; + } + [document createGroup:group]; } - (void)createEntry:(id)sender { - [self.outlineViewController createEntry:nil]; + id target = [NSApp targetForAction:@selector(currentTargetGroup)]; + KPKGroup *group = [target currentTargetGroup]; + [(MPDocument *)self.document createEntry:group]; } -- (void)pickIcon:(id)sender { - [self.inspectorViewController pickIcon:sender]; +- (void)delete:(id)sender { + id target = [NSApp targetForAction:@selector(currentTargetNode)]; + KPKNode *node = [target currentTargetNode]; + [self.document deleteNode:node]; } - (void)pickExpiryDate:(id)sender { diff --git a/MacPass/MPEntryViewController.h b/MacPass/MPEntryViewController.h index 0304047f..50b40576 100644 --- a/MacPass/MPEntryViewController.h +++ b/MacPass/MPEntryViewController.h @@ -8,7 +8,7 @@ #import "MPViewController.h" #import "MPContextBarViewController.h" -#import "MPTargetItemResolving.h" +#import "MPTargetNodeResolving.h" APPKIT_EXTERN NSString *const MPEntryTableUserNameColumnIdentifier; APPKIT_EXTERN NSString *const MPEntryTableTitleColumnIdentifier; @@ -32,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 17741dfc..64cdf14f 100644 --- a/MacPass/MPEntryViewController.m +++ b/MacPass/MPEntryViewController.m @@ -340,7 +340,7 @@ NSString *const _MPTableSecurCellView = @"PasswordCell"; } #pragma mark MPTargetItemResolving -- (KPKNode *)targetItemForAction { +- (KPKEntry *)currentTargetEntry { NSInteger activeRow = [self.entryTable clickedRow]; /* Fallback to selection e.g. for toolbar actions */ if(activeRow < 0 ) { @@ -352,6 +352,15 @@ NSString *const _MPTableSecurCellView = @"PasswordCell"; return nil; } +- (KPKNode *)currentTargetNode { + KPKEntry *entry = [self currentTargetEntry]; + if(entry) { + return entry; + } + MPDocument *document = [[self windowController] document]; + return document.selectedItem; +} + #pragma mark MPDocument Notifications - (void)_didChangeCurrentItem:(NSNotification *)notification { MPDocument *document = [notification object]; @@ -597,21 +606,21 @@ NSString *const _MPTableSecurCellView = @"PasswordCell"; #pragma mark Actions - (void)copyPassword:(id)sender { - KPKEntry *selectedEntry = [[self targetItemForAction] asEntry]; + KPKEntry *selectedEntry = [[self currentTargetNode] asEntry]; if(selectedEntry) { [self _copyToPasteboard:[selectedEntry.password finalValueForEntry:selectedEntry] overlayInfo:MPOverlayInfoPassword name:nil]; } } - (void)copyUsername:(id)sender { - KPKEntry *selectedEntry = [[self targetItemForAction] asEntry]; + KPKEntry *selectedEntry = [[self currentTargetNode] asEntry]; if(selectedEntry) { [self _copyToPasteboard:[selectedEntry.username finalValueForEntry:selectedEntry] overlayInfo:MPOverlayInfoUsername name:nil]; } } - (void)copyCustomAttribute:(id)sender { - KPKEntry *selectedEntry = [[self targetItemForAction] asEntry]; + KPKEntry *selectedEntry = [[self currentTargetNode] 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"); @@ -621,14 +630,14 @@ NSString *const _MPTableSecurCellView = @"PasswordCell"; } - (void)copyURL:(id)sender { - KPKEntry *selectedEntry = [[self targetItemForAction] asEntry]; + KPKEntry *selectedEntry = [[self currentTargetNode] asEntry]; if(selectedEntry) { [self _copyToPasteboard:[selectedEntry.url finalValueForEntry:selectedEntry] overlayInfo:MPOverlayInfoURL name:nil]; } } - (void)openURL:(id)sender { - KPKEntry *selectedEntry = [[self targetItemForAction] asEntry]; + KPKEntry *selectedEntry = [[self currentTargetNode] asEntry]; if(selectedEntry && [selectedEntry.url length] > 0) { NSURL *webURL = [NSURL URLWithString:[selectedEntry.url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; NSString *scheme = [webURL scheme]; @@ -650,7 +659,7 @@ NSString *const _MPTableSecurCellView = @"PasswordCell"; } - (void)delete:(id)sender { - KPKEntry *entry = [[self targetItemForAction] asEntry]; + KPKEntry *entry = [[self currentTargetNode] asEntry]; if(!entry) { return; } diff --git a/MacPass/MPInspectorViewController.m b/MacPass/MPInspectorViewController.m index 60caa64e..d3c779ae 100644 --- a/MacPass/MPInspectorViewController.m +++ b/MacPass/MPInspectorViewController.m @@ -80,7 +80,7 @@ typedef NS_ENUM(NSUInteger, MPContentTab) { - (void)awakeFromNib { [self.bottomBar setBorderType:HNHBorderTop|HNHBorderHighlight]; - + [[self.noSelectionInfo cell] setBackgroundStyle:NSBackgroundStyleRaised]; [[self.itemImageView cell] setBackgroundStyle:NSBackgroundStyleRaised]; [self.tabView bind:NSSelectedIndexBinding toObject:self withKeyPath:@"activeTab" options:nil]; @@ -304,6 +304,7 @@ typedef NS_ENUM(NSUInteger, MPContentTab) { [self.itemNameTextField setHidden:NO]; } + #pragma mark - #pragma mark MPDocument Notifications diff --git a/MacPass/MPOutlineViewController.h b/MacPass/MPOutlineViewController.h index bc49a042..5c7c8f75 100644 --- a/MacPass/MPOutlineViewController.h +++ b/MacPass/MPOutlineViewController.h @@ -7,14 +7,14 @@ // #import "MPViewController.h" -#import "MPTargetItemResolving.h" +#import "MPTargetNodeResolving.h" APPKIT_EXTERN NSString *const MPOutlineViewDidChangeGroupSelection; @class HNHGradientView; @class MPDocument; -@interface MPOutlineViewController : MPViewController +@interface MPOutlineViewController : MPViewController @property (weak) IBOutlet HNHGradientView *bottomBar; @@ -23,8 +23,6 @@ APPKIT_EXTERN NSString *const MPOutlineViewDidChangeGroupSelection; - (void)showOutline; - (void)regsiterNotificationsForDocument:(MPDocument *)document; -- (void)createGroup:(id)sender; -- (void)createEntry:(id)sender; /** * Retrieves the current item for the current mouse location * @return Item under mouse. If the mouse isn't inside the view, nil is returned diff --git a/MacPass/MPOutlineViewController.m b/MacPass/MPOutlineViewController.m index 7e2488bd..944215ce 100644 --- a/MacPass/MPOutlineViewController.m +++ b/MacPass/MPOutlineViewController.m @@ -7,22 +7,23 @@ // #import "MPOutlineViewController.h" -#import "MPOutlineDataSource.h" +#import "MPActionHelper.h" +#import "MPConstants.h" +#import "MPContextMenuHelper.h" #import "MPDocument.h" #import "MPDocumentWindowController.h" -#import "MPContextMenuHelper.h" -#import "MPConstants.h" -#import "MPActionHelper.h" #import "MPIconHelper.h" #import "MPNotifications.h" #import "MPOutlineContextMenuDelegate.h" +#import "MPOutlineDataSource.h" -#import "KPKTree.h" -#import "KPKNode.h" -#import "KPKTimeInfo.h" +#import "KPKEntry.h" #import "KPKGroup.h" -#import "KPKNode+IconImage.h" #import "KPKMetaData.h" +#import "KPKNode.h" +#import "KPKNode+IconImage.h" +#import "KPKTimeInfo.h" +#import "KPKTree.h" #import "KPKUTIs.h" #import "HNHGradientView.h" @@ -136,17 +137,12 @@ NSString *const _MPOutlinveViewHeaderViewIdentifier = @"HeaderCell"; #pragma mark Custom Setter/Getter - (void)setDatabaseNameWrapper:(NSString *)databaseNameWrapper { if(![_databaseNameWrapper isEqualToString:databaseNameWrapper]) { - if([databaseNameWrapper length] == 0) { - _databaseNameWrapper = NSLocalizedString(@"DATABASE", "Default name database"); - } - else { - _databaseNameWrapper= [databaseNameWrapper copy]; - } + _databaseNameWrapper = (databaseNameWrapper.length == 0) ? NSLocalizedString(@"DATABASE", "Default name database") : [databaseNameWrapper copy]; } } -#pragma mark MPTargetItemResolving -- (KPKNode *)targetItemForAction { +#pragma mark MPTargetNodeResolving +- (KPKGroup *)currentTargetGroup { NSInteger row = [self.outlineView clickedRow]; if( row < 0 ) { row = [self.outlineView selectedRow]; @@ -154,6 +150,15 @@ NSString *const _MPOutlinveViewHeaderViewIdentifier = @"HeaderCell"; return [[self.outlineView itemAtRow:row] representedObject]; } +- (KPKNode *)currentTargetNode { + KPKGroup *group = [self currentTargetGroup]; + if(group) { + return group; + } + MPDocument *document = [[self windowController] document]; + return document.selectedItem; +} + #pragma mark Notifications - (void)regsiterNotificationsForDocument:(MPDocument *)document { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(_didAddGroup:) name:MPDocumentDidAddGroupNotification object:document]; @@ -197,24 +202,6 @@ NSString *const _MPOutlinveViewHeaderViewIdentifier = @"HeaderCell"; #pragma mark - #pragma mark Actions -- (void)createGroup:(id)sender { - KPKGroup *group = [[self targetItemForAction] asGroup]; - MPDocument *document = [[self windowController] document]; - if(!group) { - group = document.root; - } - [document createGroup:group]; -} - -- (void)createEntry:(id)sender { - MPDocument *document = [[self windowController] document]; - [document createEntry:[[self targetItemForAction] asGroup]]; -} - -- (void)delete:(id)sender { - [[[self windowController] document] deleteGroup:[[self targetItemForAction] asGroup]]; -} - - (void)_doubleClickedGroup:(id)sender { [[self windowController] showInspector:sender]; } @@ -290,7 +277,7 @@ NSString *const _MPOutlinveViewHeaderViewIdentifier = @"HeaderCell"; if(![document validateUserInterfaceItem:menuItem]) { return NO; } - id selected = [[self targetItemForAction] asGroup]; + id selected = [[self currentTargetNode] asGroup]; if(!selected) { return NO; } diff --git a/MacPass/MPTargetItemResolving.h b/MacPass/MPTargetNodeResolving.h similarity index 54% rename from MacPass/MPTargetItemResolving.h rename to MacPass/MPTargetNodeResolving.h index 8a6f9733..28cf37cf 100644 --- a/MacPass/MPTargetItemResolving.h +++ b/MacPass/MPTargetNodeResolving.h @@ -7,11 +7,15 @@ // #import +@class KPKEntry; +@class KPKGroup; @class KPKNode; -@protocol MPTargetItemResolving +@protocol MPTargetNodeResolving -@required -- (KPKNode *)targetItemForAction; +@optional +- (KPKNode *)currentTargetNode; +- (KPKGroup *)currentTargetGroup; +- (KPKEntry *)currentTargetEntry; @end