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