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