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