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