1 /*
2  *  Copyright © 2008 Thomas H.P. Andersen <phomes@gmail.com>
3  *  Copyright © 2007, 2008, 2009 Christian Persch
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #include <config.h>
20 
21 #include <string.h>
22 
23 #include <glib/gi18n.h>
24 #include <gtk/gtk.h>
25 
26 #include "ar-show.h"
27 #include "ar-runtime.h"
28 
29 #include "ar-help.h"
30 
31 /**
32  * ar_help_display_full:
33  * @window: a #GdkWindow get the #GdkScreen from, and to use
34  *   as parent window for an error dialogue
35  * @doc_module: the doc module name (same as DOC_MODULE from help/Makefile.am)
36  * @section: a section name, or %NULL
37  * @error: a #GError location, or %NULL
38  *
39  * Opens help or returns an error.
40  *
41  * Returns: %TRUE on success, or %FALSE on failure with @error filled in
42  */
43 gboolean
ar_help_display_full(GtkWidget * window,const char * doc_module,const char * section,GError ** error)44 ar_help_display_full (GtkWidget *window,
45                          const char *doc_module,
46                          const char *section,
47                          GError **error)
48 {
49   GdkScreen *screen;
50   char *help_uri;
51   gboolean ret;
52 
53   g_return_val_if_fail (doc_module != NULL, TRUE);
54 
55   screen = gtk_widget_get_screen (GTK_WIDGET (window));
56 
57   /* FIXME: do we need to use g_uri_escape_string for doc_module and section? */
58 
59 #if defined(WITH_HELP_METHOD_GHELP)
60   if (section != NULL) {
61     char *escaped_section;
62 
63     escaped_section = g_uri_escape_string  (section, NULL, TRUE);
64     help_uri = g_strdup_printf ("help:%s/%s", doc_module, escaped_section);
65     g_free (escaped_section);
66   } else {
67     help_uri = g_strdup_printf ("help:%s", doc_module);
68   }
69 #elif defined(WITH_HELP_METHOD_FILE)
70   const char *help_dir;
71   const char * const *langs;
72   guint i;
73 
74   langs = g_get_language_names ();
75   help_dir = ar_runtime_get_directory (AR_RUNTIME_HELP_DIRECTORY);
76 
77   help_uri = NULL;
78   for (i = 0; langs[i] != NULL; ++i) {
79     const char *lang = langs[i];
80     char *help_file_name, *path;
81 
82     /* Filter out variants */
83     if (strchr (lang, '.') != NULL ||
84         strchr (lang, '@') != NULL)
85       continue;
86 
87     help_file_name = g_strdup_printf ("%s." HELP_FILE_FORMAT,
88                                       section ? section : doc_module);
89     path = g_build_filename (help_dir,
90                              lang,
91                              help_file_name,
92                              NULL);
93     g_free (help_file_name);
94 
95     if (g_file_test (path, G_FILE_TEST_EXISTS)) {
96       help_uri = g_filename_to_uri (path, NULL, NULL);
97       g_free (path);
98       break;
99     }
100 
101     g_free (path);
102   }
103 
104   if (help_uri == NULL) {
105     g_set_error (error,
106                  g_quark_from_static_string ("games-help-error"), 0,
107                  /* %s.%s is the game name + the extension HTML or XHTML, e.g. Klondike.html" */
108                  _("Help file “%s.%s” not found"),
109                  section ? section : doc_module,
110                  HELP_FILE_FORMAT);
111     return FALSE;
112   }
113 
114 #elif defined(WITH_HELP_METHOD_LIBRARY)
115   if (section != NULL) {
116     help_uri = g_strdup_printf ("http://library.gnome.org/users/%s/stable/%s.html", doc_module, section);
117   } else {
118     help_uri = g_strdup_printf ("http://library.gnome.org/users/%s/stable/", doc_module);
119   }
120 #endif
121 
122   ret = ar_show_uri (screen, help_uri, gtk_get_current_event_time (), error);
123 
124   g_free (help_uri);
125   return ret;
126 }
127 
128 /**
129  * ar_help_display:
130  * @window: a #GdkWindow get the #GdkScreen from, and to use
131  *   as parent window for an error dialogue
132  * @doc_module: the doc module name (same as DOC_MODULE from help/Makefile.am)
133  * @section: a section name, or %NULL
134  *
135  * Opens help or displays error dialog when unable to open help.
136  */
137 void
ar_help_display(GtkWidget * window,const char * doc_module,const char * section)138 ar_help_display (GtkWidget *window,
139                     const char *doc_module,
140                     const char *section)
141 {
142   GError *error = NULL;
143 
144   if (!ar_help_display_full (window, doc_module, section, &error)) {
145     ar_show_error (window, error,
146                       _("Could not show help for “%s”"),
147                       section ? section : g_get_application_name ());
148     g_error_free (error);
149   }
150 }
151