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