Convert README to markdown
[geeqie.git] / src / window.c
1 /*
2  * Copyright (C) 2008 - 2016 The Geeqie Team
3  *
4  * Authors: Vladimir Nadvornik, Laurent Monin
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  */
20
21 #include "main.h"
22 #include "window.h"
23
24 #include "misc.h"
25 #include "pixbuf_util.h"
26 #include "ui_fileops.h"
27 #include "ui_help.h"
28
29 GtkWidget *window_new(GtkWindowType type, const gchar *role, const gchar *icon,
30                       const gchar *icon_file, const gchar *subtitle)
31 {
32         gchar *title;
33         GtkWidget *window;
34
35         window = gtk_window_new(type);
36         if (!window) return NULL;
37
38         if (subtitle)
39                 {
40                 title = g_strdup_printf("%s - %s", subtitle, GQ_APPNAME);
41                 }
42         else
43                 {
44                 title = g_strdup_printf("%s", GQ_APPNAME);
45                 }
46
47         gtk_window_set_title(GTK_WINDOW(window), title);
48         g_free(title);
49
50         window_set_icon(window, icon, icon_file);
51         gtk_window_set_role(GTK_WINDOW(window), role);
52
53         return window;
54 }
55
56 void window_set_icon(GtkWidget *window, const gchar *icon, const gchar *file)
57 {
58         if (!icon && !file) icon = PIXBUF_INLINE_ICON;
59
60         if (icon)
61                 {
62                 GdkPixbuf *pixbuf;
63
64                 pixbuf = pixbuf_inline(icon);
65                 if (pixbuf)
66                         {
67                         gtk_window_set_icon(GTK_WINDOW(window), pixbuf);
68                         g_object_unref(pixbuf);
69                         }
70                 }
71         else
72                 {
73                 gtk_window_set_icon_from_file(GTK_WINDOW(window), file, NULL);
74                 }
75 }
76
77 gboolean window_maximized(GtkWidget *window)
78 {
79         GdkWindowState state;
80
81         if (!window || !gtk_widget_get_window(window)) return FALSE;
82
83         state = gdk_window_get_state(gtk_widget_get_window(window));
84         return !!(state & GDK_WINDOW_STATE_MAXIMIZED);
85 }
86
87 /*
88  *-----------------------------------------------------------------------------
89  * Open browser with the help Documentation
90  *-----------------------------------------------------------------------------
91  */
92
93 static gchar *command_result(const gchar *binary, const gchar *command)
94 {
95         gchar *result = NULL;
96         FILE *f;
97         gchar buf[2048];
98         gint l;
99
100         if (!binary || binary[0] == '\0') return NULL;
101         if (!file_in_path(binary)) return NULL;
102
103         if (!command || command[0] == '\0') return g_strdup(binary);
104         if (command[0] == '!') return g_strdup(command + 1);
105
106         f = popen(command, "r");
107         if (!f) return NULL;
108
109         while ((l = fread(buf, sizeof(gchar), sizeof(buf), f)) > 0)
110                 {
111                 if (!result)
112                         {
113                         gint n = 0;
114
115                         while (n < l && buf[n] != '\n' && buf[n] != '\r') n++;
116                         if (n > 0) result = g_strndup(buf, n);
117                         }
118                 }
119
120         pclose(f);
121
122         return result;
123 }
124
125 static int help_browser_command(const gchar *command, const gchar *path)
126 {
127         gchar *result;
128         gchar *buf;
129         gchar *begin;
130         gchar *end;
131         int retval = -1;
132
133         if (!command || !path) return retval;
134
135         DEBUG_1("Help command pre \"%s\", \"%s\"", command, path);
136
137         buf = g_strdup(command);
138         begin = strstr(buf, "%s");
139         if (begin)
140                 {
141                 *begin = '\0';
142                 end = begin + 2;
143                 begin = buf;
144
145                 result = g_strdup_printf("%s%s%s &", begin, path, end);
146                 }
147         else
148                 {
149                 result = g_strdup_printf("%s \"%s\" &", command, path);
150                 }
151         g_free(buf);
152
153         DEBUG_1("Help command post [%s]", result);
154
155         retval = runcmd(result);
156         DEBUG_1("Help command exit code: %d", retval);
157
158         g_free(result);
159         return retval;
160 }
161
162 /*
163  * each set of 2 strings is one browser:
164  *   the 1st is the binary to look for in the path
165  *   the 2nd has 3 capabilities:
166  *        NULL     exec binary with html file path as command line
167  *        string   exec string and use results for command line
168  *        !string  use text following ! as command line, replacing optional %s with html file path
169 */
170 static gchar *html_browsers[] =
171 {
172         /* Our specific script */
173         GQ_APPNAME_LC "_html_browser", NULL,
174         /* Redhat has a nifty htmlview script to start the user's preferred browser */
175         "htmlview",     NULL,
176         /* Debian has even better approach with alternatives */
177         "sensible-browser", NULL,
178         /* GNOME 2 */
179         "gconftool-2",  "gconftool-2 -g /desktop/gnome/url-handlers/http/command",
180         /* KDE */
181         "kfmclient",    "!kfmclient exec \"%s\"",
182         /* use fallbacks */
183         "firefox",      NULL,
184         "mozilla",      NULL,
185         "konqueror",    NULL,
186         "netscape",     NULL,
187         "opera",        "!opera --remote 'openURL(%s,new-page)'",
188         NULL,           NULL
189 };
190
191 static void help_browser_run(const gchar *section)
192 {
193         gchar *name = options->helpers.html_browser.command_name;
194         gchar *cmd = options->helpers.html_browser.command_line;
195         gchar *path = g_build_filename("file://", GQ_HTMLDIR, section, NULL);
196         gchar *result = NULL;
197         gint i;
198
199         i = 0;
200         while (!result)
201                 {
202                 if ((name && *name) || (cmd && *cmd)) {
203                         DEBUG_1("Trying browser: name=%s command=%s", name, cmd);
204                         result = command_result(name, cmd);
205                         DEBUG_1("Result: %s", result);
206                         if (result)
207                                 {
208                                 int ret = help_browser_command(result, path);
209
210                                 if (ret == 0) break;
211                                 g_free(result);
212                                 result = NULL;
213                         }
214                 }
215                 if (!html_browsers[i]) break;
216                 name = html_browsers[i++];
217                 cmd = html_browsers[i++];
218                 }
219
220         if (!result)
221                 {
222                 log_printf("Unable to detect an installed browser.\n");
223                 return;
224                 }
225
226         g_free(path);
227         g_free(result);
228 }
229
230 /*
231  *-----------------------------------------------------------------------------
232  * help window
233  *-----------------------------------------------------------------------------
234  */
235
236 static GtkWidget *help_window = NULL;
237
238 static void help_window_destroy_cb(GtkWidget *window, gpointer data)
239 {
240         help_window = NULL;
241 }
242
243 void help_window_show(const gchar *key)
244 {
245         gchar *path;
246
247         if (key && strstr(key, ".html") != 0)
248                 {
249                 help_browser_run(key);
250                 return;
251                 }
252
253         if (help_window)
254                 {
255                 gtk_window_present(GTK_WINDOW(help_window));
256                 if (key) help_window_set_key(help_window, key);
257                 return;
258                 }
259
260         path = g_build_filename(GQ_HELPDIR, "README.md", NULL);
261         help_window = help_window_new(_("Help"), "help", path, key);
262         g_free(path);
263
264         g_signal_connect(G_OBJECT(help_window), "destroy",
265                          G_CALLBACK(help_window_destroy_cb), NULL);
266 }
267
268 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */