diff --git a/MacPass/MPAppDelegate.h b/MacPass/MPAppDelegate.h index 6c159652..caf9f770 100644 --- a/MacPass/MPAppDelegate.h +++ b/MacPass/MPAppDelegate.h @@ -36,8 +36,9 @@ APPKIT_EXTERN NSString *const MPDidChangeStoredKeyFilesSettings; @property (strong) IBOutlet NSMenu *exportMenu; @property (strong, readonly) MPEntryContextMenuDelegate *itemActionMenuDelegate; +@property (readonly) BOOL isTerminating; -@property (nonatomic, assign) BOOL isAllowedToStoreKeyFile; +@property (nonatomic) BOOL isAllowedToStoreKeyFile; - (IBAction)checkForUpdates:(id)sender; - (IBAction)showPreferences:(id)sender; diff --git a/MacPass/MPAppDelegate.m b/MacPass/MPAppDelegate.m index cfb655e0..84d1e5a4 100644 --- a/MacPass/MPAppDelegate.m +++ b/MacPass/MPAppDelegate.m @@ -92,6 +92,7 @@ typedef NS_OPTIONS(NSInteger, MPAppStartupState) { _userNotificationCenterDelegate = [[MPUserNotificationCenterDelegate alloc] init]; self.itemActionMenuDelegate = [[MPEntryContextMenuDelegate alloc] init]; _shouldOpenFile = NO; + _isTerminating = NO; self.startupState = MPAppStartupStateNone; [NSNotificationCenter.defaultCenter addObserver:self @@ -185,6 +186,7 @@ typedef NS_OPTIONS(NSInteger, MPAppStartupState) { } - (NSApplicationTerminateReply)applicationShouldTerminate:(NSApplication *)sender { + _isTerminating = YES; [self hideWelcomeWindow]; if(MPTemporaryFileStorageCenter.defaultCenter.hasPendingStorages) { dispatch_async(dispatch_get_main_queue(), ^{ diff --git a/MacPass/MPDocument.m b/MacPass/MPDocument.m index f6d4b08b..68cb8060 100644 --- a/MacPass/MPDocument.m +++ b/MacPass/MPDocument.m @@ -240,13 +240,6 @@ NSString *const MPDocumentGroupKey = @"MPDocumentGrou - (void)close { [self _cleanupLock]; - /* - We store the last url. Restored windows are automatically handled. - If closeAllDocuments is set, all docs get this message - */ - if(self.fileURL.isFileURL) { - [[NSUserDefaults standardUserDefaults] setObject:self.fileURL.absoluteString forKey:kMPSettingsKeyLastDatabasePath]; - } self.tree = nil; [super close]; } diff --git a/MacPass/MPDocumentController.m b/MacPass/MPDocumentController.m index bccf5692..77754dd2 100644 --- a/MacPass/MPDocumentController.m +++ b/MacPass/MPDocumentController.m @@ -83,6 +83,16 @@ [super addDocument:document]; } +- (void)removeDocument:(NSDocument *)document { + MPAppDelegate *appDelegate = (MPAppDelegate *)NSApp.delegate; + if(appDelegate.isTerminating) { + if(document.fileURL.isFileURL) { + [[NSUserDefaults standardUserDefaults] setObject:document.fileURL.absoluteString forKey:kMPSettingsKeyLastDatabasePath]; + } + } + [super removeDocument:document]; +} + - (BOOL)reopenLastDocument { if(self.documents.count > 0) { return YES; // The document is already open @@ -93,7 +103,12 @@ } else { NSString *lastPath = [NSUserDefaults.standardUserDefaults stringForKey:kMPSettingsKeyLastDatabasePath]; - documentUrl =[NSURL URLWithString:lastPath]; + documentUrl = [NSURL URLWithString:lastPath]; + NSError *error; + if(![documentUrl checkResourceIsReachableAndReturnError:&error]) { + [NSUserDefaults.standardUserDefaults removeObjectForKey:kMPSettingsKeyLastDatabasePath]; + documentUrl = nil; + } } BOOL isFileURL = documentUrl.fileURL; if(isFileURL) {