2 * Copyright (C) 2008 - 2016 The Geeqie Team
4 * Authors: Vladimir Nadvornik, Laurent Monin
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.
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.
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.
27 #include <gdk-pixbuf/gdk-pixbuf.h>
29 #include <glib-object.h>
35 #include "main-defines.h"
39 #include "pixbuf-util.h"
40 #include "ui-fileops.h"
43 #include "ui-utildlg.h"
49 operator bool() const { return (binary && *binary) || (command && *command); }
50 gchar *command_result() const;
52 const gchar *binary; /**< the binary to look for in the path */
53 const gchar *command; /**< has 3 capabilities:
54 * nullptr exec binary with html file path as command line
55 * string exec string and use results for command line
56 * !string use text following ! as command line, replacing optional %s with html file path */
59 constexpr std::array<HtmlBrowser, 10> html_browsers{{
60 /* Our specific script */
61 {GQ_APPNAME_LC "_html_browser", nullptr},
62 /* Redhat has a nifty htmlview script to start the user's preferred browser */
63 {"htmlview", nullptr},
64 /* Debian has even better approach with alternatives */
65 {"sensible-browser", nullptr},
67 {"gconftool-2", "gconftool-2 -g /desktop/gnome/url-handlers/http/command"},
69 {"kfmclient", "!kfmclient exec \"%s\""},
73 {"konqueror", nullptr},
74 {"netscape", nullptr},
75 {"opera", "!opera --remote 'openURL(%s,new-page)'"},
78 gchar *HtmlBrowser::command_result() const
80 gchar *result = nullptr;
85 if (!binary || binary[0] == '\0') return nullptr;
86 if (!file_in_path(binary)) return nullptr;
88 if (!command || command[0] == '\0') return g_strdup(binary);
89 if (command[0] == '!') return g_strdup(command + 1);
91 f = popen(command, "r");
92 if (!f) return nullptr;
94 while ((l = fread(buf, sizeof(gchar), sizeof(buf), f)) > 0)
100 while (n < l && buf[n] != '\n' && buf[n] != '\r') n++;
101 if (n > 0) result = g_strndup(buf, n);
112 GtkWidget *window_new(const gchar *role, const gchar *icon, const gchar *icon_file, const gchar *subtitle)
118 window = gtk_window_new();
120 window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
122 if (!window) return nullptr;
126 title = g_strdup_printf("%s - %s", subtitle, GQ_APPNAME);
130 title = g_strdup_printf("%s", GQ_APPNAME);
133 gtk_window_set_title(GTK_WINDOW(window), title);
136 window_set_icon(window, icon, icon_file);
137 gtk_window_set_role(GTK_WINDOW(window), role);
142 void window_set_icon(GtkWidget *window, const gchar *icon, const gchar *file)
144 if (!icon && !file) icon = PIXBUF_INLINE_ICON;
150 pixbuf = pixbuf_inline(icon);
153 gtk_window_set_icon(GTK_WINDOW(window), pixbuf);
154 g_object_unref(pixbuf);
159 gtk_window_set_icon_from_file(GTK_WINDOW(window), file, nullptr);
163 gboolean window_maximized(GtkWidget *window)
165 GdkWindowState state;
167 if (!window || !gtk_widget_get_window(window)) return FALSE;
169 state = gdk_window_get_state(gtk_widget_get_window(window));
170 return !!(state & GDK_WINDOW_STATE_MAXIMIZED);
174 *-----------------------------------------------------------------------------
175 * Open browser with the help Documentation
176 *-----------------------------------------------------------------------------
179 static int help_browser_command(const gchar *command, const gchar *path)
187 if (!command || !path) return retval;
189 DEBUG_1("Help command pre \"%s\", \"%s\"", command, path);
191 buf = g_strdup(command);
192 begin = strstr(buf, "%s");
199 result = g_strdup_printf("%s%s%s &", begin, path, end);
203 result = g_strdup_printf("%s \"%s\" &", command, path);
207 DEBUG_1("Help command post [%s]", result);
209 retval = runcmd(result);
210 DEBUG_1("Help command exit code: %d", retval);
216 static void help_browser_run(const gchar *path)
218 const auto try_browser = [path](const HtmlBrowser &html_browser)
220 if (!html_browser) return false;
222 DEBUG_1("Trying browser: name=%s command=%s", html_browser.binary, html_browser.command);
224 gchar *result = html_browser.command_result();
225 DEBUG_1("Result: %s", result);
226 if (!result) return false;
228 int ret = help_browser_command(result, path);
233 if (try_browser({options->helpers.html_browser.command_name, options->helpers.html_browser.command_line})) return;
235 for (const auto &html_browser : html_browsers)
237 if (try_browser(html_browser)) return;
240 log_printf("Unable to detect an installed browser.\n");
244 *-----------------------------------------------------------------------------
246 *-----------------------------------------------------------------------------
249 static GtkWidget *help_window = nullptr;
251 static void help_window_destroy_cb(GtkWidget *, gpointer)
253 help_window = nullptr;
256 void help_window_show(const gchar *key)
260 if (key && strstr(key, ".html") != nullptr)
262 path = g_build_filename(gq_htmldir, key, NULL);
265 if (g_strcmp0(key, "index.html") == 0)
267 path = g_build_filename("https://www.geeqie.org/help/", "GuideIndex.html", NULL);
271 path = g_build_filename("https://www.geeqie.org/help/", key, NULL);
274 help_browser_run(path);
281 gtk_window_present(GTK_WINDOW(help_window));
282 if (key) help_window_set_key(help_window, key);
286 if (!strcmp(key, "release_notes"))
288 path = g_build_filename(gq_helpdir, "README.html", NULL);
292 path = g_build_filename("file://", gq_helpdir, "README.html", NULL);
293 help_browser_run(path);
299 path = g_build_filename(gq_helpdir, "README.md", NULL);
300 help_window = help_window_new(_("Help"), "help", path, key);
303 g_signal_connect(G_OBJECT(help_window), "destroy",
304 G_CALLBACK(help_window_destroy_cb), NULL);
309 path = g_build_filename(gq_helpdir, "ChangeLog.html", NULL);
313 path = g_build_filename("file://", gq_helpdir, "ChangeLog.html", NULL);
314 help_browser_run(path);
320 path = g_build_filename(gq_helpdir, "ChangeLog", NULL);
321 help_window = help_window_new(_("Help"), "help", path, key);
324 g_signal_connect(G_OBJECT(help_window), "destroy",
325 G_CALLBACK(help_window_destroy_cb), NULL);
332 *-----------------------------------------------------------------------------
333 * on-line help search dialog
334 *-----------------------------------------------------------------------------
337 struct HelpSearchData {
339 GtkWidget *edit_widget;
343 static void help_search_window_show_icon_press(GtkEntry *, GtkEntryIconPosition, GdkEvent *, gpointer userdata)
345 auto hsd = static_cast<HelpSearchData *>(userdata);
347 g_free(hsd->text_entry);
348 hsd->text_entry = g_strdup("");
349 gq_gtk_entry_set_text(GTK_ENTRY(hsd->edit_widget), hsd->text_entry);
352 static void help_search_window_ok_cb(GenericDialog *, gpointer data)
354 auto hsd = static_cast<HelpSearchData *>(data);
355 gchar *search_command;
357 search_command = g_strconcat(options->help_search_engine,
358 gq_gtk_entry_get_text(GTK_ENTRY(hsd->edit_widget)),
360 help_browser_run(search_command);
361 g_free(search_command);
366 static void help_search_window_cancel_cb(GenericDialog *, gpointer data)
368 auto hsd = static_cast<HelpSearchData *>(data);
373 void help_search_window_show()
380 auto hsd = g_new0(HelpSearchData, 1);
381 hsd->gd = gd = generic_dialog_new(_("On-line help search"), "help_search",
383 help_search_window_cancel_cb, hsd);
384 generic_dialog_add_message(gd, nullptr, _("Search the on-line help files.\n"), nullptr, FALSE);
386 generic_dialog_add_button(gd, GQ_ICON_OK, "OK",
387 help_search_window_ok_cb, TRUE);
389 label1 = pref_label_new(GENERIC_DIALOG(gd)->vbox, _("Search engine:"));
390 gtk_label_set_xalign(GTK_LABEL(label1), 0.0);
391 gtk_label_set_yalign(GTK_LABEL(label1), 0.5);
393 label2 = pref_label_new(GENERIC_DIALOG(gd)->vbox, options->help_search_engine);
394 gtk_label_set_xalign(GTK_LABEL(label2), 0.0);
395 gtk_label_set_yalign(GTK_LABEL(label2), 0.5);
397 pref_spacer(GENERIC_DIALOG(gd)->vbox, 0);
399 table = pref_table_new(gd->vbox, 3, 1, FALSE, TRUE);
400 pref_table_label(table, 0, 0, _("Search terms:"), GTK_ALIGN_END);
401 hsd->edit_widget = gtk_entry_new();
402 gtk_widget_set_size_request(hsd->edit_widget, 300, -1);
403 gq_gtk_grid_attach_default(GTK_GRID(table), hsd->edit_widget, 1, 2, 0, 1);
404 generic_dialog_attach_default(gd, hsd->edit_widget);
405 gtk_widget_show(hsd->edit_widget);
407 gtk_entry_set_icon_from_icon_name(GTK_ENTRY(hsd->edit_widget),
408 GTK_ENTRY_ICON_SECONDARY, GQ_ICON_CLEAR);
409 gtk_entry_set_icon_tooltip_text (GTK_ENTRY(hsd->edit_widget),
410 GTK_ENTRY_ICON_SECONDARY, _("Clear"));
411 g_signal_connect(GTK_ENTRY(hsd->edit_widget), "icon-press",
412 G_CALLBACK(help_search_window_show_icon_press), hsd);
414 gtk_widget_grab_focus(hsd->edit_widget);
416 gtk_widget_show(gd->dialog);
418 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */