30bbc7522bf73716f197d510bfda7e66fd1612ae
[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}
38 #elif  _WIN32
39 #define HTMLOPENARGV {"wscript", 0, 0}
40 #else
41 #define HTMLOPENARGV {"xdg-open", 0, 0}
42 #endif
43
44 static const gchar *artists[] = { "Bastián Díaz", "Hugo Alejandro", NULL};
45
46 static void
47 about_new (GtkMenuItem *mmm, GtkWindow *parent)
48 {
49   GtkWidget *about =  gtk_about_dialog_new ();
50
51   gtk_about_dialog_set_logo_icon_name (GTK_ABOUT_DIALOG (about), "pspp");
52
53   gtk_window_set_icon_name (GTK_WINDOW (about), "pspp");
54
55   gtk_about_dialog_set_website (GTK_ABOUT_DIALOG (about), PACKAGE_URL);
56
57   gtk_about_dialog_set_version (GTK_ABOUT_DIALOG (about),
58                                 announced_version);
59
60   gtk_about_dialog_set_authors (GTK_ABOUT_DIALOG (about),
61                                 (const gchar **) authors);
62
63   gtk_about_dialog_set_artists (GTK_ABOUT_DIALOG (about),
64                                 artists);
65
66   gtk_about_dialog_set_license (GTK_ABOUT_DIALOG (about),
67                                 copyleft);
68
69   gtk_about_dialog_set_comments (GTK_ABOUT_DIALOG (about),
70                                  _("A program for the analysis of sampled data"));
71
72   gtk_about_dialog_set_copyright (GTK_ABOUT_DIALOG (about),
73                                   "Free Software Foundation");
74
75   gtk_about_dialog_set_translator_credits
76     (
77      GTK_ABOUT_DIALOG (about),
78      /* TRANSLATORS: Do not translate this string.  Instead, put the names of the people
79         who have helped in the translation. */
80      _("translator-credits")
81      );
82
83   gtk_window_set_transient_for (GTK_WINDOW (about), parent);
84
85   gtk_window_set_modal (GTK_WINDOW (about), TRUE);
86
87   gtk_dialog_run (GTK_DIALOG (about));
88
89   gtk_widget_hide (about);
90 }
91
92
93 /* Opening the htmluri in windows via cmd /start uri opens
94    the windows command shell for a moment. The alternative is
95    to start a script via wscript. This will not be visible*/
96 #ifdef _WIN32
97 static gboolean open_windows_help (const gchar *helpuri,
98                                    GError **err)
99 {
100   gchar *vbsfilename = NULL;
101   gchar *vbs = NULL;
102   gboolean result;
103   vbsfilename = g_build_filename (g_get_tmp_dir (),
104                                   "pspp-help-open.vbs",
105                                   NULL);
106   vbs = g_strdup_printf("CreateObject(\"WScript.Shell\").Run \"%s\"",
107                         helpuri);
108   result = g_file_set_contents (vbsfilename,
109                                 vbs,
110                                 strlen(vbs),
111                                 err);
112   g_free (vbs);
113   if (!result)
114     goto error;
115
116   gchar *argv[] = {"wscript",vbsfilename,0};
117
118   result = g_spawn_async (NULL, argv,
119                           NULL, G_SPAWN_SEARCH_PATH,
120                           NULL, NULL, NULL, err);
121  error:
122   g_free (vbsfilename);
123   return result;
124 }
125 #endif
126
127 /* Open the manual at PAGE with the following priorities
128    First: local yelp help system
129    Second: browser with local html doc dir in path pspp.html/<helppage>.html
130    Third:  browers with Internet html help at gnu.org */
131 void
132 online_help (const char *page)
133 {
134   GError *err = NULL;
135   GError *htmlerr = NULL;
136   gchar *argv[3] = { "yelp", 0, 0};
137   gchar *htmlargv[3] = HTMLOPENARGV;
138   gchar *htmlfilename = NULL;
139   gchar *htmlfullname = NULL;
140   gchar *htmluri = NULL;
141
142   if (page == NULL)
143     {
144       argv[1] = g_strdup_printf ("file://%s", relocate (DOCDIR "/pspp.xml"));
145       htmlfilename = g_strdup ("index.html");
146     }
147   else
148     {
149       gchar **tokens = NULL;
150       const int maxtokens = 5;
151       int idx ;
152       argv[1] = g_strdup_printf ("file://%s#%s",
153                                  relocate (DOCDIR "/pspp.xml"), page);
154       /* The page will be translated to the htmlfilename
155          page                   htmlfilename
156          GRAPH#SCATTERPLOT      SCATTERPLOT.html
157          QUICK-CLUSTER          QUICK-CLUSTER.html
158          which is valid for the multiple page html doc*/
159       tokens = g_strsplit (page, "#", maxtokens);
160       for (idx = 0; idx < maxtokens && tokens[idx]; idx++)
161         ;
162       htmlfilename = g_strdup_printf ("%s.html", tokens[idx-1]);
163       g_strfreev (tokens);
164     }
165   /* Hint: pspp.html is a directory...*/
166   htmlfullname = g_strdup_printf ("%s/%s", relocate (DOCDIR "/pspp.html"),
167                                   htmlfilename);
168   if (g_file_test (relocate (DOCDIR "/pspp.html"), G_FILE_TEST_IS_DIR))
169     {
170       GError *urierr = NULL;
171       htmluri =  g_filename_to_uri (htmlfullname,NULL, &urierr);
172       if (!htmluri)
173         {
174           msg (ME, _("Help path conversion error: %s"), urierr->message);
175           htmluri = htmlfullname;
176         }
177       g_clear_error (&urierr);
178     }
179   else
180     htmluri = g_strdup_printf (PACKAGE_URL "manual/html_node/%s",
181                                htmlfilename);
182   g_free (htmlfullname);
183   g_free (htmlfilename);
184   htmlargv[1] = htmluri;
185
186   /* The following **SHOULD** work but it does not on 28.5.2016
187      g_app_info_launch_default_for_uri (htmluri, NULL, &err);
188      osx: wine is started to launch the uri...
189      windows: not so bad, but the first access does not work*/
190
191   if (! (g_spawn_async (NULL, argv,
192                         NULL, G_SPAWN_SEARCH_PATH,
193                         NULL, NULL,   NULL,   &err) ||
194 #ifdef _WIN32
195          open_windows_help (htmluri, &htmlerr))
196 #else
197          g_spawn_async (NULL, htmlargv,
198                         NULL, G_SPAWN_SEARCH_PATH,
199                         NULL, NULL,   NULL,   &htmlerr))
200 #endif
201       )
202     {
203       msg (ME, _("Cannot open reference manual via yelp: %s. "
204                  "Cannot open via html: %s "
205                  "with uri: %s "
206                  "The PSSP manual is also available at %s"),
207                   err->message,
208                   htmlerr->message,
209                   htmluri,
210                   PACKAGE_URL "documentation.html");
211     }
212
213   g_free (argv[1]);
214   g_free (htmluri);
215   g_clear_error (&err);
216   g_clear_error (&htmlerr);
217 }
218
219 static void
220 reference_manual (GtkMenuItem *menu, gpointer data)
221 {
222   online_help (NULL);
223 }
224
225 GtkWidget *
226 create_help_menu (GtkWindow *toplevel)
227 {
228   GtkWidget *menuitem = gtk_menu_item_new_with_mnemonic (_("_Help"));
229   GtkWidget *menu = gtk_menu_new ();
230
231   GtkWidget *help_about = gtk_menu_item_new_with_mnemonic (_("_About"));
232   GtkWidget *help_ref = gtk_menu_item_new_with_mnemonic (_("_Reference Manual"));
233
234   GtkAccelGroup *accel_group = gtk_accel_group_new ();
235
236   gtk_window_add_accel_group (toplevel, accel_group);
237
238   gtk_widget_add_accelerator (help_ref,
239                               "activate", accel_group,
240                               GDK_KEY_F1, 0,
241                               GTK_ACCEL_VISIBLE);
242
243   gtk_menu_attach (GTK_MENU (menu), help_ref, 0, 1, 0, 1);
244   gtk_menu_attach (GTK_MENU (menu), help_about, 0, 1, 1, 2);
245
246   g_signal_connect (help_about, "activate", G_CALLBACK (about_new), toplevel);
247   g_signal_connect (help_ref, "activate", G_CALLBACK (reference_manual), NULL);
248
249   g_object_set (menuitem, "submenu", menu, NULL);
250
251   gtk_widget_show_all (menuitem);
252
253   return menuitem;
254 }