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