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