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