Revision: 29270
          http://sourceforge.net/p/bibdesk/svn/29270
Author:   hofman
Date:     2025-06-25 14:43:51 +0000 (Wed, 25 Jun 2025)
Log Message:
-----------
Handle openingfies at startup from applicationDidFinishLaunching: for default 
launch, and from applicationWillFinishLaunching: on relaunch. Handle reopen 
ourselves when we want to open an empty or default file.

Modified Paths:
--------------
    trunk/bibdesk/BDSKAppController.h
    trunk/bibdesk/BDSKAppController.m
    trunk/bibdesk/BDSKDocumentController.h
    trunk/bibdesk/BDSKDocumentController.m

Modified: trunk/bibdesk/BDSKAppController.h
===================================================================
--- trunk/bibdesk/BDSKAppController.h   2025-05-22 18:12:48 UTC (rev 29269)
+++ trunk/bibdesk/BDSKAppController.h   2025-06-25 14:43:51 UTC (rev 29270)
@@ -49,7 +49,7 @@
        IBOutlet NSMenu *bookmarksMenu;
     IBOutlet NSMenu *historyMenu;
     IBOutlet NSMenu *shareMenu;
-    BOOL didCheckReopen;
+    BOOL didReopen;
 }
 
 - (IBAction)visitWebSite:(nullable id)sender;

Modified: trunk/bibdesk/BDSKAppController.m
===================================================================
--- trunk/bibdesk/BDSKAppController.m   2025-05-22 18:12:48 UTC (rev 29269)
+++ trunk/bibdesk/BDSKAppController.m   2025-06-25 14:43:51 UTC (rev 29270)
@@ -265,6 +265,62 @@
         [[BDSKTypeManager sharedManager] updateCustomFields];
 }
 
+static BOOL fileIsInTrash(NSURL *fileURL)
+{
+    NSCParameterAssert([fileURL isFileURL]);
+    NSURLRelationship relationship;
+    if ([[NSFileManager defaultManager] getRelationship:&relationship 
ofDirectory:NSTrashDirectory inDomain:0 toItemAtURL:fileURL error:NULL])
+        return relationship == NSURLRelationshipContains;
+    FSRef fileRef;
+    Boolean result = false;
+#pragma clang diagnostic push
+#pragma clang diagnostic ignored "-Wdeprecated-declarations"
+    if (CFURLGetFSRef((__bridge CFURLRef)fileURL, &fileRef)) {
+        FSDetermineIfRefIsEnclosedByFolder(0, kTrashFolderType, &fileRef, 
&result);
+        if (result == false)
+            FSDetermineIfRefIsEnclosedByFolder(0, kSystemTrashFolderType, 
&fileRef, &result);
+    }
+    return result;
+#pragma clang diagnostic pop
+}
+
+- (void)openFilesAtStartup {
+    didReopen = YES;
+    NSUserDefaults *sud = [NSUserDefaults standardUserDefaults];
+    NSInteger option = [sud boolForKey:BDSKIsRelaunchKey] ? 
BDSKStartupOpenLastOpenFiles : [sud integerForKey:BDSKStartupBehaviorKey];
+    [sud removeObjectForKey:BDSKIsRelaunchKey];
+    switch (option) {
+        case BDSKStartupOpenUntitledFile:
+            [[NSDocumentController sharedDocumentController] 
openUntitledDocumentAndDisplay:YES error:NULL];
+            break;
+        case BDSKStartupDoNothing:
+            break;
+        case BDSKStartupOpenDialog:
+            [[NSDocumentController sharedDocumentController] openDocument:nil];
+            break;
+        case BDSKStartupOpenDefaultFile:
+            {
+                NSURL *fileURL = [NSURL fileURLWithAliasData:[sud 
objectForKey:BDSKDefaultBibFileAliasKey] bookmarkData:[sud 
objectForKey:BDSKDefaultBibFileBookmarkKey]];
+                if (fileURL && NO == fileIsInTrash(fileURL))
+                    [[NSDocumentController sharedDocumentController] 
openDocumentWithContentsOfURL:fileURL display:YES 
completionHandler:^(NSDocument *document, BOOL documentWasAlreadyOpen, NSError 
*error){}];
+            }
+            break;
+        case BDSKStartupOpenLastOpenFiles:
+            {
+                NSArray *files = [sud objectForKey:BDSKLastOpenFileNamesKey];
+                NSURL *fileURL;
+                for (NSDictionary *dict in [files reverseObjectEnumerator]){
+                    fileURL = [NSURL fileURLWithAliasData:[dict 
objectForKey:@"_BDAlias"] bookmarkData:[dict objectForKey:@"bookmark"]] ?: 
[NSURL fileURLWithPath:[dict objectForKey:@"fileName"] isDirectory:NO];
+                    if(fileURL)
+                        [[NSDocumentController sharedDocumentController] 
openDocumentWithContentsOfURL:fileURL display:YES 
completionHandler:^(NSDocument *document, BOOL documentWasAlreadyOpen, NSError 
*error){}];
+                }
+            }
+            break;
+        default:
+            break;
+    }
+}
+
 #pragma mark Application delegate
 
 - (void)applicationWillFinishLaunching:(NSNotification *)aNotification{
@@ -306,13 +362,14 @@
     
     // this registered the value transformers for radio buttons
     [BDSKRadioTransformer class];
+    
+    if ([sud boolForKey:BDSKIsRelaunchKey])
+        [self openFilesAtStartup];
 }
 
 - (void)applicationDidFinishLaunching:(NSNotification *)aNotification{
-    if (didCheckReopen == NO && [[NSApp windows] count] == 0 && 
[(BDSKDocumentController *)[NSDocumentController sharedDocumentController] 
openedFile] == NO)
-        [self applicationShouldOpenUntitledFile:NSApp];
-    didCheckReopen = YES;
-    [[NSUserDefaults standardUserDefaults] 
removeObjectForKey:BDSKIsRelaunchKey];
+    if (didReopen == NO && [[[aNotification userInfo] 
objectForKey:NSApplicationLaunchIsDefaultLaunchKey] boolValue])
+        [self openFilesAtStartup];
     
     // register our help book, so it's available for methods that don't 
register this, e.g. the web group
     [[NSHelpManager sharedHelpManager] registerBooksInBundle:[NSBundle 
mainBundle]];
@@ -362,64 +419,11 @@
     [NSApp setAutomaticCustomizeTouchBarMenuItemEnabled:YES];
 }
 
