065ce9e7d1af7b21ace82050aae8c2e214371801
[geeqie.git] / src / ui-utildlg.cc
1 /*
2  * Copyright (C) 2004 John Ellis
3  * Copyright (C) 2008 - 2016 The Geeqie Team
4  *
5  * Author: John Ellis
6  *
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.
11  *
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.
16  *
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.
20  */
21
22 #include "ui-utildlg.h"
23
24 #include <cstdint>
25 #include <cstdio>
26 #include <cstring>
27 #include <ctime>
28
29 #include <gdk/gdk.h>
30 #include <gio/gio.h>
31 #include <glib-object.h>
32
33 #include <config.h>
34
35 #include "compat.h"
36 #include "debug.h"
37 #include "filedata.h"
38 #include "intl.h"
39 #include "main-defines.h"
40 #include "misc.h"
41 #include "options.h"
42 #include "rcfile.h"
43 #include "ui-fileops.h"
44 #include "ui-misc.h"
45 #include "ui-pathsel.h"
46 #include "ui-tabcomp.h"
47 #include "window.h"
48
49 /*
50  *-----------------------------------------------------------------------------
51  * generic dialog
52  *-----------------------------------------------------------------------------
53  */
54
55 struct DialogWindow
56 {
57         gint x;
58         gint y;
59         gint w;
60         gint h;
61         gchar *title;
62         gchar *role;
63 };
64
65 static GList *dialog_windows = nullptr;
66
67 static void generic_dialog_save_window(const gchar *title, const gchar *role, gint x, gint y, gint h, gint w)
68 {
69         GList *work;
70
71         work = g_list_first(dialog_windows);
72         while (work)
73                 {
74                 auto dw = static_cast<DialogWindow *>(work->data);
75                 if (g_strcmp0(dw->title ,title) == 0 && g_strcmp0(dw->role, role) == 0)
76                         {
77                         dw->x = x;
78                         dw->y = y;
79                         dw->w = w;
80                         dw->h = h;
81                         return;
82                         }
83                 work = work->next;
84                 }
85
86         auto dw = g_new0(DialogWindow, 1);
87         dw->title = g_strdup(title);
88         dw->role = g_strdup(role);
89         dw->x = x;
90         dw->y = y;
91         dw->w = w;
92         dw->h = h;
93
94         dialog_windows = g_list_append(dialog_windows, dw);
95 }
96
97 static gboolean generic_dialog_find_window(const gchar *title, const gchar *role, gint *x, gint *y, gint *h, gint *w)
98 {
99         GList *work;
100
101         work = g_list_first(dialog_windows);
102         while (work)
103                 {
104                 auto dw = static_cast<DialogWindow *>(work->data);
105
106                 if (g_strcmp0(dw->title,title) == 0 && g_strcmp0(dw->role, role) == 0)
107                         {
108                         *x = dw->x;
109                         *y = dw->y;
110                         *w = dw->w;
111                         *h = dw->h;
112                         return TRUE;
113                         }
114                 work = work->next;
115                 }
116         return FALSE;
117 }
118
119 void generic_dialog_close(GenericDialog *gd)
120 {
121         gchar *ident_string;
122         gchar *full_title;
123         gchar *actual_title;
124         gint x;
125         gint y;
126         gint h;
127         gint w;
128
129         gdk_window_get_root_origin(gtk_widget_get_window (gd->dialog), &x, &y);
130         w = gdk_window_get_width(gtk_widget_get_window (gd->dialog));
131         h = gdk_window_get_height(gtk_widget_get_window (gd->dialog));
132
133         /* The window title is modified in window.cc: window_new()
134          * by appending the string " - Geeqie"
135          */
136         ident_string = g_strconcat(" - ", GQ_APPNAME, NULL);
137         full_title = g_strdup(gtk_window_get_title(GTK_WINDOW(gd->dialog)));
138         actual_title = strndup(full_title, g_strrstr(full_title, ident_string) - full_title);
139
140         generic_dialog_save_window(actual_title, gtk_window_get_role(GTK_WINDOW(gd->dialog)), x, y, w, h);
141
142         gq_gtk_widget_destroy(gd->dialog);
143         g_free(gd);
144         g_free(ident_string);
145         g_free(full_title);
146         g_free(actual_title);
147 }
148
149 static void generic_dialog_click_cb(GtkWidget *widget, gpointer data)
150 {
151         auto gd = static_cast<GenericDialog *>(data);
152         void (*func)(GenericDialog *, gpointer);
153         gboolean auto_close;
154
155         func = reinterpret_cast<void(*)(GenericDialog *, gpointer)>(g_object_get_data(G_OBJECT(widget), "dialog_function"));
156         auto_close = gd->auto_close;
157
158         if (func) func(gd, gd->data);
159         if (auto_close) generic_dialog_close(gd);
160 }
161
162 static gboolean generic_dialog_default_key_press_cb(GtkWidget *widget, GdkEventKey *event, gpointer data)
163 {
164         auto gd = static_cast<GenericDialog *>(data);
165
166         if (event->keyval == GDK_KEY_Return && gtk_widget_has_focus(widget)
167             && gd->default_cb)
168                 {
169                 gboolean auto_close;
170
171                 auto_close = gd->auto_close;
172                 gd->default_cb(gd, gd->data);
173                 if (auto_close) generic_dialog_close(gd);
174
175                 return TRUE;
176                 }
177         return FALSE;
178 }
179
180 void generic_dialog_attach_default(GenericDialog *gd, GtkWidget *widget)
181 {
182         if (!gd || !widget) return;
183         g_signal_connect(G_OBJECT(widget), "key_press_event",
184                          G_CALLBACK(generic_dialog_default_key_press_cb), gd);
185 }
186
187 static gboolean generic_dialog_key_press_cb(GtkWidget *widget, GdkEventKey *event, gpointer data)
188 {
189         auto gd = static_cast<GenericDialog *>(data);
190         gboolean auto_close = gd->auto_close;
191
192         if (event->keyval == GDK_KEY_Escape)
193                 {
194                 if (gd->cancel_cb)
195                         {
196                         gd->cancel_cb(gd, gd->data);
197                         if (auto_close) generic_dialog_close(gd);
198                         }
199                 else
200                         {
201                         if (auto_close) generic_dialog_click_cb(widget, data);
202                         }
203                 return TRUE;
204                 }
205         return FALSE;
206 }
207
208 static gboolean generic_dialog_delete_cb(GtkWidget *, GdkEventAny *, gpointer data)
209 {
210         auto gd = static_cast<GenericDialog *>(data);
211         gboolean auto_close;
212
213         auto_close = gd->auto_close;
214
215         if (gd->cancel_cb) gd->cancel_cb(gd, gd->data);
216         if (auto_close) generic_dialog_close(gd);
217
218         return TRUE;
219 }
220
221 static void generic_dialog_show_cb(GtkWidget *widget, gpointer data)
222 {
223         auto gd = static_cast<GenericDialog *>(data);
224         if (gd->cancel_button)
225                 {
226                 gtk_box_reorder_child(GTK_BOX(gd->hbox), gd->cancel_button, -1);
227                 }
228
229         g_signal_handlers_disconnect_by_func(G_OBJECT(widget), (gpointer)(generic_dialog_show_cb), gd);
230 }
231
232 gboolean generic_dialog_get_alternative_button_order(GtkWidget *widget)
233 {
234         GtkSettings *settings;
235         GObjectClass *klass;
236         gboolean alternative_order = FALSE;
237
238         settings = gtk_settings_get_for_screen(gtk_widget_get_screen(widget));
239         klass = G_OBJECT_CLASS(GTK_SETTINGS_GET_CLASS(settings));
240         if (g_object_class_find_property(klass, "gtk-alternative-button-order"))
241                 {
242                 g_object_get(settings, "gtk-alternative-button-order", &alternative_order, NULL);
243                 }
244
245         return alternative_order;
246 }
247
248 GtkWidget *generic_dialog_add_button(GenericDialog *gd, const gchar *icon_name, const gchar *text,
249                                      void (*func_cb)(GenericDialog *, gpointer), gboolean is_default)
250 {
251         GtkWidget *button;
252         gboolean alternative_order;
253
254         button = pref_button_new(nullptr, icon_name, text,
255                                  G_CALLBACK(generic_dialog_click_cb), gd);
256
257         gtk_widget_set_can_default(button, TRUE);
258         g_object_set_data(G_OBJECT(button), "dialog_function", reinterpret_cast<void *>(func_cb));
259
260         gq_gtk_container_add(GTK_WIDGET(gd->hbox), button);
261
262         alternative_order = generic_dialog_get_alternative_button_order(gd->hbox);
263
264         if (is_default)
265                 {
266                 gtk_widget_grab_default(button);
267                 gtk_widget_grab_focus(button);
268                 gd->default_cb = func_cb;
269
270                 if (!alternative_order) gtk_box_reorder_child(GTK_BOX(gd->hbox), button, -1);
271                 }
272         else
273                 {
274                 if (!alternative_order) gtk_box_reorder_child(GTK_BOX(gd->hbox), button, 0);
275                 }
276
277         gtk_widget_show(button);
278
279         return button;
280 }
281
282 /**
283  * @brief
284  * @param gd
285  * @param icon_stock_id
286  * @param heading
287  * @param text
288  * @param expand Used as the "expand" and "fill" parameters in the eventual call to gq_gtk_box_pack_start()
289  * @returns
290  *
291  *
292  */
293 GtkWidget *generic_dialog_add_message(GenericDialog *gd, const gchar *icon_name,
294                                       const gchar *heading, const gchar *text, gboolean expand)
295 {
296         GtkWidget *hbox;
297         GtkWidget *vbox;
298         GtkWidget *label;
299
300         hbox = pref_box_new(gd->vbox, expand, GTK_ORIENTATION_HORIZONTAL, PREF_PAD_SPACE);
301         if (icon_name)
302                 {
303                 GtkWidget *image;
304
305                 image = gtk_image_new_from_icon_name(icon_name, GTK_ICON_SIZE_DIALOG);
306                 gtk_widget_set_halign(GTK_WIDGET(image), GTK_ALIGN_CENTER);
307                 gtk_widget_set_valign(GTK_WIDGET(image), GTK_ALIGN_START);
308                 gq_gtk_box_pack_start(GTK_BOX(hbox), image, FALSE, FALSE, 0);
309                 gtk_widget_show(image);
310                 }
311
312         vbox = pref_box_new(hbox, TRUE, GTK_ORIENTATION_VERTICAL, PREF_PAD_SPACE);
313         if (heading)
314                 {
315                 label = pref_label_new(vbox, heading);
316                 pref_label_bold(label, TRUE, TRUE);
317                 gtk_label_set_xalign(GTK_LABEL(label), 0.0);
318                 gtk_label_set_yalign(GTK_LABEL(label), 0.5);
319                 }
320         if (text)
321                 {
322                 label = pref_label_new(vbox, text);
323                 gtk_label_set_xalign(GTK_LABEL(label), 0.0);
324                 gtk_label_set_yalign(GTK_LABEL(label), 0.5);
325                 gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
326                 }
327
328         return vbox;
329 }
330
331 void generic_dialog_windows_load_config(const gchar **attribute_names, const gchar **attribute_values)
332 {
333         auto dw =  g_new0(DialogWindow, 1);
334         gchar *title = nullptr;
335         gchar *role = nullptr;
336         gint x = 0;
337         gint y = 0;
338         gint w = 0;
339         gint h = 0;
340
341         while (*attribute_names)
342                 {
343                 const gchar *option = *attribute_names++;
344                 const gchar *value = *attribute_values++;
345                 if (READ_CHAR_FULL("title", title)) continue;
346                 if (READ_CHAR_FULL("role", role)) continue;
347                 if (READ_INT_FULL("x", x)) continue;
348                 if (READ_INT_FULL("y", y)) continue;
349                 if (READ_INT_FULL("w", w)) continue;
350                 if (READ_INT_FULL("h", h)) continue;
351
352                 log_printf("unknown attribute %s = %s\n", option, value);
353                 }
354
355         if (title && title[0] != 0)
356                 {
357                 dw->title = g_strdup(title);
358                 dw->role = g_strdup(role);
359                 dw->x = x;
360                 dw->y = y;
361                 dw->w = w;
362                 dw->h = h;
363
364                 dialog_windows = g_list_append(dialog_windows, dw);
365                 }
366 }
367
368 void generic_dialog_windows_write_config(GString *outstr, gint indent)
369 {
370         GList *work;
371
372         if (options->save_dialog_window_positions && dialog_windows)
373                 {
374                 WRITE_NL(); WRITE_STRING("<%s>", "dialogs");
375                 indent++;
376
377                 work = g_list_first(dialog_windows);
378                 while (work)
379                         {
380                         auto dw = static_cast<DialogWindow *>(work->data);
381                         WRITE_NL(); WRITE_STRING("<window ");
382                         write_char_option(outstr, indent + 1, "title", dw->title);
383                         write_char_option(outstr, indent + 1, "role", dw->role);
384                         WRITE_INT(*dw, x);
385                         WRITE_INT(*dw, y);
386                         WRITE_INT(*dw, w);
387                         WRITE_INT(*dw, h);
388                         WRITE_STRING("/>");
389                         work = work->next;
390                         }
391                 indent--;
392                 WRITE_NL(); WRITE_STRING("</%s>", "dialogs");
393                 }
394 }
395
396 static void generic_dialog_setup(GenericDialog *gd,
397                                  const gchar *title,
398                                  const gchar *role,
399                                  GtkWidget *parent, gboolean auto_close,
400                                  void (*cancel_cb)(GenericDialog *, gpointer), gpointer data)
401 {
402         GtkWidget *vbox;
403         gint x;
404         gint y;
405         gint w;
406         gint h;
407         GtkWidget *scrolled;
408
409         gd->auto_close = auto_close;
410         gd->data = data;
411         gd->cancel_cb = cancel_cb;
412
413         gd->dialog = window_new(role, nullptr, nullptr, title);
414         DEBUG_NAME(gd->dialog);
415         gtk_window_set_type_hint(GTK_WINDOW(gd->dialog), GDK_WINDOW_TYPE_HINT_DIALOG);
416
417         if (options->save_dialog_window_positions)
418                 {
419                 if (generic_dialog_find_window(title, role, &x, &y, &w, &h))
420                         {
421                         gtk_window_set_default_size(GTK_WINDOW(gd->dialog), w, h);
422                         gq_gtk_window_move(GTK_WINDOW(gd->dialog), x, y);
423                         }
424                 }
425
426         if (parent)
427                 {
428                 GtkWindow *window = nullptr;
429
430                 if (GTK_IS_WINDOW(parent))
431                         {
432                         window = GTK_WINDOW(parent);
433                         }
434                 else
435                         {
436                         GtkWidget *top;
437
438                         top = gtk_widget_get_toplevel(parent);
439                         if (GTK_IS_WINDOW(top) && gtk_widget_is_toplevel(top)) window = GTK_WINDOW(top);
440                         }
441
442                 if (window) gtk_window_set_transient_for(GTK_WINDOW(gd->dialog), window);
443                 }
444
445         g_signal_connect(G_OBJECT(gd->dialog), "delete_event",
446                          G_CALLBACK(generic_dialog_delete_cb), gd);
447         g_signal_connect(G_OBJECT(gd->dialog), "key_press_event",
448                          G_CALLBACK(generic_dialog_key_press_cb), gd);
449
450         gtk_window_set_resizable(GTK_WINDOW(gd->dialog), TRUE);
451         gtk_container_set_border_width(GTK_CONTAINER(gd->dialog), PREF_PAD_BORDER);
452
453         scrolled = gq_gtk_scrolled_window_new(nullptr, nullptr);
454         gtk_scrolled_window_set_propagate_natural_height(GTK_SCROLLED_WINDOW(scrolled), TRUE);
455         gtk_scrolled_window_set_propagate_natural_width(GTK_SCROLLED_WINDOW(scrolled), TRUE);
456         vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, PREF_PAD_BUTTON_SPACE);
457         gq_gtk_container_add(GTK_WIDGET(scrolled), vbox);
458         gq_gtk_container_add(GTK_WIDGET(gd->dialog), scrolled);
459         gtk_widget_show(scrolled);
460
461         gtk_widget_show(vbox);
462
463         gd->vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, PREF_PAD_GAP);
464         gq_gtk_box_pack_start(GTK_BOX(vbox), gd->vbox, TRUE, TRUE, 0);
465         gtk_widget_show(gd->vbox);
466
467         gd->hbox = gtk_button_box_new(GTK_ORIENTATION_HORIZONTAL);
468         gtk_button_box_set_layout(GTK_BUTTON_BOX(gd->hbox), GTK_BUTTONBOX_END);
469         gtk_box_set_spacing(GTK_BOX(gd->hbox), PREF_PAD_BUTTON_GAP);
470         gq_gtk_box_pack_start(GTK_BOX(vbox), gd->hbox, FALSE, FALSE, 0);
471         gtk_widget_show(gd->hbox);
472
473         if (gd->cancel_cb)
474                 {
475                 gd->cancel_button = generic_dialog_add_button(gd, GQ_ICON_CANCEL, _("Cancel"), gd->cancel_cb, TRUE);
476                 }
477         else
478                 {
479                 gd->cancel_button = nullptr;
480                 }
481
482         if (generic_dialog_get_alternative_button_order(gd->hbox))
483                 {
484                 g_signal_connect(G_OBJECT(gd->dialog), "show",
485                                  G_CALLBACK(generic_dialog_show_cb), gd);
486                 }
487
488         gd->default_cb = nullptr;
489 }
490
491 GenericDialog *generic_dialog_new(const gchar *title,
492                                   const gchar *role,
493                                   GtkWidget *parent, gboolean auto_close,
494                                   void (*cancel_cb)(GenericDialog *, gpointer), gpointer data)
495 {
496         GenericDialog *gd;
497
498         gd = g_new0(GenericDialog, 1);
499         generic_dialog_setup(gd, title, role,
500                              parent, auto_close, cancel_cb, data);
501         return gd;
502 }
503 /*
504  *-----------------------------------------------------------------------------
505  * simple warning dialog
506  *-----------------------------------------------------------------------------
507  */
508
509 static void warning_dialog_ok_cb(GenericDialog *, gpointer)
510 {
511         /* no op */
512 }
513
514 GenericDialog *warning_dialog(const gchar *heading, const gchar *text,
515                               const gchar *icon_name, GtkWidget *parent)
516 {
517         GenericDialog *gd;
518
519         gd = generic_dialog_new(heading, "warning", parent, TRUE, nullptr, nullptr);
520         generic_dialog_add_button(gd, GQ_ICON_OK, "OK", warning_dialog_ok_cb, TRUE);
521
522         generic_dialog_add_message(gd, icon_name, heading, text, TRUE);
523
524         gtk_widget_show(gd->dialog);
525
526         return gd;
527 }
528
529 /*
530  *-----------------------------------------------------------------------------
531  * AppImage version update notification message with fade-out
532  *-----------------------------------------------------------------------------
533  *
534  * If the current version is not on GitHub, assume a newer one is available
535  * and show a fade-out message.
536  */
537
538 struct AppImageData
539 {
540         GThreadPool *thread_pool;
541         GtkWidget *window;
542         guint id;
543 };
544
545 static gboolean appimage_notification_close_cb(gpointer data)
546 {
547         auto appimage_data = static_cast<AppImageData *>(data);
548
549         if (appimage_data->window && gtk_widget_get_opacity(appimage_data->window) != 0)
550                 {
551                 g_source_remove(appimage_data->id);
552                 }
553
554         if (appimage_data->window)
555                 {
556                 gq_gtk_widget_destroy(appimage_data->window);
557                 }
558
559         g_thread_pool_free(appimage_data->thread_pool, TRUE, TRUE);
560         g_free(appimage_data);
561
562         return G_SOURCE_REMOVE;
563 }
564
565 static gboolean appimage_notification_fade_cb(gpointer data)
566 {
567         auto appimage_data = static_cast<AppImageData *>(data);
568
569         gtk_widget_set_opacity(appimage_data->window, (gtk_widget_get_opacity(appimage_data->window) - 0.02));
570
571         if (gtk_widget_get_opacity(appimage_data->window) == 0)
572                 {
573                 g_idle_add(appimage_notification_close_cb, data);
574
575                 return FALSE;
576                 }
577
578         return TRUE;
579 }
580
581 static gboolean user_close_cb(GtkWidget *, GdkEvent *, gpointer data)
582 {
583         auto appimage_data = static_cast<AppImageData *>(data);
584
585         g_idle_add(appimage_notification_close_cb, appimage_data);
586
587         return FALSE;
588 }
589
590 static void show_notification_message(AppImageData *appimage_data)
591 {
592         GtkBuilder *builder;
593
594         builder = gtk_builder_new_from_resource(GQ_RESOURCE_PATH_UI "/appimage-notification.ui");
595
596         appimage_data->window = GTK_WIDGET(gtk_builder_get_object(builder, "appimage_notification"));
597
598         GdkRectangle workarea;
599         gdk_monitor_get_workarea(gdk_display_get_primary_monitor(gdk_display_get_default()),
600                              &workarea);
601         gq_gtk_window_move(GTK_WINDOW(appimage_data->window), workarea.width * 0.8, workarea.height / 20);
602         g_signal_connect(appimage_data->window, "focus-in-event", G_CALLBACK(user_close_cb), appimage_data);
603         appimage_data->id = g_timeout_add(100, appimage_notification_fade_cb, appimage_data);
604
605         g_object_unref(builder);
606         gtk_widget_show(appimage_data->window);
607 }
608
609 void appimage_notification_func(gpointer data, gpointer)
610 {
611         auto appimage_data = static_cast<AppImageData *>(data);
612         gboolean internet_available = FALSE;
613         gchar **version_split;
614         GFile *file_github;
615         GFileInfo *file_info;
616         GNetworkMonitor *net_mon;
617         GSocketConnectable *geeqie_github;
618         struct tm current_version_date;
619         int64_t file_github_date;
620         unsigned int year;
621         unsigned int month;
622         unsigned int day;
623
624         /* If this is a release version, do not check for updates */
625         if (g_strrstr(VERSION, "git"))
626                 {
627                 net_mon = g_network_monitor_get_default();
628                 geeqie_github = g_network_address_parse_uri("https://github.com/", 80, nullptr);
629
630                 if (geeqie_github)
631                         {
632                         internet_available = g_network_monitor_can_reach(net_mon, geeqie_github, nullptr, nullptr);
633                         g_object_unref(geeqie_github);
634                         }
635
636                 if (internet_available)
637                         {
638                         /* VERSION looks like: 2.0.1+git20220116-c791cbee */
639                         version_split = g_strsplit_set(VERSION, "+-", -1);
640
641                         file_github = g_file_new_for_uri("https://github.com/BestImageViewer/geeqie/releases/download/continuous/Geeqie-latest-x86_64.AppImage");
642                         file_info = g_file_query_info(file_github, "time::modified", G_FILE_QUERY_INFO_NONE, nullptr, nullptr);
643
644                         file_github_date = g_file_info_get_attribute_uint64(file_info, "time::modified");
645
646                         sscanf(version_split[1] + 3, "%4u%2u%2u", &year, &month, &day);
647                         current_version_date.tm_year  = year - 1900;
648                         current_version_date.tm_mon   = month - 1;
649                         current_version_date.tm_mday  = day;
650                         current_version_date.tm_hour  = 0;
651                         current_version_date.tm_min   = 0;
652                         current_version_date.tm_sec   = 0;
653                         current_version_date.tm_isdst = 0;
654
655                         if (file_github_date > mktime(&current_version_date))
656                                 {
657                                 show_notification_message(appimage_data);
658                                 }
659
660                         g_object_unref(file_github);
661                         g_object_unref(file_info);
662                         g_strfreev(version_split);
663                         }
664                 }
665 }
666
667 void appimage_notification()
668 {
669         AppImageData *appimage_data;
670
671         appimage_data = g_new0(AppImageData, 1);
672
673         appimage_data->thread_pool = g_thread_pool_new(appimage_notification_func, appimage_data, 1, FALSE, nullptr);
674         g_thread_pool_push(appimage_data->thread_pool, appimage_data, nullptr);
675 }
676
677 /*
678  *-----------------------------------------------------------------------------
679  * generic file ops dialog routines
680  *-----------------------------------------------------------------------------
681  */
682
683 void file_dialog_close(FileDialog *fdlg)
684 {
685         file_data_unref(fdlg->source_fd);
686         g_free(fdlg->dest_path);
687         if (fdlg->source_list) filelist_free(fdlg->source_list);
688
689         generic_dialog_close(GENERIC_DIALOG(fdlg));
690 }
691
692 FileDialog *file_dialog_new(const gchar *title,
693                             const gchar *role,
694                             GtkWidget *parent,
695                             void (*cancel_cb)(FileDialog *, gpointer), gpointer data)
696 {
697         FileDialog *fdlg = nullptr;
698
699         fdlg = g_new0(FileDialog, 1);
700
701         generic_dialog_setup(GENERIC_DIALOG(fdlg), title,
702                              role, parent, FALSE,
703                              reinterpret_cast<void(*)(GenericDialog *, gpointer)>(cancel_cb), data);
704
705         return fdlg;
706 }
707
708 GtkWidget *file_dialog_add_button(FileDialog *fdlg, const gchar *stock_id, const gchar *text,
709                                   void (*func_cb)(FileDialog *, gpointer), gboolean is_default)
710 {
711         return generic_dialog_add_button(GENERIC_DIALOG(fdlg), stock_id, text,
712                                          reinterpret_cast<void(*)(GenericDialog *, gpointer)>(func_cb), is_default);
713 }
714
715 static void file_dialog_entry_cb(GtkWidget *, gpointer data)
716 {
717         auto fdlg = static_cast<FileDialog *>(data);
718         g_free(fdlg->dest_path);
719         fdlg->dest_path = remove_trailing_slash(gq_gtk_entry_get_text(GTK_ENTRY(fdlg->entry)));
720 }
721
722 static void file_dialog_entry_enter_cb(const gchar *, gpointer data)
723 {
724         auto gd = static_cast<GenericDialog *>(data);
725
726         file_dialog_entry_cb(nullptr, data);
727
728         if (gd->default_cb) gd->default_cb(gd, gd->data);
729 }
730
731 void file_dialog_add_path_widgets(FileDialog *fdlg, const gchar *default_path, const gchar *path,
732                                   const gchar *history_key, const gchar *filter, const gchar *filter_desc)
733 {
734         GtkWidget *tabcomp;
735         GtkWidget *list;
736
737         if (fdlg->entry) return;
738
739         tabcomp = tab_completion_new_with_history(&fdlg->entry, nullptr,
740                   history_key, -1, file_dialog_entry_enter_cb, fdlg);
741         gq_gtk_box_pack_end(GTK_BOX(GENERIC_DIALOG(fdlg)->vbox), tabcomp, FALSE, FALSE, 0);
742         generic_dialog_attach_default(GENERIC_DIALOG(fdlg), fdlg->entry);
743         gtk_widget_show(tabcomp);
744
745         if (path && path[0] == G_DIR_SEPARATOR)
746                 {
747                 fdlg->dest_path = g_strdup(path);
748                 }
749         else
750                 {
751                 const gchar *base;
752
753                 base = tab_completion_set_to_last_history(fdlg->entry);
754
755                 if (!base) base = default_path;
756                 if (!base) base = homedir();
757
758                 if (path)
759                         {
760                         fdlg->dest_path = g_build_filename(base, path, NULL);
761                         }
762                 else
763                         {
764                         fdlg->dest_path = g_strdup(base);
765                         }
766                 }
767
768         list = path_selection_new_with_files(fdlg->entry, fdlg->dest_path, filter, filter_desc);
769         path_selection_add_select_func(fdlg->entry, file_dialog_entry_enter_cb, fdlg);
770         gq_gtk_box_pack_end(GTK_BOX(GENERIC_DIALOG(fdlg)->vbox), list, TRUE, TRUE, 0);
771         gtk_widget_show(list);
772
773         gtk_widget_grab_focus(fdlg->entry);
774         if (fdlg->dest_path)
775                 {
776                 gq_gtk_entry_set_text(GTK_ENTRY(fdlg->entry), fdlg->dest_path);
777                 gtk_editable_set_position(GTK_EDITABLE(fdlg->entry), strlen(fdlg->dest_path));
778                 }
779
780         g_signal_connect(G_OBJECT(fdlg->entry), "changed",
781                          G_CALLBACK(file_dialog_entry_cb), fdlg);
782 }
783
784 #pragma GCC diagnostic push
785 #pragma GCC diagnostic ignored "-Wunused-function"
786 void file_dialog_add_filter_unused(FileDialog *fdlg, const gchar *filter, const gchar *filter_desc, gboolean set)
787 {
788         if (!fdlg->entry) return;
789         path_selection_add_filter(fdlg->entry, filter, filter_desc, set);
790 }
791
792 void file_dialog_clear_filter_unused(FileDialog *fdlg)
793 {
794         if (!fdlg->entry) return;
795         path_selection_clear_filter(fdlg->entry);
796 }
797 #pragma GCC diagnostic pop
798
799 void file_dialog_sync_history(FileDialog *fdlg, gboolean dir_only)
800 {
801         if (!fdlg->dest_path) return;
802
803         if (!dir_only ||
804             (dir_only && isdir(fdlg->dest_path)) )
805                 {
806                 tab_completion_append_to_history(fdlg->entry, fdlg->dest_path);
807                 }
808         else
809                 {
810                 gchar *buf = remove_level_from_path(fdlg->dest_path);
811                 tab_completion_append_to_history(fdlg->entry, buf);
812                 g_free(buf);
813                 }
814 }
815 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */