using NSWindowController.document instead of custom properites

This commit is contained in:
michael starke
2015-07-15 10:22:07 +02:00
parent 37882bb9ad
commit 135875bea4
4 changed files with 62 additions and 39 deletions

View File

@@ -144,6 +144,9 @@
if(!self.isDirty) { if(!self.isDirty) {
return; return;
} }
if(!self.document) {
return; // no document, just leave
}
/* Update all stuff that might have changed */ /* Update all stuff that might have changed */
KPKMetaData *metaData = ((MPDocument *)self.document).tree.metaData; KPKMetaData *metaData = ((MPDocument *)self.document).tree.metaData;
[self _setupDatabaseTab:metaData]; [self _setupDatabaseTab:metaData];
@@ -155,7 +158,7 @@
- (void)showSettingsTab:(MPDatabaseSettingsTab)tab { - (void)showSettingsTab:(MPDatabaseSettingsTab)tab {
/* /*
We need to make sure the window is loaded We need to make sure the window is loaded
so we just call the the getter and led the loading commence so we just call the the getter and let the loading commence
*/ */
if(![self window]) { if(![self window]) {
return; return;

View File

@@ -100,17 +100,17 @@ typedef void (^MPPasswordChangedBlock)(BOOL didChangePassword);
[self.toolbarDelegate registerNotificationsForDocument:document]; [self.toolbarDelegate registerNotificationsForDocument:document];
self.toolbar = [[NSToolbar alloc] initWithIdentifier:@"MainWindowToolbar"]; self.toolbar = [[NSToolbar alloc] initWithIdentifier:@"MainWindowToolbar"];
[self.toolbar setAutosavesConfiguration:YES]; self.toolbar.autosavesConfiguration = YES;
[self.toolbar setAllowsUserCustomization:YES]; self.toolbar.allowsUserCustomization = YES;
[self.toolbar setDelegate:self.toolbarDelegate]; self.toolbar.delegate = self.toolbarDelegate;
[self.window setToolbar:self.toolbar]; self.window.toolbar = self.toolbar;
self.toolbarDelegate.toolbar = _toolbar; self.toolbarDelegate.toolbar = self.toolbar;
[self.splitView setTranslatesAutoresizingMaskIntoConstraints:NO]; [self.splitView setTranslatesAutoresizingMaskIntoConstraints:NO];
NSView *outlineView = [self.outlineViewController view]; NSView *outlineView = self.outlineViewController.view;
NSView *inspectorView = [self.inspectorViewController view]; NSView *inspectorView = self.inspectorViewController.view;
NSView *entryView = [self.entryViewController view]; NSView *entryView = self.entryViewController.view;
[self.splitView addSubview:outlineView]; [self.splitView addSubview:outlineView];
[self.splitView addSubview:entryView]; [self.splitView addSubview:entryView];
[self.splitView addSubview:inspectorView]; [self.splitView addSubview:inspectorView];
@@ -143,10 +143,10 @@ typedef void (^MPPasswordChangedBlock)(BOOL didChangePassword);
if(viewController && viewController.view) { if(viewController && viewController.view) {
newContentView = viewController.view; newContentView = viewController.view;
} }
NSView *contentView = [[self window] contentView]; NSView *contentView = self.window.contentView;
NSView *oldSubView = nil; NSView *oldSubView = nil;
if([[contentView subviews] count] == 1) { if(contentView.subviews.count == 1) {
oldSubView = [contentView subviews][0]; oldSubView = contentView.subviews[0];
} }
if(oldSubView == newContentView) { if(oldSubView == newContentView) {
return; // View is already present return; // View is already present
@@ -192,8 +192,8 @@ typedef void (^MPPasswordChangedBlock)(BOOL didChangePassword);
#pragma mark Actions #pragma mark Actions
- (void)saveDocument:(id)sender { - (void)saveDocument:(id)sender {
self.passwordChangedBlock = nil; self.passwordChangedBlock = nil;
MPDocument *document = [self document]; MPDocument *document = self.document;
NSString *fileType = [document fileType]; NSString *fileType = document.fileType;
/* we did open as legacy */ /* we did open as legacy */
if([fileType isEqualToString:MPLegacyDocumentUTI]) { if([fileType isEqualToString:MPLegacyDocumentUTI]) {
if(document.tree.minimumVersion != KPKLegacyVersion) { if(document.tree.minimumVersion != KPKLegacyVersion) {
@@ -206,7 +206,10 @@ typedef void (^MPPasswordChangedBlock)(BOOL didChangePassword);
[alert addButtonWithTitle:NSLocalizedString(@"CANCEL", "Cancel")]; [alert addButtonWithTitle:NSLocalizedString(@"CANCEL", "Cancel")];
//[[alert buttons][2] setKeyEquivalent:[NSString stringWithFormat:@"%c", 0x1b]]; //[[alert buttons][2] setKeyEquivalent:[NSString stringWithFormat:@"%c", 0x1b]];
[alert beginSheetModalForWindow:[self window] modalDelegate:self didEndSelector:@selector(_dataLossOnSaveAlertDidEnd:returnCode:contextInfo:) contextInfo:NULL]; [alert beginSheetModalForWindow:self.window
modalDelegate:self
didEndSelector:@selector(_dataLossOnSaveAlertDidEnd:returnCode:contextInfo:)
contextInfo:NULL];
return; return;
} }
} }
@@ -297,10 +300,15 @@ typedef void (^MPPasswordChangedBlock)(BOOL didChangePassword);
- (void)editPassword:(id)sender { - (void)editPassword:(id)sender {
if(!self.passwordEditWindowController) { if(!self.passwordEditWindowController) {
self.passwordEditWindowController = [[MPPasswordEditWindowController alloc] initWithDocument:[self document]]; self.passwordEditWindowController = [[MPPasswordEditWindowController alloc] init];
self.passwordEditWindowController.delegate = self; self.passwordEditWindowController.delegate = self;
} }
[NSApp beginSheet:[self.passwordEditWindowController window] modalForWindow:[self window] modalDelegate:nil didEndSelector:NULL contextInfo:NULL]; [self.document addWindowController:self.passwordEditWindowController];
[NSApp beginSheet:self.passwordEditWindowController.window
modalForWindow:self.window
modalDelegate:self
didEndSelector:@selector(_editPasswordSheetDidEnd:returnCode:contextInfo:)
contextInfo:NULL];
} }
- (void)showDatabaseSettings:(id)sender { - (void)showDatabaseSettings:(id)sender {
@@ -481,7 +489,10 @@ typedef void (^MPPasswordChangedBlock)(BOOL didChangePassword);
[alert addButtonWithTitle:NSLocalizedString(@"CHANGE_PASSWORD_WITH_DOTS", "")]; [alert addButtonWithTitle:NSLocalizedString(@"CHANGE_PASSWORD_WITH_DOTS", "")];
[alert addButtonWithTitle:NSLocalizedString(@"CANCEL", "")]; [alert addButtonWithTitle:NSLocalizedString(@"CANCEL", "")];
[[alert buttons][1] setKeyEquivalent:[NSString stringWithFormat:@"%c", 0x1b]]; [[alert buttons][1] setKeyEquivalent:[NSString stringWithFormat:@"%c", 0x1b]];
[alert beginSheetModalForWindow:[self window] modalDelegate:self didEndSelector:@selector(_enforcePasswordChangeAlertDidEnd:returnCode:contextInfo:) contextInfo:NULL]; [alert beginSheetModalForWindow:self.window
modalDelegate:self
didEndSelector:@selector(_enforcePasswordChangeAlertDidEnd:returnCode:contextInfo:)
contextInfo:NULL];
} }
else if(document.shouldRecommendPasswordChange) { else if(document.shouldRecommendPasswordChange) {
NSAlert *alert = [[NSAlert alloc] init]; NSAlert *alert = [[NSAlert alloc] init];
@@ -491,7 +502,10 @@ typedef void (^MPPasswordChangedBlock)(BOOL didChangePassword);
[alert addButtonWithTitle:NSLocalizedString(@"CHANGE_PASSWORD_WITH_DOTS", "")]; [alert addButtonWithTitle:NSLocalizedString(@"CHANGE_PASSWORD_WITH_DOTS", "")];
[alert addButtonWithTitle:NSLocalizedString(@"CANCEL", "")]; [alert addButtonWithTitle:NSLocalizedString(@"CANCEL", "")];
[[alert buttons][1] setKeyEquivalent:[NSString stringWithFormat:@"%c", 0x1b]]; [[alert buttons][1] setKeyEquivalent:[NSString stringWithFormat:@"%c", 0x1b]];
[alert beginSheetModalForWindow:[self window] modalDelegate:self didEndSelector:@selector(_recommentPasswordChangeAlertDidEnd:returnCode:contextInfo:) contextInfo:NULL]; [alert beginSheetModalForWindow:self.window
modalDelegate:self
didEndSelector:@selector(_recommentPasswordChangeAlertDidEnd:returnCode:contextInfo:)
contextInfo:NULL];
} }
} }
@@ -537,23 +551,35 @@ typedef void (^MPPasswordChangedBlock)(BOOL didChangePassword);
}); });
} }
#pragma mark Sheet handling
- (void)_editPasswordSheetDidEnd:(NSWindow *)sheet returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo {
[self.document removeWindowController:self.passwordEditWindowController];
self.passwordEditWindowController = nil;
}
#pragma mark - #pragma mark -
#pragma mark UI Helper #pragma mark UI Helper
- (void)_showDatabaseSetting:(MPDatabaseSettingsTab)tab { - (void)_showDatabaseSetting:(MPDatabaseSettingsTab)tab {
if(!self.documentSettingsWindowController) { if(!self.documentSettingsWindowController) {
_documentSettingsWindowController = [[MPDatabaseSettingsWindowController alloc] init]; self.documentSettingsWindowController = [[MPDatabaseSettingsWindowController alloc] init];
} }
[self.document addWindowController:_documentSettingsWindowController]; [self.document addWindowController:self.documentSettingsWindowController];
[self.documentSettingsWindowController showSettingsTab:tab]; [self.documentSettingsWindowController showSettingsTab:tab];
[[NSApplication sharedApplication] beginSheet:[self.documentSettingsWindowController window] [[NSApplication sharedApplication] beginSheet:self.documentSettingsWindowController.window
modalForWindow:[self window] modalForWindow:self.window
modalDelegate:nil modalDelegate:self
didEndSelector:NULL didEndSelector:@selector(_settingsSheetDidEnd:returnCode:contextInfo:)
contextInfo:NULL]; contextInfo:NULL];
} }
- (void)_settingsSheetDidEnd:(NSWindow *)sheet returnCode:(NSInteger)returnCode contextInfo:(void *)contextInfo {
/* cleanup the window controller */
[self.document removeWindowController:self.documentSettingsWindowController];
self.documentSettingsWindowController = nil;
}
- (BOOL)_isInspectorVisible { - (BOOL)_isInspectorVisible {
NSView *inspectorView = [self.inspectorViewController view]; NSView *inspectorView = [self.inspectorViewController view];
return (nil != [inspectorView superview]); return (nil != [inspectorView superview]);

View File

@@ -35,13 +35,6 @@
//@property (nonatomic,assign) BOOL allowsEmptyPasswordOrKey; //@property (nonatomic,assign) BOOL allowsEmptyPasswordOrKey;
@property (weak) id<MPPasswordEditWindowDelegate> delegate; @property (weak) id<MPPasswordEditWindowDelegate> delegate;
/**
* Dedicated initializer for the Windowcontroller
* @param document The Database document that is currently active
* @return initalized windowcontroller
*/
- (id)initWithDocument:(MPDocument *)document;
- (IBAction)clearKey:(id)sender; - (IBAction)clearKey:(id)sender;
- (IBAction)generateKey:(id)sender; - (IBAction)generateKey:(id)sender;

View File

@@ -18,7 +18,6 @@
@interface MPPasswordEditWindowController () @interface MPPasswordEditWindowController ()
@property (nonatomic, weak) MPDocument *currentDocument;
@property (nonatomic, assign) BOOL showPassword; @property (nonatomic, assign) BOOL showPassword;
@property (nonatomic, assign) BOOL enablePassword; @property (nonatomic, assign) BOOL enablePassword;
@property (nonatomic, assign) BOOL hasValidPasswordOrKey; @property (nonatomic, assign) BOOL hasValidPasswordOrKey;
@@ -32,13 +31,12 @@
return @"PasswordEditWindow"; return @"PasswordEditWindow";
} }
- (id)initWithDocument:(MPDocument *)document { - (id)initWithWindow:(NSWindow *)window {
self = [super initWithWindow:nil]; self = [super initWithWindow:window];
if(self){ if(self){
//_allowsEmptyPasswordOrKey = YES; //_allowsEmptyPasswordOrKey = YES;
_showPassword = NO; _showPassword = NO;
_hasValidPasswordOrKey = NO; _hasValidPasswordOrKey = NO;
_currentDocument = document;
} }
return self; return self;
} }
@@ -47,7 +45,8 @@
[super windowDidLoad]; [super windowDidLoad];
[self.togglePasswordButton bind:NSValueBinding toObject:self withKeyPath:NSStringFromSelector(@selector(showPassword)) options:nil]; [self.togglePasswordButton bind:NSValueBinding toObject:self withKeyPath:NSStringFromSelector(@selector(showPassword)) options:nil];
[[self window] setDefaultButtonCell:[self.changePasswordButton cell]]; [[self window] setDefaultButtonCell:[self.changePasswordButton cell]];
self.enablePassword = _currentDocument.compositeKey.hasPassword; MPDocument *document = self.document;
self.enablePassword = document.compositeKey.hasPassword;
} }
- (void)updateView { - (void)updateView {
@@ -107,7 +106,8 @@
- (IBAction)save:(id)sender { - (IBAction)save:(id)sender {
const BOOL hasPassword = ([self.hasPasswordSwitchButton state] == NSOnState); const BOOL hasPassword = ([self.hasPasswordSwitchButton state] == NSOnState);
NSString *password = hasPassword ? [self.passwordTextField stringValue] : nil; NSString *password = hasPassword ? [self.passwordTextField stringValue] : nil;
[_currentDocument changePassword:password keyFileURL:[self.keyfilePathControl URL]]; MPDocument *document = self.document;
[document changePassword:password keyFileURL:[self.keyfilePathControl URL]];
[self dismissSheet:NSRunStoppedResponse]; [self dismissSheet:NSRunStoppedResponse];
if(self.delegate && [self.delegate respondsToSelector:@selector(didFinishPasswordEditing:)]) { if(self.delegate && [self.delegate respondsToSelector:@selector(didFinishPasswordEditing:)]) {
[self.delegate didFinishPasswordEditing:YES]; [self.delegate didFinishPasswordEditing:YES];
@@ -126,7 +126,8 @@
} }
- (IBAction)generateKey:(id)sender { - (IBAction)generateKey:(id)sender {
NSData *data = [NSData generateKeyfiledataForVersion:_currentDocument.tree.minimumVersion]; MPDocument *document = self.document;
NSData *data = [NSData generateKeyfiledataForVersion:document.tree.minimumVersion];
if(data) { if(data) {
NSSavePanel *savePanel = [NSSavePanel savePanel]; NSSavePanel *savePanel = [NSSavePanel savePanel];
[savePanel setAllowedFileTypes:@[@"key", @"xml"]]; [savePanel setAllowedFileTypes:@[@"key", @"xml"]];