-static BOOL fileIsInTrash(NSURL *fileURL)
-{
-    NSCParameterAssert([fileURL isFileURL]);    
-    NSURLRelationship relationship;
-    if ([[NSFileManager defaultManager] getRelationship:&relationship 
ofDirectory:NSTrashDirectory inDomain:0 toItemAtURL:fileURL error:NULL])
-        return relationship == NSURLRelationshipContains;
-    FSRef fileRef;
-    Boolean result = false;
-#pragma clang diagnostic push
-#pragma clang diagnostic ignored "-Wdeprecated-declarations"
-    if (CFURLGetFSRef((__bridge CFURLRef)fileURL, &fileRef)) {
-        FSDetermineIfRefIsEnclosedByFolder(0, kTrashFolderType, &fileRef, 
&result);
-        if (result == false)
-            FSDetermineIfRefIsEnclosedByFolder(0, kSystemTrashFolderType, 
&fileRef, &result);
-    }
-    return result;
-#pragma clang diagnostic pop
+- (BOOL)applicationShouldOpenUntitledFile:(NSApplication *)sender {
+    return NO;
 }
 
-- (BOOL)applicationShouldOpenUntitledFile:(NSApplication *)sender
-{
-    didCheckReopen = YES;
-    NSUserDefaults*sud = [NSUserDefaults standardUserDefaults];
-    NSInteger option = [sud boolForKey:BDSKIsRelaunchKey] ? 
BDSKStartupOpenLastOpenFiles : [sud integerForKey:BDSKStartupBehaviorKey];
-    switch (option) {
-        case BDSKStartupOpenUntitledFile:
-            return YES;
-        case BDSKStartupDoNothing:
-            return NO;
-        case BDSKStartupOpenDialog:
-            [[NSDocumentController sharedDocumentController] openDocument:nil];
-            return NO;
-        case BDSKStartupOpenDefaultFile:
-            {
-                NSURL *fileURL = [NSURL fileURLWithAliasData:[sud 
objectForKey:BDSKDefaultBibFileAliasKey] bookmarkData:[sud 
objectForKey:BDSKDefaultBibFileBookmarkKey]];
-                if (fileURL && NO == fileIsInTrash(fileURL))
-                    [[NSDocumentController sharedDocumentController] 
openDocumentWithContentsOfURL:fileURL display:YES 
completionHandler:^(NSDocument *document, BOOL documentWasAlreadyOpen, NSError 
*error){}];
-            }
-            return NO;
-        case BDSKStartupOpenLastOpenFiles:
-            {
-                NSArray *files = [sud objectForKey:BDSKLastOpenFileNamesKey];
-                NSURL *fileURL;
-                for (NSDictionary *dict in [files reverseObjectEnumerator]){
-                    fileURL = [NSURL fileURLWithAliasData:[dict 
objectForKey:@"_BDAlias"] bookmarkData:[dict objectForKey:@"bookmark"]] ?: 
[NSURL fileURLWithPath:[dict objectForKey:@"fileName"] isDirectory:NO];
-                    if(fileURL)
-                        [[NSDocumentController sharedDocumentController] 
openDocumentWithContentsOfURL:fileURL display:YES 
completionHandler:^(NSDocument *document, BOOL documentWasAlreadyOpen, NSError 
*error){}];
-                }
-            }
-            return NO;
-        default:
-            return NO;
-    }
-}
-
 - (void)application:(NSApplication *)sender openURLs:(NSArray *)urls {
-    didCheckReopen = YES;
-    
     NSAppleEventDescriptor *errr = [[[NSAppleEventManager 
sharedAppleEventManager] currentAppleEvent] descriptorForKeyword:'errr'];
     BOOL errorReporting = errr ? [errr booleanValue] : YES;
     
@@ -470,8 +474,12 @@
 
 // we don't want to reopen last open files or show an Open dialog when 
re-activating the app
 - (BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication 
hasVisibleWindows:(BOOL)flag {
+    if (flag)
+        return YES;
     NSInteger startupOption = [[NSUserDefaults standardUserDefaults] 
integerForKey:BDSKStartupBehaviorKey];
-    return flag || (startupOption == BDSKStartupOpenUntitledFile || 
startupOption == BDSKStartupOpenDefaultFile);
+    if (startupOption == BDSKStartupOpenUntitledFile || startupOption == 
BDSKStartupOpenDefaultFile)
+        [self openFilesAtStartup];
+    return NO;
 }
 
 - (void)applicationDidBecomeActive:(NSNotification *)aNotification{

Modified: trunk/bibdesk/BDSKDocumentController.h
===================================================================
--- trunk/bibdesk/BDSKDocumentController.h      2025-05-22 18:12:48 UTC (rev 
29269)
+++ trunk/bibdesk/BDSKDocumentController.h      2025-06-25 14:43:51 UTC (rev 
29270)
@@ -51,14 +51,10 @@
     NSMutableDictionary *customOpenSettings;
     
     NSInteger openType;
-    
-    BOOL openedFile;
 }
 
 @property (nonatomic, readonly) BOOL autosavesInPlace;
 
-@property (nonatomic, readonly) BOOL openedFile;
-
 @property (nonatomic, nullable, readonly) id mainDocument;
 
 - (nullable BibDocument *)currentBibliographyDocumentAndDisplay:(BOOL)display 
error:(NSError **)outError;

Modified: trunk/bibdesk/BDSKDocumentController.m
===================================================================
--- trunk/bibdesk/BDSKDocumentController.m      2025-05-22 18:12:48 UTC (rev 
29269)
+++ trunk/bibdesk/BDSKDocumentController.m      2025-06-25 14:43:51 UTC (rev 
29270)
@@ -84,7 +84,7 @@
 
 @implementation BDSKDocumentController
 
-@synthesize autosavesInPlace, openedFile, mainDocument;
+@synthesize autosavesInPlace, mainDocument;
 
 - (instancetype)init {
     if ((self = [super init]) && didInitialize == NO) {
@@ -98,8 +98,6 @@
         
         didInitialize = YES;
         
-        openedFile = NO;
-        
         // we must set this now, as the system may check it very early
         // defaults may not have been registered at this point
         // but the default value is 0, so that is equivalenty to not being set
@@ -345,7 +343,6 @@
 }
 
 - (void)openDocumentWithContentsOfURL:(NSURL *)absoluteURL 
display:(BOOL)displayDocument completionHandler:(void (^)(NSDocument *document, 
BOOL documentWasAlreadyOpen, NSError *error))completionHandler {
-    openedFile = YES;
     
     // Get the search string keyword if available (Spotlight passes this)
     NSAppleEventDescriptor *event = [[NSAppleEventManager 
sharedAppleEventManager] currentAppleEvent];

This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.



_______________________________________________
Bibdesk-commit mailing list
Bibdesk-commit@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/bibdesk-commit

Reply via email to