Simplify vflist_get_formatted()
[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 #include "ui_misc.h"
29 #include "ui_utildlg.h"
30
31 GtkWidget *window_new(GtkWindowType type, const gchar *role, const gchar *icon,
32                       const gchar *icon_file, const gchar *subtitle)
33 {
34         gchar *title;
35         GtkWidget *window;
36
37         window = gtk_window_new(type);
38         if (!window) return NULL;
39
40         if (subtitle)
41                 {
42                 title = g_strdup_printf("%s - %s", subtitle, GQ_APPNAME);
43                 }
44         else
45                 {
46                 title = g_strdup_printf("%s", GQ_APPNAME);
47                 }
48
49         gtk_window_set_title(GTK_WINDOW(window), title);
50         g_free(title);
51
52         window_set_icon(window, icon, icon_file);
53         gtk_window_set_role(GTK_WINDOW(window), role);
54
55         return window;
56 }
57
58 void window_set_icon(GtkWidget *window, const gchar *icon, const gchar *file)
59 {
60         if (!icon && !file) icon = PIXBUF_INLINE_ICON;
61
62         if (icon)
63                 {
64                 GdkPixbuf *pixbuf;
65
66                 pixbuf = pixbuf_inline(icon);
67                 if (pixbuf)
68                         {
69                         gtk_window_set_icon(GTK_WINDOW(window), pixbuf);
70                         g_object_unref(pixbuf);
71                         }
72                 }
73         else
74                 {
75                 gtk_window_set_icon_from_file(GTK_WINDOW(window), file, NULL);
76                 }
77 }
78
79 gboolean window_maximized(GtkWidget *window)
80 {
81         GdkWindowState state;
82
83         if (!window || !gtk_widget_get_window(window)) return FALSE;
84
85         state = gdk_window_get_state(gtk_widget_get_window(window));
86         return !!(state & GDK_WINDOW_STATE_MAXIMIZED);
87 }
88
89 /*
90  *-----------------------------------------------------------------------------
91  * Open browser with the help Documentation
92  *-----------------------------------------------------------------------------
93  */
94
95 static gchar *command_result(const gchar *binary, const gchar *command)
96 {
97         gchar *result = NULL;
98         FILE *f;
99         gchar buf[2048];
100         gint l;
101
102         if (!binary || binary[0] == '\0') return NULL;
103         if (!file_in_path(binary)) return NULL;
104
105         if (!command || command[0] == '\0') return g_strdup(binary);
106         if (command[0] == '!') return g_strdup(command + 1);
107
108         f = popen(command, "r");
109         if (!f) return NULL;
110
111         while ((l = fread(buf, sizeof(gchar), sizeof(buf), f)) > 0)
112                 {
113                 if (!result)
114                         {
115                         gint n = 0;
116
117                         while (n < l && buf[n] != '\n' && buf[n] != '\r') n++;
118                         if (n > 0) result = g_strndup(buf, n);
119                         }
120                 }
121
122         pclose(f);
123
124         return result;
125 }
126
127 static int help_browser_command(const gchar *command, const gchar *path)
128 {
129         gchar *result;
130         gchar *buf;
131         gchar *begin;
132         gchar *end;
133         int retval = -1;
134
135         if (!command || !path) return retval;
136
137         DEBUG_1("Help command pre \"%s\", \"%s\"", command, path);
138
139         buf = g_strdup(command);
140         begin = strstr(buf, "%s");
141         if (begin)
142                 {
143                 *begin = '\0';
144                 end = begin + 2;
145                 begin = buf;
146
147                 result = g_strdup_printf("%s%s%s &", begin, path, end);
148                 }
149         else
150                 {
151                 result = g_strdup_printf("%s \"%s\" &", command, path);
152                 }
153         g_free(buf);
154
155         DEBUG_1("Help command post [%s]", result);
156
157         retval = runcmd(result);
158         DEBUG_1("Help command exit code: %d", retval);
159
160         g_free(result);
161         return retval;
162 }
163
164 /*
165  * each set of 2 strings is one browser:
166  *   the 1st is the binary to look for in the path
167  *   the 2nd has 3 capabilities:
168  *        NULL     exec binary with html file path as command line
169  *        string   exec string and use results for command line
170  *        !string  use text following ! as command line, replacing optional %s with html file path
171 */
172 static gchar *html_browsers[] =
173 {
174         /* Our specific script */
175         GQ_APPNAME_LC "_html_browser", NULL,
176         /* Redhat has a nifty htmlview script to start the user's preferred browser */
177         "htmlview",     NULL,
178         /* Debian has even better approach with alternatives */
179         "sensible-browser", NULL,
180         /* GNOME 2 */
181         "gconftool-2",  "gconftool-2 -g /desktop/gnome/url-handlers/http/command",
182         /* KDE */
183         "kfmclient",    "!kfmclient exec \"%s\"",
184         /* use fallbacks */
185         "firefox",      NULL,
186         "mozilla",      NULL,
187         "konqueror",    NULL,
188         "netscape",     NULL,
189         "opera",        "!opera --remote 'openURL(%s,new-page)'",
190         NULL,           NULL
191 };
192
193 static void help_browser_run(const gchar *path)
194 {
195         gchar *name = options->helpers.html_browser.command_name;
196         gchar *cmd = options->helpers.html_browser.command_line;
197         gchar *result = NULL;
198         gint i;
199
200         i = 0;
201         while (!result)
202                 {
203                 if ((name && *name) || (cmd && *cmd)) {
204                         DEBUG_1("Trying browser: name=%s command=%s", name, cmd);
205                         result = command_result(name, cmd);
206                         DEBUG_1("Result: %s", result);
207                         if (result)
208                                 {
209                                 int ret = help_browser_command(result, path);
210
211                                 if (ret == 0) break;
212                                 g_free(result);
213                                 result = NULL;
214                         }
215                 }
216                 if (!html_browsers[i]) break;
217                 name = html_browsers[i++];
218                 cmd = html_browsers[i++];
219                 }
220
221         if (!result)
222                 {
223                 log_printf("Unable to detect an installed browser.\n");
224                 return;
225                 }
226
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                 path = g_build_filename(gq_htmldir, key, NULL);
250                 if (!isfile(path))
251                         {
252                         if (g_strcmp0(key, "index.html") == 0)
253                                 {
254                                 path = g_build_filename("http://geeqie.org/help/", "GuideIndex.html", NULL);
255                                 }
256                         else
257                                 {
258                                 path = g_build_filename("http://geeqie.org/help/", key, NULL);
259                                 }
260                         }
261                 help_browser_run(path);
262                 g_free(path);
263                 return;
264                 }
265
266         if (help_window)
267                 {
268                 gtk_window_present(GTK_WINDOW(help_window));
269                 if (key) help_window_set_key(help_window, key);
270                 return;
271                 }
272
273         if (!strcmp(key, "release_notes"))
274                 {
275                 path = g_build_filename(gq_helpdir, "README.html", NULL);
276                 if (isfile(path))
277                         {
278                         g_free(path);
279                         path = g_build_filename("file://", gq_helpdir, "README.html", NULL);
280                         help_browser_run(path);
281                         g_free(path);
282                         }
283                 else
284                         {
285                         g_free(path);
286                         path = g_build_filename(gq_helpdir, "README.md", NULL);
287                         help_window = help_window_new(_("Help"), "help", path, key);
288                         g_free(path);
289
290                         g_signal_connect(G_OBJECT(help_window), "destroy",
291                                          G_CALLBACK(help_window_destroy_cb), NULL);
292                         }
293                 }
294         else
295                 {
296                 path = g_build_filename(gq_helpdir, "ChangeLog.html", NULL);
297                 if (isfile(path))
298                         {
299                         g_free(path);
300                         path = g_build_filename("file://", gq_helpdir, "ChangeLog.html", NULL);
301                         help_browser_run(path);
302                         g_free(path);
303                         }
304                 else
305                         {
306                         g_free(path);
307                         path = g_build_filename(gq_helpdir, "ChangeLog", NULL);
308                         help_window = help_window_new(_("Help"), "help", path, key);
309                         g_free(path);
310
311                         g_signal_connect(G_OBJECT(help_window), "destroy",
312                                          G_CALLBACK(help_window_destroy_cb), NULL);
313                         }
314
315                 }
316 }
317
318 /*
319  *-----------------------------------------------------------------------------
320  * on-line help search dialog
321  *-----------------------------------------------------------------------------
322  */
323
324 typedef struct _HelpSearchData HelpSearchData;
325 struct _HelpSearchData {
326         GenericDialog *gd;
327         GtkWidget *edit_widget;
328         gchar *text_entry;
329 };
330
331 static void help_search_window_show_icon_press(GtkEntry *entry, GtkEntryIconPosition pos,
332                                                                         GdkEvent *event, gpointer userdata)
333 {
334         HelpSearchData *hsd = userdata;
335
336         g_free(hsd->text_entry);
337         hsd->text_entry = g_strdup("");
338         gtk_entry_set_text(GTK_ENTRY(hsd->edit_widget), hsd->text_entry);
339 }
340
341 static void help_search_window_ok_cb(GenericDialog *gd, gpointer data)
342 {
343         HelpSearchData *hsd = data;
344         gchar *search_command;
345
346         search_command = g_strconcat(options->help_search_engine,
347                                                 gtk_entry_get_text(GTK_ENTRY(hsd->edit_widget)),
348                                                 NULL);
349         help_browser_run(search_command);
350         g_free(search_command);
351
352         g_free(hsd);
353 }
354
355 static void help_search_window_cancel_cb(GenericDialog *gd, gpointer data)
356 {
357         HelpSearchData *hsd = data;
358
359         g_free(hsd);
360 }
361
362 void help_search_window_show()
363 {
364         HelpSearchData *hsd;
365         GenericDialog *gd;
366         GtkWidget *table;
367         GtkWidget *label1;
368         GtkWidget *label2;
369
370         hsd = g_new0(HelpSearchData, 1);
371         hsd->gd = gd = generic_dialog_new(_("On-line help search"), "help_search",
372                                 NULL, TRUE,
373                                 help_search_window_cancel_cb, hsd);
374         generic_dialog_add_message(gd, NULL, _("Search the on-line help files.\n"), NULL, FALSE);
375
376         generic_dialog_add_button(gd, GTK_STOCK_OK, NULL,
377                                   help_search_window_ok_cb, TRUE);
378
379         label1 = pref_label_new(GENERIC_DIALOG(gd)->vbox, _("Search engine:"));
380 #if GTK_CHECK_VERSION(3,16,0)
381         gtk_label_set_xalign(GTK_LABEL(label1), 0.0);
382         gtk_label_set_yalign(GTK_LABEL(label1), 0.5);
383 #else
384    gtk_misc_set_alignment(GTK_MISC(label1), 0.0, 0.5);
385 #endif
386
387         label2 = pref_label_new(GENERIC_DIALOG(gd)->vbox, options->help_search_engine);
388 #if GTK_CHECK_VERSION(3,16,0)
389         gtk_label_set_xalign(GTK_LABEL(label2), 0.0);
390         gtk_label_set_yalign(GTK_LABEL(label2), 0.5);
391 #else
392    gtk_misc_set_alignment(GTK_MISC(label2), 0.0, 0.5);
393 #endif
394         pref_spacer(GENERIC_DIALOG(gd)->vbox, 0);
395
396         table = pref_table_new(gd->vbox, 3, 1, FALSE, TRUE);
397         pref_table_label(table, 0, 0, _("Search terms:"), 1.0);
398         hsd->edit_widget = gtk_entry_new();
399         gtk_widget_set_size_request(hsd->edit_widget, 300, -1);
400         gtk_table_attach_defaults(GTK_TABLE(table), hsd->edit_widget, 1, 2, 0, 1);
401         generic_dialog_attach_default(gd, hsd->edit_widget);
402         gtk_widget_show(hsd->edit_widget);
403
404         gtk_entry_set_icon_from_stock(GTK_ENTRY(hsd->edit_widget),
405                                                 GTK_ENTRY_ICON_SECONDARY, GTK_STOCK_CLEAR);
406         gtk_entry_set_icon_tooltip_text (GTK_ENTRY(hsd->edit_widget),
407                                                 GTK_ENTRY_ICON_SECONDARY, _("Clear"));
408         g_signal_connect(GTK_ENTRY(hsd->edit_widget), "icon-press",
409                                                 G_CALLBACK(help_search_window_show_icon_press), hsd);
410
411         gtk_widget_grab_focus(hsd->edit_widget);
412
413         gtk_widget_show(gd->dialog);
414 }
415 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */