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