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