Only store last document, when MacPass is terminating

Moved code from MPDocument to MPDocumentController
This commit is contained in:
Michael Starke
2021-12-15 11:37:28 +01:00
parent 7376a5c887
commit 66ad8cee17
4 changed files with 20 additions and 9 deletions

View File

@@ -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;

View File

@@ -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(), ^{

View File

@@ -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];
}

View File

@@ -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) {