Using NSObjectController for entry inspector

This commit is contained in:
michael starke
2015-12-17 19:07:44 +01:00
parent 8bce3907f3
commit c7590dcb60
6 changed files with 169 additions and 225 deletions

View File

@@ -50,14 +50,22 @@ typedef NS_ENUM(NSUInteger, MPEntryTab) {
@property (nonatomic, assign) BOOL showPassword;
@property (nonatomic, assign) MPEntryTab activeTab;
@property (strong) NSPopover *activePopover;
@property (strong) NSObjectController *entryController;
@property (readonly, nonatomic) KPKEntry *contentEntry;
@property (nonatomic, weak) KPKEntry *entry;
//@property (nonatomic, weak) KPKEntry *entry;
@property (strong) MPTemporaryFileStorage *quicklookStorage;
@end
@implementation MPEntryInspectorViewController
static NSString *kMPContentBindingString1 = @"content.%@";
static NSString *kMPContentBindingString2 = @"content.%@.%@";
static NSString *kMPContentBindingString3 = @"content.%@.%@.%@";
- (NSString *)nibName {
return @"EntryInspectorView";
}
@@ -77,10 +85,19 @@ typedef NS_ENUM(NSUInteger, MPEntryTab) {
_attachmentTableDelegate.viewController = self;
_customFieldTableDelegate.viewController = self;
_activeTab = MPEntryTabGeneral;
_entryController = [[NSObjectController alloc] init];
_entryController.objectClass = [KPKEntry class];
}
return self;
}
- (KPKEntry *)contentEntry {
if([self.entryController.content isKindOfClass:[KPKEntry class]]) {
return self.entryController.content;
}
return nil;
}
- (void)didLoadView {
[self _addScrollViewWithView:self.generalView atTab:MPEntryTabGeneral];
@@ -104,36 +121,30 @@ typedef NS_ENUM(NSUInteger, MPEntryTab) {
self.windowAssociationsTableView.delegate = _windowAssociationsTableDelegate;
[self.windowAssociationsTableView bind:NSContentBinding toObject:_windowAssociationsController withKeyPath:NSStringFromSelector(@selector(arrangedObjects)) options:nil];
[self.windowAssociationsTableView bind:NSSelectionIndexesBinding toObject:_windowAssociationsController withKeyPath:NSSelectionIndexesBinding options:nil];
self.windowTitleComboBox.delegate = _windowTitleMenuDelegate;
[self.passwordTextField bind:NSStringFromSelector(@selector(showPassword)) toObject:self withKeyPath:NSStringFromSelector(@selector(showPassword)) options:nil];
[self.togglePassword bind:NSValueBinding toObject:self withKeyPath:NSStringFromSelector(@selector(showPassword)) options:nil];
[self _setupViewBindings];
}
- (void)setupBindings:(MPDocument *)document {
[self bind:NSStringFromSelector(@selector(entry)) toObject:document withKeyPath:NSStringFromSelector(@selector(selectedEntry)) options:nil];
[self.entryController bind:NSContentObjectBinding toObject:document withKeyPath:NSStringFromSelector(@selector(selectedEntry)) options:nil];
}
- (void)setEntry:(KPKEntry *)entry {
if(_entry != entry) {
_entry = entry;
[self _updateContent];
}
}
- (void)regsiterNotificationsForDocument:(MPDocument *)document {
- (void)registerNotificationsForDocument:(MPDocument *)document {
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(_didAddEntry:)
name:MPDocumentDidAddEntryNotification
object:document];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(_willSave:)
name:MPDocumentWillSaveNotification
object:document];
// [[NSNotificationCenter defaultCenter] addObserver:self
// selector:@selector(_willSave:)
// name:MPDocumentWillSaveNotification
// object:document];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(_didBeginEditingSelectedItem:)
name:MPDocumentDidBeginEditingSelectedItem
@@ -159,12 +170,12 @@ typedef NS_ENUM(NSUInteger, MPEntryTab) {
- (void)addCustomField:(id)sender {
MPDocument *document = [[self windowController] document];
[document createCustomAttribute:self.entry];
[document createCustomAttribute:self.entryController.content];
}
- (void)removeCustomField:(id)sender {
NSUInteger index = [sender tag];
KPKAttribute *attribute = self.entry.customAttributes[index];
[self.entry removeCustomAttribute:attribute];
KPKAttribute *attribute = self.contentEntry.customAttributes[index];
[self.contentEntry removeCustomAttribute:attribute];
}
- (void)saveAttachment:(id)sender {
@@ -172,15 +183,15 @@ typedef NS_ENUM(NSUInteger, MPEntryTab) {
if(row < 0) {
return; // No selection
}
KPKBinary *binary = self.entry.binaries[row];
KPKBinary *binary = self.contentEntry.binaries[row];
NSSavePanel *savePanel = [NSSavePanel savePanel];
[savePanel setCanCreateDirectories:YES];
[savePanel setNameFieldStringValue:binary.name];
savePanel.canCreateDirectories = YES;
savePanel.nameFieldStringValue = binary.name;
[savePanel beginSheetModalForWindow:[[self windowController] window] completionHandler:^(NSInteger result) {
[savePanel beginSheetModalForWindow:self.windowController.window completionHandler:^(NSInteger result) {
if(result == NSFileHandlingPanelOKButton) {
NSError *error;
BOOL sucess = [binary saveToLocation:[savePanel URL] error:&error];
BOOL sucess = [binary saveToLocation:savePanel.URL error:&error];
if(!sucess && error) {
[NSApp presentError:error];
}
@@ -190,37 +201,37 @@ typedef NS_ENUM(NSUInteger, MPEntryTab) {
- (void)addAttachment:(id)sender {
NSOpenPanel *openPanel = [NSOpenPanel openPanel];
[openPanel setCanChooseDirectories:NO];
[openPanel setCanChooseFiles:YES];
[openPanel setAllowsMultipleSelection:YES];
[openPanel beginSheetModalForWindow:[[self windowController] window] completionHandler:^(NSInteger result) {
openPanel.canChooseDirectories = NO;
openPanel.canChooseFiles = YES;
openPanel.allowsMultipleSelection = YES;
[openPanel beginSheetModalForWindow:self.windowController.window completionHandler:^(NSInteger result) {
if(result == NSFileHandlingPanelOKButton) {
for (NSURL *attachmentURL in [openPanel URLs]) {
for (NSURL *attachmentURL in openPanel.URLs) {
KPKBinary *binary = [[KPKBinary alloc] initWithContentsOfURL:attachmentURL];
[self.entry addBinary:binary];
[self.contentEntry addBinary:binary];
}
}
}];
}
- (void)removeAttachment:(id)sender {
NSInteger row = [self.attachmentTableView selectedRow];
NSInteger row = self.attachmentTableView.selectedRow;
if(row < 0) {
return; // no selection
}
KPKBinary *binary = self.entry.binaries[row];
[self.entry removeBinary:binary];
KPKBinary *binary = self.contentEntry.binaries[row];
[self.contentEntry removeBinary:binary];
}
- (void)addWindowAssociation:(id)sender {
KPKWindowAssociation *associtation = [[KPKWindowAssociation alloc] initWithWindowTitle:NSLocalizedString(@"DEFAULT_WINDOW_TITLE", "") keystrokeSequence:nil];
[self.entry.autotype addAssociation:associtation];
[self.contentEntry.autotype addAssociation:associtation];
}
- (void)removeWindowAssociation:(id)sender {
NSInteger row = [self.windowAssociationsTableView selectedRow];
if(row > - 1 && row < [self.entry.autotype.associations count]) {
[self.entry.autotype removeAssociation:self.entry.autotype.associations[row]];
if(row > - 1 && row < [self.contentEntry.autotype.associations count]) {
[self.contentEntry.autotype removeAssociation:self.contentEntry.autotype.associations[row]];
}
}
@@ -273,7 +284,7 @@ typedef NS_ENUM(NSUInteger, MPEntryTab) {
- (void)_updatePreviewItemForPanel:(QLPreviewPanel *)panel {
NSInteger row = [self.attachmentTableView selectedRow];
NSAssert(row > -1, @"Row needs to be selected");
KPKBinary *binary = self.entry.binaries[row];
KPKBinary *binary = self.contentEntry.binaries[row];
MPTemporaryFileStorage *oldStorage = (MPTemporaryFileStorage *)panel.dataSource;
[[MPTemporaryFileStorageCenter defaultCenter] unregisterStorage:oldStorage];
panel.dataSource = [[MPTemporaryFileStorageCenter defaultCenter] storageForBinary:binary];
@@ -286,7 +297,7 @@ typedef NS_ENUM(NSUInteger, MPEntryTab) {
[self.generatePasswordButton setEnabled:NO];
MPPasswordCreatorViewController *viewController = [[MPPasswordCreatorViewController alloc] init];
viewController.allowsEntryDefaults = YES;
viewController.entry = self.entry;
viewController.entry = self.contentEntry;
[self _showPopopver:viewController atView:self.passwordTextField onEdge:NSMinYEdge];
}
@@ -315,8 +326,8 @@ typedef NS_ENUM(NSUInteger, MPEntryTab) {
if([controller respondsToSelector:@selector(generatedPassword)]) {
NSString *password = [controller generatedPassword];
/* We should only use the password if there is actually one */
if([password length] > 0) {
self.entry.password = [controller generatedPassword];
if(password.length > 0) {
self.contentEntry.password = [controller generatedPassword];
}
}
/* TODO: Check for Icon wizard */
@@ -349,13 +360,13 @@ typedef NS_ENUM(NSUInteger, MPEntryTab) {
NSDictionary *views = NSDictionaryOfVariableBindings(view, scrollView);
[scrollView.superview addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[scrollView]|"
options:0
metrics:nil
views:views ]];
options:0
metrics:nil
views:views ]];
[scrollView.superview addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[scrollView]|"
options:0
metrics:nil
views:views]];
options:0
metrics:nil
views:views]];
[clipView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[view]|"
options:0
metrics:nil
@@ -365,7 +376,7 @@ typedef NS_ENUM(NSUInteger, MPEntryTab) {
#pragma mark -
#pragma mark Entry Selection
- (void)_updateContent {
- (void)_setupViewBindings {
[self _bindEntry];
[self _bindAttachments];
[self _bindCustomFields];
@@ -373,100 +384,103 @@ typedef NS_ENUM(NSUInteger, MPEntryTab) {
}
- (void)_bindEntry {
static NSArray *items;
if(!items) {
items = @[ self.titleTextField,
self.passwordTextField,
self.usernameTextField,
self.URLTextField,
self.expiresCheckButton,
self.tagsTokenField ];
}
if(self.entry) {
[self.titleTextField bind:NSValueBinding toObject:self.entry withKeyPath:NSStringFromSelector(@selector(title)) options:nil];
[self.passwordTextField bind:NSValueBinding toObject:self.entry withKeyPath:NSStringFromSelector(@selector(password)) options:nil];
[self.usernameTextField bind:NSValueBinding toObject:self.entry withKeyPath:NSStringFromSelector(@selector(username)) options:nil];
[self.URLTextField bind:NSValueBinding toObject:self.entry withKeyPath:NSStringFromSelector(@selector(url)) options:nil];
[self.expiresCheckButton bind:NSTitleBinding
toObject:self.entry.timeInfo
withKeyPath:NSStringFromSelector(@selector(expirationDate))
options:@{ NSValueTransformerNameBindingOption:MPExpiryDateValueTransformer }];
[self.expiresCheckButton bind:NSValueBinding toObject:self.entry.timeInfo withKeyPath:NSStringFromSelector(@selector(expires)) options:nil];
[self.tagsTokenField bind:NSValueBinding toObject:self.entry withKeyPath:NSStringFromSelector(@selector(tags)) options:nil];
[self.uuidTextField bind:NSValueBinding toObject:self.entry.uuid withKeyPath:NSStringFromSelector(@selector(UUIDString)) options:nil];
self.uuidTextField.editable = NO;
/* Setup enable/disable */
for(id item in items) {
[item bind:NSEnabledBinding toObject:self.entry withKeyPath:NSStringFromSelector(@selector(isEditable)) options:nil];
}
}
else {
for(id item in items) {
[item unbind:NSValueBinding];
[item unbind:NSEnabledBinding];
}
[self.uuidTextField unbind:NSValueBinding];
[self.expiresCheckButton unbind:NSTitleBinding];
}
[self.titleTextField bind:NSValueBinding
toObject:self.entryController
withKeyPath:[NSString stringWithFormat:kMPContentBindingString1, NSStringFromSelector(@selector(title))]
options:@{ NSNullPlaceholderBindingOption: NSLocalizedString(@"NONE", "")} ];
[self.passwordTextField bind:NSValueBinding
toObject:self.entryController
withKeyPath:[NSString stringWithFormat:kMPContentBindingString1, NSStringFromSelector(@selector(password))]
options:@{ NSNullPlaceholderBindingOption: NSLocalizedString(@"NONE", "") }];
[self.usernameTextField bind:NSValueBinding
toObject:self.entryController
withKeyPath:[NSString stringWithFormat:kMPContentBindingString1, NSStringFromSelector(@selector(username))]
options:@{ NSNullPlaceholderBindingOption: NSLocalizedString(@"NONE", "") }];
[self.URLTextField bind:NSValueBinding
toObject:self.entryController
withKeyPath:[NSString stringWithFormat:kMPContentBindingString1, NSStringFromSelector(@selector(url))]
options:@{ NSNullPlaceholderBindingOption: NSLocalizedString(@"NONE", "")}];
[self.expiresCheckButton bind:NSTitleBinding
toObject:self.entryController
withKeyPath:[NSString stringWithFormat:kMPContentBindingString2, NSStringFromSelector(@selector(timeInfo)), NSStringFromSelector(@selector(expirationDate))]
options:@{ NSValueTransformerNameBindingOption:MPExpiryDateValueTransformer }];
[self.expiresCheckButton bind:NSValueBinding
toObject:self.entryController
withKeyPath:[NSString stringWithFormat:kMPContentBindingString2, NSStringFromSelector(@selector(timeInfo)), NSStringFromSelector(@selector(expires))]
options:nil];
[self.tagsTokenField bind:NSValueBinding
toObject:self.entryController
withKeyPath:[NSString stringWithFormat:kMPContentBindingString1, NSStringFromSelector(@selector(tags))]
options:nil];
[self.uuidTextField bind:NSValueBinding
toObject:self.entryController
withKeyPath:[NSString stringWithFormat:kMPContentBindingString2, NSStringFromSelector(@selector(uuid)), NSStringFromSelector(@selector(UUIDString))]
options:nil];
self.uuidTextField.editable = NO;
/*for(id item in items) {
[item bind:NSEnabledBinding toObject:self.entryController withKeyPath:NSStringFromSelector(@selector(isEditable)) options:nil];
}*/
}
- (void)_bindAttachments {
if(self.entry) {
[_attachmentsController bind:NSContentArrayBinding toObject:self.entry withKeyPath:NSStringFromSelector(@selector(binaries)) options:nil];
}
else if([_attachmentsController content] != nil){
[_attachmentsController unbind:NSContentArrayBinding];
[_attachmentsController setContent:nil];
}
[_attachmentsController bind:NSContentArrayBinding
toObject:self.entryController
withKeyPath:[NSString stringWithFormat:kMPContentBindingString1, NSStringFromSelector(@selector(binaries))]
options:nil];
}
- (void)_bindCustomFields {
if(self.entry) {
[_customFieldsController bind:NSContentArrayBinding toObject:self.entry withKeyPath:NSStringFromSelector(@selector(customAttributes)) options:nil];
}
else if ([_customFieldsController content] != nil ) {
[_customFieldsController unbind:NSContentArrayBinding];
[_customFieldsController setContent:nil];
}
[_customFieldsController bind:NSContentArrayBinding
toObject:self.entryController
withKeyPath:[NSString stringWithFormat:kMPContentBindingString1, NSStringFromSelector(@selector(customAttributes))]
options:nil];
}
- (void)_bindAutotype {
if(self.entry) {
[self.enableAutotypeCheckButton bind:NSValueBinding toObject:self.entry.autotype withKeyPath:NSStringFromSelector(@selector(isEnabled)) options:nil];
[self.obfuscateAutotypeCheckButton bind:NSValueBinding toObject:self.entry.autotype withKeyPath:NSStringFromSelector(@selector(obfuscateDataTransfer)) options:nil];
[self.customEntrySequenceTextField bind:NSEnabledBinding toObject:self.entry.autotype withKeyPath:NSStringFromSelector(@selector(isEnabled)) options:nil];
[self.customEntrySequenceTextField bind:NSValueBinding toObject:self.entry.autotype withKeyPath:NSStringFromSelector(@selector(defaultKeystrokeSequence)) options:@{ NSValidatesImmediatelyBindingOption: @(YES) }];
[_windowAssociationsController bind:NSContentArrayBinding toObject:self.entry.autotype withKeyPath:NSStringFromSelector(@selector(associations)) options:nil];
//[self.windowTitleComboBox setStringValue:@""];
NSString *selectedWindowTitlePath = [[NSString alloc] initWithFormat:@"selection.%@", NSStringFromSelector(@selector(windowTitle))];
[self.windowTitleComboBox bind:NSValueBinding toObject:_windowAssociationsController withKeyPath:selectedWindowTitlePath options:nil];
NSString *selectedWindowKeyStrokesPath = [[NSString alloc] initWithFormat:@"selection.%@", NSStringFromSelector(@selector(keystrokeSequence))];
[self.associationSequenceTextField bind:NSValueBinding toObject:_windowAssociationsController withKeyPath:selectedWindowKeyStrokesPath options:nil];
}
else {
[self.enableAutotypeCheckButton unbind:NSValueBinding];
[self.customEntrySequenceTextField unbind:NSEnabledBinding];
[self.customEntrySequenceTextField unbind:NSValueBinding];
if([_windowAssociationsController content] != nil) {
[_windowAssociationsController unbind:NSContentArrayBinding];
[_windowAssociationsController setContent:nil];
}
[self.windowTitleComboBox unbind:NSValueBinding];
[self.associationSequenceTextField unbind:NSValueBinding];
}
- (void)_bindAutotype {
[self.enableAutotypeCheckButton bind:NSValueBinding
toObject:self.entryController
withKeyPath:[NSString stringWithFormat:kMPContentBindingString2, NSStringFromSelector(@selector(autotype)), NSStringFromSelector(@selector(isEnabled))] options:nil];
[self.obfuscateAutotypeCheckButton bind:NSValueBinding
toObject:self.entryController
withKeyPath:[NSString stringWithFormat:kMPContentBindingString2, NSStringFromSelector(@selector(autotype)), NSStringFromSelector(@selector(obfuscateDataTransfer))]
options:nil];
[self.customEntrySequenceTextField bind:NSEnabledBinding
toObject:self.entryController
withKeyPath:[NSString stringWithFormat:kMPContentBindingString2, NSStringFromSelector(@selector(autotype)), NSStringFromSelector(@selector(isEnabled))]
options:nil];
[self.customEntrySequenceTextField bind:NSValueBinding
toObject:self.entryController
withKeyPath:[NSString stringWithFormat:kMPContentBindingString2, NSStringFromSelector(@selector(autotype)), NSStringFromSelector(@selector(defaultKeystrokeSequence))]
options:@{ NSValidatesImmediatelyBindingOption: @(YES) }];
[_windowAssociationsController bind:NSContentArrayBinding
toObject:self.entryController
withKeyPath:[NSString stringWithFormat:kMPContentBindingString2, NSStringFromSelector(@selector(autotype)), NSStringFromSelector(@selector(associations))]
options:nil];
[self.windowTitleComboBox setStringValue:@""];
[self.windowTitleComboBox bind:NSValueBinding
toObject:_windowAssociationsController
withKeyPath:[NSString stringWithFormat:@"selection.%@", NSStringFromSelector(@selector(windowTitle))]
options:nil];
[self.associationSequenceTextField bind:NSValueBinding
toObject:_windowAssociationsController
withKeyPath:[NSString stringWithFormat:@"selection.%@", NSStringFromSelector(@selector(keystrokeSequence))]
options:nil];
}
- (void)_toggleEditing:(BOOL)edit {
NSArray <NSTextField *> *textFields = @[self.titleTextField,
self.usernameTextField,
self.URLTextField,
self.passwordTextField,
self.tagsTokenField
/*self.createdTextField,
self.modifiedTextField*/
];
self.usernameTextField,
self.URLTextField,
self.passwordTextField,
self.tagsTokenField
/*self.createdTextField,
self.modifiedTextField*/
];
for(NSTextField *t in textFields) {
t.editable = edit;
t.selectable = edit;
@@ -483,7 +497,7 @@ typedef NS_ENUM(NSUInteger, MPEntryTab) {
- (void)_willSave:(NSNotification *)notification {
// Force selected textfield to end editing
[[[self view] window] makeFirstResponder:nil];
[self.view.window makeFirstResponder:nil];
}
@end