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