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