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