Keep systems happy which do not have sys/resource.h
[pspp] / src / ui / gui / main.c
index cb5519b5f3ba2e3f0e15e189a432b2ad3b49d8b2..9a7f8e1987a7d8a7def941e471df7f37bc500e79 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>
+#if ENABLE_RELOCATABLE && defined(__APPLE__)
+#include <sys/resource.h>
+static const bool apple_relocatable = true;
+#else
+static const bool apple_relocatable = false;
+#if HAVE_SYS_RESOURCE_H
+#include <sys/resource.h>
+#else
+/* Dummy definitions to keep the compiler happy. */
+struct rlimit
+{
+  int rlim_cur;
+  int rlim_max;
+};
+#define RLIMIT_NOFILE 0
+#endif
 #endif
 
 #include "language/lexer/include-path.h"
@@ -238,7 +254,6 @@ wait_for_splash (GApplication *app, GtkWindow *x)
     }
 }
 
-
 static void
 on_activate (GApplication * app, gpointer ud)
 {
@@ -250,15 +265,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);
@@ -283,7 +333,6 @@ process_pre_start_arguments (int *argc, char ***argv)
   g_option_context_free (oc);
 }
 
-#if ENABLE_RELOCATABLE && defined(__APPLE__)
 static void
 pspp_macos_setenv (const char * progname)
 {
@@ -340,30 +389,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]);