Revert "Keep systems happy which do not have sys/resource.h"
[pspp] / src / ui / gui / main.c
index f1b525f31c92d5467a4c26478f306a2b397c4212..ecb07f83ff378fb6c2c725220396642a68b551f5 100644 (file)
@@ -1,5 +1,6 @@
 /* PSPPIRE - a graphical user interface for PSPP.
-   Copyright (C) 2004, 2005, 2006, 2010, 2011, 2012, 2013, 2014, 2015, 2016  Free Software Foundation
+   Copyright (C) 2004, 2005, 2006, 2010, 2011, 2012, 2013, 2014, 2015, 2016,
+   2020 Free Software Foundation
 
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
 
 #include <gtk/gtk.h>
 #include <stdlib.h>
-#if ENABLE_RELOCATABLE && defined(__APPLE__)
 #include <sys/stat.h>
+#include <sys/resource.h>
+#if ENABLE_RELOCATABLE && defined(__APPLE__)
+static const bool apple_relocatable = true;
+#else
+static const bool apple_relocatable = false;
 #endif
 
 #include "language/lexer/include-path.h"
@@ -238,7 +243,6 @@ wait_for_splash (GApplication *app, GtkWindow *x)
     }
 }
 
-
 static void
 on_activate (GApplication * app, gpointer ud)
 {
@@ -250,15 +254,50 @@ on_activate (GApplication * app, gpointer ud)
   wait_for_splash (app, x);
 }
 
+GtkWindow *
+find_empty_data_window (GApplication *app)
+{
+  GList *wl = gtk_application_get_windows (GTK_APPLICATION (app));
+  while (wl)
+    {
+      if (wl->data && PSPPIRE_IS_DATA_WINDOW (GTK_WINDOW (wl->data)) &&
+          psppire_data_window_is_empty (PSPPIRE_DATA_WINDOW (wl->data)))
+        return GTK_WINDOW (wl->data);
+      wl = wl->next;
+    }
+  return NULL;
+}
+
+GtkWindow *
+find_psppire_window (GApplication *app)
+{
+  GList *wl = gtk_application_get_windows (GTK_APPLICATION (app));
+  while (wl)
+    {
+      if (wl->data && PSPPIRE_IS_WINDOW (GTK_WINDOW (wl->data)))
+        return GTK_WINDOW (wl->data);
+      wl = wl->next;
+    }
+  return NULL;
+}
 
 static void
 on_open (GApplication *app, GFile **files, gint n_files, gchar * hint,
          gpointer ud)
 {
-  post_initialise (app);
+  /* If the application is already open and we open another file
+     via xdg-open on GNU/Linux or via the file manager, then open is
+     called. Check if we already have a psppire window. */
+  if (find_psppire_window (app) == NULL)
+    post_initialise (app);
+
+  /* When a new data file is opened, then try to find an empty
+     data window which will then be replaced as in the open file
+     dialog */
+  GtkWindow *victim = find_empty_data_window (app);
 
   gchar *file = g_file_get_parse_name (files[0]);
-  GtkWindow *x = psppire_preload_file (file);
+  GtkWindow *x = psppire_preload_file (file, victim);
   g_free (file);
 
   wait_for_splash (app, x);
@@ -280,9 +319,9 @@ process_pre_start_arguments (int *argc, char ***argv)
   g_option_context_set_ignore_unknown_options (oc, FALSE);
   g_option_context_add_main_entries (oc, oe, NULL);
   g_option_context_parse (oc, argc, argv, NULL);
+  g_option_context_free (oc);
 }
 
-#if ENABLE_RELOCATABLE && defined(__APPLE__)
 static void
 pspp_macos_setenv (const char * progname)
 {
@@ -339,30 +378,29 @@ pspp_macos_setenv (const char * progname)
         }
     }
 }
-#endif
 
 int
 main (int argc, char *argv[])
 {
-
-#if ENABLE_RELOCATABLE && defined(__APPLE__)
-  /* remove MacOS session identifier from the command line args */
-  gint newargc = 0;
-  for (gint i = 0; i < argc; i++)
+  if (apple_relocatable)
     {
-      if (!g_str_has_prefix (argv[i], "-psn_"))
+      /* remove MacOS session identifier from the command line args */
+      gint newargc = 0;
+      for (gint i = 0; i < argc; i++)
         {
-          argv[newargc] = argv[i];
-          newargc++;
+          if (!g_str_has_prefix (argv[i], "-psn_"))
+            {
+              argv[newargc] = argv[i];
+              newargc++;
+            }
         }
+      if (argc > newargc)
+        {
+          argv[newargc] = NULL; /* glib expects NULL terminated array */
+          argc = newargc;
+        }
+      pspp_macos_setenv (argv[0]);
     }
-  if (argc > newargc)
-    {
-      argv[newargc] = NULL; /* glib expects NULL terminated array */
-      argc = newargc;
-    }
-  pspp_macos_setenv (argv[0]);
-#endif
 
   set_program_name (argv[0]);
 
@@ -401,5 +439,6 @@ main (int argc, char *argv[])
     g_action_map_add_action (G_ACTION_MAP (app), G_ACTION (act_new_data));
   }
 
+  g_object_set (G_OBJECT (app), "register-session", TRUE, NULL);
   return g_application_run (G_APPLICATION (app), argc, argv);
 }