added help function via html if yelp is not installed
[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 #ifdef __APPLE__
35 #define HTMLOPENAPP "open"
36 #elif  _WIN32
37 #define HTMLOPENAPP "start"
38 #else
39 #define HTMLOPENAPP "xdg-open"
40 #endif
41
42 static const gchar *artists[] = { "Bastián Díaz", "Hugo Alejandro", NULL};
43
44 static void
45 about_new (GtkMenuItem *mmm, GtkWindow *parent)
46 {
47   GtkWidget *about =  gtk_about_dialog_new ();
48
49   gtk_about_dialog_set_logo_icon_name (GTK_ABOUT_DIALOG (about), "pspp");
50
51   gtk_window_set_icon_name (GTK_WINDOW (about), "pspp");
52
53   gtk_about_dialog_set_website (GTK_ABOUT_DIALOG (about), PACKAGE_URL);
54
55   gtk_about_dialog_set_version (GTK_ABOUT_DIALOG (about),
56                                 announced_version);
57
58   gtk_about_dialog_set_authors (GTK_ABOUT_DIALOG (about),
59                                 (const gchar **) authors);
60
61   gtk_about_dialog_set_artists (GTK_ABOUT_DIALOG (about),
62                                 artists);
63
64   gtk_about_dialog_set_license (GTK_ABOUT_DIALOG (about),
65                                 copyleft);
66
67   gtk_about_dialog_set_comments (GTK_ABOUT_DIALOG (about),
68                                  _("A program for the analysis of sampled data"));
69
70   gtk_about_dialog_set_copyright (GTK_ABOUT_DIALOG (about),
71                                   "Free Software Foundation");
72
73   gtk_about_dialog_set_translator_credits 
74     (
75      GTK_ABOUT_DIALOG (about),
76      /* TRANSLATORS: Do not translate this string.  Instead, put the names of the people
77         who have helped in the translation. */
78      _("translator-credits")
79      );
80
81   gtk_window_set_transient_for (GTK_WINDOW (about), parent);
82
83   gtk_window_set_modal (GTK_WINDOW (about), TRUE);
84
85   gtk_dialog_run (GTK_DIALOG (about));
86
87   gtk_widget_hide (about);
88 }
89
90 /* Open the manual at PAGE */
91 void
92 online_help (const char *page)
93 {
94   GError *err = NULL;
95   GError *htmlerr = NULL;
96   gchar *argv[3] = { "yelp", 0, 0};
97   gchar *htmlargv[3] = { HTMLOPENAPP, 0, 0};
98   gchar *htmlfilename = NULL;
99   gchar *htmlfullname = NULL;
100
101   if (page == NULL)
102     {
103       argv[1] = g_strdup_printf ("file://%s", relocate (DOCDIR "/pspp.xml"));
104       htmlfilename = g_strdup ("index.html");
105       htmlargv[1] = g_strdup_printf ("file://%s", htmlfilename);
106     }
107   else
108     {
109       gchar **tokens = NULL;
110       const int maxtokens = 5;
111       int idx = 0;
112       argv[1] = g_strdup_printf ("file://%s#%s",
113                                  relocate (DOCDIR "/pspp.xml"), page);
114       tokens = g_strsplit (page, "#", maxtokens);
115       for(;tokens[idx] && idx < maxtokens;idx++);
116       htmlfilename = g_strdup_printf ("%s.html", tokens[idx-1]);
117       g_strfreev (tokens);
118     }
119
120   htmlfullname = g_strdup_printf ("%s/%s", relocate (DOCDIR "/pspp.html"),
121                                   htmlfilename);
122   if (g_file_test (relocate (DOCDIR "/pspp.html"), G_FILE_TEST_EXISTS))
123     htmlargv[1] = g_strdup_printf ("file://%s", htmlfullname);
124   else
125     htmlargv[1] = g_strdup_printf (PACKAGE_URL "manual/html_node/%s",
126                                    htmlfilename);
127
128   g_free (htmlfullname);
129   g_free (htmlfilename);
130
131   if (! (g_spawn_async (NULL, argv,
132                         NULL, G_SPAWN_SEARCH_PATH,
133                         NULL, NULL,   NULL,   &err) ||
134          g_spawn_async (NULL, htmlargv,
135                         NULL, G_SPAWN_SEARCH_PATH,
136                         NULL, NULL,   NULL,   &htmlerr))
137       )
138     {
139       msg (ME, _("Cannot open reference manual via yelp: %s. "
140                  "Cannot open via html: %s "
141                  "The PSSP manual is also available at %s"),
142                   err->message,
143                   htmlerr->message,
144                   PACKAGE_URL "documentation.html");
145     }
146
147   g_free (argv[1]);
148   g_free (htmlargv[1]);
149   g_clear_error (&err);
150   g_clear_error (&htmlerr);
151 }
152
153 static void
154 reference_manual (GtkMenuItem *menu, gpointer data)
155 {
156   online_help (NULL);
157 }
158
159 GtkWidget *
160 create_help_menu (GtkWindow *toplevel)
161 {
162   GtkWidget *menuitem = gtk_menu_item_new_with_mnemonic (_("_Help"));
163   GtkWidget *menu = gtk_menu_new ();
164
165   GtkWidget *help_about = gtk_menu_item_new_with_mnemonic (_("_About"));
166   GtkWidget *help_ref = gtk_menu_item_new_with_mnemonic (_("_Reference Manual"));
167
168   GtkAccelGroup *accel_group = gtk_accel_group_new ();
169   
170   gtk_window_add_accel_group (toplevel, accel_group);
171
172   gtk_widget_add_accelerator (help_ref,
173                               "activate", accel_group,
174                               GDK_KEY_F1, 0,
175                               GTK_ACCEL_VISIBLE);
176
177   gtk_menu_attach (GTK_MENU (menu), help_ref, 0, 1, 0, 1);
178   gtk_menu_attach (GTK_MENU (menu), help_about, 0, 1, 1, 2);
179
180   g_signal_connect (help_about, "activate", G_CALLBACK (about_new), toplevel);
181   g_signal_connect (help_ref, "activate", G_CALLBACK (reference_manual), NULL);
182   
183   g_object_set (menuitem, "submenu", menu, NULL);
184
185   gtk_widget_show_all (menuitem);
186   
187   return menuitem;
188 }