help-menu.c: fix default html browser start on windows - correct uri
[pspp] / src / ui / gui / help-menu.c
1 /* PSPPIRE - a graphical user interface for PSPP.
2    Copyright (C) 2006, 2007, 2010, 2011, 2012, 2013, 2015, 2016  Free Software Foundation
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17
18 #include <config.h>
19
20 #include <gtk/gtk.h>
21
22 #include <libpspp/copyleft.h>
23 #include <libpspp/version.h>
24 #include "help-menu.h"
25 #include <libpspp/message.h>
26
27 #include "gl/configmake.h"
28 #include "gl/relocatable.h"
29
30 #include <gettext.h>
31 #define _(msgid) gettext (msgid)
32 #define N_(msgid) msgid
33
34 /* Try to open html documentation uri via the default
35    browser on the operating system */
36 #ifdef __APPLE__
37 #define HTMLOPENARGV {"open", 0, 0, 0, 0}
38 #define HTMLOPENARGIDX 1
39 #elif  _WIN32
40 #define HTMLOPENARGV {"cmd","/C", "start", 0, 0}
41 #define HTMLOPENARGIDX 3
42 #else
43 #define HTMLOPENARGV {"xdg-open", 0, 0, 0, 0}
44 #define HTMLOPENARGIDX 1
45 #endif
46
47 static const gchar *artists[] = { "Bastián Díaz", "Hugo Alejandro", NULL};
48
49 static void
50 about_new (GtkMenuItem *mmm, GtkWindow *parent)
51 {
52   GtkWidget *about =  gtk_about_dialog_new ();
53
54   gtk_about_dialog_set_logo_icon_name (GTK_ABOUT_DIALOG (about), "pspp");
55
56   gtk_window_set_icon_name (GTK_WINDOW (about), "pspp");
57
58   gtk_about_dialog_set_website (GTK_ABOUT_DIALOG (about), PACKAGE_URL);
59
60   gtk_about_dialog_set_version (GTK_ABOUT_DIALOG (about),
61                                 announced_version);
62
63   gtk_about_dialog_set_authors (GTK_ABOUT_DIALOG (about),
64                                 (const gchar **) authors);
65
66   gtk_about_dialog_set_artists (GTK_ABOUT_DIALOG (about),
67                                 artists);
68
69   gtk_about_dialog_set_license (GTK_ABOUT_DIALOG (about),
70                                 copyleft);
71
72   gtk_about_dialog_set_comments (GTK_ABOUT_DIALOG (about),
73                                  _("A program for the analysis of sampled data"));
74
75   gtk_about_dialog_set_copyright (GTK_ABOUT_DIALOG (about),
76                                   "Free Software Foundation");
77
78   gtk_about_dialog_set_translator_credits 
79     (
80      GTK_ABOUT_DIALOG (about),
81      /* TRANSLATORS: Do not translate this string.  Instead, put the names of the people
82         who have helped in the translation. */
83      _("translator-credits")
84      );
85
86   gtk_window_set_transient_for (GTK_WINDOW (about), parent);
87
88   gtk_window_set_modal (GTK_WINDOW (about), TRUE);
89
90   gtk_dialog_run (GTK_DIALOG (about));
91
92   gtk_widget_hide (about);
93 }
94
95 /* Open the manual at PAGE with the following priorities
96    First: local yelp help system
97    Second: browser with local html doc dir in path pspp.html/<helppage>.html
98    Third:  browers with Internet html help at gnu.org */
99 void
100 online_help (const char *page)
101 {
102   GError *err = NULL;
103   GError *htmlerr = NULL;
104   gchar *argv[3] = { "yelp", 0, 0};
105   gchar *htmlargv[5] = HTMLOPENARGV;
106   gchar *htmlfilename = NULL;
107   gchar *htmlfullname = NULL;
108
109   if (page == NULL)
110     {
111       argv[1] = g_strdup_printf ("file://%s", relocate (DOCDIR "/pspp.xml"));
112       htmlfilename = g_strdup ("index.html");
113     }
114   else
115     {
116       gchar **tokens = NULL;
117       const int maxtokens = 5;
118       int idx = 0;
119       argv[1] = g_strdup_printf ("file://%s#%s",
120                                  relocate (DOCDIR "/pspp.xml"), page);
121       /* The page will be translated to the htmlfilename
122          page                   htmlfilename
123          GRAPH#SCATTERPLOT      SCATTERPLOT.html
124          QUICK-CLUSTER          QUICK-CLUSTER.html
125          which is valid for the multiple page html doc*/
126       tokens = g_strsplit (page, "#", maxtokens);
127       for(;tokens[idx] && idx < maxtokens;idx++);
128       htmlfilename = g_strdup_printf ("%s.html", tokens[idx-1]);
129       g_strfreev (tokens);
130     }
131   /* Hint: pspp.html is a directory...*/
132   htmlfullname = g_strdup_printf ("%s/%s", relocate (DOCDIR "/pspp.html"),
133                                   htmlfilename);
134   if (g_file_test (relocate (DOCDIR "/pspp.html"), G_FILE_TEST_IS_DIR))
135     {
136       GError *urierr = NULL;
137       gchar *uriname =  g_filename_to_uri (htmlfullname,NULL, &urierr);
138       if (uriname)
139         htmlargv[HTMLOPENARGIDX] = uriname;
140       else
141         {
142           msg (ME, _("Help path conversion error: %s"), urierr->message);
143           htmlargv[HTMLOPENARGIDX] = htmlfullname;
144         }
145       g_clear_error (&urierr);
146     }
147   else
148     htmlargv[HTMLOPENARGIDX] = g_strdup_printf (PACKAGE_URL "manual/html_node/%s",
149                                                 htmlfilename);
150   g_free (htmlfullname);
151   g_free (htmlfilename);
152
153   if (! (g_spawn_async (NULL, argv,
154                         NULL, G_SPAWN_SEARCH_PATH,
155                         NULL, NULL,   NULL,   &err) ||
156          g_spawn_async (NULL, htmlargv,
157                         NULL, G_SPAWN_SEARCH_PATH,
158                         NULL, NULL,   NULL,   &htmlerr))
159       )
160     {
161       msg (ME, _("Cannot open reference manual via yelp: %s. "
162                  "Cannot open via html: %s "
163                  "with uri: %s "
164                  "The PSSP manual is also available at %s"),
165                   err->message,
166                   htmlerr->message,
167                   htmlargv[HTMLOPENARGIDX],
168                   PACKAGE_URL "documentation.html");
169     }
170
171   g_free (argv[1]);
172   g_free (htmlargv[HTMLOPENARGIDX]);
173   g_clear_error (&err);
174   g_clear_error (&htmlerr);
175 }
176
177 static void
178 reference_manual (GtkMenuItem *menu, gpointer data)
179 {
180   online_help (NULL);
181 }
182
183 GtkWidget *
184 create_help_menu (GtkWindow *toplevel)
185 {
186   GtkWidget *menuitem = gtk_menu_item_new_with_mnemonic (_("_Help"));
187   GtkWidget *menu = gtk_menu_new ();
188
189   GtkWidget *help_about = gtk_menu_item_new_with_mnemonic (_("_About"));
190   GtkWidget *help_ref = gtk_menu_item_new_with_mnemonic (_("_Reference Manual"));
191
192   GtkAccelGroup *accel_group = gtk_accel_group_new ();
193   
194   gtk_window_add_accel_group (toplevel, accel_group);
195
196   gtk_widget_add_accelerator (help_ref,
197                               "activate", accel_group,
198                               GDK_KEY_F1, 0,
199                               GTK_ACCEL_VISIBLE);
200
201   gtk_menu_attach (GTK_MENU (menu), help_ref, 0, 1, 0, 1);
202   gtk_menu_attach (GTK_MENU (menu), help_about, 0, 1, 1, 2);
203
204   g_signal_connect (help_about, "activate", G_CALLBACK (about_new), toplevel);
205   g_signal_connect (help_ref, "activate", G_CALLBACK (reference_manual), NULL);
206   
207   g_object_set (menuitem, "submenu", menu, NULL);
208
209   gtk_widget_show_all (menuitem);
210   
211   return menuitem;
212 }