19a51b6f3ad2f2f520e65badac5ab13e7721b1de
[geeqie.git] / src / utilops.cc
1 /*
2  * Copyright (C) 2006 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 "main.h"
23 #include "utilops.h"
24
25 #include "cache.h"
26 #include "editors.h"
27 #include "exif.h"
28 #include "filedata.h"
29 #include "filefilter.h"
30 #include "image.h"
31 #include "metadata.h"
32 #include "misc.h"
33 #include "thumb-standard.h"
34 #include "trash.h"
35 #include "ui-bookmark.h"
36 #include "ui-fileops.h"
37 #include "ui-misc.h"
38
39 namespace
40 {
41
42 struct PixmapErrors
43 {
44         GdkPixbuf *error;
45         GdkPixbuf *warning;
46         GdkPixbuf *apply;
47 };
48
49 GdkPixbuf *file_util_get_error_icon(FileData *fd, GList *list, GtkWidget *widget)
50 {
51         static PixmapErrors pe = [widget]() -> PixmapErrors
52         {
53                 GtkIconTheme *icon_theme = gtk_icon_theme_get_default();
54
55                 gint size;
56                 if (!gtk_icon_size_lookup(GTK_ICON_SIZE_MENU, &size, &size))
57                         {
58                         size = 16;
59                         }
60
61                 GdkPixbuf *pb_error = gq_gtk_icon_theme_load_icon_copy(icon_theme, GQ_ICON_DIALOG_ERROR, size, GTK_ICON_LOOKUP_USE_BUILTIN);
62                 GdkPixbuf *pb_warning = gq_gtk_icon_theme_load_icon_copy(icon_theme, GQ_ICON_DIALOG_WARNING, size, GTK_ICON_LOOKUP_USE_BUILTIN);
63                 GdkPixbuf *pb_apply = gq_gtk_icon_theme_load_icon_copy(icon_theme, GQ_ICON_APPLY, size, GTK_ICON_LOOKUP_USE_BUILTIN);
64                 return {pb_error, pb_warning, pb_apply};
65         }();
66
67         gint error = file_data_sc_verify_ci(fd, list);
68
69         if (error & CHANGE_ERROR_MASK)
70                 {
71                 return pe.error;
72                 }
73
74         if (error)
75                 {
76                 return pe.warning;
77                 }
78
79         return pe.apply;
80 }
81
82 }
83
84 enum {
85         DIALOG_WIDTH = 750
86 };
87
88 enum ClipboardDestination {
89         CLIPBOARD_TEXT_PLAIN    = 0,
90         CLIPBOARD_TEXT_URI_LIST = 1,
91         CLIPBOARD_X_SPECIAL_GNOME_COPIED_FILES  = 2,
92         CLIPBOARD_UTF8_STRING   = 3
93 };
94
95 static GtkTargetEntry target_types[] =
96 {
97         {const_cast<gchar *>("text/plain"), 0, CLIPBOARD_TEXT_PLAIN},
98         {const_cast<gchar *>("text/uri-list"), 0, CLIPBOARD_TEXT_URI_LIST},
99         {const_cast<gchar *>("x-special/gnome-copied-files"), 0, CLIPBOARD_X_SPECIAL_GNOME_COPIED_FILES},
100         {const_cast<gchar *>("UTF8_STRING"), 0, CLIPBOARD_UTF8_STRING},
101 };
102 static gint target_types_n = 4;
103
104 struct ClipboardData
105 {
106         GList *path_list; /**< g_strdup(fd->path) */
107         gboolean quoted;
108 };
109
110 /*
111  *--------------------------------------------------------------------------
112  * Adds 1 or 2 images (if 2, side by side) to a GenericDialog
113  *--------------------------------------------------------------------------
114  */
115
116 enum {
117         DIALOG_DEF_IMAGE_DIM_X = 150,
118         DIALOG_DEF_IMAGE_DIM_Y = 100
119 };
120
121 static void generic_dialog_add_image(GenericDialog *gd, GtkWidget *box,
122                                      FileData *fd1, const gchar *header1,
123                                      gboolean second_image,
124                                      FileData *fd2, const gchar *header2,
125                                      gboolean show_filename)
126 {
127         ImageWindow *imd;
128         GtkWidget *preview_box = nullptr;
129         GtkWidget *vbox;
130         GtkWidget *label = nullptr;
131
132         if (!box) box = gd->vbox;
133
134         if (second_image)
135                 {
136                 preview_box = pref_box_new(box, FALSE, GTK_ORIENTATION_VERTICAL, PREF_PAD_SPACE);
137                 }
138
139         /* image 1 */
140
141         vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, PREF_PAD_GAP);
142         if (preview_box)
143                 {
144                 GtkWidget *sep;
145
146                 gq_gtk_box_pack_start(GTK_BOX(preview_box), vbox, FALSE, TRUE, 0);
147
148                 sep = gtk_separator_new(GTK_ORIENTATION_HORIZONTAL);
149                 gq_gtk_box_pack_start(GTK_BOX(preview_box), sep, FALSE, FALSE, 0);
150                 gtk_widget_show(sep);
151                 }
152         else
153                 {
154                 gq_gtk_box_pack_start(GTK_BOX(box), vbox, FALSE, TRUE, PREF_PAD_GAP);
155                 }
156         gtk_widget_show(vbox);
157
158         if (header1)
159                 {
160                 GtkWidget *head;
161
162                 head = pref_label_new(vbox, header1);
163                 pref_label_bold(head, TRUE, FALSE);
164                 gtk_label_set_xalign(GTK_LABEL(head), 0.0);
165                 gtk_label_set_yalign(GTK_LABEL(head), 0.5);
166                 }
167
168         imd = image_new(FALSE);
169         g_object_set(G_OBJECT(imd->pr), "zoom_expand", FALSE, NULL);
170         gtk_widget_set_size_request(imd->widget, DIALOG_DEF_IMAGE_DIM_X, DIALOG_DEF_IMAGE_DIM_Y);
171         gq_gtk_box_pack_start(GTK_BOX(vbox), imd->widget, TRUE, TRUE, 0);
172         image_change_fd(imd, fd1, 0.0);
173         gtk_widget_show(imd->widget);
174
175         if (show_filename)
176                 {
177                 label = pref_label_new(vbox, (fd1 == nullptr) ? "" : fd1->name);
178                 }
179
180         /* only the first image is stored (for use in gd_image_set) */
181         g_object_set_data(G_OBJECT(gd->dialog), "img_image", imd);
182         g_object_set_data(G_OBJECT(gd->dialog), "img_label", label);
183
184
185         /* image 2 */
186
187         if (preview_box)
188                 {
189                 vbox = pref_box_new(preview_box, TRUE, GTK_ORIENTATION_VERTICAL, PREF_PAD_GAP);
190
191                 if (header2)
192                         {
193                         GtkWidget *head;
194
195                         head = pref_label_new(vbox, header2);
196                         pref_label_bold(head, TRUE, FALSE);
197                         gtk_label_set_xalign(GTK_LABEL(head), 0.0);
198                         gtk_label_set_yalign(GTK_LABEL(head), 0.5);
199                         }
200
201                 imd = image_new(FALSE);
202                 g_object_set(G_OBJECT(imd->pr), "zoom_expand", FALSE, NULL);
203                 gtk_widget_set_size_request(imd->widget, DIALOG_DEF_IMAGE_DIM_X, DIALOG_DEF_IMAGE_DIM_Y);
204                 gq_gtk_box_pack_start(GTK_BOX(vbox), imd->widget, TRUE, TRUE, 0);
205                 if (fd2) image_change_fd(imd, fd2, 0.0);
206                 gtk_widget_show(imd->widget);
207
208                 if (show_filename)
209                         {
210                         label = pref_label_new(vbox, (fd2 == nullptr) ? "" : fd2->name);
211                         }
212                 g_object_set_data(G_OBJECT(gd->dialog), "img_image2", imd);
213                 g_object_set_data(G_OBJECT(gd->dialog), "img_label2", label);
214                 }
215 }
216
217 /*
218  *--------------------------------------------------------------------------
219  * Wrappers to aid in setting additional dialog properties (unde mouse, etc.)
220  *--------------------------------------------------------------------------
221  */
222
223 /**
224  * @brief
225  * @param title
226  * @param role
227  * @param parent
228  * @param auto_close
229  * @param cancel_cb
230  * @param data
231  * @returns
232  *
233  * \image html file_util_gen_dlg.png "Typical implementation" width=200
234  */
235 GenericDialog *file_util_gen_dlg(const gchar *title,
236                                  const gchar *role,
237                                  GtkWidget *parent, gboolean auto_close,
238                                  void (*cancel_cb)(GenericDialog *, gpointer), gpointer data)
239 {
240         GenericDialog *gd;
241
242         gd = generic_dialog_new(title, role, parent, auto_close, cancel_cb, data);
243         if (options->place_dialogs_under_mouse)
244                 {
245                 gq_gtk_window_set_position(GTK_WINDOW(gd->dialog), GTK_WIN_POS_MOUSE);
246                 }
247
248         return gd;
249 }
250
251 /**
252  * @brief
253  * @param title
254  * @param role
255  * @param parent
256  * @param cancel_cb
257  * @param data
258  * @returns
259  *
260  * \image html file_util_file_dlg.png "Typical implementation including optional filter, buttons and path widgets" width=300
261  */
262 FileDialog *file_util_file_dlg(const gchar *title,
263                                const gchar *role,
264                                GtkWidget *parent,
265                                void (*cancel_cb)(FileDialog *, gpointer), gpointer data)
266 {
267         FileDialog *fdlg;
268
269         fdlg = file_dialog_new(title, role, parent, cancel_cb, data);
270         if (options->place_dialogs_under_mouse)
271                 {
272                 gq_gtk_window_set_position(GTK_WINDOW(GENERIC_DIALOG(fdlg)->dialog), GTK_WIN_POS_MOUSE);
273                 }
274
275         gtk_window_set_modal(GTK_WINDOW(fdlg->gd.dialog), TRUE);
276
277         return fdlg;
278 }
279
280 /* this warning dialog is copied from SLIK's ui_utildg.c,
281  * because it does not have a mouse center option,
282  * and we must center it before show, implement it here.
283  */
284 static void file_util_warning_dialog_ok_cb(GenericDialog *, gpointer)
285 {
286         /* no op */
287 }
288
289 GenericDialog *file_util_warning_dialog(const gchar *heading, const gchar *message,
290                                         const gchar *icon_name, GtkWidget *parent)
291 {
292         GenericDialog *gd;
293
294         gd = file_util_gen_dlg(heading, "warning", parent, TRUE, nullptr, nullptr);
295         generic_dialog_add_message(gd, icon_name, heading, message, TRUE);
296         generic_dialog_add_button(gd, GQ_ICON_OK, "OK", file_util_warning_dialog_ok_cb, TRUE);
297         if (options->place_dialogs_under_mouse)
298                 {
299                 gq_gtk_window_set_position(GTK_WINDOW(gd->dialog), GTK_WIN_POS_MOUSE);
300                 }
301         gtk_widget_show(gd->dialog);
302
303         return gd;
304 }
305
306 static gint filename_base_length(const gchar *name)
307 {
308         gint n;
309
310         if (!name) return 0;
311
312         n = strlen(name);
313
314         if (filter_name_exists(name))
315                 {
316                 const gchar *ext;
317
318                 ext = registered_extension_from_path(name);
319                 if (ext) n -= strlen(ext);
320                 }
321
322         return n;
323 }
324
325
326
327
328 enum UtilityType {
329         UTILITY_TYPE_COPY,
330         UTILITY_TYPE_MOVE,
331         UTILITY_TYPE_RENAME,
332         UTILITY_TYPE_RENAME_FOLDER,
333         UTILITY_TYPE_EDITOR,
334         UTILITY_TYPE_FILTER,
335         UTILITY_TYPE_DELETE,
336         UTILITY_TYPE_DELETE_LINK,
337         UTILITY_TYPE_DELETE_FOLDER,
338         UTILITY_TYPE_CREATE_FOLDER,
339         UTILITY_TYPE_WRITE_METADATA
340 };
341
342 enum UtilityPhase {
343         UTILITY_PHASE_START = 0,
344         UTILITY_PHASE_INTERMEDIATE,
345         UTILITY_PHASE_ENTERING,
346         UTILITY_PHASE_CHECKED,
347         UTILITY_PHASE_DONE,
348         UTILITY_PHASE_CANCEL,
349         UTILITY_PHASE_DISCARD
350 };
351
352 enum {
353         UTILITY_RENAME = 0,
354         UTILITY_RENAME_AUTO,
355         UTILITY_RENAME_FORMATTED
356 };
357
358 struct UtilityDataMessages {
359         const gchar *title;
360         const gchar *question;
361         const gchar *desc_flist;
362         const gchar *desc_source_fd;
363         const gchar *fail;
364 };
365
366 struct UtilityData {
367         UtilityType type;
368         UtilityPhase phase;
369
370         FileData *dir_fd;
371         GList *content_list;
372         GList *flist;
373
374         FileData *sel_fd;
375
376         GtkWidget *parent;
377         GenericDialog *gd;
378         FileDialog *fdlg;
379
380         guint update_idle_id; /* event source id */
381         guint perform_idle_id; /* event source id */
382
383         gboolean with_sidecars; /* operate on grouped or single files; TRUE = use file_data_sc_, FALSE = use file_data_ functions */
384
385         /* alternative dialog parts */
386         GtkWidget *notebook;
387
388         UtilityDataMessages messages;
389
390         /* helper entries for various modes */
391         GtkWidget *rename_entry;
392         GtkWidget *rename_label;
393         GtkWidget *auto_entry_front;
394         GtkWidget *auto_entry_end;
395         GtkWidget *auto_spin_start;
396         GtkWidget *auto_spin_pad;
397         GtkWidget *format_entry;
398         GtkWidget *format_spin;
399
400         GtkWidget *listview;
401
402
403         gchar *dest_path;
404
405         /* data for the operation itself, internal or external */
406         gboolean external; /* TRUE for external command, FALSE for internal */
407
408         gchar *external_command;
409         gpointer resume_data;
410         gboolean show_rename_button;
411
412         FileUtilDoneFunc done_func;
413         void (*details_func)(UtilityData *ud, FileData *fd);
414         gboolean (*finalize_func)(FileData *fd);
415         gboolean (*discard_func)(FileData *fd);
416         gpointer done_data;
417 };
418
419 enum {
420         UTILITY_COLUMN_FD = 0,
421         UTILITY_COLUMN_PIXBUF,
422         UTILITY_COLUMN_PATH,
423         UTILITY_COLUMN_NAME,
424         UTILITY_COLUMN_SIDECARS,
425         UTILITY_COLUMN_DEST_PATH,
426         UTILITY_COLUMN_DEST_NAME,
427         UTILITY_COLUMN_COUNT
428 };
429
430 struct UtilityDelayData {
431         UtilityType type;
432         UtilityPhase phase;
433         GList *flist;
434         gchar *dest_path;
435         gchar *editor_key;
436         GtkWidget *parent;
437         guint idle_id; /* event source id */
438         };
439
440 static void generic_dialog_image_set(UtilityData *ud, FileData *fd)
441 {
442         ImageWindow *imd;
443         GtkWidget *label;
444         FileData *fd2 = nullptr;
445         gchar *buf;
446
447         imd = static_cast<ImageWindow *>(g_object_get_data(G_OBJECT(ud->gd->dialog), "img_image"));
448         label = static_cast<GtkWidget *>(g_object_get_data(G_OBJECT(ud->gd->dialog), "img_label"));
449
450         if (!imd) return;
451
452         image_change_fd(imd, fd, 0.0);
453         buf = g_strjoin("\n", text_from_time(fd->date), text_from_size(fd->size), NULL);
454         if (label) gtk_label_set_text(GTK_LABEL(label), buf);
455         g_free(buf);
456
457         if (ud->type == UTILITY_TYPE_RENAME || ud->type == UTILITY_TYPE_COPY || ud->type == UTILITY_TYPE_MOVE)
458                 {
459                 imd = static_cast<ImageWindow *>(g_object_get_data(G_OBJECT(ud->gd->dialog), "img_image2"));
460                 label = static_cast<GtkWidget *>(g_object_get_data(G_OBJECT(ud->gd->dialog), "img_label2"));
461
462                 if (imd)
463                         {
464                         if (isfile(fd->change->dest))
465                                 {
466                                 fd2 = file_data_new_group(fd->change->dest);
467                                 image_change_fd(imd, fd2, 0.0);
468                                 buf = g_strjoin("\n", text_from_time(fd2->date), text_from_size(fd2->size), NULL);
469                                 if (label && fd->change->dest) gtk_label_set_text(GTK_LABEL(label), buf);
470                                 file_data_unref(fd2);
471                                 g_free(buf);
472                                 }
473                         else
474                                 {
475                                 image_change_fd(imd, nullptr, 0.0);
476                                 if (label) gtk_label_set_text(GTK_LABEL(label), "");
477                                 }
478                         }
479                 }
480 }
481
482 static gboolean file_util_write_metadata_first(UtilityType type, UtilityPhase phase, GList *flist, const gchar *dest_path, const gchar *editor_key, GtkWidget *parent);
483
484 enum {
485         UTILITY_LIST_MIN_WIDTH =  250,
486         UTILITY_LIST_MIN_HEIGHT = 150
487 };
488
489 /* thumbnail spec has a max depth of 4 (.thumb??/fail/appname/??.png) */
490 enum {
491         UTILITY_DELETE_MAX_DEPTH = 5
492 };
493
494 static UtilityData *file_util_data_new(UtilityType type)
495 {
496         UtilityData *ud;
497
498         ud = g_new0(UtilityData, 1);
499
500         ud->type = type;
501         ud->phase = UTILITY_PHASE_START;
502
503         if (type == UTILITY_TYPE_CREATE_FOLDER)
504                 {
505                 ud->show_rename_button = FALSE;
506                 }
507         else
508                 {
509                 ud->show_rename_button = TRUE;
510                 }
511
512         return ud;
513 }
514
515 static void file_util_data_free(UtilityData *ud)
516 {
517         if (!ud) return;
518
519         if (ud->update_idle_id) g_source_remove(ud->update_idle_id);
520         if (ud->perform_idle_id) g_source_remove(ud->perform_idle_id);
521
522         file_data_unref(ud->dir_fd);
523         filelist_free(ud->content_list);
524         filelist_free(ud->flist);
525
526         if (ud->gd) generic_dialog_close(ud->gd);
527
528         g_free(ud->dest_path);
529         g_free(ud->external_command);
530
531         g_free(ud);
532 }
533
534 static GtkTreeViewColumn *file_util_dialog_add_list_column(GtkWidget *view, const gchar *text, gboolean image, gint n)
535 {
536         GtkTreeViewColumn *column;
537         GtkCellRenderer *renderer;
538
539         column = gtk_tree_view_column_new();
540         gtk_tree_view_column_set_title(column, text);
541         gtk_tree_view_column_set_min_width(column, 4);
542         if (image)
543                 {
544                 gtk_tree_view_column_set_min_width(column, 20);
545                 gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_FIXED);
546                 renderer = gtk_cell_renderer_pixbuf_new();
547                 gtk_tree_view_column_pack_start(column, renderer, TRUE);
548                 gtk_tree_view_column_add_attribute(column, renderer, "pixbuf", n);
549                 }
550         else
551                 {
552                 gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_GROW_ONLY);
553                 renderer = gtk_cell_renderer_text_new();
554                 gtk_tree_view_column_pack_start(column, renderer, TRUE);
555                 gtk_tree_view_column_add_attribute(column, renderer, "text", n);
556                 }
557         gtk_tree_view_append_column(GTK_TREE_VIEW(view), column);
558
559         return column;
560 }
561
562 static void file_util_dialog_list_select(GtkWidget *view, gint n)
563 {
564         GtkTreeModel *store;
565         GtkTreeIter iter;
566         GtkTreeSelection *selection;
567
568         store = gtk_tree_view_get_model(GTK_TREE_VIEW(view));
569         if (gtk_tree_model_iter_nth_child(store, &iter, nullptr, n))
570                 {
571                 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(view));
572                 gtk_tree_selection_select_iter(selection, &iter);
573                 }
574 }
575
576 static GtkWidget *file_util_dialog_add_list(GtkWidget *box, GList *list, gboolean full_paths, gboolean with_sidecars)
577 {
578         GtkWidget *scrolled;
579         GtkWidget *view;
580         GtkListStore *store;
581
582         scrolled = gq_gtk_scrolled_window_new(nullptr, nullptr);
583         gq_gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled), GTK_SHADOW_IN);
584         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled),
585                                        GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
586         gq_gtk_box_pack_start(GTK_BOX(box), scrolled, TRUE, TRUE, 0);
587         gtk_widget_show(scrolled);
588
589         store = gtk_list_store_new(UTILITY_COLUMN_COUNT, G_TYPE_POINTER, GDK_TYPE_PIXBUF, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING);
590         view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
591         g_object_unref(store);
592
593         gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(view), TRUE);
594         gtk_tree_view_set_enable_search(GTK_TREE_VIEW(view), FALSE);
595
596         file_util_dialog_add_list_column(view, "", TRUE, UTILITY_COLUMN_PIXBUF);
597
598         if (full_paths)
599                 {
600                 file_util_dialog_add_list_column(view, _("Path"), FALSE, UTILITY_COLUMN_PATH);
601                 }
602         else
603                 {
604                 file_util_dialog_add_list_column(view, _("Name"), FALSE, UTILITY_COLUMN_NAME);
605                 }
606
607         gtk_widget_set_size_request(view, UTILITY_LIST_MIN_WIDTH, UTILITY_LIST_MIN_HEIGHT);
608         gq_gtk_container_add(GTK_WIDGET(scrolled), view);
609         gtk_widget_show(view);
610
611         while (list)
612                 {
613                 auto fd = static_cast<FileData *>(list->data);
614                 GtkTreeIter iter;
615                 gchar *sidecars;
616
617                 sidecars = with_sidecars ? file_data_sc_list_to_string(fd) : nullptr;
618                 GdkPixbuf *icon = file_util_get_error_icon(fd, list, view);
619                 gtk_list_store_append(store, &iter);
620                 gtk_list_store_set(store, &iter,
621                                    UTILITY_COLUMN_FD, fd,
622                                    UTILITY_COLUMN_PIXBUF, icon,
623                                    UTILITY_COLUMN_PATH, fd->path,
624                                    UTILITY_COLUMN_NAME, fd->name,
625                                    UTILITY_COLUMN_SIDECARS, sidecars,
626                                    UTILITY_COLUMN_DEST_PATH, fd->change ? fd->change->dest : "error",
627                                    UTILITY_COLUMN_DEST_NAME, fd->change ? filename_from_path(fd->change->dest) : "error",
628                                    -1);
629                 g_free(sidecars);
630
631                 list = list->next;
632                 }
633
634         return view;
635 }
636
637
638 static gboolean file_util_perform_ci_internal(gpointer data);
639 void file_util_dialog_run(UtilityData *ud);
640 static gint file_util_perform_ci_cb(gpointer resume_data, EditorFlags flags, GList *list, gpointer data);
641
642 /* call file_util_perform_ci_internal or start_editor_from_filelist_full */
643
644
645 static void file_util_resume_cb(GenericDialog *, gpointer data)
646 {
647         auto ud = static_cast<UtilityData *>(data);
648         if (ud->external)
649                 editor_resume(ud->resume_data);
650         else
651                 file_util_perform_ci_internal(ud);
652 }
653
654 static void file_util_abort_cb(GenericDialog *, gpointer data)
655 {
656         auto ud = static_cast<UtilityData *>(data);
657         if (ud->external)
658                 editor_skip(ud->resume_data);
659         else
660                 file_util_perform_ci_cb(nullptr, EDITOR_ERROR_SKIPPED, ud->flist, ud);
661
662 }
663
664
665 static gint file_util_perform_ci_cb(gpointer resume_data, EditorFlags flags, GList *list, gpointer data)
666 {
667         auto ud = static_cast<UtilityData *>(data);
668         gint ret = EDITOR_CB_CONTINUE;
669
670         ud->resume_data = resume_data;
671
672         if (EDITOR_ERRORS_BUT_SKIPPED(flags))
673                 {
674                 GString *msg = g_string_new(editor_get_error_str(flags));
675                 GenericDialog *d;
676                 g_string_append(msg, "\n");
677                 g_string_append(msg, ud->messages.fail);
678                 g_string_append(msg, "\n");
679                 while (list)
680                         {
681                         auto fd = static_cast<FileData *>(list->data);
682
683                         g_string_append(msg, fd->path);
684                         g_string_append(msg, "\n");
685                         list = list->next;
686                         }
687                 if (resume_data)
688                         {
689                         g_string_append(msg, _("\n Continue multiple file operation?"));
690                         d = file_util_gen_dlg(ud->messages.fail, "dlg_confirm",
691                                               nullptr, TRUE,
692                                               file_util_abort_cb, ud);
693
694                         generic_dialog_add_message(d, GQ_ICON_DIALOG_WARNING, nullptr, msg->str, TRUE);
695
696                         generic_dialog_add_button(d, GQ_ICON_GO_NEXT, _("Co_ntinue"),
697                                                   file_util_resume_cb, TRUE);
698                         gtk_widget_show(d->dialog);
699                         ret = EDITOR_CB_SUSPEND;
700                         }
701                 else
702                         {
703                         file_util_warning_dialog(ud->messages.fail, msg->str, GQ_ICON_DIALOG_ERROR, nullptr);
704                         }
705                 g_string_free(msg, TRUE);
706                 }
707
708
709         while (list)  /* be careful, file_util_perform_ci_internal can pass ud->flist as list */
710                 {
711                 auto fd = static_cast<FileData *>(list->data);
712                 list = list->next;
713
714                 if (!EDITOR_ERRORS(flags)) /* files were successfully deleted, call the maint functions */
715                         {
716                         if (ud->with_sidecars)
717                                 file_data_sc_apply_ci(fd);
718                         else
719                                 file_data_apply_ci(fd);
720                         }
721
722                 ud->flist = g_list_remove(ud->flist, fd);
723
724                 if (ud->finalize_func)
725                         {
726                         ud->finalize_func(fd);
727                         }
728
729                 if (ud->with_sidecars)
730                         file_data_sc_free_ci(fd);
731                 else
732                         file_data_free_ci(fd);
733                 file_data_unref(fd);
734                 }
735
736         if (!resume_data) /* end of the list */
737                 {
738                 ud->phase = UTILITY_PHASE_DONE;
739                 file_util_dialog_run(ud);
740                 }
741
742         return ret;
743 }
744
745
746 /*
747  * Perform the operation described by FileDataChangeInfo on all files in the list
748  * it is an alternative to start_editor_from_filelist_full, it should use similar interface
749  */
750
751
752 static gboolean file_util_perform_ci_internal(gpointer data)
753 {
754         auto ud = static_cast<UtilityData *>(data);
755
756         if (!ud->perform_idle_id)
757                 {
758                 /* this function was called directly
759                    just setup idle callback and wait until we are called again
760                 */
761
762                 /* this is removed when ud is destroyed */
763                 ud->perform_idle_id = g_idle_add(file_util_perform_ci_internal, ud);
764                 return G_SOURCE_CONTINUE;
765                 }
766
767         g_assert(ud->flist);
768
769         if (ud->flist)
770                 {
771                 gint ret;
772
773                 /* take a single entry each time, this allows better control over the operation */
774                 GList *single_entry = g_list_append(nullptr, ud->flist->data);
775                 gboolean last = !ud->flist->next;
776                 EditorFlags status = EDITOR_ERROR_STATUS;
777
778                 if (ud->with_sidecars ? file_data_sc_perform_ci(static_cast<FileData *>(single_entry->data))
779                                       : file_data_perform_ci(static_cast<FileData *>(single_entry->data)))
780                         status = static_cast<EditorFlags>(0); /* OK */
781
782                 ret = file_util_perform_ci_cb(GINT_TO_POINTER(!last), status, single_entry, ud);
783                 g_list_free(single_entry);
784
785                 if (ret == EDITOR_CB_SUSPEND || last) return G_SOURCE_REMOVE;
786
787                 if (ret == EDITOR_CB_SKIP)
788                         {
789                         file_util_perform_ci_cb(nullptr, EDITOR_ERROR_SKIPPED, ud->flist, ud);
790                         return G_SOURCE_REMOVE;
791                         }
792                 }
793
794         return G_SOURCE_CONTINUE;
795 }
796
797 static void file_util_perform_ci_dir(UtilityData *ud, gboolean internal, gboolean ext_result)
798 {
799         switch (ud->type)
800                 {
801                 case UTILITY_TYPE_DELETE_LINK:
802                         {
803                         g_assert(ud->dir_fd->sidecar_files == nullptr); // directories should not have sidecars
804                         if ((internal && file_data_perform_ci(ud->dir_fd)) ||
805                             (!internal && ext_result))
806                                 {
807                                 file_data_apply_ci(ud->dir_fd);
808                                 }
809                         else
810                                 {
811                                 gchar *text;
812
813                                 text = g_strdup_printf("%s:\n\n%s", ud->messages.fail, ud->dir_fd->path);
814                                 file_util_warning_dialog(ud->messages.fail, text, GQ_ICON_DIALOG_ERROR, nullptr);
815                                 g_free(text);
816                                 }
817                         file_data_free_ci(ud->dir_fd);
818                         break;
819                         }
820                 case UTILITY_TYPE_DELETE_FOLDER:
821                         {
822                         FileData *fail = nullptr;
823                         GList *work;
824                         work = ud->content_list;
825                         while (work)
826                                 {
827                                 FileData *fd;
828
829                                 fd = static_cast<FileData *>(work->data);
830                                 work = work->next;
831
832                                 if (!fail)
833                                         {
834                                         if ((internal && file_data_sc_perform_ci(fd)) ||
835                                             (!internal && ext_result))
836                                                 {
837                                                 file_data_sc_apply_ci(fd);
838                                                 }
839                                         else
840                                                 {
841                                                 if (internal) fail = file_data_ref(fd);
842                                                 }
843                                         }
844                                 file_data_sc_free_ci(fd);
845                                 }
846
847                         if (!fail)
848                                 {
849                                 g_assert(ud->dir_fd->sidecar_files == nullptr); // directories should not have sidecars
850                                 if ((internal && file_data_sc_perform_ci(ud->dir_fd)) ||
851                                     (!internal && ext_result))
852                                         {
853                                         file_data_apply_ci(ud->dir_fd);
854                                         }
855                                 else
856                                         {
857                                         fail = file_data_ref(ud->dir_fd);
858                                         }
859                                 }
860
861                         if (fail)
862                                 {
863                                 gchar *text;
864                                 GenericDialog *gd;
865
866                                 text = g_strdup_printf("%s:\n\n%s", ud->messages.fail, ud->dir_fd->path);
867                                 gd = file_util_warning_dialog(ud->messages.fail, text, GQ_ICON_DIALOG_ERROR, nullptr);
868                                 g_free(text);
869
870                                 if (fail != ud->dir_fd)
871                                         {
872                                         pref_spacer(gd->vbox, PREF_PAD_GROUP);
873                                         text = g_strdup_printf(_("Removal of folder contents failed at this file:\n\n%s"),
874                                                                 fail->path);
875                                         pref_label_new(gd->vbox, text);
876                                         g_free(text);
877                                         }
878
879                                 file_data_unref(fail);
880                                 }
881                         break;
882                         }
883                 case UTILITY_TYPE_RENAME_FOLDER:
884                         {
885                         FileData *fail = nullptr;
886                         GList *work;
887                         g_assert(ud->dir_fd->sidecar_files == nullptr); // directories should not have sidecars
888
889                         if ((internal && file_data_sc_perform_ci(ud->dir_fd)) ||
890                             (!internal && ext_result))
891                                 {
892                                 file_data_sc_apply_ci(ud->dir_fd);
893                                 }
894                         else
895                                 {
896                                 fail = file_data_ref(ud->dir_fd);
897                                 }
898
899
900                         work = ud->content_list;
901                         while (work)
902                                 {
903                                 FileData *fd;
904
905                                 fd = static_cast<FileData *>(work->data);
906                                 work = work->next;
907
908                                 if (!fail)
909                                         {
910                                         file_data_sc_apply_ci(fd);
911                                         }
912                                 file_data_sc_free_ci(fd);
913                                 }
914
915                         if (fail)
916                                 {
917                                 gchar *text;
918
919                                 text = g_strdup_printf("%s:\n\n%s", ud->messages.fail, ud->dir_fd->path);
920                                 file_util_warning_dialog(ud->messages.fail, text, GQ_ICON_DIALOG_ERROR, nullptr);
921                                 g_free(text);
922
923                                 file_data_unref(fail);
924                                 }
925                         break;
926                         }
927                 case UTILITY_TYPE_CREATE_FOLDER:
928                         {
929                         if ((internal && mkdir_utf8(ud->dir_fd->path, 0755)) ||
930                             (!internal && ext_result))
931                                 {
932                                 file_data_check_changed_files(ud->dir_fd); /* this will update the FileData and send notification */
933                                 }
934                         else
935                                 {
936                                 gchar *text;
937
938                                 text = g_strdup_printf("%s:\n\n%s", ud->messages.fail, ud->dir_fd->path);
939                                 file_util_warning_dialog(ud->messages.fail, text, GQ_ICON_DIALOG_ERROR, nullptr);
940                                 g_free(text);
941                                 }
942
943                         break;
944                         }
945                 default:
946                         g_warning("unhandled operation");
947                 }
948         ud->phase = UTILITY_PHASE_DONE;
949         file_util_dialog_run(ud);
950 }
951
952 static gint file_util_perform_ci_dir_cb(gpointer, EditorFlags flags, GList *, gpointer data)
953 {
954         auto ud = static_cast<UtilityData *>(data);
955         file_util_perform_ci_dir(ud, FALSE, !EDITOR_ERRORS_BUT_SKIPPED(flags));
956         return EDITOR_CB_CONTINUE; /* does not matter, there was just single directory */
957 }
958
959 void file_util_perform_ci(UtilityData *ud)
960 {
961         switch (ud->type)
962                 {
963                 case UTILITY_TYPE_COPY:
964                         ud->external_command = g_strdup(CMD_COPY);
965                         break;
966                 case UTILITY_TYPE_MOVE:
967                         ud->external_command = g_strdup(CMD_MOVE);
968                         break;
969                 case UTILITY_TYPE_RENAME:
970                 case UTILITY_TYPE_RENAME_FOLDER:
971                         ud->external_command = g_strdup(CMD_RENAME);
972                         break;
973                 case UTILITY_TYPE_DELETE:
974                 case UTILITY_TYPE_DELETE_LINK:
975                 case UTILITY_TYPE_DELETE_FOLDER:
976                         ud->external_command = g_strdup(CMD_DELETE);
977                         break;
978                 case UTILITY_TYPE_CREATE_FOLDER:
979                         ud->external_command = g_strdup(CMD_FOLDER);
980                         break;
981                 case UTILITY_TYPE_FILTER:
982                 case UTILITY_TYPE_EDITOR:
983                         g_assert(ud->external_command != nullptr); /* it should be already set */
984                         break;
985                 case UTILITY_TYPE_WRITE_METADATA:
986                         ud->external_command = nullptr;
987                 }
988
989         if (is_valid_editor_command(ud->external_command))
990                 {
991                 EditorFlags flags;
992
993                 ud->external = TRUE;
994
995                 if (ud->dir_fd)
996                         {
997                         flags = start_editor_from_file_full(ud->external_command, ud->dir_fd, file_util_perform_ci_dir_cb, ud);
998                         }
999                 else
1000                         {
1001                         if (editor_blocks_file(ud->external_command))
1002                                 {
1003                                 DEBUG_1("Starting %s and waiting for results", ud->external_command);
1004                                 flags = start_editor_from_filelist_full(ud->external_command, ud->flist, nullptr, file_util_perform_ci_cb, ud);
1005                                 }
1006                         else
1007                                 {
1008                                 /* start the editor without callback and finish the operation internally */
1009                                 DEBUG_1("Starting %s and finishing the operation", ud->external_command);
1010                                 flags = start_editor_from_filelist(ud->external_command, ud->flist);
1011                                 file_util_perform_ci_internal(ud);
1012                                 }
1013                         }
1014
1015                 if (EDITOR_ERRORS(flags))
1016                         {
1017                         gchar *text = g_strdup_printf(_("%s\nUnable to start external command.\n"), editor_get_error_str(flags));
1018                         file_util_warning_dialog(ud->messages.fail, text, GQ_ICON_DIALOG_ERROR, nullptr);
1019                         g_free(text);
1020
1021                         ud->gd = nullptr;
1022                         ud->phase = UTILITY_PHASE_CANCEL;
1023                         file_util_dialog_run(ud);
1024                         }
1025                 }
1026         else
1027                 {
1028                 ud->external = FALSE;
1029                 if (ud->dir_fd)
1030                         {
1031                         file_util_perform_ci_dir(ud, TRUE, FALSE);
1032                         }
1033                 else
1034                         {
1035                         file_util_perform_ci_internal(ud);
1036                         }
1037                 }
1038 }
1039
1040 static void file_util_check_resume_cb(GenericDialog *, gpointer data)
1041 {
1042         auto ud = static_cast<UtilityData *>(data);
1043         ud->phase = UTILITY_PHASE_CHECKED;
1044         file_util_dialog_run(ud);
1045 }
1046
1047 static void file_util_check_abort_cb(GenericDialog *, gpointer data)
1048 {
1049         auto ud = static_cast<UtilityData *>(data);
1050         ud->phase = UTILITY_PHASE_START;
1051         file_util_dialog_run(ud);
1052 }
1053
1054 void file_util_check_ci(UtilityData *ud)
1055 {
1056         gint error = CHANGE_OK;
1057         gchar *desc = nullptr;
1058
1059         if (ud->type != UTILITY_TYPE_CREATE_FOLDER &&
1060             ud->type != UTILITY_TYPE_RENAME_FOLDER)
1061                 {
1062                 if (ud->dest_path && !isdir(ud->dest_path))
1063                         {
1064                         error = CHANGE_GENERIC_ERROR;
1065                         desc = g_strdup_printf(_("%s is not a directory"), ud->dest_path);
1066                         }
1067                 else if (ud->dir_fd)
1068                         {
1069                         g_assert(ud->dir_fd->sidecar_files == nullptr); // directories should not have sidecars
1070                         error = file_data_verify_ci(ud->dir_fd, ud->flist);
1071                         if (error) desc = file_data_get_error_string(error);
1072                         }
1073                 else
1074                         {
1075                         error = file_data_verify_ci_list(ud->flist, &desc, ud->with_sidecars);
1076                         }
1077                 }
1078         else
1079                 {
1080                 if (ud->type == UTILITY_TYPE_CREATE_FOLDER || ud->type == UTILITY_TYPE_RENAME_FOLDER)
1081                         {
1082                         if (isdir(ud->dest_path) || isfile(ud->dest_path))
1083                                 {
1084                                 error = CHANGE_DEST_EXISTS;
1085                                 desc = g_strdup_printf(_("%s already exists"), ud->dest_path);
1086                                 }
1087                         }
1088                 }
1089
1090         if (!error)
1091                 {
1092                 ud->phase = UTILITY_PHASE_CHECKED;
1093                 file_util_dialog_run(ud);
1094                 return;
1095                 }
1096
1097         if (!(error & CHANGE_ERROR_MASK))
1098                 {
1099                 /* just a warning */
1100                 GenericDialog *d;
1101
1102                 d = file_util_gen_dlg(ud->messages.title, "dlg_confirm",
1103                                         ud->parent, TRUE,
1104                                         file_util_check_abort_cb, ud);
1105
1106                 generic_dialog_add_message(d, GQ_ICON_DIALOG_WARNING, _("Really continue?"), desc, TRUE);
1107
1108                 generic_dialog_add_button(d, GQ_ICON_GO_NEXT, _("Co_ntinue"),
1109                                           file_util_check_resume_cb, TRUE);
1110                 gtk_widget_show(d->dialog);
1111                 }
1112         else
1113                 {
1114                 /* fatal error */
1115                 GenericDialog *d;
1116
1117                 d = file_util_gen_dlg(ud->messages.title, "dlg_confirm",
1118                                         ud->parent, TRUE,
1119                                         file_util_check_abort_cb, ud);
1120                 generic_dialog_add_message(d, GQ_ICON_DIALOG_WARNING, _("This operation can't continue:"), desc, TRUE);
1121
1122                 gtk_widget_show(d->dialog);
1123                 }
1124         g_free(desc);
1125 }
1126
1127
1128
1129
1130
1131 static void file_util_cancel_cb(GenericDialog *gd, gpointer data)
1132 {
1133         auto ud = static_cast<UtilityData *>(data);
1134
1135         generic_dialog_close(gd);
1136
1137         ud->gd = nullptr;
1138
1139         ud->phase = UTILITY_PHASE_CANCEL;
1140         file_util_dialog_run(ud);
1141 }
1142
1143 static void file_util_discard_cb(GenericDialog *gd, gpointer data)
1144 {
1145         auto ud = static_cast<UtilityData *>(data);
1146
1147         generic_dialog_close(gd);
1148
1149         ud->gd = nullptr;
1150
1151         ud->phase = UTILITY_PHASE_DISCARD;
1152         file_util_dialog_run(ud);
1153 }
1154
1155 static void file_util_ok_cb(GenericDialog *gd, gpointer data)
1156 {
1157         auto ud = static_cast<UtilityData *>(data);
1158
1159         generic_dialog_close(gd);
1160
1161         ud->gd = nullptr;
1162
1163         file_util_dialog_run(ud);
1164 }
1165
1166 static void file_util_fdlg_cancel_cb(FileDialog *fdlg, gpointer data)
1167 {
1168         auto ud = static_cast<UtilityData *>(data);
1169
1170         file_dialog_close(fdlg);
1171
1172         ud->fdlg = nullptr;
1173
1174         ud->phase = UTILITY_PHASE_CANCEL;
1175         file_util_dialog_run(ud);
1176 }
1177
1178 static void file_util_dest_folder_update_path(UtilityData *ud)
1179 {
1180         g_free(ud->dest_path);
1181         ud->dest_path = g_strdup(gq_gtk_entry_get_text(GTK_ENTRY(ud->fdlg->entry)));
1182
1183         switch (ud->type)
1184                 {
1185                 case UTILITY_TYPE_COPY:
1186                         file_data_sc_update_ci_copy_list(ud->flist, ud->dest_path);
1187                         break;
1188                 case UTILITY_TYPE_MOVE:
1189                         file_data_sc_update_ci_move_list(ud->flist, ud->dest_path);
1190                         break;
1191                 case UTILITY_TYPE_FILTER:
1192                 case UTILITY_TYPE_EDITOR:
1193                         file_data_sc_update_ci_unspecified_list(ud->flist, ud->dest_path);
1194                         break;
1195                 case UTILITY_TYPE_CREATE_FOLDER:
1196                         file_data_unref(ud->dir_fd);
1197                         ud->dir_fd = file_data_new_dir(ud->dest_path);
1198                         break;
1199                 case UTILITY_TYPE_DELETE:
1200                 case UTILITY_TYPE_DELETE_LINK:
1201                 case UTILITY_TYPE_DELETE_FOLDER:
1202                 case UTILITY_TYPE_RENAME:
1203                 case UTILITY_TYPE_RENAME_FOLDER:
1204                 case UTILITY_TYPE_WRITE_METADATA:
1205                         g_warning("unhandled operation");
1206                 }
1207 }
1208
1209 static void file_util_fdlg_rename_cb(FileDialog *fdlg, gpointer data)
1210 {
1211         auto ud = static_cast<UtilityData *>(data);
1212         gchar *desc = nullptr;
1213         GenericDialog *d = nullptr;
1214
1215         file_util_dest_folder_update_path(ud);
1216         if (isdir(ud->dest_path))
1217                 {
1218                 file_dialog_sync_history(fdlg, TRUE);
1219                 file_dialog_close(fdlg);
1220                 ud->fdlg = nullptr;
1221                 file_util_dialog_run(ud);
1222                 }
1223         else
1224                 {
1225                 /* During copy/move operations it is necessary to ensure that the
1226                  * target directory exists before continuing with the next step.
1227                  * If not revert to the select directory dialog
1228                  */
1229                 desc = g_strdup_printf(_("%s is not a directory"), ud->dest_path);
1230
1231                 d = file_util_gen_dlg(ud->messages.title, "dlg_confirm",
1232                                         ud->parent, TRUE,
1233                                         file_util_check_abort_cb, ud);
1234                 generic_dialog_add_message(d, GQ_ICON_DIALOG_WARNING, _("This operation can't continue:"), desc, TRUE);
1235
1236                 gtk_widget_show(d->dialog);
1237                 ud->phase = UTILITY_PHASE_START;
1238
1239                 file_dialog_close(fdlg);
1240                 ud->fdlg = nullptr;
1241                 g_free(desc);
1242                 }
1243 }
1244
1245 static void file_util_fdlg_ok_cb(FileDialog *fdlg, gpointer data)
1246 {
1247         auto ud = static_cast<UtilityData *>(data);
1248
1249         file_util_dest_folder_update_path(ud);
1250         if (isdir(ud->dest_path)) file_dialog_sync_history(fdlg, TRUE);
1251         file_dialog_close(fdlg);
1252
1253         ud->fdlg = nullptr;
1254         ud->phase = UTILITY_PHASE_ENTERING;
1255
1256         file_util_dialog_run(ud);
1257 }
1258
1259 static void file_util_dest_folder_entry_cb(GtkWidget *, gpointer data)
1260 {
1261         auto ud = static_cast<UtilityData *>(data);
1262         file_util_dest_folder_update_path(ud);
1263 }
1264
1265 /* format: * = filename without extension, ## = number position, extension is kept */
1266 static gchar *file_util_rename_multiple_auto_format_name(const gchar *format, const gchar *name, gint n)
1267 {
1268         gchar *new_name;
1269         gchar *parsed;
1270         const gchar *ext;
1271         gchar *middle;
1272         gchar *tmp;
1273         gchar *pad_start;
1274         gchar *pad_end;
1275         gint padding;
1276
1277         if (!format || !name) return nullptr;
1278
1279         tmp = g_strdup(format);
1280         pad_start = strchr(tmp, '#');
1281         if (pad_start)
1282                 {
1283                 pad_end = pad_start;
1284                 padding = 0;
1285                 while (*pad_end == '#')
1286                         {
1287                         pad_end++;
1288                         padding++;
1289                         }
1290                 *pad_start = '\0';
1291
1292                 parsed = g_strdup_printf("%s%0*d%s", tmp, padding, n, pad_end);
1293                 g_free(tmp);
1294                 }
1295         else
1296                 {
1297                 parsed = tmp;
1298                 }
1299
1300         ext = registered_extension_from_path(name);
1301
1302         middle = strchr(parsed, '*');
1303         if (middle)
1304                 {
1305                 gchar *base;
1306
1307                 *middle = '\0';
1308                 middle++;
1309
1310                 base = remove_extension_from_path(name);
1311                 new_name = g_strconcat(parsed, base, middle, ext, NULL);
1312                 g_free(base);
1313                 }
1314         else
1315                 {
1316                 new_name = g_strconcat(parsed, ext, NULL);
1317                 }
1318
1319         g_free(parsed);
1320
1321         return new_name;
1322 }
1323
1324
1325 static void file_util_rename_preview_update(UtilityData *ud)
1326 {
1327         GtkTreeModel *store;
1328         GtkTreeSelection *selection;
1329         GtkTreeIter iter, iter_selected;
1330         GtkTreePath *path_iter, *path_selected;
1331         const gchar *front;
1332         const gchar *end;
1333         const gchar *format;
1334         gboolean valid;
1335         gint start_n;
1336         gint padding;
1337         gint n;
1338         gint mode;
1339         gchar *dirname;
1340         gchar *destname;
1341
1342         mode = gtk_notebook_get_current_page(GTK_NOTEBOOK(ud->notebook));
1343
1344         if (mode == UTILITY_RENAME)
1345                 {
1346                 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(ud->listview));
1347                 if (gtk_tree_selection_get_selected(selection, &store, &iter))
1348                         {
1349                         FileData *fd;
1350                         const gchar *dest = gq_gtk_entry_get_text(GTK_ENTRY(ud->rename_entry));
1351
1352                         gtk_tree_model_get(store, &iter, UTILITY_COLUMN_FD, &fd, -1);
1353                         g_assert(ud->with_sidecars); /* sidecars must be renamed too, it would break the pairing otherwise */
1354
1355                         dirname = g_path_get_dirname(fd->change->dest);
1356                         destname = g_build_filename(dirname, dest, NULL);
1357                         switch (ud->type)
1358                                 {
1359                                 case UTILITY_TYPE_RENAME:
1360                                         file_data_sc_update_ci_rename(fd, dest);
1361                                         break;
1362                                 case UTILITY_TYPE_COPY:
1363                                         file_data_sc_update_ci_copy(fd, destname);
1364                                         break;
1365                                 case UTILITY_TYPE_MOVE:
1366                                         file_data_sc_update_ci_move(fd, destname);
1367                                         break;
1368                                 default:;
1369                                 }
1370                         generic_dialog_image_set(ud, fd);
1371
1372                         gtk_list_store_set(GTK_LIST_STORE(store), &iter,
1373                                    UTILITY_COLUMN_DEST_PATH, fd->change->dest,
1374                                    UTILITY_COLUMN_DEST_NAME, filename_from_path(fd->change->dest),
1375                                    -1);
1376                         }
1377                 }
1378         else
1379                 {
1380                 front = gq_gtk_entry_get_text(GTK_ENTRY(ud->auto_entry_front));
1381                 end = gq_gtk_entry_get_text(GTK_ENTRY(ud->auto_entry_end));
1382                 padding = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(ud->auto_spin_pad));
1383
1384                 format = gq_gtk_entry_get_text(GTK_ENTRY(ud->format_entry));
1385
1386                 g_free(options->cp_mv_rn.auto_end);
1387                 options->cp_mv_rn.auto_end = g_strdup(end);
1388                 options->cp_mv_rn.auto_padding = padding;
1389
1390                 if (mode == UTILITY_RENAME_FORMATTED)
1391                         {
1392                         start_n = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(ud->format_spin));
1393                         options->cp_mv_rn.formatted_start = start_n;
1394                         }
1395                 else
1396                         {
1397                         start_n = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(ud->auto_spin_start));
1398                         options->cp_mv_rn.auto_start = start_n;
1399                         }
1400
1401                 store = gtk_tree_view_get_model(GTK_TREE_VIEW(ud->listview));
1402                 n = start_n;
1403                 valid = gtk_tree_model_get_iter_first(store, &iter);
1404                 while (valid)
1405                         {
1406                         gchar *dest;
1407                         FileData *fd;
1408                         gtk_tree_model_get(store, &iter, UTILITY_COLUMN_FD, &fd, -1);
1409
1410                         if (mode == UTILITY_RENAME_FORMATTED)
1411                                 {
1412                                 dest = file_util_rename_multiple_auto_format_name(format, fd->name, n);
1413                                 }
1414                         else
1415                                 {
1416                                 dest = g_strdup_printf("%s%0*d%s", front, padding, n, end);
1417                                 }
1418
1419                         g_assert(ud->with_sidecars); /* sidecars must be renamed too, it would break the pairing otherwise */
1420
1421                         dirname = g_path_get_dirname(fd->change->dest);
1422                         destname = g_build_filename(dirname, dest, NULL);
1423
1424                         switch (ud->type)
1425                                 {
1426                                 case UTILITY_TYPE_RENAME:
1427                                         file_data_sc_update_ci_rename(fd, dest);
1428                                         break;
1429                                 case UTILITY_TYPE_COPY:
1430                                         file_data_sc_update_ci_copy(fd, destname);
1431                                         break;
1432                                 case UTILITY_TYPE_MOVE:
1433                                         file_data_sc_update_ci_move(fd, destname);
1434                                         break;
1435                                 default:;
1436                                 }
1437
1438                         g_free(dirname);
1439                         g_free(destname);
1440                         g_free(dest);
1441
1442                         selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(ud->listview));
1443                         gtk_tree_selection_get_selected(selection, &store, &iter_selected);
1444                         path_iter=gtk_tree_model_get_path(store,&iter);
1445                         path_selected=gtk_tree_model_get_path(store,&iter_selected);
1446                         if (!gtk_tree_path_compare(path_iter,path_selected))
1447                                 {
1448                                 generic_dialog_image_set(ud, fd);
1449                                 }
1450                         gtk_tree_path_free(path_iter);
1451                         gtk_tree_path_free(path_selected);
1452
1453                         gtk_list_store_set(GTK_LIST_STORE(store), &iter,
1454                                            UTILITY_COLUMN_DEST_PATH, fd->change->dest,
1455                                            UTILITY_COLUMN_DEST_NAME, filename_from_path(fd->change->dest),
1456                                            -1);
1457                         n++;
1458                         valid = gtk_tree_model_iter_next(store, &iter);
1459                         }
1460                 }
1461
1462         /* Check the other entries in the list - if there are
1463          * multiple destination filenames with the same name the
1464          * error icons must be updated
1465          */
1466         valid = gtk_tree_model_get_iter_first(store, &iter);
1467         while (valid)
1468                 {
1469                 FileData *fd;
1470                 gtk_tree_model_get(store, &iter, UTILITY_COLUMN_FD, &fd, -1);
1471
1472                 gtk_list_store_set(GTK_LIST_STORE(store), &iter,
1473                            UTILITY_COLUMN_PIXBUF, file_util_get_error_icon(fd, ud->flist, ud->listview),
1474                            -1);
1475                 valid = gtk_tree_model_iter_next(store, &iter);
1476                 }
1477
1478 }
1479
1480 static void file_util_rename_preview_entry_cb(GtkWidget *, gpointer data)
1481 {
1482         auto ud = static_cast<UtilityData *>(data);
1483         file_util_rename_preview_update(ud);
1484 }
1485
1486 static void file_util_rename_preview_adj_cb(GtkWidget *, gpointer data)
1487 {
1488         auto ud = static_cast<UtilityData *>(data);
1489         file_util_rename_preview_update(ud);
1490 }
1491
1492 static gboolean file_util_rename_idle_cb(gpointer data)
1493 {
1494         auto ud = static_cast<UtilityData *>(data);
1495
1496         file_util_rename_preview_update(ud);
1497
1498         ud->update_idle_id = 0;
1499         return G_SOURCE_REMOVE;
1500 }
1501
1502 static void file_util_rename_preview_order_cb(GtkTreeModel *, GtkTreePath *, GtkTreeIter *, gpointer data)
1503 {
1504         auto ud = static_cast<UtilityData *>(data);
1505
1506         if (ud->update_idle_id) return;
1507
1508         ud->update_idle_id = g_idle_add(file_util_rename_idle_cb, ud);
1509 }
1510
1511
1512 static gboolean file_util_preview_cb(GtkTreeSelection *, GtkTreeModel *store,
1513                                      GtkTreePath *tpath, gboolean path_currently_selected,
1514                                      gpointer data)
1515 {
1516         auto ud = static_cast<UtilityData *>(data);
1517         GtkTreeIter iter;
1518         FileData *fd = nullptr;
1519
1520         if (path_currently_selected ||
1521             !gtk_tree_model_get_iter(store, &iter, tpath)) return TRUE;
1522
1523         gtk_tree_model_get(store, &iter, UTILITY_COLUMN_FD, &fd, -1);
1524         generic_dialog_image_set(ud, fd);
1525
1526         ud->sel_fd = fd;
1527
1528         if (ud->type == UTILITY_TYPE_RENAME || ud->type == UTILITY_TYPE_COPY || ud->type == UTILITY_TYPE_MOVE)
1529                 {
1530                 const gchar *name = filename_from_path(fd->change->dest);
1531
1532                 gtk_widget_grab_focus(ud->rename_entry);
1533                 gtk_label_set_text(GTK_LABEL(ud->rename_label), fd->name);
1534                 g_signal_handlers_block_by_func(ud->rename_entry, (gpointer)(file_util_rename_preview_entry_cb), ud);
1535                 gq_gtk_entry_set_text(GTK_ENTRY(ud->rename_entry), name);
1536                 gtk_editable_select_region(GTK_EDITABLE(ud->rename_entry), 0, filename_base_length(name));
1537                 g_signal_handlers_unblock_by_func(ud->rename_entry, (gpointer)file_util_rename_preview_entry_cb, ud);
1538                 }
1539
1540         return TRUE;
1541 }
1542
1543
1544
1545 static void box_append_safe_delete_status(GenericDialog *gd)
1546 {
1547         GtkWidget *label;
1548         gchar *buf;
1549
1550         buf = file_util_safe_delete_status();
1551         label = pref_label_new(gd->vbox, buf);
1552         g_free(buf);
1553
1554         gtk_label_set_xalign(GTK_LABEL(label), 1.0);
1555         gtk_label_set_yalign(GTK_LABEL(label), 0.5);
1556         gtk_widget_set_sensitive(label, FALSE);
1557 }
1558
1559 static void file_util_details_cb(GenericDialog *, gpointer data)
1560 {
1561         auto ud = static_cast<UtilityData *>(data);
1562         if (ud->details_func && ud->sel_fd)
1563                 {
1564                 ud->details_func(ud, ud->sel_fd);
1565                 }
1566 }
1567
1568 static void file_util_dialog_init_simple_list(UtilityData *ud)
1569 {
1570         GtkWidget *box;
1571         GtkTreeSelection *selection;
1572         gchar *dir_msg;
1573
1574         const gchar *icon_name;
1575         const gchar *msg;
1576
1577         /** @FIXME use ud->stock_id */
1578         if (ud->type == UTILITY_TYPE_DELETE ||
1579             ud->type == UTILITY_TYPE_DELETE_LINK ||
1580             ud->type == UTILITY_TYPE_DELETE_FOLDER)
1581                 {
1582                 icon_name = GQ_ICON_DELETE;
1583                 msg = _("Delete");
1584                 }
1585         else
1586                 {
1587                 icon_name = GQ_ICON_OK;
1588                 msg = "OK";
1589                 }
1590
1591         ud->gd = file_util_gen_dlg(ud->messages.title, "dlg_confirm",
1592                                    ud->parent, FALSE,  file_util_cancel_cb, ud);
1593         if (ud->discard_func) generic_dialog_add_button(ud->gd, GQ_ICON_REVERT, _("Discard changes"), file_util_discard_cb, FALSE);
1594         if (ud->details_func) generic_dialog_add_button(ud->gd, GQ_ICON_DIALOG_INFO, _("File details"), file_util_details_cb, FALSE);
1595
1596         generic_dialog_add_button(ud->gd, icon_name, msg, file_util_ok_cb, TRUE);
1597
1598         if (ud->dir_fd)
1599                 {
1600                 dir_msg = g_strdup_printf("%s\n\n%s\n", ud->messages.desc_source_fd, ud->dir_fd->path);
1601                 }
1602         else
1603                 {
1604                 dir_msg = g_strdup("");
1605                 }
1606
1607         box = generic_dialog_add_message(ud->gd, GQ_ICON_DIALOG_QUESTION,
1608                                          ud->messages.question,
1609                                          dir_msg, TRUE);
1610
1611         g_free(dir_msg);
1612
1613         box = pref_group_new(box, TRUE, ud->messages.desc_flist, GTK_ORIENTATION_HORIZONTAL);
1614
1615         ud->listview = file_util_dialog_add_list(box, ud->flist, FALSE, ud->with_sidecars);
1616         if (ud->with_sidecars) file_util_dialog_add_list_column(ud->listview, _("Sidecars"), FALSE, UTILITY_COLUMN_SIDECARS);
1617
1618         if (ud->type == UTILITY_TYPE_WRITE_METADATA) file_util_dialog_add_list_column(ud->listview, _("Write to file"), FALSE, UTILITY_COLUMN_DEST_NAME);
1619
1620         selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(ud->listview));
1621         gtk_tree_selection_set_mode(selection, GTK_SELECTION_SINGLE);
1622         gtk_tree_selection_set_select_function(selection, file_util_preview_cb, ud, nullptr);
1623
1624         generic_dialog_add_image(ud->gd, box, nullptr, nullptr, FALSE, nullptr, nullptr, FALSE);
1625
1626         if (ud->type == UTILITY_TYPE_DELETE ||
1627             ud->type == UTILITY_TYPE_DELETE_LINK ||
1628             ud->type == UTILITY_TYPE_DELETE_FOLDER)
1629                 box_append_safe_delete_status(ud->gd);
1630
1631         gtk_widget_show(ud->gd->dialog);
1632
1633         file_util_dialog_list_select(ud->listview, 0);
1634 }
1635
1636 static void file_util_dialog_init_dest_folder(UtilityData *ud)
1637 {
1638         FileDialog *fdlg;
1639         GtkWidget *label;
1640         const gchar *icon_name;
1641
1642         if (ud->type == UTILITY_TYPE_COPY)
1643                 {
1644                 icon_name = GQ_ICON_COPY;
1645                 }
1646         else
1647                 {
1648                 icon_name = GQ_ICON_OK;
1649                 }
1650
1651         fdlg = file_util_file_dlg(ud->messages.title, "dlg_dest_folder", ud->parent,
1652                                   file_util_fdlg_cancel_cb, ud);
1653
1654         ud->fdlg = fdlg;
1655
1656         generic_dialog_add_message(GENERIC_DIALOG(fdlg), nullptr, ud->messages.question, nullptr, FALSE);
1657
1658         label = pref_label_new(GENERIC_DIALOG(fdlg)->vbox, _("Choose the destination folder."));
1659         gtk_label_set_xalign(GTK_LABEL(label), 0.0);
1660         gtk_label_set_yalign(GTK_LABEL(label), 0.5);
1661
1662         pref_spacer(GENERIC_DIALOG(fdlg)->vbox, 0);
1663
1664         if (ud->show_rename_button == TRUE)
1665                 {
1666                 if (options->with_rename)
1667                         {
1668                         file_dialog_add_button(fdlg, icon_name, ud->messages.title, file_util_fdlg_ok_cb, TRUE);
1669                         file_dialog_add_button(fdlg, GQ_ICON_EDIT, _("With Rename"), file_util_fdlg_rename_cb, TRUE);
1670                         }
1671                 else
1672                         {
1673                         file_dialog_add_button(fdlg, GQ_ICON_EDIT, _("With Rename"), file_util_fdlg_rename_cb, TRUE);
1674                         file_dialog_add_button(fdlg, icon_name, ud->messages.title, file_util_fdlg_ok_cb, TRUE);
1675                         }
1676                 }
1677         else
1678                 {
1679                 file_dialog_add_button(fdlg, icon_name, ud->messages.title, file_util_fdlg_ok_cb, TRUE);
1680                 }
1681
1682         file_dialog_add_path_widgets(fdlg, nullptr, ud->dest_path, "move_copy", nullptr, nullptr);
1683
1684         g_signal_connect(G_OBJECT(fdlg->entry), "changed",
1685                          G_CALLBACK(file_util_dest_folder_entry_cb), ud);
1686
1687         gtk_widget_show(GENERIC_DIALOG(fdlg)->dialog);
1688 }
1689
1690
1691 static GtkWidget *furm_simple_vlabel(GtkWidget *box, const gchar *text, gboolean expand)
1692 {
1693         GtkWidget *vbox;
1694         GtkWidget *label;
1695
1696         vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 0);
1697         gq_gtk_box_pack_start(GTK_BOX(box), vbox, expand, expand, 0);
1698         gtk_widget_show(vbox);
1699
1700         label = gtk_label_new(text);
1701         gq_gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
1702         gtk_widget_show(label);
1703
1704         return vbox;
1705 }
1706
1707
1708 static void file_util_dialog_init_source_dest(UtilityData *ud, gboolean second_image)
1709 {
1710         GtkTreeModel *store;
1711         GtkTreeSelection *selection;
1712         GtkWidget *box;
1713         GtkWidget *hbox;
1714         GtkWidget *box2;
1715         GtkWidget *table;
1716         GtkWidget *combo;
1717         GtkWidget *page;
1718         gchar *destination_message;
1719
1720         ud->gd = file_util_gen_dlg(ud->messages.title, "dlg_confirm",
1721                                    ud->parent, FALSE,  file_util_cancel_cb, ud);
1722
1723         box = generic_dialog_add_message(ud->gd, nullptr, ud->messages.question, nullptr, TRUE);
1724
1725         if (ud->discard_func) generic_dialog_add_button(ud->gd, GQ_ICON_REVERT, _("Discard changes"), file_util_discard_cb, FALSE);
1726         if (ud->details_func) generic_dialog_add_button(ud->gd, GQ_ICON_DIALOG_INFO, _("File details"), file_util_details_cb, FALSE);
1727
1728         generic_dialog_add_button(ud->gd, GQ_ICON_OK, ud->messages.title, file_util_ok_cb, TRUE);
1729
1730         if (ud->type == UTILITY_TYPE_COPY || ud->type == UTILITY_TYPE_MOVE)
1731                 {
1732                 destination_message = g_strconcat(ud->messages.desc_flist," to: ", ud->dest_path, NULL);
1733                 }
1734         else
1735                 {
1736                 destination_message = g_strdup(ud->messages.desc_flist);
1737                 }
1738
1739         box = pref_group_new(box, TRUE, destination_message, GTK_ORIENTATION_HORIZONTAL);
1740         g_free(destination_message);
1741
1742         ud->listview = file_util_dialog_add_list(box, ud->flist, FALSE, ud->with_sidecars);
1743         file_util_dialog_add_list_column(ud->listview, _("Sidecars"), FALSE, UTILITY_COLUMN_SIDECARS);
1744
1745         file_util_dialog_add_list_column(ud->listview, _("New name"), FALSE, UTILITY_COLUMN_DEST_NAME);
1746
1747         selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(ud->listview));
1748         gtk_tree_selection_set_mode(GTK_TREE_SELECTION(selection), GTK_SELECTION_SINGLE);
1749         gtk_tree_selection_set_select_function(selection, file_util_preview_cb, ud, nullptr);
1750
1751         gtk_tree_view_set_reorderable(GTK_TREE_VIEW(ud->listview), TRUE);
1752
1753         store = gtk_tree_view_get_model(GTK_TREE_VIEW(ud->listview));
1754         g_signal_connect(G_OBJECT(store), "row_changed",
1755                          G_CALLBACK(file_util_rename_preview_order_cb), ud);
1756         gtk_widget_set_size_request(ud->listview, 300, 150);
1757
1758         if (second_image)
1759                 {
1760                 generic_dialog_add_image(ud->gd, box, nullptr, "Source", TRUE, nullptr, "Destination", TRUE);
1761                 }
1762         else
1763                 {
1764                 generic_dialog_add_image(ud->gd, box, nullptr, nullptr, FALSE, nullptr, nullptr, FALSE);
1765                 }
1766
1767         gtk_widget_show(ud->gd->dialog);
1768
1769
1770         ud->notebook = gtk_notebook_new();
1771
1772         gq_gtk_box_pack_start(GTK_BOX(ud->gd->vbox), ud->notebook, FALSE, FALSE, 0);
1773         gtk_widget_show(ud->notebook);
1774
1775
1776         page = gtk_box_new(GTK_ORIENTATION_VERTICAL, PREF_PAD_GAP);
1777         gtk_notebook_append_page(GTK_NOTEBOOK(ud->notebook), page, gtk_label_new(_("Manual rename")));
1778         gtk_widget_show(page);
1779
1780         table = pref_table_new(page, 2, 2, FALSE, FALSE);
1781
1782         pref_table_label(table, 0, 0, _("Original name:"), GTK_ALIGN_END);
1783         ud->rename_label = pref_table_label(table, 1, 0, "", GTK_ALIGN_START);
1784
1785         pref_table_label(table, 0, 1, _("New name:"), GTK_ALIGN_END);
1786
1787         ud->rename_entry = gtk_entry_new();
1788         gq_gtk_grid_attach(GTK_GRID(table), ud->rename_entry, 1, 2, 1, 2, static_cast<GtkAttachOptions>(GTK_EXPAND | GTK_FILL), static_cast<GtkAttachOptions>(0), 0, 0);
1789         generic_dialog_attach_default(GENERIC_DIALOG(ud->gd), ud->rename_entry);
1790         gtk_widget_grab_focus(ud->rename_entry);
1791
1792         g_signal_connect(G_OBJECT(ud->rename_entry), "changed",
1793                          G_CALLBACK(file_util_rename_preview_entry_cb), ud);
1794
1795         gtk_widget_show(ud->rename_entry);
1796
1797         page = gtk_box_new(GTK_ORIENTATION_VERTICAL, PREF_PAD_GAP);
1798         gtk_notebook_append_page(GTK_NOTEBOOK(ud->notebook), page, gtk_label_new(_("Auto rename")));
1799         gtk_widget_show(page);
1800
1801
1802         hbox = pref_box_new(page, FALSE, GTK_ORIENTATION_HORIZONTAL, PREF_PAD_GAP);
1803
1804         box2 = furm_simple_vlabel(hbox, _("Begin text"), TRUE);
1805
1806         combo = history_combo_new(&ud->auto_entry_front, "", "numerical_rename_prefix", -1);
1807         g_signal_connect(G_OBJECT(ud->auto_entry_front), "changed",
1808                          G_CALLBACK(file_util_rename_preview_entry_cb), ud);
1809         gq_gtk_box_pack_start(GTK_BOX(box2), combo, TRUE, TRUE, 0);
1810         gtk_widget_show(combo);
1811
1812         box2 = furm_simple_vlabel(hbox, _("Start #"), FALSE);
1813
1814         ud->auto_spin_start = pref_spin_new(box2, nullptr, nullptr,
1815                                             0.0, 1000000.0, 1.0, 0, options->cp_mv_rn.auto_start,
1816                                             G_CALLBACK(file_util_rename_preview_adj_cb), ud);
1817
1818         box2 = furm_simple_vlabel(hbox, _("End text"), TRUE);
1819
1820         combo = history_combo_new(&ud->auto_entry_end, options->cp_mv_rn.auto_end, "numerical_rename_suffix", -1);
1821         g_signal_connect(G_OBJECT(ud->auto_entry_end), "changed",
1822                          G_CALLBACK(file_util_rename_preview_entry_cb), ud);
1823         gq_gtk_box_pack_start(GTK_BOX(box2), combo, TRUE, TRUE, 0);
1824         gtk_widget_show(combo);
1825
1826         ud->auto_spin_pad = pref_spin_new(page, _("Padding:"), nullptr,
1827                                           1.0, 8.0, 1.0, 0, options->cp_mv_rn.auto_padding,
1828                                           G_CALLBACK(file_util_rename_preview_adj_cb), ud);
1829
1830         page = gtk_box_new(GTK_ORIENTATION_VERTICAL, PREF_PAD_GAP);
1831         gtk_notebook_append_page(GTK_NOTEBOOK(ud->notebook), page, gtk_label_new(_("Formatted rename")));
1832         gtk_widget_show(page);
1833
1834         hbox = pref_box_new(page, FALSE, GTK_ORIENTATION_HORIZONTAL, PREF_PAD_GAP);
1835
1836         box2 = furm_simple_vlabel(hbox, _("Format (* = original name, ## = numbers)"), TRUE);
1837
1838         combo = history_combo_new(&ud->format_entry, "", "auto_rename_format", -1);
1839         g_signal_connect(G_OBJECT(ud->format_entry), "changed",
1840                          G_CALLBACK(file_util_rename_preview_entry_cb), ud);
1841         gq_gtk_box_pack_start(GTK_BOX(box2), combo, TRUE, TRUE, 0);
1842         gtk_widget_show(combo);
1843
1844         box2 = furm_simple_vlabel(hbox, _("Start #"), FALSE);
1845
1846         ud->format_spin = pref_spin_new(box2, nullptr, nullptr,
1847                                         0.0, 1000000.0, 1.0, 0, options->cp_mv_rn.formatted_start,
1848                                         G_CALLBACK(file_util_rename_preview_adj_cb), ud);
1849
1850         file_util_dialog_list_select(ud->listview, 0);
1851 }
1852
1853 static void file_util_finalize_all(UtilityData *ud)
1854 {
1855         GList *work = ud->flist;
1856
1857         if (ud->phase == UTILITY_PHASE_CANCEL) return;
1858         if (ud->phase == UTILITY_PHASE_DONE && !ud->finalize_func) return;
1859         if (ud->phase == UTILITY_PHASE_DISCARD && !ud->discard_func) return;
1860
1861         while (work)
1862                 {
1863                 auto fd = static_cast<FileData *>(work->data);
1864                 work = work->next;
1865                 if (ud->phase == UTILITY_PHASE_DONE) ud->finalize_func(fd);
1866                 else if (ud->phase == UTILITY_PHASE_DISCARD) ud->discard_func(fd);
1867                 }
1868 }
1869
1870 static gboolean file_util_exclude_fd(UtilityData *ud, FileData *fd)
1871 {
1872         GtkTreeModel *store;
1873         GtkTreeIter iter;
1874         gboolean valid;
1875
1876         if (!g_list_find(ud->flist, fd)) return FALSE;
1877
1878         store = gtk_tree_view_get_model(GTK_TREE_VIEW(ud->listview));
1879         valid = gtk_tree_model_get_iter_first(store, &iter);
1880         while (valid)
1881                 {
1882                 FileData *store_fd;
1883                 gtk_tree_model_get(store, &iter, UTILITY_COLUMN_FD, &store_fd, -1);
1884
1885                 if (store_fd == fd)
1886                         {
1887                         gtk_list_store_remove(GTK_LIST_STORE(store), &iter);
1888                         break;
1889                         }
1890                 valid = gtk_tree_model_iter_next(store, &iter);
1891                 }
1892
1893         ud->flist = g_list_remove(ud->flist, fd);
1894
1895         if (ud->with_sidecars)
1896                 file_data_sc_free_ci(fd);
1897         else
1898                 file_data_free_ci(fd);
1899
1900         file_data_unref(fd);
1901         return TRUE;
1902 }
1903
1904 void file_util_dialog_run(UtilityData *ud)
1905 {
1906         switch (ud->phase)
1907                 {
1908                 case UTILITY_PHASE_START:
1909                         /* create the dialogs */
1910                         switch (ud->type)
1911                                 {
1912                                 case UTILITY_TYPE_DELETE:
1913                                 case UTILITY_TYPE_DELETE_LINK:
1914                                 case UTILITY_TYPE_DELETE_FOLDER:
1915                                 case UTILITY_TYPE_EDITOR:
1916                                 case UTILITY_TYPE_WRITE_METADATA:
1917                                         file_util_dialog_init_simple_list(ud);
1918                                         ud->phase = UTILITY_PHASE_ENTERING;
1919                                         break;
1920                                 case UTILITY_TYPE_RENAME:
1921                                         file_util_dialog_init_source_dest(ud, TRUE);
1922                                         ud->phase = UTILITY_PHASE_ENTERING;
1923                                         break;
1924                                 case UTILITY_TYPE_COPY:
1925                                 case UTILITY_TYPE_MOVE:
1926                                         file_util_dialog_init_dest_folder(ud);
1927                                         ud->phase = UTILITY_PHASE_INTERMEDIATE;
1928                                         break;
1929                                 case UTILITY_TYPE_FILTER:
1930                                 case UTILITY_TYPE_CREATE_FOLDER:
1931                                         file_util_dialog_init_dest_folder(ud);
1932                                         ud->phase = UTILITY_PHASE_ENTERING;
1933                                         break;
1934                                 case UTILITY_TYPE_RENAME_FOLDER:
1935                                         ud->phase = UTILITY_PHASE_CANCEL; /**< @FIXME not handled for now */
1936                                         file_util_dialog_run(ud);
1937                                         return;
1938                                 }
1939                         break;
1940                 case UTILITY_PHASE_INTERMEDIATE:
1941                         switch (ud->type)
1942                                 {
1943                                 case UTILITY_TYPE_COPY:
1944                                 case UTILITY_TYPE_MOVE:
1945                                         file_util_dialog_init_source_dest(ud, TRUE);
1946                                         break;
1947                                 default:;
1948                                 }
1949                         ud->phase = UTILITY_PHASE_ENTERING;
1950                         break;
1951                 case UTILITY_PHASE_ENTERING:
1952                         file_util_check_ci(ud);
1953                         break;
1954                 case UTILITY_PHASE_CHECKED:
1955                         file_util_perform_ci(ud);
1956                         break;
1957                 case UTILITY_PHASE_CANCEL:
1958                 case UTILITY_PHASE_DONE:
1959                 case UTILITY_PHASE_DISCARD:
1960
1961                         file_util_finalize_all(ud);
1962
1963                         /* both DISCARD and DONE finishes the operation for good */
1964                         if (ud->done_func)
1965                                 ud->done_func((ud->phase != UTILITY_PHASE_CANCEL), ud->dest_path, ud->done_data);
1966
1967                         if (ud->with_sidecars)
1968                                 file_data_sc_free_ci_list(ud->flist);
1969                         else
1970                                 file_data_free_ci_list(ud->flist);
1971
1972                         /* directory content is always handled including sidecars */
1973                         file_data_sc_free_ci_list(ud->content_list);
1974
1975                         if (ud->dir_fd) file_data_free_ci(ud->dir_fd);
1976                         file_util_data_free(ud);
1977                         break;
1978                 }
1979 }
1980
1981
1982
1983
1984 static void file_util_warn_op_in_progress(const gchar *title)
1985 {
1986         file_util_warning_dialog(title, _("Another operation in progress.\n"), GQ_ICON_DIALOG_ERROR, nullptr);
1987 }
1988
1989 static void file_util_details_dialog_close_cb(GtkWidget *, gpointer data)
1990 {
1991         g_object_unref(GTK_WIDGET(data));
1992
1993 }
1994
1995 static void file_util_details_dialog_destroy_cb(GtkWidget *widget, gpointer data)
1996 {
1997         auto ud = static_cast<UtilityData *>(data);
1998         g_signal_handlers_disconnect_by_func(ud->gd->dialog, (gpointer)(file_util_details_dialog_close_cb), widget);
1999 }
2000
2001
2002 static void file_util_details_dialog_ok_cb(GenericDialog *, gpointer)
2003 {
2004         /* no op */
2005 }
2006
2007 static void file_util_details_dialog_exclude(GenericDialog *gd, gpointer data, gboolean discard)
2008 {
2009         auto ud = static_cast<UtilityData *>(data);
2010         auto fd = static_cast<FileData *>(g_object_get_data(G_OBJECT(gd->dialog), "file_data"));
2011
2012         if (!fd) return;
2013         file_util_exclude_fd(ud, fd);
2014
2015         if (discard && ud->discard_func) ud->discard_func(fd);
2016
2017         /* all files were excluded, this has the same effect as pressing the cancel button in the confirmation dialog*/
2018         if (!ud->flist)
2019                 {
2020                 /* both dialogs will be closed anyway, the signals would cause duplicate calls */
2021                 g_signal_handlers_disconnect_by_func(ud->gd->dialog, (gpointer)(file_util_details_dialog_close_cb), gd->dialog);
2022                 g_signal_handlers_disconnect_by_func(gd->dialog, (gpointer)(file_util_details_dialog_destroy_cb), ud);
2023
2024                 file_util_cancel_cb(ud->gd, ud);
2025                 }
2026 }
2027
2028 static void file_util_details_dialog_exclude_cb(GenericDialog *gd, gpointer data)
2029 {
2030         file_util_details_dialog_exclude(gd, data, FALSE);
2031 }
2032
2033 static void file_util_details_dialog_discard_cb(GenericDialog *gd, gpointer data)
2034 {
2035         file_util_details_dialog_exclude(gd, data, TRUE);
2036 }
2037
2038 static gchar *file_util_details_get_message(UtilityData *ud, FileData *fd, const gchar **icon_name)
2039 {
2040         GString *message = g_string_new("");
2041         gint error;
2042         g_string_append_printf(message, _("File: '%s'\n"), fd->path);
2043
2044         if (ud->with_sidecars && fd->sidecar_files)
2045                 {
2046                 GList *work = fd->sidecar_files;
2047                 g_string_append(message, _("with sidecar files:\n"));
2048
2049                 while (work)
2050                         {
2051                         auto sfd = static_cast<FileData *>(work->data);
2052                         work =work->next;
2053                         g_string_append_printf(message, _(" '%s'\n"), sfd->path);
2054                         }
2055                 }
2056
2057         g_string_append(message, _("\nStatus: "));
2058
2059         error = ud->with_sidecars ? file_data_sc_verify_ci(fd, ud->flist) : file_data_verify_ci(fd, ud->flist);
2060
2061         if (error)
2062                 {
2063                 gchar *err_msg = file_data_get_error_string(error);
2064                 g_string_append(message, err_msg);
2065                 if (icon_name) *icon_name = (error & CHANGE_ERROR_MASK) ? GQ_ICON_DIALOG_ERROR : GQ_ICON_DIALOG_WARNING;
2066                 g_free(err_msg);
2067                 }
2068         else
2069                 {
2070                 g_string_append(message, _("no problem detected"));
2071                 if (icon_name) *icon_name = GQ_ICON_DIALOG_INFO;
2072                 }
2073
2074         return g_string_free(message, FALSE);;
2075 }
2076
2077 static void file_util_details_dialog(UtilityData *ud, FileData *fd)
2078 {
2079         GenericDialog *gd;
2080         GtkWidget *box;
2081         gchar *message;
2082         const gchar *icon_name;
2083
2084         gd = file_util_gen_dlg(_("File details"), "details", ud->gd->dialog, TRUE, nullptr, ud);
2085         generic_dialog_add_button(gd, GQ_ICON_CLOSE, _("Close"), file_util_details_dialog_ok_cb, TRUE);
2086         generic_dialog_add_button(gd, GQ_ICON_REMOVE, _("Exclude file"), file_util_details_dialog_exclude_cb, FALSE);
2087
2088         g_object_set_data(G_OBJECT(gd->dialog), "file_data", fd);
2089
2090         g_signal_connect(G_OBJECT(gd->dialog), "destroy",
2091                          G_CALLBACK(file_util_details_dialog_destroy_cb), ud);
2092
2093         /* in case the ud->gd->dialog is closed during editing */
2094         g_signal_connect(G_OBJECT(ud->gd->dialog), "destroy",
2095                          G_CALLBACK(file_util_details_dialog_close_cb), gd->dialog);
2096
2097
2098         message = file_util_details_get_message(ud, fd, &icon_name);
2099
2100         box = generic_dialog_add_message(gd, icon_name, _("File details"), message, TRUE);
2101
2102         generic_dialog_add_image(gd, box, fd, nullptr, FALSE, nullptr, nullptr, FALSE);
2103
2104         gtk_widget_show(gd->dialog);
2105
2106         g_free(message);
2107 }
2108
2109 static void file_util_write_metadata_details_dialog(UtilityData *ud, FileData *fd)
2110 {
2111         GenericDialog *gd;
2112         GtkWidget *box;
2113         GtkWidget *table;
2114         GtkWidget *frame;
2115         GtkWidget *label;
2116         GList *keys = nullptr;
2117         GList *work;
2118         gchar *message1;
2119         gchar *message2;
2120         gint i;
2121         const gchar *icon_name;
2122
2123         if (fd && fd->modified_xmp)
2124                 {
2125                 keys = g_hash_table_get_keys(fd->modified_xmp);
2126                 }
2127
2128         g_assert(keys);
2129
2130
2131         gd = file_util_gen_dlg(_("Overview of changed metadata"), "details", ud->gd->dialog, TRUE, nullptr, ud);
2132         generic_dialog_add_button(gd, GQ_ICON_CLOSE, _("Close"), file_util_details_dialog_ok_cb, TRUE);
2133         generic_dialog_add_button(gd, GQ_ICON_REMOVE, _("Exclude file"), file_util_details_dialog_exclude_cb, FALSE);
2134         generic_dialog_add_button(gd, GQ_ICON_REVERT, _("Discard changes"), file_util_details_dialog_discard_cb, FALSE);
2135
2136         g_object_set_data(G_OBJECT(gd->dialog), "file_data", fd);
2137
2138         g_signal_connect(G_OBJECT(gd->dialog), "destroy",
2139                          G_CALLBACK(file_util_details_dialog_destroy_cb), ud);
2140
2141         /* in case the ud->gd->dialog is closed during editing */
2142         g_signal_connect(G_OBJECT(ud->gd->dialog), "destroy",
2143                          G_CALLBACK(file_util_details_dialog_close_cb), gd->dialog);
2144
2145         message1 = file_util_details_get_message(ud, fd, &icon_name);
2146
2147         if (fd->change && fd->change->dest)
2148                 {
2149                 message2 = g_strdup_printf(_("The following metadata tags will be written to\n'%s'."), fd->change->dest);
2150                 }
2151         else
2152                 {
2153                 message2 = g_strdup_printf(_("The following metadata tags will be written to the image file itself."));
2154                 }
2155
2156         box = generic_dialog_add_message(gd, icon_name, _("Overview of changed metadata"), message1, TRUE);
2157
2158         box = pref_group_new(box, TRUE, message2, GTK_ORIENTATION_HORIZONTAL);
2159
2160         frame = pref_frame_new(box, TRUE, nullptr, GTK_ORIENTATION_HORIZONTAL, 2);
2161         table = pref_table_new(frame, 2, g_list_length(keys), FALSE, TRUE);
2162
2163         work = keys;
2164         i = 0;
2165         while (work)
2166                 {
2167                 auto key = static_cast<const gchar *>(work->data);
2168                 gchar *title = exif_get_description_by_key(key);
2169                 gchar *title_f = g_strdup_printf("%s:", title);
2170                 gchar *value = metadata_read_string(fd, key, METADATA_FORMATTED);
2171                 work = work->next;
2172
2173
2174                 label = gtk_label_new(title_f);
2175                 gtk_label_set_xalign(GTK_LABEL(label), 1.0);
2176                 gtk_label_set_yalign(GTK_LABEL(label), 0.0);
2177
2178                 pref_label_bold(label, TRUE, FALSE);
2179                 gq_gtk_grid_attach(GTK_GRID(table), label, 0, 1, i, i + 1,  GTK_FILL, GTK_FILL,  2, 2);
2180                 gtk_widget_show(label);
2181
2182                 label = gtk_label_new(value);
2183
2184                 gtk_label_set_xalign(GTK_LABEL(label), 0.0);
2185                 gtk_label_set_yalign(GTK_LABEL(label), 0.0);
2186
2187                 gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
2188                 gq_gtk_grid_attach(GTK_GRID(table), label,  1, 2, i, i + 1, GTK_FILL, GTK_FILL,  2, 2);
2189                 gtk_widget_show(label);
2190
2191                 g_free(title);
2192                 g_free(title_f);
2193                 g_free(value);
2194                 i++;
2195                 }
2196
2197         generic_dialog_add_image(gd, box, fd, nullptr, FALSE, nullptr, nullptr, FALSE);
2198
2199         gtk_widget_set_size_request(gd->dialog, DIALOG_WIDTH, -1);
2200         gtk_widget_show(gd->dialog);
2201
2202         g_list_free(keys);
2203         g_free(message1);
2204         g_free(message2);
2205 }
2206
2207
2208 static void file_util_mark_ungrouped_files(GList *work)
2209 {
2210         while (work)
2211                 {
2212                 auto fd = static_cast<FileData *>(work->data);
2213                 file_data_set_regroup_when_finished(fd, TRUE);
2214                 work = work->next;
2215                 }
2216 }
2217
2218 static void file_util_delete_full(FileData *source_fd, GList *flist, GtkWidget *parent, UtilityPhase phase, FileUtilDoneFunc done_func, gpointer done_data)
2219 {
2220         UtilityData *ud;
2221         GList *ungrouped = nullptr;
2222         gchar *message;
2223
2224         if (source_fd)
2225                 flist = g_list_append(flist, file_data_ref(source_fd));
2226
2227         if (!flist) return;
2228
2229         flist = file_data_process_groups_in_selection(flist, TRUE, &ungrouped);
2230
2231         if (!file_data_sc_add_ci_delete_list(flist))
2232                 {
2233                 file_util_warn_op_in_progress(_("File deletion failed"));
2234                 file_data_disable_grouping_list(ungrouped, FALSE);
2235                 filelist_free(flist);
2236                 filelist_free(ungrouped);
2237                 return;
2238                 }
2239
2240         file_util_mark_ungrouped_files(ungrouped);
2241         filelist_free(ungrouped);
2242
2243         ud = file_util_data_new(UTILITY_TYPE_DELETE);
2244
2245         ud->phase = phase;
2246
2247         ud->with_sidecars = TRUE;
2248
2249         ud->dir_fd = nullptr;
2250         ud->flist = flist;
2251         ud->content_list = nullptr;
2252         ud->parent = parent;
2253         ud->done_data = done_data;
2254         ud->done_func = done_func;
2255
2256         ud->details_func = file_util_details_dialog;
2257         if(options->file_ops.safe_delete_enable)
2258                 {
2259                 message = _("This will move the following files to the Trash bin");
2260                 }
2261         else
2262                 {
2263                 message = _("This will permanently delete the following files");
2264                 }
2265         ud->messages.title = _("Delete");
2266         ud->messages.question = _("Delete files?");
2267         ud->messages.desc_flist = message;
2268         ud->messages.desc_source_fd = "";
2269         ud->messages.fail = _("File deletion failed");
2270
2271         file_util_dialog_run(ud);
2272 }
2273
2274
2275 static void file_util_write_metadata_full(FileData *source_fd, GList *flist, GtkWidget *parent, UtilityPhase phase, FileUtilDoneFunc done_func, gpointer done_data)
2276 {
2277         UtilityData *ud;
2278
2279         if (source_fd)
2280                 flist = g_list_append(flist, file_data_ref(source_fd));
2281
2282         if (!flist) return;
2283
2284         if (!file_data_add_ci_write_metadata_list(flist))
2285                 {
2286                 file_util_warn_op_in_progress(_("Can't write metadata"));
2287                 filelist_free(flist);
2288                 return;
2289                 }
2290
2291         ud = file_util_data_new(UTILITY_TYPE_WRITE_METADATA);
2292
2293         ud->phase = phase;
2294
2295         ud->with_sidecars = FALSE; /* operate on individual files, not groups */
2296
2297         ud->dir_fd = nullptr;
2298         ud->flist = flist;
2299         ud->content_list = nullptr;
2300         ud->parent = parent;
2301
2302         ud->done_func = done_func;
2303         ud->done_data = done_data;
2304
2305         ud->details_func = file_util_write_metadata_details_dialog;
2306         ud->finalize_func = metadata_write_queue_remove;
2307         ud->discard_func = metadata_write_queue_remove;
2308
2309         ud->messages.title = _("Write metadata");
2310         ud->messages.question = _("Write metadata?");
2311         ud->messages.desc_flist = _("This will write the changed metadata into the following files");
2312         ud->messages.desc_source_fd = "";
2313         ud->messages.fail = _("Metadata writing failed");
2314
2315         file_util_dialog_run(ud);
2316 }
2317
2318 static void file_util_move_full(FileData *source_fd, GList *flist, const gchar *dest_path, GtkWidget *parent, UtilityPhase phase)
2319 {
2320         UtilityData *ud;
2321         GList *ungrouped = nullptr;
2322
2323         if (source_fd)
2324                 flist = g_list_append(flist, file_data_ref(source_fd));
2325
2326         if (!flist) return;
2327
2328         flist = file_data_process_groups_in_selection(flist, TRUE, &ungrouped);
2329
2330         if (!file_data_sc_add_ci_move_list(flist, dest_path))
2331                 {
2332                 file_util_warn_op_in_progress(_("Move failed"));
2333                 file_data_disable_grouping_list(ungrouped, FALSE);
2334                 filelist_free(flist);
2335                 filelist_free(ungrouped);
2336                 return;
2337                 }
2338
2339         file_util_mark_ungrouped_files(ungrouped);
2340         filelist_free(ungrouped);
2341
2342         ud = file_util_data_new(UTILITY_TYPE_MOVE);
2343
2344         ud->phase = phase;
2345
2346         ud->with_sidecars = TRUE;
2347
2348         ud->dir_fd = nullptr;
2349         ud->flist = flist;
2350         ud->content_list = nullptr;
2351         ud->parent = parent;
2352         ud->details_func = file_util_details_dialog;
2353
2354         if (dest_path) ud->dest_path = g_strdup(dest_path);
2355
2356         ud->messages.title = _("Move");
2357         ud->messages.question = _("Move files?");
2358         ud->messages.desc_flist = _("This will move the following files");
2359         ud->messages.desc_source_fd = "";
2360         ud->messages.fail = _("Move failed");
2361
2362         file_util_dialog_run(ud);
2363 }
2364
2365 static void file_util_copy_full(FileData *source_fd, GList *flist, const gchar *dest_path, GtkWidget *parent, UtilityPhase phase)
2366 {
2367         UtilityData *ud;
2368         GList *ungrouped = nullptr;
2369
2370         if (source_fd)
2371                 flist = g_list_append(flist, file_data_ref(source_fd));
2372
2373         if (!flist) return;
2374
2375         if (file_util_write_metadata_first(UTILITY_TYPE_COPY, phase, flist, dest_path, nullptr, parent))
2376                 return;
2377
2378         flist = file_data_process_groups_in_selection(flist, TRUE, &ungrouped);
2379
2380         if (!file_data_sc_add_ci_copy_list(flist, dest_path))
2381                 {
2382                 file_util_warn_op_in_progress(_("Copy failed"));
2383                 file_data_disable_grouping_list(ungrouped, FALSE);
2384                 filelist_free(flist);
2385                 filelist_free(ungrouped);
2386                 return;
2387                 }
2388
2389         file_util_mark_ungrouped_files(ungrouped);
2390         filelist_free(ungrouped);
2391
2392         ud = file_util_data_new(UTILITY_TYPE_COPY);
2393
2394         ud->phase = phase;
2395
2396         ud->with_sidecars = TRUE;
2397
2398         ud->dir_fd = nullptr;
2399         ud->flist = flist;
2400         ud->content_list = nullptr;
2401         ud->parent = parent;
2402         ud->details_func = file_util_details_dialog;
2403
2404         if (dest_path) ud->dest_path = g_strdup(dest_path);
2405
2406         ud->messages.title = _("Copy");
2407         ud->messages.question = _("Copy files?");
2408         ud->messages.desc_flist = _("This will copy the following files");
2409         ud->messages.desc_source_fd = "";
2410         ud->messages.fail = _("Copy failed");
2411
2412         file_util_dialog_run(ud);
2413 }
2414
2415 static void file_util_rename_full(FileData *source_fd, GList *flist, const gchar *dest_path, GtkWidget *parent, UtilityPhase phase)
2416 {
2417         UtilityData *ud;
2418         GList *ungrouped = nullptr;
2419
2420         if (source_fd)
2421                 flist = g_list_append(flist, file_data_ref(source_fd));
2422
2423         if (!flist) return;
2424
2425         flist = file_data_process_groups_in_selection(flist, TRUE, &ungrouped);
2426
2427         if (!file_data_sc_add_ci_rename_list(flist, dest_path))
2428                 {
2429                 file_util_warn_op_in_progress(_("Rename failed"));
2430                 file_data_disable_grouping_list(ungrouped, FALSE);
2431                 filelist_free(flist);
2432                 filelist_free(ungrouped);
2433                 return;
2434                 }
2435
2436         file_util_mark_ungrouped_files(ungrouped);
2437         filelist_free(ungrouped);
2438
2439         ud = file_util_data_new(UTILITY_TYPE_RENAME);
2440
2441         ud->phase = phase;
2442
2443         ud->with_sidecars = TRUE;
2444
2445         ud->dir_fd = nullptr;
2446         ud->flist = flist;
2447         ud->content_list = nullptr;
2448         ud->parent = parent;
2449
2450         ud->details_func = file_util_details_dialog;
2451
2452         ud->messages.title = _("Rename");
2453         ud->messages.question = _("Rename files?");
2454         ud->messages.desc_flist = _("This will rename the following files");
2455         ud->messages.desc_source_fd = "";
2456         ud->messages.fail = _("Rename failed");
2457
2458         file_util_dialog_run(ud);
2459 }
2460
2461 static void file_util_start_editor_full(const gchar *key, FileData *source_fd, GList *flist, const gchar *dest_path, const gchar *working_directory, GtkWidget *parent, UtilityPhase phase)
2462 {
2463         UtilityData *ud;
2464         GList *ungrouped = nullptr;
2465
2466         if (editor_no_param(key))
2467                 {
2468                 gchar *file_directory = nullptr;
2469                 if (!working_directory)
2470                         {
2471                         /* working directory was not specified, try to extract it from the files */
2472                         if (source_fd)
2473                                 file_directory = remove_level_from_path(source_fd->path);
2474
2475                         if (!file_directory && flist)
2476                                 file_directory = remove_level_from_path((static_cast<FileData *>(flist->data))->path);
2477                         working_directory = file_directory;
2478                         }
2479
2480                 /* just start the editor, don't care about files */
2481                 start_editor(key, working_directory);
2482                 g_free(file_directory);
2483                 filelist_free(flist);
2484                 return;
2485                 }
2486
2487
2488         if (source_fd)
2489                 {
2490                 /* flist is most probably NULL
2491                    operate on source_fd and it's sidecars
2492                 */
2493                 flist = g_list_concat(filelist_copy(source_fd->sidecar_files), flist);
2494                 flist = g_list_append(flist, file_data_ref(source_fd));
2495                 }
2496
2497         if (!flist) return;
2498
2499         if (file_util_write_metadata_first(UTILITY_TYPE_FILTER, phase, flist, dest_path, key, parent))
2500                 return;
2501
2502         flist = file_data_process_groups_in_selection(flist, TRUE, &ungrouped);
2503
2504         if (!file_data_sc_add_ci_unspecified_list(flist, dest_path))
2505                 {
2506                 file_util_warn_op_in_progress(_("Can't run external editor"));
2507                 file_data_disable_grouping_list(ungrouped, FALSE);
2508                 filelist_free(flist);
2509                 filelist_free(ungrouped);
2510                 return;
2511                 }
2512
2513         file_util_mark_ungrouped_files(ungrouped);
2514         filelist_free(ungrouped);
2515
2516         if (editor_is_filter(key))
2517                 ud = file_util_data_new(UTILITY_TYPE_FILTER);
2518         else
2519                 ud = file_util_data_new(UTILITY_TYPE_EDITOR);
2520
2521
2522         /* ask for destination if we don't have it */
2523         if (ud->type == UTILITY_TYPE_FILTER && dest_path == nullptr) phase = UTILITY_PHASE_START;
2524
2525         ud->phase = phase;
2526
2527         ud->with_sidecars = TRUE;
2528
2529         ud->external_command = g_strdup(key);
2530
2531         ud->dir_fd = nullptr;
2532         ud->flist = flist;
2533         ud->content_list = nullptr;
2534         ud->parent = parent;
2535
2536         ud->details_func = file_util_details_dialog;
2537
2538         if (dest_path) ud->dest_path = g_strdup(dest_path);
2539
2540         ud->messages.title = _("Editor");
2541         ud->messages.question = _("Run editor?");
2542         ud->messages.desc_flist = _("This will copy the following files");
2543         ud->messages.desc_source_fd = "";
2544         ud->messages.fail = _("External command failed");
2545
2546         file_util_dialog_run(ud);
2547 }
2548
2549 static GList *file_util_delete_dir_remaining_folders(GList *dlist)
2550 {
2551         GList *rlist = nullptr;
2552
2553         while (dlist)
2554                 {
2555                 FileData *fd;
2556
2557                 fd = static_cast<FileData *>(dlist->data);
2558                 dlist = dlist->next;
2559
2560                 if (!fd->name ||
2561                     (strcmp(fd->name, THUMB_FOLDER_GLOBAL) != 0 &&
2562                      strcmp(fd->name, THUMB_FOLDER_LOCAL) != 0 &&
2563                      strcmp(fd->name, GQ_CACHE_LOCAL_METADATA) != 0) )
2564                         {
2565                         rlist = g_list_prepend(rlist, fd);
2566                         }
2567                 }
2568
2569         return g_list_reverse(rlist);
2570 }
2571
2572 static gboolean file_util_delete_dir_empty_path(UtilityData *ud, FileData *fd, gint level)
2573 {
2574         GList *dlist;
2575         GList *flist;
2576         GList *work;
2577
2578         gboolean ok = TRUE;
2579
2580         DEBUG_1("deltree into: %s", fd->path);
2581
2582         level++;
2583         if (level > UTILITY_DELETE_MAX_DEPTH)
2584                 {
2585                 log_printf("folder recursion depth past %d, giving up\n", UTILITY_DELETE_MAX_DEPTH);
2586                 // ud->fail_fd = fd
2587                 return 0;
2588                 }
2589
2590         if (!filelist_read_lstat(fd, &flist, &dlist))
2591                 {
2592                 // ud->fail_fd = fd
2593                 return 0;
2594                 }
2595
2596         if (ok)
2597                 {
2598                 ok = file_data_sc_add_ci_delete(fd);
2599                 if (ok)
2600                         {
2601                         ud->content_list = g_list_prepend(ud->content_list, fd);
2602                         }
2603                 // ud->fail_fd = fd
2604                 }
2605
2606         work = dlist;
2607         while (work && ok)
2608                 {
2609                 FileData *lfd;
2610
2611                 lfd = static_cast<FileData *>(work->data);
2612                 work = work->next;
2613
2614                 ok = file_util_delete_dir_empty_path(ud, lfd, level);
2615                 }
2616
2617         work = flist;
2618         while (work && ok)
2619                 {
2620                 FileData *lfd;
2621
2622                 lfd = static_cast<FileData *>(work->data);
2623                 work = work->next;
2624
2625                 DEBUG_1("deltree child: %s", lfd->path);
2626
2627                 ok = file_data_sc_add_ci_delete(lfd);
2628                 if (ok)
2629                         {
2630                         ud->content_list = g_list_prepend(ud->content_list, lfd);
2631                         }
2632                 // ud->fail_fd = fd
2633                 }
2634
2635         filelist_free(dlist);
2636         filelist_free(flist);
2637
2638
2639         DEBUG_1("deltree done: %s", fd->path);
2640
2641         return ok;
2642 }
2643
2644 static gboolean file_util_delete_dir_prepare(UtilityData *ud, GList *flist, GList *dlist)
2645 {
2646         gboolean ok = TRUE;
2647         GList *work;
2648
2649
2650         work = dlist;
2651         while (work && ok)
2652                 {
2653                 FileData *fd;
2654
2655                 fd = static_cast<FileData *>(work->data);
2656                 work = work->next;
2657
2658                 ok = file_util_delete_dir_empty_path(ud, fd, 0);
2659                 }
2660
2661         work = flist;
2662         if (ok && file_data_sc_add_ci_delete_list(flist))
2663                 {
2664                 ud->content_list = g_list_concat(filelist_copy(flist), ud->content_list);
2665                 }
2666         else
2667                 {
2668                 ok = FALSE;
2669                 }
2670
2671         if (ok)
2672                 {
2673                 ok = file_data_sc_add_ci_delete(ud->dir_fd);
2674                 }
2675
2676         if (!ok)
2677                 {
2678                 work = ud->content_list;
2679                 while (work)
2680                         {
2681                         FileData *fd;
2682
2683                         fd = static_cast<FileData *>(work->data);
2684                         work = work->next;
2685                         file_data_sc_free_ci(fd);
2686                         }
2687                 }
2688
2689         return ok;
2690 }
2691
2692 static void file_util_delete_dir_full(FileData *fd, GtkWidget *parent, UtilityPhase phase)
2693 {
2694         GList *dlist;
2695         GList *flist;
2696         GList *rlist;
2697
2698         if (!isdir(fd->path)) return;
2699
2700         if (islink(fd->path))
2701                 {
2702                 UtilityData *ud;
2703                 ud = file_util_data_new(UTILITY_TYPE_DELETE_LINK);
2704
2705                 ud->phase = phase;
2706                 ud->with_sidecars = TRUE;
2707                 ud->dir_fd = file_data_ref(fd);
2708                 ud->content_list = nullptr;
2709                 ud->flist = nullptr;
2710
2711                 ud->parent = parent;
2712
2713                 ud->messages.title = _("Delete folder");
2714                 ud->messages.question = _("Delete symbolic link?");
2715                 ud->messages.desc_flist = "";
2716                 ud->messages.desc_source_fd = _("This will delete the symbolic link.\n"
2717                                                 "The folder this link points to will not be deleted.");
2718                 ud->messages.fail = _("Link deletion failed");
2719
2720                 file_util_dialog_run(ud);
2721                 return;
2722                 }
2723
2724         if (!access_file(fd->path, W_OK | X_OK))
2725                 {
2726                 gchar *text;
2727
2728                 text = g_strdup_printf(_("Unable to remove folder %s\n"
2729                                          "Permissions do not allow writing to the folder."), fd->path);
2730                 file_util_warning_dialog(_("Delete failed"), text, GQ_ICON_DIALOG_ERROR, parent);
2731                 g_free(text);
2732
2733                 return;
2734                 }
2735
2736         if (!filelist_read_lstat(fd, &flist, &dlist))
2737                 {
2738                 gchar *text;
2739
2740                 text = g_strdup_printf(_("Unable to list contents of folder %s"), fd->path);
2741                 file_util_warning_dialog(_("Delete failed"), text, GQ_ICON_DIALOG_ERROR, parent);
2742                 g_free(text);
2743
2744                 return;
2745                 }
2746
2747         rlist = file_util_delete_dir_remaining_folders(dlist);
2748         if (rlist)
2749                 {
2750                 GenericDialog *gd;
2751                 GtkWidget *box;
2752                 gchar *text;
2753
2754                 gd = file_util_gen_dlg(_("Folder contains subfolders"), "dlg_warning",
2755                                         parent, TRUE, nullptr, nullptr);
2756                 generic_dialog_add_button(gd, GQ_ICON_CLOSE, _("Close"), nullptr, TRUE);
2757
2758                 text = g_strdup_printf(_("Unable to delete the folder:\n\n%s\n\n"
2759                                          "This folder contains subfolders which must be moved before it can be deleted."),
2760                                         fd->path);
2761                 box = generic_dialog_add_message(gd, GQ_ICON_DIALOG_WARNING,
2762                                                  _("Folder contains subfolders"),
2763                                                  text, TRUE);
2764                 g_free(text);
2765
2766                 box = pref_group_new(box, TRUE, _("Subfolders:"), GTK_ORIENTATION_VERTICAL);
2767
2768                 rlist = filelist_sort_path(rlist);
2769                 file_util_dialog_add_list(box, rlist, FALSE, FALSE);
2770
2771                 gtk_widget_show(gd->dialog);
2772                 }
2773         else
2774                 {
2775                 UtilityData *ud;
2776                 ud = file_util_data_new(UTILITY_TYPE_DELETE_FOLDER);
2777
2778                 ud->phase = phase;
2779                 ud->with_sidecars = TRUE;
2780                 ud->dir_fd = file_data_ref(fd);
2781                 ud->content_list = nullptr; /* will be filled by file_util_delete_dir_prepare */
2782                 ud->flist = flist = filelist_sort_path(flist);
2783
2784                 ud->parent = parent;
2785
2786                 ud->messages.title = _("Delete folder");
2787                 ud->messages.question = _("Delete folder?");
2788                 ud->messages.desc_flist = _("The folder contains these files:");
2789                 ud->messages.desc_source_fd = _("This will delete the folder.\n"
2790                                                 "The contents of this folder will also be deleted.");
2791                 ud->messages.fail = _("File deletion failed");
2792
2793                 if (!file_util_delete_dir_prepare(ud, flist, dlist))
2794                         {
2795                         gchar *text;
2796
2797                         text = g_strdup_printf(_("Unable to list contents of folder %s"), fd->path);
2798                         file_util_warning_dialog(_("Delete failed"), text, GQ_ICON_DIALOG_ERROR, parent);
2799                         g_free(text);
2800                         file_data_unref(ud->dir_fd);
2801                         file_util_data_free(ud);
2802                         }
2803                 else
2804                         {
2805                         filelist_free(dlist);
2806                         file_util_dialog_run(ud);
2807                         return;
2808                         }
2809                 }
2810
2811         g_list_free(rlist);
2812         filelist_free(dlist);
2813         filelist_free(flist);
2814 }
2815
2816 static gboolean file_util_rename_dir_scan(UtilityData *ud, FileData *fd)
2817 {
2818         GList *dlist;
2819         GList *flist;
2820         GList *work;
2821
2822         gboolean ok = TRUE;
2823
2824         if (!filelist_read_lstat(fd, &flist, &dlist))
2825                 {
2826                 // ud->fail_fd = fd
2827                 return 0;
2828                 }
2829
2830         ud->content_list = g_list_concat(flist, ud->content_list);
2831
2832         work = dlist;
2833         while (work && ok)
2834                 {
2835                 FileData *lfd;
2836
2837                 lfd = static_cast<FileData *>(work->data);
2838                 work = work->next;
2839
2840                 ud->content_list = g_list_prepend(ud->content_list, file_data_ref(lfd));
2841                 ok = file_util_rename_dir_scan(ud, lfd);
2842                 }
2843
2844         filelist_free(dlist);
2845
2846         return ok;
2847 }
2848
2849 static gboolean file_util_rename_dir_prepare(UtilityData *ud, const gchar *new_path)
2850 {
2851         gboolean ok;
2852         GList *work;
2853         gint orig_len = strlen(ud->dir_fd->path);
2854
2855         ok = file_util_rename_dir_scan(ud, ud->dir_fd);
2856
2857         work = ud->content_list;
2858
2859         while (ok && work)
2860                 {
2861                 gchar *np;
2862                 FileData *fd;
2863
2864                 fd = static_cast<FileData *>(work->data);
2865                 work = work->next;
2866
2867                 g_assert(strncmp(fd->path, ud->dir_fd->path, orig_len) == 0);
2868
2869                 np = g_strconcat(new_path, fd->path + orig_len, NULL);
2870
2871                 ok = file_data_sc_add_ci_rename(fd, np);
2872
2873                 DEBUG_1("Dir rename: %s -> %s", fd->path, np);
2874                 g_free(np);
2875                 }
2876
2877         if (ok)
2878                 {
2879                 ok = file_data_sc_add_ci_rename(ud->dir_fd, new_path);
2880                 }
2881
2882         if (!ok)
2883                 {
2884                 work = ud->content_list;
2885                 while (work)
2886                         {
2887                         FileData *fd;
2888
2889                         fd = static_cast<FileData *>(work->data);
2890                         work = work->next;
2891                         file_data_sc_free_ci(fd);
2892                         }
2893                 }
2894
2895         return ok;
2896 }
2897
2898
2899 static void file_util_rename_dir_full(FileData *fd, const gchar *new_path, GtkWidget *parent, UtilityPhase phase, FileUtilDoneFunc done_func, gpointer done_data)
2900 {
2901         UtilityData *ud;
2902
2903         ud = file_util_data_new(UTILITY_TYPE_RENAME_FOLDER);
2904
2905         ud->phase = phase;
2906         ud->with_sidecars = TRUE; /* does not matter, the directory should not have sidecars
2907                                     and the content must be handled including sidecars */
2908
2909         ud->dir_fd = file_data_ref(fd);
2910         ud->flist = nullptr;
2911         ud->content_list = nullptr;
2912         ud->parent = parent;
2913
2914         ud->done_func = done_func;
2915         ud->done_data = done_data;
2916         ud->dest_path = g_strdup(new_path);
2917
2918         ud->messages.title = _("Rename");
2919         ud->messages.question = _("Rename folder?");
2920         ud->messages.desc_flist = _("The folder contains the following files");
2921         ud->messages.desc_source_fd = "";
2922         ud->messages.fail = _("Rename failed");
2923
2924         if (!file_util_rename_dir_prepare(ud, new_path))
2925                 {
2926                 file_util_warn_op_in_progress(ud->messages.fail);
2927                 file_util_data_free(ud);
2928                 return;
2929                 }
2930
2931         file_util_dialog_run(ud);
2932 }
2933
2934 static void file_util_create_dir_full(const gchar *path, const gchar *dest_path, GtkWidget *parent, UtilityPhase phase, FileUtilDoneFunc done_func, gpointer done_data)
2935 {
2936         UtilityData *ud;
2937
2938         ud = file_util_data_new(UTILITY_TYPE_CREATE_FOLDER);
2939
2940         ud->phase = phase;
2941         ud->with_sidecars = TRUE;
2942
2943         ud->dir_fd = nullptr;
2944         ud->flist = nullptr;
2945         ud->content_list = nullptr;
2946         ud->parent = parent;
2947
2948         if (dest_path)
2949                 {
2950                 g_assert_not_reached(); // not used in current design
2951                 ud->dest_path = g_strdup(dest_path);
2952                 }
2953         else
2954                 {
2955                 gchar *buf = g_build_filename(path, _("New folder"), nullptr);
2956                 ud->dest_path = unique_filename(buf, nullptr, " ", FALSE);
2957                 g_free(buf);
2958                 }
2959
2960         ud->done_func = done_func;
2961         ud->done_data = done_data;
2962
2963         ud->messages.title = _("Create Folder");
2964         ud->messages.question = _("Create folder?");
2965         ud->messages.desc_flist = "";
2966         ud->messages.desc_source_fd = "";
2967         ud->messages.fail = _("Can't create folder");
2968
2969         file_util_dialog_run(ud);
2970 }
2971
2972 static gboolean file_util_write_metadata_first_after_done(gpointer data)
2973 {
2974         auto dd = static_cast<UtilityDelayData *>(data);
2975
2976         /* start the delayed operation with original arguments */
2977         switch (dd->type)
2978                 {
2979                 case UTILITY_TYPE_FILTER:
2980                 case UTILITY_TYPE_EDITOR:
2981                         file_util_start_editor_full(dd->editor_key, nullptr, dd->flist, dd->dest_path, nullptr, dd->parent, dd->phase);
2982                         break;
2983                 case UTILITY_TYPE_COPY:
2984                         file_util_copy_full(nullptr, dd->flist, dd->dest_path, dd->parent, dd->phase);
2985                         break;
2986                 default:
2987                         g_warning("unsupported type");
2988                 }
2989         g_free(dd->dest_path);
2990         g_free(dd->editor_key);
2991         g_free(dd);
2992         return G_SOURCE_REMOVE;
2993 }
2994
2995 static void file_util_write_metadata_first_done(gboolean success, const gchar *, gpointer data)
2996 {
2997         auto dd = static_cast<UtilityDelayData *>(data);
2998
2999         if (success)
3000                 {
3001                 dd->idle_id = g_idle_add(file_util_write_metadata_first_after_done, dd);
3002                 return;
3003                 }
3004
3005         /* the operation was cancelled */
3006         filelist_free(dd->flist);
3007         g_free(dd->dest_path);
3008         g_free(dd->editor_key);
3009         g_free(dd);
3010 }
3011
3012 static gboolean file_util_write_metadata_first(UtilityType type, UtilityPhase phase, GList *flist, const gchar *dest_path, const gchar *editor_key, GtkWidget *parent)
3013 {
3014         GList *unsaved = nullptr;
3015         UtilityDelayData *dd;
3016
3017         GList *work;
3018
3019         work = flist;
3020         while (work)
3021                 {
3022                 auto fd = static_cast<FileData *>(work->data);
3023                 work = work->next;
3024
3025                 if (fd->change)
3026                         {
3027                         filelist_free(unsaved);
3028                         return FALSE; /* another op. in progress, let the caller handle it */
3029                         }
3030
3031                 if (fd->modified_xmp) /* has unsaved metadata */
3032                         {
3033                         unsaved = g_list_prepend(unsaved, file_data_ref(fd));
3034                         }
3035                 }
3036
3037         if (!unsaved) return FALSE;
3038
3039         /* save arguments of the original operation */
3040
3041         dd = g_new0(UtilityDelayData, 1);
3042
3043         dd->type = type;
3044         dd->phase = phase;
3045         dd->flist = flist;
3046         dd->dest_path = g_strdup(dest_path);
3047         dd->editor_key = g_strdup(editor_key);
3048         dd->parent = parent;
3049
3050         file_util_write_metadata(nullptr, unsaved, parent, FALSE, file_util_write_metadata_first_done, dd);
3051         return TRUE;
3052 }
3053
3054
3055 /* full-featured entry points
3056 */
3057
3058 void file_util_delete(FileData *source_fd, GList *source_list, GtkWidget *parent)
3059 {
3060         if (options->file_ops.safe_delete_enable == FALSE)
3061                 {
3062                 file_util_delete_full(source_fd, source_list, parent, options->file_ops.confirm_delete ? UTILITY_PHASE_START : UTILITY_PHASE_ENTERING, nullptr, nullptr);
3063                 }
3064         else
3065                 {
3066                 file_util_delete_full(source_fd, source_list, parent, options->file_ops.confirm_move_to_trash ? UTILITY_PHASE_START : UTILITY_PHASE_ENTERING, nullptr, nullptr);
3067                 }
3068 }
3069
3070 void file_util_delete_notify_done(FileData *source_fd, GList *source_list, GtkWidget *parent, FileUtilDoneFunc done_func, gpointer done_data)
3071 {
3072         file_util_delete_full(source_fd, source_list, parent, options->file_ops.confirm_delete ? UTILITY_PHASE_START : UTILITY_PHASE_ENTERING, done_func, done_data);
3073 }
3074
3075 void file_util_write_metadata(FileData *source_fd, GList *source_list, GtkWidget *parent, gboolean force_dialog, FileUtilDoneFunc done_func, gpointer done_data)
3076 {
3077         file_util_write_metadata_full(source_fd, source_list, parent,
3078                                       ((options->metadata.save_in_image_file && options->metadata.confirm_write) || force_dialog) ? UTILITY_PHASE_START : UTILITY_PHASE_ENTERING,
3079                                       done_func, done_data);
3080 }
3081
3082 void file_util_copy(FileData *source_fd, GList *source_list, const gchar *dest_path, GtkWidget *parent)
3083 {
3084         file_util_copy_full(source_fd, source_list, dest_path, parent, UTILITY_PHASE_START);
3085 }
3086
3087 void file_util_move(FileData *source_fd, GList *source_list, const gchar *dest_path, GtkWidget *parent)
3088 {
3089         file_util_move_full(source_fd, source_list, dest_path, parent, UTILITY_PHASE_START);
3090 }
3091
3092 void file_util_rename(FileData *source_fd, GList *source_list, GtkWidget *parent)
3093 {
3094         file_util_rename_full(source_fd, source_list, nullptr, parent, UTILITY_PHASE_START);
3095 }
3096
3097 /* these avoid the location entry dialog unless there is an error, list must be files only and
3098  * dest_path must be a valid directory path
3099  */
3100 void file_util_move_simple(GList *list, const gchar *dest_path, GtkWidget *parent)
3101 {
3102         file_util_move_full(nullptr, list, dest_path, parent, UTILITY_PHASE_ENTERING);
3103 }
3104
3105 void file_util_copy_simple(GList *list, const gchar *dest_path, GtkWidget *parent)
3106 {
3107         file_util_copy_full(nullptr, list, dest_path, parent, UTILITY_PHASE_ENTERING);
3108 }
3109
3110 void file_util_rename_simple(FileData *fd, const gchar *dest_path, GtkWidget *parent)
3111 {
3112         file_util_rename_full(fd, nullptr, dest_path, parent, UTILITY_PHASE_ENTERING);
3113 }
3114
3115
3116 void file_util_start_editor_from_file(const gchar *key, FileData *fd, GtkWidget *parent)
3117 {
3118         file_util_start_editor_full(key, fd, nullptr, nullptr, nullptr, parent, UTILITY_PHASE_ENTERING);
3119 }
3120
3121 void file_util_start_editor_from_filelist(const gchar *key, GList *list, const gchar *working_directory, GtkWidget *parent)
3122 {
3123         file_util_start_editor_full(key, nullptr, list, nullptr, working_directory, parent, UTILITY_PHASE_ENTERING);
3124 }
3125
3126 #pragma GCC diagnostic push
3127 #pragma GCC diagnostic ignored "-Wunused-function"
3128 void file_util_start_filter_from_file_unused(const gchar *key, FileData *fd, const gchar *dest_path, GtkWidget *parent)
3129 {
3130         file_util_start_editor_full(key, fd, nullptr, dest_path, nullptr, parent, UTILITY_PHASE_ENTERING);
3131 }
3132 #pragma GCC diagnostic pop
3133
3134 void file_util_start_filter_from_filelist(const gchar *key, GList *list, const gchar *dest_path, GtkWidget *parent)
3135 {
3136         file_util_start_editor_full(key, nullptr, list, dest_path, nullptr, parent, UTILITY_PHASE_ENTERING);
3137 }
3138
3139 void file_util_delete_dir(FileData *fd, GtkWidget *parent)
3140 {
3141         file_util_delete_dir_full(fd, parent, UTILITY_PHASE_START);
3142 }
3143
3144 void file_util_create_dir(const gchar *path, GtkWidget *parent, FileUtilDoneFunc done_func, gpointer done_data)
3145 {
3146         file_util_create_dir_full(path, nullptr, parent, UTILITY_PHASE_START, done_func, done_data);
3147 }
3148
3149 void file_util_rename_dir(FileData *source_fd, const gchar *new_path, GtkWidget *parent, FileUtilDoneFunc done_func, gpointer done_data)
3150 {
3151         file_util_rename_dir_full(source_fd, new_path, parent, UTILITY_PHASE_ENTERING, done_func, done_data);
3152 }
3153
3154 /**
3155  * @brief
3156  * @param clipboard
3157  * @param selection_data
3158  * @param info
3159  * @param data #_ClipboardData
3160  *
3161  *
3162  */
3163 #ifdef HAVE_GTK4
3164 static void clipboard_get_func(GtkClipboard *clipboard, GtkSelectionData *selection_data, guint info, gpointer data)
3165 {
3166 /* @FIXME GTK4 stub */
3167 }
3168 #else
3169 static void clipboard_get_func(GtkClipboard *clipboard, GtkSelectionData *selection_data, guint info, gpointer data)
3170 {
3171         auto cbd = static_cast<ClipboardData *>(data);
3172         gchar *file_path;
3173         gchar *file_path_quoted = nullptr;
3174         gchar *file_path_uri;
3175         GString *path_list_str;
3176         GList *work;
3177
3178         path_list_str = g_string_new("");
3179         work = cbd->path_list;
3180
3181         if (clipboard == gtk_clipboard_get(GDK_SELECTION_CLIPBOARD) && info == CLIPBOARD_X_SPECIAL_GNOME_COPIED_FILES)
3182                 {
3183                 g_string_append(path_list_str, "copy");
3184
3185                 while (work)
3186                         {
3187                         file_path = static_cast<gchar *>(work->data);
3188                         work = work->next;
3189
3190                         file_path_uri = g_filename_to_uri(file_path, nullptr, nullptr);
3191                         g_string_append(path_list_str, "\n");
3192                         g_string_append(path_list_str, file_path_uri);
3193                         g_free(file_path_uri);
3194                         }
3195                 }
3196         else
3197                 {
3198                 while (work)
3199                         {
3200                         file_path = static_cast<gchar *>(work->data);
3201                         work = work->next;
3202
3203                         if (cbd->quoted)
3204                                 {
3205                                 file_path_quoted = g_shell_quote(file_path);
3206                                 g_string_append(path_list_str, file_path_quoted);
3207                                 g_free(file_path_quoted);
3208                                 }
3209                         else
3210                                 {
3211                                 g_string_append(path_list_str, file_path);
3212                                 }
3213
3214                         if (work)
3215                                 {
3216                                 g_string_append_c(path_list_str, ' ');
3217                                 }
3218                         }
3219                 }
3220
3221         gtk_selection_data_set(selection_data, gtk_selection_data_get_target(selection_data), 8, reinterpret_cast<guchar *>(path_list_str->str), path_list_str->len);
3222
3223         g_string_free(path_list_str, TRUE);
3224 }
3225 #endif
3226
3227 /**
3228  * @brief
3229  * @param UNUSED
3230  * @param data _ClipboardData
3231  *
3232  *
3233  */
3234 static void clipboard_clear_func(GtkClipboard *, gpointer data)
3235 {
3236         auto cbd = static_cast<ClipboardData *>(data);
3237
3238         g_list_free_full(cbd->path_list, g_free);
3239         g_free(cbd);
3240 }
3241
3242 void file_util_copy_path_to_clipboard(FileData *fd, gboolean quoted)
3243 {
3244         ClipboardData *cbd;
3245
3246         if (!fd || !*fd->path) return;
3247
3248
3249         if (options->clipboard_selection == CLIPBOARD_PRIMARY || options->clipboard_selection == CLIPBOARD_BOTH)
3250                 {
3251                 cbd = g_new0(ClipboardData, 1);
3252                 cbd->path_list = nullptr;
3253                 cbd->quoted = quoted;
3254                 cbd->path_list = g_list_append(cbd->path_list, g_strdup(fd->path));
3255
3256                 gtk_clipboard_set_with_data(gtk_clipboard_get(GDK_SELECTION_PRIMARY), target_types, target_types_n, clipboard_get_func, clipboard_clear_func, cbd);
3257                 }
3258
3259         if (options->clipboard_selection == CLIPBOARD_CLIPBOARD || options->clipboard_selection == CLIPBOARD_BOTH)
3260                 {
3261                 cbd = g_new0(ClipboardData, 1);
3262                 cbd->path_list = nullptr;
3263                 cbd->quoted = quoted;
3264                 cbd->path_list = g_list_append(cbd->path_list, g_strdup(fd->path));
3265
3266                 gtk_clipboard_set_with_data(gtk_clipboard_get(GDK_SELECTION_CLIPBOARD), target_types, target_types_n, clipboard_get_func, clipboard_clear_func, cbd);
3267                 }
3268 }
3269
3270 /**
3271  * @brief
3272  * @param fd_list List of fd
3273  * @param quoted
3274  *
3275  *
3276  */
3277 void file_util_copy_path_list_to_clipboard(GList *fd_list, gboolean quoted)
3278 {
3279         ClipboardData *cbd;
3280         FileData *fd;
3281         GList *work;
3282
3283         if (options->clipboard_selection == CLIPBOARD_PRIMARY || options->clipboard_selection == CLIPBOARD_BOTH)
3284                 {
3285                 cbd = g_new0(ClipboardData, 1);
3286                 cbd->path_list = nullptr;
3287                 cbd->quoted = quoted;
3288                 work = fd_list;
3289
3290                 while (work)
3291                         {
3292                         fd = static_cast<FileData *>(work->data);
3293                         work = work->next;
3294
3295                         if (!fd || !*fd->path) continue;
3296
3297                         cbd->path_list = g_list_append(cbd->path_list, g_strdup(fd->path));
3298                         }
3299
3300                         gtk_clipboard_set_with_data(gtk_clipboard_get(GDK_SELECTION_PRIMARY), target_types, target_types_n, clipboard_get_func, clipboard_clear_func, cbd);
3301                 }
3302
3303         if (options->clipboard_selection == CLIPBOARD_CLIPBOARD || options->clipboard_selection == CLIPBOARD_BOTH)
3304                 {
3305                 cbd = g_new0(ClipboardData, 1);
3306                 cbd->path_list = nullptr;
3307                 cbd->quoted = quoted;
3308                 work = fd_list;
3309
3310                 while (work)
3311                         {
3312                         fd = static_cast<FileData *>(work->data);
3313                         work = work->next;
3314
3315                         if (!fd || !*fd->path) continue;
3316
3317                         cbd->path_list = g_list_append(cbd->path_list, g_strdup(fd->path));
3318                         }
3319
3320                         gtk_clipboard_set_with_data(gtk_clipboard_get(GDK_SELECTION_CLIPBOARD), target_types, target_types_n, clipboard_get_func, clipboard_clear_func, cbd);
3321                 }
3322
3323         filelist_free(fd_list);
3324 }
3325
3326 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */