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