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