2 * Copyright (C) 2004 John Ellis
3 * Copyright (C) 2008 - 2016 The Geeqie Team
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 #include "ui-utildlg.h"
30 #include "ui-fileops.h"
32 #include "ui-pathsel.h"
33 #include "ui-tabcomp.h"
37 *-----------------------------------------------------------------------------
39 *-----------------------------------------------------------------------------
52 static GList *dialog_windows = nullptr;
54 static void generic_dialog_save_window(const gchar *title, const gchar *role, gint x, gint y, gint h, gint w)
58 work = g_list_first(dialog_windows);
61 auto dw = static_cast<DialogWindow *>(work->data);
62 if (g_strcmp0(dw->title ,title) == 0 && g_strcmp0(dw->role, role) == 0)
73 auto dw = g_new0(DialogWindow, 1);
74 dw->title = g_strdup(title);
75 dw->role = g_strdup(role);
81 dialog_windows = g_list_append(dialog_windows, dw);
84 static gboolean generic_dialog_find_window(const gchar *title, const gchar *role, gint *x, gint *y, gint *h, gint *w)
88 work = g_list_first(dialog_windows);
91 auto dw = static_cast<DialogWindow *>(work->data);
93 if (g_strcmp0(dw->title,title) == 0 && g_strcmp0(dw->role, role) == 0)
106 void generic_dialog_close(GenericDialog *gd)
113 gdk_window_get_root_origin(gtk_widget_get_window (gd->dialog), &x, &y);
114 w = gdk_window_get_width(gtk_widget_get_window (gd->dialog));
115 h = gdk_window_get_height(gtk_widget_get_window (gd->dialog));
117 /* The window title is modified in window.cc: window_new()
118 * by appending the string " - Geeqie"
120 ident_string = g_strconcat(" - ", GQ_APPNAME, NULL);
121 full_title = g_strdup(gtk_window_get_title(GTK_WINDOW(gd->dialog)));
122 actual_title = strndup(full_title, g_strrstr(full_title, ident_string) - full_title);
124 generic_dialog_save_window(actual_title, gtk_window_get_role(GTK_WINDOW(gd->dialog)), x, y, w, h);
126 gq_gtk_widget_destroy(gd->dialog);
128 g_free(ident_string);
130 g_free(actual_title);
133 static void generic_dialog_click_cb(GtkWidget *widget, gpointer data)
135 auto gd = static_cast<GenericDialog *>(data);
136 void (*func)(GenericDialog *, gpointer);
139 func = reinterpret_cast<void(*)(GenericDialog *, gpointer)>(g_object_get_data(G_OBJECT(widget), "dialog_function"));
140 auto_close = gd->auto_close;
142 if (func) func(gd, gd->data);
143 if (auto_close) generic_dialog_close(gd);
146 static gboolean generic_dialog_default_key_press_cb(GtkWidget *widget, GdkEventKey *event, gpointer data)
148 auto gd = static_cast<GenericDialog *>(data);
150 if (event->keyval == GDK_KEY_Return && gtk_widget_has_focus(widget)
155 auto_close = gd->auto_close;
156 gd->default_cb(gd, gd->data);
157 if (auto_close) generic_dialog_close(gd);
164 void generic_dialog_attach_default(GenericDialog *gd, GtkWidget *widget)
166 if (!gd || !widget) return;
167 g_signal_connect(G_OBJECT(widget), "key_press_event",
168 G_CALLBACK(generic_dialog_default_key_press_cb), gd);
171 static gboolean generic_dialog_key_press_cb(GtkWidget *widget, GdkEventKey *event, gpointer data)
173 auto gd = static_cast<GenericDialog *>(data);
174 gboolean auto_close = gd->auto_close;
176 if (event->keyval == GDK_KEY_Escape)
180 gd->cancel_cb(gd, gd->data);
181 if (auto_close) generic_dialog_close(gd);
185 if (auto_close) generic_dialog_click_cb(widget, data);
192 static gboolean generic_dialog_delete_cb(GtkWidget *, GdkEventAny *, gpointer data)
194 auto gd = static_cast<GenericDialog *>(data);
197 auto_close = gd->auto_close;
199 if (gd->cancel_cb) gd->cancel_cb(gd, gd->data);
200 if (auto_close) generic_dialog_close(gd);
205 static void generic_dialog_show_cb(GtkWidget *widget, gpointer data)
207 auto gd = static_cast<GenericDialog *>(data);
208 if (gd->cancel_button)
210 gtk_box_reorder_child(GTK_BOX(gd->hbox), gd->cancel_button, -1);
213 g_signal_handlers_disconnect_by_func(G_OBJECT(widget), (gpointer)(generic_dialog_show_cb), gd);
216 gboolean generic_dialog_get_alternative_button_order(GtkWidget *widget)
218 GtkSettings *settings;
220 gboolean alternative_order = FALSE;
222 settings = gtk_settings_get_for_screen(gtk_widget_get_screen(widget));
223 klass = G_OBJECT_CLASS(GTK_SETTINGS_GET_CLASS(settings));
224 if (g_object_class_find_property(klass, "gtk-alternative-button-order"))
226 g_object_get(settings, "gtk-alternative-button-order", &alternative_order, NULL);
229 return alternative_order;
232 GtkWidget *generic_dialog_add_button(GenericDialog *gd, const gchar *icon_name, const gchar *text,
233 void (*func_cb)(GenericDialog *, gpointer), gboolean is_default)
236 gboolean alternative_order;
238 button = pref_button_new(nullptr, icon_name, text,
239 G_CALLBACK(generic_dialog_click_cb), gd);
241 gtk_widget_set_can_default(button, TRUE);
242 g_object_set_data(G_OBJECT(button), "dialog_function", reinterpret_cast<void *>(func_cb));
244 gq_gtk_container_add(GTK_WIDGET(gd->hbox), button);
246 alternative_order = generic_dialog_get_alternative_button_order(gd->hbox);
250 gtk_widget_grab_default(button);
251 gtk_widget_grab_focus(button);
252 gd->default_cb = func_cb;
254 if (!alternative_order) gtk_box_reorder_child(GTK_BOX(gd->hbox), button, -1);
258 if (!alternative_order) gtk_box_reorder_child(GTK_BOX(gd->hbox), button, 0);
261 gtk_widget_show(button);
269 * @param icon_stock_id
272 * @param expand Used as the "expand" and "fill" parameters in the eventual call to gq_gtk_box_pack_start()
277 GtkWidget *generic_dialog_add_message(GenericDialog *gd, const gchar *icon_name,
278 const gchar *heading, const gchar *text, gboolean expand)
284 hbox = pref_box_new(gd->vbox, expand, GTK_ORIENTATION_HORIZONTAL, PREF_PAD_SPACE);
289 image = gtk_image_new_from_icon_name(icon_name, GTK_ICON_SIZE_DIALOG);
290 gtk_widget_set_halign(GTK_WIDGET(image), GTK_ALIGN_CENTER);
291 gtk_widget_set_valign(GTK_WIDGET(image), GTK_ALIGN_START);
292 gq_gtk_box_pack_start(GTK_BOX(hbox), image, FALSE, FALSE, 0);
293 gtk_widget_show(image);
296 vbox = pref_box_new(hbox, TRUE, GTK_ORIENTATION_VERTICAL, PREF_PAD_SPACE);
299 label = pref_label_new(vbox, heading);
300 pref_label_bold(label, TRUE, TRUE);
301 gtk_label_set_xalign(GTK_LABEL(label), 0.0);
302 gtk_label_set_yalign(GTK_LABEL(label), 0.5);
306 label = pref_label_new(vbox, text);
307 gtk_label_set_xalign(GTK_LABEL(label), 0.0);
308 gtk_label_set_yalign(GTK_LABEL(label), 0.5);
309 gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
315 void generic_dialog_windows_load_config(const gchar **attribute_names, const gchar **attribute_values)
317 auto dw = g_new0(DialogWindow, 1);
318 gchar *title = nullptr;
319 gchar *role = nullptr;
325 while (*attribute_names)
327 const gchar *option = *attribute_names++;
328 const gchar *value = *attribute_values++;
329 if (READ_CHAR_FULL("title", title)) continue;
330 if (READ_CHAR_FULL("role", role)) continue;
331 if (READ_INT_FULL("x", x)) continue;
332 if (READ_INT_FULL("y", y)) continue;
333 if (READ_INT_FULL("w", w)) continue;
334 if (READ_INT_FULL("h", h)) continue;
336 log_printf("unknown attribute %s = %s\n", option, value);
339 if (title && title[0] != 0)
341 dw->title = g_strdup(title);
342 dw->role = g_strdup(role);
348 dialog_windows = g_list_append(dialog_windows, dw);
352 void generic_dialog_windows_write_config(GString *outstr, gint indent)
356 if (options->save_dialog_window_positions && dialog_windows)
358 WRITE_NL(); WRITE_STRING("<%s>", "dialogs");
361 work = g_list_first(dialog_windows);
364 auto dw = static_cast<DialogWindow *>(work->data);
365 WRITE_NL(); WRITE_STRING("<window ");
366 write_char_option(outstr, indent + 1, "title", dw->title);
367 write_char_option(outstr, indent + 1, "role", dw->role);
376 WRITE_NL(); WRITE_STRING("</%s>", "dialogs");
380 static void generic_dialog_setup(GenericDialog *gd,
383 GtkWidget *parent, gboolean auto_close,
384 void (*cancel_cb)(GenericDialog *, gpointer), gpointer data)
390 gd->auto_close = auto_close;
392 gd->cancel_cb = cancel_cb;
394 gd->dialog = window_new(role, nullptr, nullptr, title);
395 DEBUG_NAME(gd->dialog);
396 gtk_window_set_type_hint(GTK_WINDOW(gd->dialog), GDK_WINDOW_TYPE_HINT_DIALOG);
398 if (options->save_dialog_window_positions)
400 if (generic_dialog_find_window(title, role, &x, &y, &w, &h))
402 gtk_window_set_default_size(GTK_WINDOW(gd->dialog), w, h);
403 gq_gtk_window_move(GTK_WINDOW(gd->dialog), x, y);
409 GtkWindow *window = nullptr;
411 if (GTK_IS_WINDOW(parent))
413 window = GTK_WINDOW(parent);
419 top = gtk_widget_get_toplevel(parent);
420 if (GTK_IS_WINDOW(top) && gtk_widget_is_toplevel(top)) window = GTK_WINDOW(top);
423 if (window) gtk_window_set_transient_for(GTK_WINDOW(gd->dialog), window);
426 g_signal_connect(G_OBJECT(gd->dialog), "delete_event",
427 G_CALLBACK(generic_dialog_delete_cb), gd);
428 g_signal_connect(G_OBJECT(gd->dialog), "key_press_event",
429 G_CALLBACK(generic_dialog_key_press_cb), gd);
431 gtk_window_set_resizable(GTK_WINDOW(gd->dialog), TRUE);
432 gtk_container_set_border_width(GTK_CONTAINER(gd->dialog), PREF_PAD_BORDER);
434 scrolled = gq_gtk_scrolled_window_new(nullptr, nullptr);
435 gtk_scrolled_window_set_propagate_natural_height(GTK_SCROLLED_WINDOW(scrolled), TRUE);
436 gtk_scrolled_window_set_propagate_natural_width(GTK_SCROLLED_WINDOW(scrolled), TRUE);
437 vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, PREF_PAD_BUTTON_SPACE);
438 gq_gtk_container_add(GTK_WIDGET(scrolled), vbox);
439 gq_gtk_container_add(GTK_WIDGET(gd->dialog), scrolled);
440 gtk_widget_show(scrolled);
442 gtk_widget_show(vbox);
444 gd->vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, PREF_PAD_GAP);
445 gq_gtk_box_pack_start(GTK_BOX(vbox), gd->vbox, TRUE, TRUE, 0);
446 gtk_widget_show(gd->vbox);
448 gd->hbox = gtk_button_box_new(GTK_ORIENTATION_HORIZONTAL);
449 gtk_button_box_set_layout(GTK_BUTTON_BOX(gd->hbox), GTK_BUTTONBOX_END);
450 gtk_box_set_spacing(GTK_BOX(gd->hbox), PREF_PAD_BUTTON_GAP);
451 gq_gtk_box_pack_start(GTK_BOX(vbox), gd->hbox, FALSE, FALSE, 0);
452 gtk_widget_show(gd->hbox);
456 gd->cancel_button = generic_dialog_add_button(gd, GQ_ICON_CANCEL, _("Cancel"), gd->cancel_cb, TRUE);
460 gd->cancel_button = nullptr;
463 if (generic_dialog_get_alternative_button_order(gd->hbox))
465 g_signal_connect(G_OBJECT(gd->dialog), "show",
466 G_CALLBACK(generic_dialog_show_cb), gd);
469 gd->default_cb = nullptr;
472 GenericDialog *generic_dialog_new(const gchar *title,
474 GtkWidget *parent, gboolean auto_close,
475 void (*cancel_cb)(GenericDialog *, gpointer), gpointer data)
479 gd = g_new0(GenericDialog, 1);
480 generic_dialog_setup(gd, title, role,
481 parent, auto_close, cancel_cb, data);
485 *-----------------------------------------------------------------------------
486 * simple warning dialog
487 *-----------------------------------------------------------------------------
490 static void warning_dialog_ok_cb(GenericDialog *, gpointer)
495 GenericDialog *warning_dialog(const gchar *heading, const gchar *text,
496 const gchar *icon_name, GtkWidget *parent)
500 gd = generic_dialog_new(heading, "warning", parent, TRUE, nullptr, nullptr);
501 generic_dialog_add_button(gd, GQ_ICON_OK, "OK", warning_dialog_ok_cb, TRUE);
503 generic_dialog_add_message(gd, icon_name, heading, text, TRUE);
505 gtk_widget_show(gd->dialog);
511 *-----------------------------------------------------------------------------
512 * AppImage version update notification message with fade-out
513 *-----------------------------------------------------------------------------
515 * If the current version is not on GitHub, assume a newer one is available
516 * and show a fade-out message.
521 GThreadPool *thread_pool;
526 static gboolean appimage_notification_close_cb(gpointer data)
528 auto appimage_data = static_cast<AppImageData *>(data);
530 if (appimage_data->window && gtk_widget_get_opacity(appimage_data->window) != 0)
532 g_source_remove(appimage_data->id);
535 if (appimage_data->window)
537 g_object_unref(appimage_data->window);
540 g_thread_pool_free(appimage_data->thread_pool, TRUE, TRUE);
541 g_free(appimage_data);
543 return G_SOURCE_REMOVE;
546 static gboolean appimage_notification_fade_cb(gpointer data)
548 auto appimage_data = static_cast<AppImageData *>(data);
550 gtk_widget_set_opacity(appimage_data->window, (gtk_widget_get_opacity(appimage_data->window) - 0.02));
552 if (gtk_widget_get_opacity(appimage_data->window) == 0)
554 g_idle_add(appimage_notification_close_cb, data);
562 static gboolean user_close_cb(GtkWidget *, GdkEvent *, gpointer data)
564 auto appimage_data = static_cast<AppImageData *>(data);
566 g_idle_add(appimage_notification_close_cb, appimage_data);
571 static void show_notification_message(AppImageData *appimage_data)
575 builder = gtk_builder_new_from_resource(GQ_RESOURCE_PATH_UI "/appimage-notification.ui");
577 appimage_data->window = GTK_WIDGET(gtk_builder_get_object(builder, "appimage_notification"));
579 GdkRectangle workarea;
580 gdk_monitor_get_workarea(gdk_display_get_primary_monitor(gdk_display_get_default()),
582 gq_gtk_window_move(GTK_WINDOW(appimage_data->window), workarea.width * 0.8, workarea.height / 20);
583 g_signal_connect(appimage_data->window, "focus-in-event", G_CALLBACK(user_close_cb), appimage_data);
584 appimage_data->id = g_timeout_add(100, appimage_notification_fade_cb, appimage_data);
586 g_object_unref(builder);
587 gtk_widget_show(appimage_data->window);
590 void appimage_notification_func(gpointer data, gpointer)
592 auto appimage_data = static_cast<AppImageData *>(data);
593 gboolean internet_available = FALSE;
594 gchar **version_split;
596 GFileInfo *file_info;
597 GNetworkMonitor *net_mon;
598 GSocketConnectable *geeqie_github;
599 struct tm current_version_date;
600 int64_t file_github_date;
601 unsigned int year, month, day;
603 /* If this is a release version, do not check for updates */
604 if (g_strrstr(VERSION, "git"))
606 net_mon = g_network_monitor_get_default();
607 geeqie_github = g_network_address_parse_uri("https://github.com/", 80, nullptr);
611 internet_available = g_network_monitor_can_reach(net_mon, geeqie_github, nullptr, nullptr);
612 g_object_unref(geeqie_github);
615 if (internet_available)
617 /* VERSION looks like: 2.0.1+git20220116-c791cbee */
618 version_split = g_strsplit_set(VERSION, "+-", -1);
620 file_github = g_file_new_for_uri("https://github.com/BestImageViewer/geeqie/releases/download/continuous/Geeqie-latest-x86_64.AppImage");
621 file_info = g_file_query_info(file_github, "time::modified", G_FILE_QUERY_INFO_NONE, nullptr, nullptr);
623 file_github_date = g_file_info_get_attribute_uint64(file_info, "time::modified");
625 sscanf(version_split[1] + 3, "%4u%2u%2u", &year, &month, &day);
626 current_version_date.tm_year = year - 1900;
627 current_version_date.tm_mon = month - 1;
628 current_version_date.tm_mday = day;
629 current_version_date.tm_hour = 0;
630 current_version_date.tm_min = 0;
631 current_version_date.tm_sec = 0;
632 current_version_date.tm_isdst = 0;
634 if (file_github_date > mktime(¤t_version_date))
636 show_notification_message(appimage_data);
639 g_object_unref(file_github);
640 g_object_unref(file_info);
641 g_strfreev(version_split);
646 void appimage_notification()
648 AppImageData *appimage_data;
650 appimage_data = g_new0(AppImageData, 1);
652 appimage_data->thread_pool = g_thread_pool_new(appimage_notification_func, appimage_data, 1, FALSE, nullptr);
653 g_thread_pool_push(appimage_data->thread_pool, appimage_data, nullptr);
657 *-----------------------------------------------------------------------------
658 * generic file ops dialog routines
659 *-----------------------------------------------------------------------------
662 void file_dialog_close(FileDialog *fdlg)
664 file_data_unref(fdlg->source_fd);
665 g_free(fdlg->dest_path);
666 if (fdlg->source_list) filelist_free(fdlg->source_list);
668 generic_dialog_close(GENERIC_DIALOG(fdlg));
671 FileDialog *file_dialog_new(const gchar *title,
674 void (*cancel_cb)(FileDialog *, gpointer), gpointer data)
676 FileDialog *fdlg = nullptr;
678 fdlg = g_new0(FileDialog, 1);
680 generic_dialog_setup(GENERIC_DIALOG(fdlg), title,
682 reinterpret_cast<void(*)(GenericDialog *, gpointer)>(cancel_cb), data);
687 GtkWidget *file_dialog_add_button(FileDialog *fdlg, const gchar *stock_id, const gchar *text,
688 void (*func_cb)(FileDialog *, gpointer), gboolean is_default)
690 return generic_dialog_add_button(GENERIC_DIALOG(fdlg), stock_id, text,
691 reinterpret_cast<void(*)(GenericDialog *, gpointer)>(func_cb), is_default);
694 static void file_dialog_entry_cb(GtkWidget *, gpointer data)
696 auto fdlg = static_cast<FileDialog *>(data);
697 g_free(fdlg->dest_path);
698 fdlg->dest_path = remove_trailing_slash(gq_gtk_entry_get_text(GTK_ENTRY(fdlg->entry)));
701 static void file_dialog_entry_enter_cb(const gchar *, gpointer data)
703 auto gd = static_cast<GenericDialog *>(data);
705 file_dialog_entry_cb(nullptr, data);
707 if (gd->default_cb) gd->default_cb(gd, gd->data);
710 void file_dialog_add_path_widgets(FileDialog *fdlg, const gchar *default_path, const gchar *path,
711 const gchar *history_key, const gchar *filter, const gchar *filter_desc)
716 if (fdlg->entry) return;
718 tabcomp = tab_completion_new_with_history(&fdlg->entry, nullptr,
719 history_key, -1, file_dialog_entry_enter_cb, fdlg);
720 gq_gtk_box_pack_end(GTK_BOX(GENERIC_DIALOG(fdlg)->vbox), tabcomp, FALSE, FALSE, 0);
721 generic_dialog_attach_default(GENERIC_DIALOG(fdlg), fdlg->entry);
722 gtk_widget_show(tabcomp);
724 if (path && path[0] == G_DIR_SEPARATOR)
726 fdlg->dest_path = g_strdup(path);
732 base = tab_completion_set_to_last_history(fdlg->entry);
734 if (!base) base = default_path;
735 if (!base) base = homedir();
739 fdlg->dest_path = g_build_filename(base, path, NULL);
743 fdlg->dest_path = g_strdup(base);
747 list = path_selection_new_with_files(fdlg->entry, fdlg->dest_path, filter, filter_desc);
748 path_selection_add_select_func(fdlg->entry, file_dialog_entry_enter_cb, fdlg);
749 gq_gtk_box_pack_end(GTK_BOX(GENERIC_DIALOG(fdlg)->vbox), list, TRUE, TRUE, 0);
750 gtk_widget_show(list);
752 gtk_widget_grab_focus(fdlg->entry);
755 gq_gtk_entry_set_text(GTK_ENTRY(fdlg->entry), fdlg->dest_path);
756 gtk_editable_set_position(GTK_EDITABLE(fdlg->entry), strlen(fdlg->dest_path));
759 g_signal_connect(G_OBJECT(fdlg->entry), "changed",
760 G_CALLBACK(file_dialog_entry_cb), fdlg);
763 #pragma GCC diagnostic push
764 #pragma GCC diagnostic ignored "-Wunused-function"
765 void file_dialog_add_filter_unused(FileDialog *fdlg, const gchar *filter, const gchar *filter_desc, gboolean set)
767 if (!fdlg->entry) return;
768 path_selection_add_filter(fdlg->entry, filter, filter_desc, set);
771 void file_dialog_clear_filter_unused(FileDialog *fdlg)
773 if (!fdlg->entry) return;
774 path_selection_clear_filter(fdlg->entry);
776 #pragma GCC diagnostic pop
778 void file_dialog_sync_history(FileDialog *fdlg, gboolean dir_only)
780 if (!fdlg->dest_path) return;
783 (dir_only && isdir(fdlg->dest_path)) )
785 tab_completion_append_to_history(fdlg->entry, fdlg->dest_path);
789 gchar *buf = remove_level_from_path(fdlg->dest_path);
790 tab_completion_append_to_history(fdlg->entry, buf);
794 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */