diff --git a/MacPass/MPDatabaseSettingsWindowController.m b/MacPass/MPDatabaseSettingsWindowController.m index d00c6545..4b7b0c72 100644 --- a/MacPass/MPDatabaseSettingsWindowController.m +++ b/MacPass/MPDatabaseSettingsWindowController.m @@ -144,6 +144,9 @@ if(!self.isDirty) { return; } + if(!self.document) { + return; // no document, just leave + } /* Update all stuff that might have changed */ KPKMetaData *metaData = ((MPDocument *)self.document).tree.metaData; [self _setupDatabaseTab:metaData]; @@ -155,7 +158,7 @@ - (void)showSettingsTab:(MPDatabaseSettingsTab)tab { /* 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]) { return; diff --git a/MacPass/MPDocumentWindowController.m b/MacPass/MPDocumentWindowController.m index f90f7a12..5dedac86 100644 --- a/MacPass/MPDocumentWindowController.m +++ b/MacPass/MPDocumentWindowController.m @@ -100,17 +100,17 @@ typedef void (^MPPasswordChangedBlock)(BOOL didChangePassword); [self.toolbarDelegate registerNotificationsForDocument:document]; self.toolbar = [[NSToolbar alloc] initWithIdentifier:@"MainWindowToolbar"]; - [self.toolbar setAutosavesConfiguration:YES]; - [self.toolbar setAllowsUserCustomization:YES]; - [self.toolbar setDelegate:self.toolbarDelegate]; - [self.window setToolbar:self.toolbar]; - self.toolbarDelegate.toolbar = _toolbar; + self.toolbar.autosavesConfiguration = YES; + self.toolbar.allowsUserCustomization = YES; + self.toolbar.delegate = self.toolbarDelegate; + self.window.toolbar = self.toolbar; + self.toolbarDelegate.toolbar = self.toolbar; [self.splitView setTranslatesAutoresizingMaskIntoConstraints:NO]; - NSView *outlineView = [self.outlineViewController view]; - NSView *inspectorView = [self.inspectorViewController view]; - NSView *entryView = [self.entryViewController view]; + NSView *outlineView = self.outlineViewController.view; + NSView *inspectorView = self.inspectorViewController.view; + NSView *entryView = self.entryViewController.view; [self.splitView addSubview:outlineView]; [self.splitView addSubview:entryView]; [self.splitView addSubview:inspectorView]; @@ -143,10 +143,10 @@ typedef void (^MPPasswordChangedBlock)(BOOL didChangePassword); if(viewController && viewController.view) { newContentView = viewController.view; } - NSView *contentView = [[self window] contentView]; + NSView *contentView = self.window.contentView; NSView *oldSubView = nil; - if([[contentView subviews] count] == 1) { - oldSubView = [contentView subviews][0]; + if(contentView.subviews.count == 1) { + oldSubView = contentView.subviews[0]; } if(oldSubView == newContentView) { return; // View is already present @@ -192,8 +192,8 @@ typedef void (^MPPasswordChangedBlock)(BOOL didChangePassword); #pragma mark Actions - (void)saveDocument:(id)sender { self.passwordChangedBlock = nil; - MPDocument *document = [self document]; - NSString *fileType = [document fileType]; + MPDocument *document = self.document; + NSString *fileType = document.fileType; /* we did open as legacy */ if([fileType isEqualToString:MPLegacyDocumentUTI]) { if(document.tree.minimumVersion != KPKLegacyVersion) { @@ -206,7 +206,10 @@ typedef void (^MPPasswordChangedBlock)(BOOL didChangePassword); [alert addButtonWithTitle:NSLocalizedString(@"CANCEL", "Cancel")]; //[[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; } } @@ -297,10 +300,15 @@ typedef void (^MPPasswordChangedBlock)(BOOL didChangePassword); - (void)editPassword:(id)sender { if(!self.passwordEditWindowController) { - self.passwordEditWindowController = [[MPPasswordEditWindowController alloc] initWithDocument:[self document]]; + self.passwordEditWindowController = [[MPPasswordEditWindowController alloc] init]; 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 { @@ -481,7 +489,10 @@ typedef void (^MPPasswordChangedBlock)(BOOL didChangePassword); [alert addButtonWithTitle:NSLocalizedString(@"CHANGE_PASSWORD_WITH_DOTS", "")]; [alert addButtonWithTitle:NSLocalizedString(@"CANCEL", "")]; [[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) { NSAlert *alert = [[NSAlert alloc] init]; @@ -491,7 +502,10 @@ typedef void (^MPPasswordChangedBlock)(BOOL didChangePassword); [alert addButtonWithTitle:NSLocalizedString(@"CHANGE_PASSWORD_WITH_DOTS", "")]; [alert addButtonWithTitle:NSLocalizedString(@"CANCEL", "")]; [[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 UI Helper - (void)_showDatabaseSetting:(MPDatabaseSettingsTab)tab { 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]; - [[NSApplication sharedApplication] beginSheet:[self.documentSettingsWindowController window] - modalForWindow:[self window] - modalDelegate:nil - didEndSelector:NULL + [[NSApplication sharedApplication] beginSheet:self.documentSettingsWindowController.window + modalForWindow:self.window + modalDelegate:self + didEndSelector:@selector(_settingsSheetDidEnd:returnCode:contextInfo:) 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 { NSView *inspectorView = [self.inspectorViewController view]; return (nil != [inspectorView superview]); diff --git a/MacPass/MPPasswordEditWindowController.h b/MacPass/MPPasswordEditWindowController.h index 719448c0..6733ea60 100644 --- a/MacPass/MPPasswordEditWindowController.h +++ b/MacPass/MPPasswordEditWindowController.h @@ -35,13 +35,6 @@ //@property (nonatomic,assign) BOOL allowsEmptyPasswordOrKey; @property (weak) id 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)generateKey:(id)sender; diff --git a/MacPass/MPPasswordEditWindowController.m b/MacPass/MPPasswordEditWindowController.m index c69c3f6f..47a6e742 100644 --- a/MacPass/MPPasswordEditWindowController.m +++ b/MacPass/MPPasswordEditWindowController.m @@ -18,7 +18,6 @@ @interface MPPasswordEditWindowController () -@property (nonatomic, weak) MPDocument *currentDocument; @property (nonatomic, assign) BOOL showPassword; @property (nonatomic, assign) BOOL enablePassword; @property (nonatomic, assign) BOOL hasValidPasswordOrKey; @@ -32,13 +31,12 @@ return @"PasswordEditWindow"; } -- (id)initWithDocument:(MPDocument *)document { - self = [super initWithWindow:nil]; +- (id)initWithWindow:(NSWindow *)window { + self = [super initWithWindow:window]; if(self){ //_allowsEmptyPasswordOrKey = YES; _showPassword = NO; _hasValidPasswordOrKey = NO; - _currentDocument = document; } return self; } @@ -47,7 +45,8 @@ [super windowDidLoad]; [self.togglePasswordButton bind:NSValueBinding toObject:self withKeyPath:NSStringFromSelector(@selector(showPassword)) options:nil]; [[self window] setDefaultButtonCell:[self.changePasswordButton cell]]; - self.enablePassword = _currentDocument.compositeKey.hasPassword; + MPDocument *document = self.document; + self.enablePassword = document.compositeKey.hasPassword; } - (void)updateView { @@ -107,7 +106,8 @@ - (IBAction)save:(id)sender { const BOOL hasPassword = ([self.hasPasswordSwitchButton state] == NSOnState); 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]; if(self.delegate && [self.delegate respondsToSelector:@selector(didFinishPasswordEditing:)]) { [self.delegate didFinishPasswordEditing:YES]; @@ -126,7 +126,8 @@ } - (IBAction)generateKey:(id)sender { - NSData *data = [NSData generateKeyfiledataForVersion:_currentDocument.tree.minimumVersion]; + MPDocument *document = self.document; + NSData *data = [NSData generateKeyfiledataForVersion:document.tree.minimumVersion]; if(data) { NSSavePanel *savePanel = [NSSavePanel savePanel]; [savePanel setAllowedFileTypes:@[@"key", @"xml"]];