From 6d8c2b34bea625004a9760abc14127ce92bc251a Mon Sep 17 00:00:00 2001 From: Michael Starke Date: Tue, 28 Nov 2017 18:33:37 +0100 Subject: [PATCH] rough version of {PICKFIELD} --- MacPass.xcodeproj/project.pbxproj | 16 +++ MacPass/MPPickfieldTableModel.h | 33 ++++++ MacPass/MPPickfieldTableModel.m | 72 +++++++++++++ MacPass/MPPickfieldViewController.h | 20 ++++ MacPass/MPPickfieldViewController.m | 95 +++++++++++++++++ MacPass/MPTreeDelegate.m | 17 ++++ MacPass/PickcharView.xib | 2 +- MacPass/PickfieldView.xib | 153 ++++++++++++++++++++++++++++ 8 files changed, 407 insertions(+), 1 deletion(-) create mode 100644 MacPass/MPPickfieldTableModel.h create mode 100644 MacPass/MPPickfieldTableModel.m create mode 100644 MacPass/MPPickfieldViewController.h create mode 100644 MacPass/MPPickfieldViewController.m create mode 100644 MacPass/PickfieldView.xib diff --git a/MacPass.xcodeproj/project.pbxproj b/MacPass.xcodeproj/project.pbxproj index 1cf58951..ae8d195a 100644 --- a/MacPass.xcodeproj/project.pbxproj +++ b/MacPass.xcodeproj/project.pbxproj @@ -176,6 +176,9 @@ 4C7F8B681A10B68400CCB83D /* WelcomeWindow.xib in Resources */ = {isa = PBXBuildFile; fileRef = 4C7F8B6A1A10B68400CCB83D /* WelcomeWindow.xib */; }; 4C80304A1E2FBAA300133E4C /* MPTestKeyMapper.m in Sources */ = {isa = PBXBuildFile; fileRef = 4C8030491E2FBAA300133E4C /* MPTestKeyMapper.m */; }; 4C811C8316ECD06E00C4BAC6 /* MPKeyfilePathControlDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 4C811C8216ECD06E00C4BAC6 /* MPKeyfilePathControlDelegate.m */; }; + 4C82046A1FCDC07800EB24A4 /* MPPickfieldViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 4C8204681FCDC07800EB24A4 /* MPPickfieldViewController.m */; }; + 4C82046B1FCDC07800EB24A4 /* PickfieldView.xib in Resources */ = {isa = PBXBuildFile; fileRef = 4C8204691FCDC07800EB24A4 /* PickfieldView.xib */; }; + 4C82046E1FCDC8A100EB24A4 /* MPPickfieldTableModel.m in Sources */ = {isa = PBXBuildFile; fileRef = 4C82046D1FCDC8A100EB24A4 /* MPPickfieldTableModel.m */; }; 4C83814215BF4677001AE468 /* MPDocumentWindowController.m in Sources */ = {isa = PBXBuildFile; fileRef = 4C83814115BF4677001AE468 /* MPDocumentWindowController.m */; }; 4C888C9016EB6C91003D34A1 /* Localizable.strings in Resources */ = {isa = PBXBuildFile; fileRef = 4C888C8E16EB6C91003D34A1 /* Localizable.strings */; }; 4C888C9316EB6F5E003D34A1 /* MPToolbarItem.m in Sources */ = {isa = PBXBuildFile; fileRef = 4C888C9216EB6F5E003D34A1 /* MPToolbarItem.m */; }; @@ -609,6 +612,11 @@ 4C8030491E2FBAA300133E4C /* MPTestKeyMapper.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MPTestKeyMapper.m; sourceTree = ""; }; 4C811C8116ECD06E00C4BAC6 /* MPKeyfilePathControlDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MPKeyfilePathControlDelegate.h; sourceTree = ""; }; 4C811C8216ECD06E00C4BAC6 /* MPKeyfilePathControlDelegate.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MPKeyfilePathControlDelegate.m; sourceTree = ""; }; + 4C8204671FCDC07800EB24A4 /* MPPickfieldViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MPPickfieldViewController.h; sourceTree = ""; }; + 4C8204681FCDC07800EB24A4 /* MPPickfieldViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MPPickfieldViewController.m; sourceTree = ""; }; + 4C8204691FCDC07800EB24A4 /* PickfieldView.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = PickfieldView.xib; sourceTree = ""; }; + 4C82046C1FCDC8A100EB24A4 /* MPPickfieldTableModel.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MPPickfieldTableModel.h; sourceTree = ""; }; + 4C82046D1FCDC8A100EB24A4 /* MPPickfieldTableModel.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MPPickfieldTableModel.m; sourceTree = ""; }; 4C83814015BF4677001AE468 /* MPDocumentWindowController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MPDocumentWindowController.h; sourceTree = ""; }; 4C83814115BF4677001AE468 /* MPDocumentWindowController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MPDocumentWindowController.m; sourceTree = ""; }; 4C840C441D773E5D0081F605 /* pl */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = pl; path = pl.lproj/DatabaseSettingsWindow.strings; sourceTree = ""; }; @@ -1082,6 +1090,8 @@ 4C6B7C7C18BE7EB0001D5D77 /* MPDocument+History.m */, 4C0AF62D195C1F2B009E658D /* MPEntrySearchContext.h */, 4C0AF62E195C1F2B009E658D /* MPEntrySearchContext.m */, + 4C82046C1FCDC8A100EB24A4 /* MPPickfieldTableModel.h */, + 4C82046D1FCDC8A100EB24A4 /* MPPickfieldTableModel.m */, ); name = Model; sourceTree = ""; @@ -1443,6 +1453,9 @@ 4CD4EE101FC7284000EA6EB3 /* MPPickcharViewController.h */, 4CD4EE111FC7284000EA6EB3 /* MPPickcharViewController.m */, 4CA4B3451FC88339007DF507 /* PickcharView.xib */, + 4C8204671FCDC07800EB24A4 /* MPPickfieldViewController.h */, + 4C8204681FCDC07800EB24A4 /* MPPickfieldViewController.m */, + 4C8204691FCDC07800EB24A4 /* PickfieldView.xib */, ); name = "View Controller"; sourceTree = ""; @@ -1701,6 +1714,7 @@ 4C3826B51AD04D8E007D7D67 /* 40_FindMailTemplate.pdf in Resources */, 4C2E381F16D11FF900037A9D /* 05_LanguagesTemplate.pdf in Resources */, 4C3826721AD04B51007D7D67 /* addFolderTemplate.pdf in Resources */, + 4C82046B1FCDC07800EB24A4 /* PickfieldView.xib in Resources */, 4C3826C21AD04D8E007D7D67 /* 55_ThumbnailTemplate.pdf in Resources */, 4C3826A61AD04D8E007D7D67 /* 22_ASCIITemplate.pdf in Resources */, 4CD78ABC16D155FF00768A1D /* 07_NotepadTemplate.pdf in Resources */, @@ -1822,6 +1836,7 @@ 4CA0B2FC15BCAF8600654E32 /* MPSettingsWindowController.m in Sources */, 4C4F72D118DF704400E8D378 /* DDHotKeyTextField.m in Sources */, 4C83814215BF4677001AE468 /* MPDocumentWindowController.m in Sources */, + 4C82046A1FCDC07800EB24A4 /* MPPickfieldViewController.m in Sources */, 4C2E382316D1421B00037A9D /* MPIconHelper.m in Sources */, 4C2E382616D1470200037A9D /* MPViewController.m in Sources */, 4C65FAE916D16DDB006E0577 /* MPPasswordInputController.m in Sources */, @@ -1870,6 +1885,7 @@ 4CE30ACC1A312B7F0063FCC6 /* MPReferenceBuilderViewController.m in Sources */, 4C6F228C19A4AA700012310C /* MPAutotypeDelay.m in Sources */, 4C3C4EAF18D7039300153127 /* MPValueTransformerHelper.m in Sources */, + 4C82046E1FCDC8A100EB24A4 /* MPPickfieldTableModel.m in Sources */, 4C978E0D19AE54AB003067DF /* MPFlagsHelper.m in Sources */, 4C6F228919A4A7F90012310C /* MPAutotypeClear.m in Sources */, 4C0B038C18E36DA400B9F9C9 /* MPFixAutotypeWindowController.m in Sources */, diff --git a/MacPass/MPPickfieldTableModel.h b/MacPass/MPPickfieldTableModel.h new file mode 100644 index 00000000..0d3c3ccf --- /dev/null +++ b/MacPass/MPPickfieldTableModel.h @@ -0,0 +1,33 @@ +// +// MPPickfieldTableModel.h +// MacPass +// +// Created by Michael Starke on 28.11.17. +// Copyright © 2017 HicknHack Software GmbH. All rights reserved. +// + +#import + +@class MPDocument; +@class KPKEntry; + +@interface MPPickFieldTableModelRowItem : NSObject + +@property (copy) NSString *name; +@property (copy) NSString *value; +@property BOOL isGroup; + ++ (instancetype)groupItemWithName:(NSString *)name; ++ (instancetype)itemWithName:(NSString *)name value:(NSString *)value; + +@end + +@interface MPPickfieldTableModel : NSObject + +@property (copy) NSArray *items; + +- (instancetype)initWithEntry:(KPKEntry *)entry inDocument:(MPDocument *)document; +- (MPPickFieldTableModelRowItem *)itemAtIndex:(NSUInteger)index; + +@end + diff --git a/MacPass/MPPickfieldTableModel.m b/MacPass/MPPickfieldTableModel.m new file mode 100644 index 00000000..62a0d96d --- /dev/null +++ b/MacPass/MPPickfieldTableModel.m @@ -0,0 +1,72 @@ +// +// MPPickfieldTableModel.m +// MacPass +// +// Created by Michael Starke on 28.11.17. +// Copyright © 2017 HicknHack Software GmbH. All rights reserved. +// + +#import "MPPickfieldTableModel.h" +#import + +@implementation MPPickFieldTableModelRowItem + +- (instancetype)init { + self = [super init]; + if(self) { + _isGroup = NO; + } + return self; +} + ++ (instancetype)groupItemWithName:(NSString *)name { + MPPickFieldTableModelRowItem *item = [self itemWithName:name value:nil]; + item.isGroup = YES; + return item; +} + ++ (instancetype)itemWithName:(NSString *)name value:(NSString *)value { + MPPickFieldTableModelRowItem *item = [[MPPickFieldTableModelRowItem alloc] init]; + item.name = name; + item.value = value; + return item; +} + +@end + +@implementation MPPickfieldTableModel + +- (instancetype)initWithEntry:(KPKEntry *)entry inDocument:(MPDocument *)document { + self = [super init]; + if(self) { + [self _setupItemsForEntry:entry document:document]; + } + return self; +} + +- (void)_setupItemsForEntry:(KPKEntry *)entry document:(MPDocument *)document { + /* Default attributes */ + NSMutableArray *items = [[NSMutableArray alloc] init]; + [items addObject:[MPPickFieldTableModelRowItem groupItemWithName:NSLocalizedString(@"ENTRY_DEFAULT_ATTRIBUTES", @"Group row for entry attributes")]]; + + for(KPKAttribute *attribute in entry.defaultAttributes) { + /* TODO exclude protected values */ + [items addObject:[MPPickFieldTableModelRowItem itemWithName:attribute.key value:attribute.value]]; + } + + [items addObject:[MPPickFieldTableModelRowItem groupItemWithName:NSLocalizedString(@"ENTRY_CUSTOM_ATTRIBUTES", @"Group row for entry attributes")]]; + for(KPKAttribute *attribute in entry.customAttributes) { + /* TODO exclude protected values */ + [items addObject:[MPPickFieldTableModelRowItem itemWithName:attribute.key value:attribute.value]]; + } + self.items = items; // copy creates an immutable copy +} + +- (MPPickFieldTableModelRowItem *)itemAtIndex:(NSUInteger)index { + if(index < _items.count) { + return _items[index]; + } + return nil; +} + +@end diff --git a/MacPass/MPPickfieldViewController.h b/MacPass/MPPickfieldViewController.h new file mode 100644 index 00000000..77232d09 --- /dev/null +++ b/MacPass/MPPickfieldViewController.h @@ -0,0 +1,20 @@ +// +// MPPickfieldViewController.h +// MacPass +// +// Created by Michael Starke on 28.11.17. +// Copyright © 2017 HicknHack Software GmbH. All rights reserved. +// + +#import + +@class MPDocument; + +@interface MPPickfieldViewController : NSViewController + +@property (weak) MPDocument *document; +@property (copy, readonly) NSString *pickedValue; + +- (IBAction)pickField:(id)sender; + +@end diff --git a/MacPass/MPPickfieldViewController.m b/MacPass/MPPickfieldViewController.m new file mode 100644 index 00000000..ca521930 --- /dev/null +++ b/MacPass/MPPickfieldViewController.m @@ -0,0 +1,95 @@ +// +// MPPickfieldViewController.m +// MacPass +// +// Created by Michael Starke on 28.11.17. +// Copyright © 2017 HicknHack Software GmbH. All rights reserved. +// + +#import "MPPickfieldViewController.h" +#import "MPPickfieldTableModel.h" + +#import +@interface MPPickfieldViewController () + +@property (nonatomic, strong, readonly) KPKEntry *representedEntry; +@property (strong) MPPickfieldTableModel *tableModel; +@property (weak) IBOutlet NSTableView *tableView; +@property (copy) NSString *pickedValue; + +@end + +@implementation MPPickfieldViewController + +@dynamic representedEntry; + +- (NSString *)nibName { + return @"PickfieldView"; +} + +- (void)viewDidLoad { + [super viewDidLoad]; + self.tableModel = [[MPPickfieldTableModel alloc] initWithEntry:self.representedEntry inDocument:nil]; +} + +- (KPKEntry *)representedEntry { + if([self.representedObject isKindOfClass:KPKEntry.class]) { + return self.representedObject; + } + return nil; +} + +- (void)pickField:(id)sender { + [NSApp stopModalWithCode:NSModalResponseOK]; +} + +- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView { + return self.tableModel.items.count; +} + +- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row { + NSTableCellView *view; + if(tableColumn) { + view = [tableView makeViewWithIdentifier:@"DataCell" owner:self]; + } + else { + view = [tableView makeViewWithIdentifier:@"HeaderCell" owner:self]; + } + MPPickFieldTableModelRowItem *rowItem = [self.tableModel itemAtIndex:row]; + view.textField.stringValue = @""; + + if(!rowItem) { + return view; + } + /* group view or first column */ + if(tableColumn == nil || [tableColumn.identifier isEqualToString:@"AutomaticTableColumnIdentifier.0"]) { + view.textField.stringValue = rowItem.name; + } + else if([tableColumn.identifier isEqualToString:@"AutomaticTableColumnIdentifier.1"]) { + view.textField.stringValue = rowItem.value; + } + + return view; +} + +- (BOOL)tableView:(NSTableView *)tableView isGroupRow:(NSInteger)row { + MPPickFieldTableModelRowItem *rowItem = [self.tableModel itemAtIndex:row]; + return rowItem.isGroup; +} + +- (BOOL)tableView:(NSTableView *)tableView shouldSelectRow:(NSInteger)row { + return ![self tableView:tableView isGroupRow:row]; +} + +- (void)tableViewSelectionDidChange:(NSNotification *)notification { + NSTableView *tableView = notification.object; + if(tableView.selectedRow < 0) { + self.pickedValue = @""; + } + else { + MPPickFieldTableModelRowItem *item = [self.tableModel itemAtIndex:tableView.selectedRow]; + self.pickedValue = item ? item.value : @""; + } +} + +@end diff --git a/MacPass/MPTreeDelegate.m b/MacPass/MPTreeDelegate.m index 14760a4e..5c67a485 100644 --- a/MacPass/MPTreeDelegate.m +++ b/MacPass/MPTreeDelegate.m @@ -25,6 +25,7 @@ #import "MPDocument.h" #import "MPSettingsHelper.h" #import "MPPickcharViewController.h" +#import "MPPickfieldViewController.h" @interface MPTreeDelegate (); @@ -80,6 +81,22 @@ } - (NSString *)tree:(KPKTree *)tree resolvePickFieldPlaceholderForEntry:(KPKEntry *)entry { + MPPickfieldViewController *pickFieldViewController = [[MPPickfieldViewController alloc] init]; + + pickFieldViewController.representedObject = entry; + + NSPanel *panel = [[NSPanel alloc] initWithContentRect:NSMakeRect(0, 0, 100, 100) + styleMask:NSWindowStyleMaskNonactivatingPanel|NSWindowStyleMaskTitled|NSWindowStyleMaskResizable + backing:NSBackingStoreRetained + defer:YES]; + panel.level = NSScreenSaverWindowLevel; + panel.contentViewController = pickFieldViewController; + panel.title = NSLocalizedString(@"PICKFIELD_WINDOW_TITLE", @"Window displayed to the user to pick an amout of characters"); + [panel center]; + if(NSModalResponseOK == [NSApp runModalForWindow:panel]) { + /* add appropriate key press comamnds? or let the pick-char view-controller handel this? */ + return pickFieldViewController.pickedValue; + } return @""; } diff --git a/MacPass/PickcharView.xib b/MacPass/PickcharView.xib index 164d89f4..5d56313b 100644 --- a/MacPass/PickcharView.xib +++ b/MacPass/PickcharView.xib @@ -154,7 +154,7 @@ - + diff --git a/MacPass/PickfieldView.xib b/MacPass/PickfieldView.xib new file mode 100644 index 00000000..bb9c1d70 --- /dev/null +++ b/MacPass/PickfieldView.xib @@ -0,0 +1,153 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +