help-menu.c: start the default html browser via wscript instead of cmd
[pspp] / src / ui / gui / help-menu.c
index e670c0b555b5b80f0ab5110dff05474d950408fb..aeef8c10c44ba2deb1d4762177fc3b56d964211e 100644 (file)
@@ -1,5 +1,5 @@
 /* PSPPIRE - a graphical user interface for PSPP.
-   Copyright (C) 2006, 2007, 2010, 2011, 2012, 2013  Free Software Foundation
+   Copyright (C) 2006, 2007, 2010, 2011, 2012, 2013, 2015, 2016  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
 #define _(msgid) gettext (msgid)
 #define N_(msgid) msgid
 
+/* Try to open html documentation uri via the default
+   browser on the operating system */
+#ifdef __APPLE__
+#define HTMLOPENARGV {"open", 0, 0}
+#elif  _WIN32
+#define HTMLOPENARGV {"wscript", 0, 0}
+#else
+#define HTMLOPENARGV {"xdg-open", 0, 0}
+#endif
 
 static const gchar *artists[] = { "Bastián Díaz", "Hugo Alejandro", NULL};
 
 static void
-about_new (GtkMenuItem *m, GtkWindow *parent)
+about_new (GtkMenuItem *mmm, GtkWindow *parent)
 {
   GtkWidget *about =  gtk_about_dialog_new ();
 
@@ -46,7 +55,7 @@ about_new (GtkMenuItem *m, GtkWindow *parent)
   gtk_about_dialog_set_website (GTK_ABOUT_DIALOG (about), PACKAGE_URL);
 
   gtk_about_dialog_set_version (GTK_ABOUT_DIALOG (about),
-                               bare_version);
+                               announced_version);
 
   gtk_about_dialog_set_authors (GTK_ABOUT_DIALOG (about),
                                (const gchar **) authors);
@@ -80,32 +89,130 @@ about_new (GtkMenuItem *m, GtkWindow *parent)
   gtk_widget_hide (about);
 }
 
-/* Open the manual at PAGE */
+
+/* Opening the htmluri in windows via cmd /start uri opens
+   the windows command shell for a moment. The alternative is
+   to start a script via wscript. This will not be visible*/
+#ifdef _WIN32
+static gboolean open_windows_help (const gchar *helpuri,
+                                   GError **err)
+{
+  gchar *vbsfilename = NULL;
+  gchar *vbs = NULL;
+  gboolean result;
+  vbsfilename = g_build_filename (g_get_tmp_dir (),
+                                  "pspp-help-open.vbs",
+                                  NULL);
+  vbs = g_strdup_printf("CreateObject(\"WScript.Shell\").Run \"%s\"",
+                        helpuri);
+  result = g_file_set_contents (vbsfilename,
+                                vbs,
+                                strlen(vbs),
+                                err);
+  g_free (vbs);
+  if (!result)
+    goto error;
+
+  gchar *argv[] = {"wscript",vbsfilename,0};
+
+  result = g_spawn_async (NULL, argv,
+                          NULL, G_SPAWN_SEARCH_PATH,
+                          NULL, NULL, NULL, err);
+ error:
+  g_free (vbsfilename);
+  return result;
+}
+#endif
+
+/* Open the manual at PAGE with the following priorities
+   First: local yelp help system
+   Second: browser with local html doc dir in path pspp.html/<helppage>.html
+   Third:  browers with Internet html help at gnu.org */
 void
 online_help (const char *page)
 {
   GError *err = NULL;
-  gchar *cmd = NULL;
-
+  GError *htmlerr = NULL;
   gchar *argv[3] = { "yelp", 0, 0};
+  gchar *htmlargv[3] = HTMLOPENARGV;
+  gchar *htmlfilename = NULL;
+  gchar *htmlfullname = NULL;
+  gchar *htmluri = NULL;
 
   if (page == NULL)
-    argv[1] = g_strdup_printf ("file://%s", relocate (DOCDIR "/pspp.xml"));
+    {
+      argv[1] = g_strdup_printf ("file://%s", relocate (DOCDIR "/pspp.xml"));
+      htmlfilename = g_strdup ("index.html");
+    }
   else
-    argv[1] = g_strdup_printf ("file://%s#%s", relocate (DOCDIR "/pspp.xml"), page);
-
-  if (! g_spawn_async (NULL, argv,
-                      NULL, G_SPAWN_SEARCH_PATH,
-                      NULL, NULL,   NULL,   &err))
     {
-      msg (ME, _("Cannot open reference manual: %s.  The PSPP user manual is "
-                 "also available at %s"),
+      gchar **tokens = NULL;
+      const int maxtokens = 5;
+      int idx = 0;
+      argv[1] = g_strdup_printf ("file://%s#%s",
+                                 relocate (DOCDIR "/pspp.xml"), page);
+      /* The page will be translated to the htmlfilename
+         page                   htmlfilename
+         GRAPH#SCATTERPLOT      SCATTERPLOT.html
+         QUICK-CLUSTER          QUICK-CLUSTER.html
+         which is valid for the multiple page html doc*/
+      tokens = g_strsplit (page, "#", maxtokens);
+      for(;tokens[idx] && idx < maxtokens;idx++);
+      htmlfilename = g_strdup_printf ("%s.html", tokens[idx-1]);
+      g_strfreev (tokens);
+    }
+  /* Hint: pspp.html is a directory...*/
+  htmlfullname = g_strdup_printf ("%s/%s", relocate (DOCDIR "/pspp.html"),
+                                  htmlfilename);
+  if (g_file_test (relocate (DOCDIR "/pspp.html"), G_FILE_TEST_IS_DIR))
+    {
+      GError *urierr = NULL;
+      htmluri =  g_filename_to_uri (htmlfullname,NULL, &urierr);
+      if (!htmluri)
+        {
+          msg (ME, _("Help path conversion error: %s"), urierr->message);
+          htmluri = htmlfullname;
+        }
+      g_clear_error (&urierr);
+    }
+  else
+    htmluri = g_strdup_printf (PACKAGE_URL "manual/html_node/%s",
+                               htmlfilename);
+  g_free (htmlfullname);
+  g_free (htmlfilename);
+  htmlargv[1] = htmluri;
+
+  /* The following **SHOULD** work but it does not on 28.5.2016
+     g_app_info_launch_default_for_uri (htmluri, NULL, &err);
+     osx: wine is started to launch the uri...
+     windows: not so bad, but the first access does not work*/
+
+  if (! (g_spawn_async (NULL, argv,
+                        NULL, G_SPAWN_SEARCH_PATH,
+                        NULL, NULL,   NULL,   &err) ||
+#ifdef _WIN32
+         open_windows_help (htmluri, &htmlerr))
+#else
+         g_spawn_async (NULL, htmlargv,
+                        NULL, G_SPAWN_SEARCH_PATH,
+                        NULL, NULL,   NULL,   &htmlerr))
+#endif
+      )
+    {
+      msg (ME, _("Cannot open reference manual via yelp: %s. "
+                 "Cannot open via html: %s "
+                 "with uri: %s "
+                 "The PSSP manual is also available at %s"),
                   err->message,
+                  htmlerr->message,
+                  htmluri,
                   PACKAGE_URL "documentation.html");
     }
 
-  g_free (cmd);
+  g_free (argv[1]);
+  g_free (htmluri);
   g_clear_error (&err);
+  g_clear_error (&htmlerr);
 }
 
 static void
@@ -114,48 +221,33 @@ reference_manual (GtkMenuItem *menu, gpointer data)
   online_help (NULL);
 }
 
+GtkWidget *
+create_help_menu (GtkWindow *toplevel)
+{
+  GtkWidget *menuitem = gtk_menu_item_new_with_mnemonic (_("_Help"));
+  GtkWidget *menu = gtk_menu_new ();
 
+  GtkWidget *help_about = gtk_menu_item_new_with_mnemonic (_("_About"));
+  GtkWidget *help_ref = gtk_menu_item_new_with_mnemonic (_("_Reference Manual"));
 
-void
-merge_help_menu (GtkUIManager *uim)
-{
-  GtkActionGroup *action_group = gtk_action_group_new ("help");
+  GtkAccelGroup *accel_group = gtk_accel_group_new ();
+  
+  gtk_window_add_accel_group (toplevel, accel_group);
 
-  static const GtkActionEntry entries[] =
-    {
-      {
-       "help", NULL,                               /* name, stock id */
-       N_("_Help"), NULL,                          /* label, accelerator */
-       NULL,
-       NULL,
-      },
-    
-      {
-       "help_reference", "help-reference-manual",            /* name, stock id */
-       N_("_Reference Manual"), NULL,               /* label, accelerator */
-       NULL,                                        /* tooltip */
-       G_CALLBACK (reference_manual)
-      },
-    
-      {
-       "help_about", "help-about",
-       NULL, NULL, NULL,
-       G_CALLBACK (about_new)
-      },
-    };
-
-  gtk_action_group_set_translation_domain (action_group, PACKAGE);
-
-  gtk_ui_manager_add_ui_from_string   (uim, "\
-      <menubar name=\"menubar\">\
-        <menu action=\"help\">\
-          <menuitem action=\"help_reference\"/>\
-          <menuitem action=\"help_about\"/>\
-        </menu>\
-       </menubar>\
-       ", -1, 0);
-
-  gtk_action_group_add_actions (action_group, entries, G_N_ELEMENTS (entries), NULL);
-
-  gtk_ui_manager_insert_action_group  (uim, action_group, 0);
+  gtk_widget_add_accelerator (help_ref,
+                             "activate", accel_group,
+                             GDK_KEY_F1, 0,
+                             GTK_ACCEL_VISIBLE);
+
+  gtk_menu_attach (GTK_MENU (menu), help_ref, 0, 1, 0, 1);
+  gtk_menu_attach (GTK_MENU (menu), help_about, 0, 1, 1, 2);
+
+  g_signal_connect (help_about, "activate", G_CALLBACK (about_new), toplevel);
+  g_signal_connect (help_ref, "activate", G_CALLBACK (reference_manual), NULL);
+  
+  g_object_set (menuitem, "submenu", menu, NULL);
+
+  gtk_widget_show_all (menuitem);
+  
+  return menuitem;
 }