Trim trailing white spaces.
[geeqie.git] / src / utilops.c
index 893854e..8636e2e 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * Geeqie
  * (C) 2006 John Ellis
- * Copyright (C) 2008 The Geeqie Team
+ * Copyright (C) 2008 - 2012 The Geeqie Team
  *
  * Author: John Ellis
  *
 #include "ui_misc.h"
 #include "ui_tabcomp.h"
 #include "editors.h"
+#include "metadata.h"
+#include "exif.h"
+
+#define DIALOG_WIDTH 750
+
+static GdkPixbuf *file_util_get_error_icon(FileData *fd, GtkWidget *widget);
 
 /*
  *--------------------------------------------------------------------------
@@ -44,7 +50,7 @@
 static void generic_dialog_add_image(GenericDialog *gd, GtkWidget *box,
                                     FileData *fd1, const gchar *header1,
                                     FileData *fd2, const gchar *header2,
-                                    gint show_filename)
+                                    gboolean show_filename)
 {
        ImageWindow *imd;
        GtkWidget *hbox = NULL;
@@ -150,13 +156,13 @@ static void generic_dialog_image_set(GenericDialog *gd, FileData *fd)
  */
 
 GenericDialog *file_util_gen_dlg(const gchar *title,
-                                const gchar *wmclass, const gchar *wmsubclass,
-                                GtkWidget *parent, gint auto_close,
+                                const gchar *role,
+                                GtkWidget *parent, gboolean auto_close,
                                 void (*cancel_cb)(GenericDialog *, gpointer), gpointer data)
 {
        GenericDialog *gd;
 
-       gd = generic_dialog_new(title, wmclass, wmsubclass, parent, auto_close, cancel_cb, data);
+       gd = generic_dialog_new(title, role, parent, auto_close, cancel_cb, data);
        if (options->place_dialogs_under_mouse)
                {
                gtk_window_set_position(GTK_WINDOW(gd->dialog), GTK_WIN_POS_MOUSE);
@@ -166,13 +172,13 @@ GenericDialog *file_util_gen_dlg(const gchar *title,
 }
 
 FileDialog *file_util_file_dlg(const gchar *title,
-                              const gchar *wmclass, const gchar *wmsubclass,
+                              const gchar *role,
                               GtkWidget *parent,
                               void (*cancel_cb)(FileDialog *, gpointer), gpointer data)
 {
        FileDialog *fdlg;
 
-       fdlg = file_dialog_new(title, wmclass, wmsubclass, parent, cancel_cb, data);
+       fdlg = file_dialog_new(title, role, parent, cancel_cb, data);
        if (options->place_dialogs_under_mouse)
                {
                gtk_window_set_position(GTK_WINDOW(GENERIC_DIALOG(fdlg)->dialog), GTK_WIN_POS_MOUSE);
@@ -195,7 +201,7 @@ GenericDialog *file_util_warning_dialog(const gchar *heading, const gchar *messa
 {
        GenericDialog *gd;
 
-       gd = file_util_gen_dlg(heading, GQ_WMCLASS, "warning", parent, TRUE, NULL, NULL);
+       gd = file_util_gen_dlg(heading, "warning", parent, TRUE, NULL, NULL);
        generic_dialog_add_message(gd, icon_stock_id, heading, message);
        generic_dialog_add_button(gd, GTK_STOCK_OK, NULL, file_util_warning_dialog_ok_cb, TRUE);
        if (options->place_dialogs_under_mouse)
@@ -238,7 +244,9 @@ typedef enum {
        UTILITY_TYPE_FILTER,
        UTILITY_TYPE_DELETE,
        UTILITY_TYPE_DELETE_LINK,
-       UTILITY_TYPE_DELETE_FOLDER
+       UTILITY_TYPE_DELETE_FOLDER,
+       UTILITY_TYPE_CREATE_FOLDER,
+       UTILITY_TYPE_WRITE_METADATA
 } UtilityType;
 
 typedef enum {
@@ -246,7 +254,8 @@ typedef enum {
        UTILITY_PHASE_ENTERING,
        UTILITY_PHASE_CHECKED,
        UTILITY_PHASE_DONE,
-       UTILITY_PHASE_CANCEL
+       UTILITY_PHASE_CANCEL,
+       UTILITY_PHASE_DISCARD
 } UtilityPhase;
 
 enum {
@@ -260,7 +269,6 @@ struct _UtilityDataMessages {
        gchar *title;
        gchar *question;
        gchar *desc_flist;
-       gchar *desc_dlist;
        gchar *desc_source_fd;
        gchar *fail;
 };
@@ -274,12 +282,17 @@ struct _UtilityData {
        FileData *dir_fd;
        GList *content_list;
        GList *flist;
+       
+       FileData *sel_fd;
 
        GtkWidget *parent;
        GenericDialog *gd;
        FileDialog *fdlg;
        
-       gint update_idle_id;
+       guint update_idle_id; /* event source id */
+       guint perform_idle_id; /* event source id */
+
+       gboolean with_sidecars; /* operate on grouped or single files; TRUE = use file_data_sc_, FALSE = use file_data_ functions */
        
        /* alternative dialog parts */
        GtkWidget *notebook;
@@ -304,12 +317,19 @@ struct _UtilityData {
        /* data for the operation itself, internal or external */
        gboolean external; /* TRUE for external command, FALSE for internal */
        
-       gint external_command;
+       gchar *external_command;
        gpointer resume_data;
+       
+       FileUtilDoneFunc done_func;
+       void (*details_func)(UtilityData *ud, FileData *fd);
+       gboolean (*finalize_func)(FileData *fd);
+       gboolean (*discard_func)(FileData *fd);
+       gpointer done_data;
 };
 
 enum {
        UTILITY_COLUMN_FD = 0,
+       UTILITY_COLUMN_PIXBUF,
        UTILITY_COLUMN_PATH,
        UTILITY_COLUMN_NAME,
        UTILITY_COLUMN_SIDECARS,
@@ -318,6 +338,20 @@ enum {
        UTILITY_COLUMN_COUNT
 };
 
+typedef struct _UtilityDelayData UtilityDelayData;
+
+struct _UtilityDelayData {
+       UtilityType type;
+       UtilityPhase phase;
+       GList *flist;
+       gchar *dest_path;
+       gchar *editor_key;
+       GtkWidget *parent;
+       guint idle_id; /* event source id */
+       };
+
+static gboolean file_util_write_metadata_first(UtilityType type, UtilityPhase phase, GList *flist, const gchar *dest_path, const gchar *editor_key, GtkWidget *parent);
+
 #define UTILITY_LIST_MIN_WIDTH  250
 #define UTILITY_LIST_MIN_HEIGHT 150
 
@@ -329,10 +363,10 @@ static UtilityData *file_util_data_new(UtilityType type)
        UtilityData *ud;
 
        ud = g_new0(UtilityData, 1);
+       
        ud->type = type;
        ud->phase = UTILITY_PHASE_START;
-       ud->update_idle_id = -1;
-       ud->external_command = -1;
+       
        return ud;
 }
 
@@ -340,7 +374,8 @@ static void file_util_data_free(UtilityData *ud)
 {
        if (!ud) return;
 
-       if (ud->update_idle_id != -1) g_source_remove(ud->update_idle_id);
+       if (ud->update_idle_id) g_source_remove(ud->update_idle_id);
+       if (ud->perform_idle_id) g_source_remove(ud->perform_idle_id);
 
        file_data_unref(ud->dir_fd);
        filelist_free(ud->content_list);
@@ -349,11 +384,12 @@ static void file_util_data_free(UtilityData *ud)
        if (ud->gd) generic_dialog_close(ud->gd);
        
        g_free(ud->dest_path);
+       g_free(ud->external_command);
 
        g_free(ud);
 }
 
-static GtkTreeViewColumn *file_util_dialog_add_list_column(GtkWidget *view, const gchar *text, gint n)
+static GtkTreeViewColumn *file_util_dialog_add_list_column(GtkWidget *view, const gchar *text, gboolean image, gint n)
 {
        GtkTreeViewColumn *column;
        GtkCellRenderer *renderer;
@@ -361,10 +397,21 @@ static GtkTreeViewColumn *file_util_dialog_add_list_column(GtkWidget *view, cons
        column = gtk_tree_view_column_new();
        gtk_tree_view_column_set_title(column, text);
        gtk_tree_view_column_set_min_width(column, 4);
-       gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_GROW_ONLY);
-       renderer = gtk_cell_renderer_text_new();
-       gtk_tree_view_column_pack_start(column, renderer, TRUE);
-       gtk_tree_view_column_add_attribute(column, renderer, "text", n);
+       if (image)
+               {
+               gtk_tree_view_column_set_min_width(column, 20);
+               gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_FIXED);
+               renderer = gtk_cell_renderer_pixbuf_new();
+               gtk_tree_view_column_pack_start(column, renderer, TRUE);
+               gtk_tree_view_column_add_attribute(column, renderer, "pixbuf", n);
+               }
+       else
+               {
+               gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_GROW_ONLY);
+               renderer = gtk_cell_renderer_text_new();
+               gtk_tree_view_column_pack_start(column, renderer, TRUE);
+               gtk_tree_view_column_add_attribute(column, renderer, "text", n);
+               }
        gtk_tree_view_append_column(GTK_TREE_VIEW(view), column);
 
        return column;
@@ -384,7 +431,7 @@ static void file_util_dialog_list_select(GtkWidget *view, gint n)
                }
 }
 
-static GtkWidget *file_util_dialog_add_list(GtkWidget *box, GList *list, gint full_paths)
+static GtkWidget *file_util_dialog_add_list(GtkWidget *box, GList *list, gboolean full_paths, gboolean with_sidecars)
 {
        GtkWidget *scrolled;
        GtkWidget *view;
@@ -393,24 +440,26 @@ static GtkWidget *file_util_dialog_add_list(GtkWidget *box, GList *list, gint fu
        scrolled = gtk_scrolled_window_new(NULL, NULL);
        gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled), GTK_SHADOW_IN);
        gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled),
-                                      GTK_POLICY_AUTOMATIC, GTK_POLICY_ALWAYS);
+                                      GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
        gtk_box_pack_start(GTK_BOX(box), scrolled, TRUE, TRUE, 0);
        gtk_widget_show(scrolled);
 
-       store = gtk_list_store_new(UTILITY_COLUMN_COUNT, G_TYPE_POINTER, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING);
+       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);
        view = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
        g_object_unref(store);
 
        gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(view), TRUE);
        gtk_tree_view_set_enable_search(GTK_TREE_VIEW(view), FALSE);
 
+       file_util_dialog_add_list_column(view, "", TRUE, UTILITY_COLUMN_PIXBUF);
+
        if (full_paths)
                {
-               file_util_dialog_add_list_column(view, _("Location"), UTILITY_COLUMN_PATH);
+               file_util_dialog_add_list_column(view, _("Path"), FALSE, UTILITY_COLUMN_PATH);
                }
        else
                {
-               file_util_dialog_add_list_column(view, _("Name"), UTILITY_COLUMN_NAME);
+               file_util_dialog_add_list_column(view, _("Name"), FALSE, UTILITY_COLUMN_NAME);
                }
 
        gtk_widget_set_size_request(view, UTILITY_LIST_MIN_WIDTH, UTILITY_LIST_MIN_HEIGHT);
@@ -423,11 +472,12 @@ static GtkWidget *file_util_dialog_add_list(GtkWidget *box, GList *list, gint fu
                GtkTreeIter iter;
                gchar *sidecars;
                
-               sidecars = file_data_sc_list_to_string(fd);
-
+               sidecars = with_sidecars ? file_data_sc_list_to_string(fd) : NULL;
+               GdkPixbuf *icon = file_util_get_error_icon(fd, view);
                gtk_list_store_append(store, &iter);
                gtk_list_store_set(store, &iter,
                                   UTILITY_COLUMN_FD, fd,
+                                  UTILITY_COLUMN_PIXBUF, icon,
                                   UTILITY_COLUMN_PATH, fd->path,
                                   UTILITY_COLUMN_NAME, fd->name,
                                   UTILITY_COLUMN_SIDECARS, sidecars,
@@ -443,9 +493,9 @@ static GtkWidget *file_util_dialog_add_list(GtkWidget *box, GList *list, gint fu
 }
 
 
-void file_util_perform_ci_internal(UtilityData *ud);
+static gboolean file_util_perform_ci_internal(gpointer data);
 void file_util_dialog_run(UtilityData *ud);
-static gint file_util_perform_ci_cb(gpointer resume_data, gint flags, GList *list, gpointer data);
+static gint file_util_perform_ci_cb(gpointer resume_data, EditorFlags flags, GList *list, gpointer data);
 
 /* call file_util_perform_ci_internal or start_editor_from_filelist_full */
 
@@ -470,13 +520,14 @@ static void file_util_abort_cb(GenericDialog *gd, gpointer data)
 }
 
 
-static gint file_util_perform_ci_cb(gpointer resume_data, gint flags, GList *list, gpointer data)
+static gint file_util_perform_ci_cb(gpointer resume_data, EditorFlags flags, GList *list, gpointer data)
 {
        UtilityData *ud = data;
+       gint ret = EDITOR_CB_CONTINUE;
+
        ud->resume_data = resume_data;
        
-       gint ret = EDITOR_CB_CONTINUE;
-       if ((flags & EDITOR_ERROR_MASK) && !(flags & EDITOR_ERROR_SKIPPED))
+       if (EDITOR_ERRORS_BUT_SKIPPED(flags))
                {
                GString *msg = g_string_new(editor_get_error_str(flags));
                GenericDialog *d;
@@ -494,7 +545,7 @@ static gint file_util_perform_ci_cb(gpointer resume_data, gint flags, GList *lis
                if (resume_data)
                        {
                        g_string_append(msg, _("\n Continue multiple file operation?"));
-                       d = file_util_gen_dlg(ud->messages.fail, GQ_WMCLASS, "dlg_confirm",
+                       d = file_util_gen_dlg(ud->messages.fail, "dlg_confirm",
                                              NULL, TRUE,
                                              file_util_abort_cb, ud);
 
@@ -516,15 +567,27 @@ static gint file_util_perform_ci_cb(gpointer resume_data, gint flags, GList *lis
        while (list)  /* be careful, file_util_perform_ci_internal can pass ud->flist as list */
                {
                FileData *fd = list->data;
-               list = list->next; 
+               list = list->next;
 
-               if (!(flags & EDITOR_ERROR_MASK)) /* files were successfully deleted, call the maint functions */
+               if (!EDITOR_ERRORS(flags)) /* files were successfully deleted, call the maint functions */
                        {
-                       file_data_sc_apply_ci(fd);
+                       if (ud->with_sidecars)
+                               file_data_sc_apply_ci(fd);
+                       else
+                               file_data_apply_ci(fd);
                        }
                
                ud->flist = g_list_remove(ud->flist, fd);
-               file_data_sc_free_ci(fd);
+               
+               if (ud->finalize_func)
+                       {
+                       ud->finalize_func(fd);
+                       }
+
+               if (ud->with_sidecars)
+                       file_data_sc_free_ci(fd);
+               else
+                       file_data_free_ci(fd);
                file_data_unref(fd);
                }
                
@@ -541,37 +604,52 @@ static gint file_util_perform_ci_cb(gpointer resume_data, gint flags, GList *lis
 /*
  * Perform the operation described by FileDataChangeInfo on all files in the list
  * it is an alternative to start_editor_from_filelist_full, it should use similar interface
- */ 
+ */
 
 
-static void file_util_perform_ci_internal(UtilityData *ud)
+static gboolean file_util_perform_ci_internal(gpointer data)
 {
+       UtilityData *ud = data;
+
+       if (!ud->perform_idle_id)
+               {
+               /* this function was called directly
+                  just setup idle callback and wait until we are called again
+               */
+               
+               /* this is removed when ud is destroyed */
+               ud->perform_idle_id = g_idle_add(file_util_perform_ci_internal, ud);
+               return TRUE;
+               }
+
+       g_assert(ud->flist);
        
-       while (ud->flist)
+       if (ud->flist)
                {
                gint ret;
                
                /* take a single entry each time, this allows better control over the operation */
                GList *single_entry = g_list_append(NULL, ud->flist->data);
                gboolean last = !ud->flist->next;
-               gint status = EDITOR_ERROR_STATUS;
+               EditorFlags status = EDITOR_ERROR_STATUS;
        
-               if (file_data_sc_perform_ci(single_entry->data))
+               if (ud->with_sidecars ? file_data_sc_perform_ci(single_entry->data)
+                                     : file_data_perform_ci(single_entry->data))
                        status = 0; /* OK */
                
                ret = file_util_perform_ci_cb(GINT_TO_POINTER(!last), status, single_entry, ud);
                g_list_free(single_entry);
                
-               if (ret == EDITOR_CB_SUSPEND || last) return;
+               if (ret == EDITOR_CB_SUSPEND || last) return FALSE;
                
                if (ret == EDITOR_CB_SKIP)
                        {
                        file_util_perform_ci_cb(NULL, EDITOR_ERROR_SKIPPED, ud->flist, ud);
+                       return FALSE;
                        }
-               
-               /* FIXME: convert the loop to idle call */
-               
                }
+       
+       return TRUE;
 }
 
 static void file_util_perform_ci_dir(UtilityData *ud, gboolean internal, gboolean ext_result)
@@ -580,21 +658,21 @@ static void file_util_perform_ci_dir(UtilityData *ud, gboolean internal, gboolea
                {
                case UTILITY_TYPE_DELETE_LINK:
                        {
-                       if (internal && file_data_sc_perform_ci(ud->dir_fd) ||
-                           !internal && ext_result)
+                       g_assert(ud->dir_fd->sidecar_files == NULL); // directories should not have sidecars
+                       if ((internal && file_data_perform_ci(ud->dir_fd)) ||
+                           (!internal && ext_result))
                                {
-                               file_data_sc_apply_ci(ud->dir_fd);
+                               file_data_apply_ci(ud->dir_fd);
                                }
                        else
                                {
                                gchar *text;
 
-                               text = g_strdup_printf("Unable to remove symbolic link:\n %s", ud->dir_fd->path);
-                               file_util_warning_dialog(_("Delete failed"), text,
-                                                        GTK_STOCK_DIALOG_ERROR, NULL);
+                               text = g_strdup_printf("%s:\n\n%s", ud->messages.fail, ud->dir_fd->path);
+                               file_util_warning_dialog(ud->messages.fail, text, GTK_STOCK_DIALOG_ERROR, NULL);
                                g_free(text);
                                }
-                       file_data_sc_free_ci(ud->dir_fd);
+                       file_data_free_ci(ud->dir_fd);
                        break;
                        }
                case UTILITY_TYPE_DELETE_FOLDER:
@@ -611,8 +689,8 @@ static void file_util_perform_ci_dir(UtilityData *ud, gboolean internal, gboolea
                                
                                if (!fail)
                                        {
-                                       if (internal && file_data_sc_perform_ci(fd) ||
-                                           !internal && ext_result)
+                                       if ((internal && file_data_sc_perform_ci(fd)) ||
+                                           (!internal && ext_result))
                                                {
                                                file_data_sc_apply_ci(fd);
                                                }
@@ -626,10 +704,11 @@ static void file_util_perform_ci_dir(UtilityData *ud, gboolean internal, gboolea
 
                        if (!fail)
                                {
-                               if (internal && file_data_sc_perform_ci(ud->dir_fd) ||
-                                   !internal && ext_result)
+                               g_assert(ud->dir_fd->sidecar_files == NULL); // directories should not have sidecars
+                               if ((internal && file_data_sc_perform_ci(ud->dir_fd)) ||
+                                   (!internal && ext_result))
                                        {
-                                       file_data_sc_apply_ci(ud->dir_fd);
+                                       file_data_apply_ci(ud->dir_fd);
                                        }
                                else
                                        {
@@ -642,8 +721,8 @@ static void file_util_perform_ci_dir(UtilityData *ud, gboolean internal, gboolea
                                gchar *text;
                                GenericDialog *gd;
 
-                               text = g_strdup_printf(_("Unable to delete folder:\n\n%s"), ud->dir_fd->path);
-                               gd = file_util_warning_dialog(_("Delete failed"), text, GTK_STOCK_DIALOG_ERROR, NULL);
+                               text = g_strdup_printf("%s:\n\n%s", ud->messages.fail, ud->dir_fd->path);
+                               gd = file_util_warning_dialog(ud->messages.fail, text, GTK_STOCK_DIALOG_ERROR, NULL);
                                g_free(text);
 
                                if (fail != ud->dir_fd)
@@ -663,8 +742,10 @@ static void file_util_perform_ci_dir(UtilityData *ud, gboolean internal, gboolea
                        {
                        FileData *fail = NULL;
                        GList *work;
-                       if (internal && file_data_sc_perform_ci(ud->dir_fd) ||
-                           !internal && ext_result)
+                       g_assert(ud->dir_fd->sidecar_files == NULL); // directories should not have sidecars
+
+                       if ((internal && file_data_sc_perform_ci(ud->dir_fd)) ||
+                           (!internal && ext_result))
                                {
                                file_data_sc_apply_ci(ud->dir_fd);
                                }
@@ -692,16 +773,33 @@ static void file_util_perform_ci_dir(UtilityData *ud, gboolean internal, gboolea
                        if (fail)
                                {
                                gchar *text;
-                               GenericDialog *gd;
 
-                               text = g_strdup_printf(_("Unable to rename folder:\n\n%s"), ud->dir_fd->path);
-                               gd = file_util_warning_dialog(_("Rename failed"), text, GTK_STOCK_DIALOG_ERROR, NULL);
+                               text = g_strdup_printf("%s:\n\n%s", ud->messages.fail, ud->dir_fd->path);
+                               (void) file_util_warning_dialog(ud->messages.fail, text, GTK_STOCK_DIALOG_ERROR, NULL);
                                g_free(text);
 
                                file_data_unref(fail);
                                }
                        break;
                        }
+               case UTILITY_TYPE_CREATE_FOLDER:
+                       {
+                       if ((internal && mkdir_utf8(ud->dir_fd->path, 0755)) ||
+                           (!internal && ext_result))
+                               {
+                               file_data_check_changed_files(ud->dir_fd); /* this will update the FileData and send notification */
+                               }
+                       else
+                               {
+                               gchar *text;
+
+                               text = g_strdup_printf("%s:\n\n%s", ud->messages.fail, ud->dir_fd->path);
+                               (void) file_util_warning_dialog(ud->messages.fail, text, GTK_STOCK_DIALOG_ERROR, NULL);
+                               g_free(text);
+                               }
+                       
+                       break;
+                       }
                default:
                        g_warning("unhandled operation");
                }
@@ -709,10 +807,10 @@ static void file_util_perform_ci_dir(UtilityData *ud, gboolean internal, gboolea
        file_util_dialog_run(ud);
 }
 
-static gint file_util_perform_ci_dir_cb(gpointer resume_data, gint flags, GList *list, gpointer data)
+static gint file_util_perform_ci_dir_cb(gpointer resume_data, EditorFlags flags, GList *list, gpointer data)
 {
        UtilityData *ud = data;
-       file_util_perform_ci_dir(ud, FALSE, !((flags & EDITOR_ERROR_MASK) && !(flags & EDITOR_ERROR_SKIPPED)));
+       file_util_perform_ci_dir(ud, FALSE, !EDITOR_ERRORS_BUT_SKIPPED(flags));
        return EDITOR_CB_CONTINUE; /* does not matter, there was just single directory */
 }
 
@@ -721,29 +819,34 @@ void file_util_perform_ci(UtilityData *ud)
        switch (ud->type)
                {
                case UTILITY_TYPE_COPY:
-                       ud->external_command = CMD_COPY;
+                       ud->external_command = g_strdup(CMD_COPY);
                        break;
                case UTILITY_TYPE_MOVE:
-                       ud->external_command = CMD_MOVE;
+                       ud->external_command = g_strdup(CMD_MOVE);
                        break;
                case UTILITY_TYPE_RENAME:
                case UTILITY_TYPE_RENAME_FOLDER:
-                       ud->external_command = CMD_RENAME;
+                       ud->external_command = g_strdup(CMD_RENAME);
                        break;
                case UTILITY_TYPE_DELETE:
                case UTILITY_TYPE_DELETE_LINK:
                case UTILITY_TYPE_DELETE_FOLDER:
-                       ud->external_command = CMD_DELETE;
+                       ud->external_command = g_strdup(CMD_DELETE);
+                       break;
+               case UTILITY_TYPE_CREATE_FOLDER:
+                       ud->external_command = g_strdup(CMD_FOLDER);
                        break;
                case UTILITY_TYPE_FILTER:
                case UTILITY_TYPE_EDITOR:
-                       g_assert(ud->external_command != -1); /* it should be already set */
+                       g_assert(ud->external_command != NULL); /* it should be already set */
                        break;
+               case UTILITY_TYPE_WRITE_METADATA:
+                       ud->external_command = NULL;
                }
 
        if (is_valid_editor_command(ud->external_command))
                {
-               gint flags;
+               EditorFlags flags;
                
                ud->external = TRUE;
                
@@ -753,10 +856,21 @@ void file_util_perform_ci(UtilityData *ud)
                        }
                else
                        {
-                       flags = start_editor_from_filelist_full(ud->external_command, ud->flist, file_util_perform_ci_cb, ud);
+                       if (editor_blocks_file(ud->external_command))
+                               {
+                               DEBUG_1("Starting %s and waiting for results", ud->external_command);
+                               flags = start_editor_from_filelist_full(ud->external_command, ud->flist, NULL, file_util_perform_ci_cb, ud);
+                               }
+                       else
+                               {
+                               /* start the editor without callback and finish the operation internally */
+                               DEBUG_1("Starting %s and finishing the operation", ud->external_command);
+                               flags = start_editor_from_filelist(ud->external_command, ud->flist);
+                               file_util_perform_ci_internal(ud);
+                               }
                        }
 
-               if (flags)
+               if (EDITOR_ERRORS(flags))
                        {
                        gchar *text = g_strdup_printf(_("%s\nUnable to start external command.\n"), editor_get_error_str(flags));
                        file_util_warning_dialog(ud->messages.fail, text, GTK_STOCK_DIALOG_ERROR, NULL);
@@ -777,6 +891,122 @@ void file_util_perform_ci(UtilityData *ud)
                }
 }
 
+static GdkPixbuf *file_util_get_error_icon(FileData *fd, GtkWidget *widget)
+{
+       static GdkPixbuf *pb_warning;
+       static GdkPixbuf *pb_error;
+       static GdkPixbuf *pb_apply;
+       gint error;
+       
+       if (!pb_warning)
+               {
+               pb_warning = gtk_widget_render_icon(widget, GTK_STOCK_DIALOG_WARNING, GTK_ICON_SIZE_MENU, NULL);
+               }
+
+       if (!pb_error)
+               {
+               pb_error = gtk_widget_render_icon(widget, GTK_STOCK_DIALOG_ERROR, GTK_ICON_SIZE_MENU, NULL);
+               }
+
+       if (!pb_apply)
+               {
+               pb_apply = gtk_widget_render_icon(widget, GTK_STOCK_APPLY, GTK_ICON_SIZE_MENU, NULL);
+               }
+       
+       error = file_data_sc_verify_ci(fd);
+       
+       if (!error) return pb_apply;
+
+       if (error & CHANGE_ERROR_MASK)
+               {
+               return pb_error;
+               }
+       else
+               {
+               return pb_warning;
+               }
+}
+
+static void file_util_check_resume_cb(GenericDialog *gd, gpointer data)
+{
+       UtilityData *ud = data;
+       ud->phase = UTILITY_PHASE_CHECKED;
+       file_util_dialog_run(ud);
+}
+
+static void file_util_check_abort_cb(GenericDialog *gd, gpointer data)
+{
+       UtilityData *ud = data;
+       ud->phase = UTILITY_PHASE_START;
+       file_util_dialog_run(ud);
+}
+
+void file_util_check_ci(UtilityData *ud)
+{
+       gint error = CHANGE_OK;
+       gchar *desc = NULL;
+       
+       if (ud->type != UTILITY_TYPE_CREATE_FOLDER &&
+           ud->type != UTILITY_TYPE_RENAME_FOLDER)
+               {
+               if (ud->dest_path && !isdir(ud->dest_path))
+                       {
+                       error = CHANGE_GENERIC_ERROR;
+                       desc = g_strdup_printf(_("%s is not a directory"), ud->dest_path);
+                       }
+               else if (ud->dir_fd)
+                       {
+                       g_assert(ud->dir_fd->sidecar_files == NULL); // directories should not have sidecars
+                       error = file_data_verify_ci(ud->dir_fd);
+                       if (error) desc = file_data_get_error_string(error);
+                       }
+               else
+                       {
+                       error = file_data_verify_ci_list(ud->flist, &desc, ud->with_sidecars);
+                       }
+               }
+
+       if (!error)
+               {
+               ud->phase = UTILITY_PHASE_CHECKED;
+               file_util_dialog_run(ud);
+               return;
+               }
+
+       if (!(error & CHANGE_ERROR_MASK))
+               {
+               /* just a warning */
+               GenericDialog *d;
+
+               d = file_util_gen_dlg(ud->messages.title, "dlg_confirm",
+                                       ud->parent, TRUE,
+                                       file_util_check_abort_cb, ud);
+
+               generic_dialog_add_message(d, GTK_STOCK_DIALOG_WARNING, _("Really continue?"), desc);
+
+               generic_dialog_add_button(d, GTK_STOCK_GO_FORWARD, _("Co_ntinue"),
+                                         file_util_check_resume_cb, TRUE);
+               gtk_widget_show(d->dialog);
+               }
+       else
+               {
+               /* fatal error */
+               GenericDialog *d;
+
+               d = file_util_gen_dlg(ud->messages.title, "dlg_confirm",
+                                       ud->parent, TRUE,
+                                       file_util_check_abort_cb, ud);
+               generic_dialog_add_message(d, GTK_STOCK_DIALOG_WARNING, _("This operation can't continue:"), desc);
+
+               gtk_widget_show(d->dialog);
+               }
+       g_free(desc);
+}
+
+
+
+
+
 static void file_util_cancel_cb(GenericDialog *gd, gpointer data)
 {
        UtilityData *ud = data;
@@ -789,53 +1019,50 @@ static void file_util_cancel_cb(GenericDialog *gd, gpointer data)
        file_util_dialog_run(ud);
 }
 
-static void file_util_ok_cb(GenericDialog *gd, gpointer data)
+static void file_util_discard_cb(GenericDialog *gd, gpointer data)
 {
        UtilityData *ud = data;
        
        generic_dialog_close(gd);
-       
-       ud->gd = NULL;
 
+       ud->gd = NULL;
+       
+       ud->phase = UTILITY_PHASE_DISCARD;
        file_util_dialog_run(ud);
 }
 
-static void file_util_fdlg_cancel_cb(FileDialog *fdlg, gpointer data)
+static void file_util_ok_cb(GenericDialog *gd, gpointer data)
 {
        UtilityData *ud = data;
        
-       file_dialog_close(fdlg);
-
-       ud->fdlg = NULL;
+       generic_dialog_close(gd);
        
-       ud->phase = UTILITY_PHASE_CANCEL;
+       ud->gd = NULL;
+
        file_util_dialog_run(ud);
 }
 
-static void file_util_fdlg_ok_cb(FileDialog *fdlg, gpointer data)
+static void file_util_fdlg_cancel_cb(FileDialog *fdlg, gpointer data)
 {
        UtilityData *ud = data;
        
        file_dialog_close(fdlg);
-       
-       ud->fdlg = NULL;
 
+       ud->fdlg = NULL;
+       
+       ud->phase = UTILITY_PHASE_CANCEL;
        file_util_dialog_run(ud);
 }
 
-
-static void file_util_dest_folder_entry_cb(GtkWidget *entry, gpointer data)
+static void file_util_dest_folder_update_path(UtilityData *ud)
 {
-       UtilityData *ud = data;
-       
        g_free(ud->dest_path);
-       ud->dest_path = g_strdup(gtk_entry_get_text(GTK_ENTRY(entry)));
+       ud->dest_path = g_strdup(gtk_entry_get_text(GTK_ENTRY(ud->fdlg->entry)));
        
        switch (ud->type)
                {
                case UTILITY_TYPE_COPY:
                        file_data_sc_update_ci_copy_list(ud->flist, ud->dest_path);
-                       break;
                case UTILITY_TYPE_MOVE:
                        file_data_sc_update_ci_move_list(ud->flist, ud->dest_path);
                        break;
@@ -843,15 +1070,39 @@ static void file_util_dest_folder_entry_cb(GtkWidget *entry, gpointer data)
                case UTILITY_TYPE_EDITOR:
                        file_data_sc_update_ci_unspecified_list(ud->flist, ud->dest_path);
                        break;
+               case UTILITY_TYPE_CREATE_FOLDER:
+                       file_data_unref(ud->dir_fd);
+                       ud->dir_fd = file_data_new_dir(ud->dest_path);
+                       break;
                case UTILITY_TYPE_DELETE:
                case UTILITY_TYPE_DELETE_LINK:
                case UTILITY_TYPE_DELETE_FOLDER:
                case UTILITY_TYPE_RENAME:
                case UTILITY_TYPE_RENAME_FOLDER:
+               case UTILITY_TYPE_WRITE_METADATA:
                        g_warning("unhandled operation");
                }
 }
 
+static void file_util_fdlg_ok_cb(FileDialog *fdlg, gpointer data)
+{
+       UtilityData *ud = data;
+       
+       file_util_dest_folder_update_path(ud);
+       if (isdir(ud->dest_path)) file_dialog_sync_history(fdlg, TRUE);
+       file_dialog_close(fdlg);
+       
+       ud->fdlg = NULL;
+
+       file_util_dialog_run(ud);
+}
+
+
+static void file_util_dest_folder_entry_cb(GtkWidget *entry, gpointer data)
+{
+       UtilityData *ud = data;
+       file_util_dest_folder_update_path(ud);
+}
 
 /* format: * = filename without extension, ## = number position, extension is kept */
 static gchar *file_util_rename_multiple_auto_format_name(const gchar *format, const gchar *name, gint n)
@@ -921,7 +1172,7 @@ static void file_util_rename_preview_update(UtilityData *ud)
        const gchar *front;
        const gchar *end;
        const gchar *format;
-       gint valid;
+       gboolean valid;
        gint start_n;
        gint padding;
        gint n;
@@ -938,10 +1189,12 @@ static void file_util_rename_preview_update(UtilityData *ud)
                        const gchar *dest = gtk_entry_get_text(GTK_ENTRY(ud->rename_entry));
                        
                        gtk_tree_model_get(store, &iter, UTILITY_COLUMN_FD, &fd, -1);
+                       g_assert(ud->with_sidecars); /* sidecars must be renamed too, it would break the pairing otherwise */
                        file_data_sc_update_ci_rename(fd, dest);
-                       gtk_list_store_set(GTK_LIST_STORE(store), &iter, 
-                                          UTILITY_COLUMN_DEST_PATH, fd->change->dest,
-                                          UTILITY_COLUMN_DEST_NAME, filename_from_path(fd->change->dest),
+                       gtk_list_store_set(GTK_LIST_STORE(store), &iter,
+                                          UTILITY_COLUMN_PIXBUF, file_util_get_error_icon(fd, ud->listview),
+                                          UTILITY_COLUMN_DEST_PATH, fd->change->dest,
+                                          UTILITY_COLUMN_DEST_NAME, filename_from_path(fd->change->dest),
                                           -1);
                        }
                return;
@@ -981,9 +1234,11 @@ static void file_util_rename_preview_update(UtilityData *ud)
                        dest = g_strdup_printf("%s%0*d%s", front, padding, n, end);
                        }
 
+               g_assert(ud->with_sidecars); /* sidecars must be renamed too, it would break the pairing otherwise */
                file_data_sc_update_ci_rename(fd, dest);
-               gtk_list_store_set(GTK_LIST_STORE(store), &iter, 
-                                  UTILITY_COLUMN_DEST_PATH, fd->change->dest,
+               gtk_list_store_set(GTK_LIST_STORE(store), &iter,
+                                  UTILITY_COLUMN_PIXBUF, file_util_get_error_icon(fd, ud->listview),
+                                  UTILITY_COLUMN_DEST_PATH, fd->change->dest,
                                   UTILITY_COLUMN_DEST_NAME, filename_from_path(fd->change->dest),
                                   -1);
                g_free(dest);
@@ -1012,24 +1267,24 @@ static gboolean file_util_rename_idle_cb(gpointer data)
 
        file_util_rename_preview_update(ud);
 
-       ud->update_idle_id = -1;
+       ud->update_idle_id = 0;
        return FALSE;
 }
 
 static void file_util_rename_preview_order_cb(GtkTreeModel *treemodel, GtkTreePath *tpath,
-                                                      GtkTreeIter *iter, gpointer data)
+                                             GtkTreeIter *iter, gpointer data)
 {
        UtilityData *ud = data;
 
-       if (ud->update_idle_id != -1) return;
+       if (ud->update_idle_id) return;
 
        ud->update_idle_id = g_idle_add(file_util_rename_idle_cb, ud);
 }
 
 
 static gboolean file_util_preview_cb(GtkTreeSelection *selection, GtkTreeModel *store,
-                                               GtkTreePath *tpath, gboolean path_currently_selected,
-                                               gpointer data)
+                                    GtkTreePath *tpath, gboolean path_currently_selected,
+                                    gpointer data)
 {
        UtilityData *ud = data;
        GtkTreeIter iter;
@@ -1041,6 +1296,8 @@ static gboolean file_util_preview_cb(GtkTreeSelection *selection, GtkTreeModel *
        gtk_tree_model_get(store, &iter, UTILITY_COLUMN_FD, &fd, -1);
        generic_dialog_image_set(ud->gd, fd);
        
+       ud->sel_fd = fd;
+       
        if (ud->type == UTILITY_TYPE_RENAME)
                {
                const gchar *name = filename_from_path(fd->change->dest);
@@ -1071,6 +1328,14 @@ static void box_append_safe_delete_status(GenericDialog *gd)
        gtk_widget_set_sensitive(label, FALSE);
 }
 
+static void file_util_details_cb(GenericDialog *gd, gpointer data)
+{
+       UtilityData *ud = data;
+       if (ud->details_func && ud->sel_fd)
+               {
+               ud->details_func(ud, ud->sel_fd);
+               }
+}
 
 static void file_util_dialog_init_simple_list(UtilityData *ud)
 {
@@ -1078,10 +1343,26 @@ static void file_util_dialog_init_simple_list(UtilityData *ud)
        GtkTreeSelection *selection;
        gchar *dir_msg;
 
-       ud->gd = file_util_gen_dlg(ud->messages.title, GQ_WMCLASS, "dlg_confirm",
+       const gchar *stock_id;
+
+       /* FIXME: use ud->stock_id */
+       if (ud->type == UTILITY_TYPE_DELETE ||
+           ud->type == UTILITY_TYPE_DELETE_LINK ||
+           ud->type == UTILITY_TYPE_DELETE_FOLDER)
+               {
+               stock_id = GTK_STOCK_DELETE;
+               }
+       else
+               {
+               stock_id = GTK_STOCK_OK;
+               }
+
+       ud->gd = file_util_gen_dlg(ud->messages.title, "dlg_confirm",
                                   ud->parent, FALSE,  file_util_cancel_cb, ud);
-       generic_dialog_add_button(ud->gd, GTK_STOCK_DELETE, NULL, file_util_ok_cb, TRUE);
+       if (ud->discard_func) generic_dialog_add_button(ud->gd, GTK_STOCK_REVERT_TO_SAVED, _("Discard changes"), file_util_discard_cb, FALSE);
+       if (ud->details_func) generic_dialog_add_button(ud->gd, GTK_STOCK_INFO, _("File details"), file_util_details_cb, FALSE);
 
+       generic_dialog_add_button(ud->gd, stock_id, NULL, file_util_ok_cb, TRUE);
 
        if (ud->dir_fd)
                {
@@ -1100,8 +1381,10 @@ static void file_util_dialog_init_simple_list(UtilityData *ud)
        
        box = pref_group_new(box, TRUE, ud->messages.desc_flist, GTK_ORIENTATION_HORIZONTAL);
 
-       ud->listview = file_util_dialog_add_list(box, ud->flist, FALSE);
-       file_util_dialog_add_list_column(ud->listview, _("Sidecars"), UTILITY_COLUMN_SIDECARS);
+       ud->listview = file_util_dialog_add_list(box, ud->flist, FALSE, ud->with_sidecars);
+       if (ud->with_sidecars) file_util_dialog_add_list_column(ud->listview, _("Sidecars"), FALSE, UTILITY_COLUMN_SIDECARS);
+
+       if (ud->type == UTILITY_TYPE_WRITE_METADATA) file_util_dialog_add_list_column(ud->listview, _("Write to file"), FALSE, UTILITY_COLUMN_DEST_NAME);
 
        selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(ud->listview));
        gtk_tree_selection_set_mode(selection, GTK_SELECTION_SINGLE);
@@ -1109,7 +1392,10 @@ static void file_util_dialog_init_simple_list(UtilityData *ud)
 
        generic_dialog_add_image(ud->gd, box, NULL, NULL, NULL, NULL, FALSE);
 
-       box_append_safe_delete_status(ud->gd);
+       if (ud->type == UTILITY_TYPE_DELETE ||
+           ud->type == UTILITY_TYPE_DELETE_LINK ||
+           ud->type == UTILITY_TYPE_DELETE_FOLDER)
+               box_append_safe_delete_status(ud->gd);
 
        gtk_widget_show(ud->gd->dialog);
 
@@ -1122,7 +1408,7 @@ static void file_util_dialog_init_dest_folder(UtilityData *ud)
        GtkWidget *label;
        const gchar *stock_id;
 
-       if (ud->type == UTILITY_TYPE_COPY) 
+       if (ud->type == UTILITY_TYPE_COPY)
                {
                stock_id = GTK_STOCK_COPY;
                }
@@ -1131,7 +1417,7 @@ static void file_util_dialog_init_dest_folder(UtilityData *ud)
                stock_id = GTK_STOCK_OK;
                }
 
-       fdlg = file_util_file_dlg(ud->messages.title, GQ_WMCLASS, "dlg_dest_folder", ud->parent,
+       fdlg = file_util_file_dlg(ud->messages.title, "dlg_dest_folder", ud->parent,
                                  file_util_fdlg_cancel_cb, ud);
        
        ud->fdlg = fdlg;
@@ -1153,7 +1439,7 @@ static void file_util_dialog_init_dest_folder(UtilityData *ud)
 }
 
 
-static GtkWidget *furm_simple_vlabel(GtkWidget *box, const gchar *text, gint expand)
+static GtkWidget *furm_simple_vlabel(GtkWidget *box, const gchar *text, gboolean expand)
 {
        GtkWidget *vbox;
        GtkWidget *label;
@@ -1181,18 +1467,22 @@ static void file_util_dialog_init_source_dest(UtilityData *ud)
        GtkWidget *combo;
        GtkWidget *page;
 
-       ud->gd = file_util_gen_dlg(ud->messages.title, GQ_WMCLASS, "dlg_confirm",
+       ud->gd = file_util_gen_dlg(ud->messages.title, "dlg_confirm",
                                   ud->parent, FALSE,  file_util_cancel_cb, ud);
 
        box = generic_dialog_add_message(ud->gd, NULL, ud->messages.question, NULL);
+
+       if (ud->discard_func) generic_dialog_add_button(ud->gd, GTK_STOCK_REVERT_TO_SAVED, _("Discard changes"), file_util_discard_cb, FALSE);
+       if (ud->details_func) generic_dialog_add_button(ud->gd, GTK_STOCK_INFO, _("File details"), file_util_details_cb, FALSE);
+
        generic_dialog_add_button(ud->gd, GTK_STOCK_OK, ud->messages.title, file_util_ok_cb, TRUE);
 
        box = pref_group_new(box, TRUE, ud->messages.desc_flist, GTK_ORIENTATION_HORIZONTAL);
 
-       ud->listview = file_util_dialog_add_list(box, ud->flist, FALSE);
-       file_util_dialog_add_list_column(ud->listview, _("Sidecars"), UTILITY_COLUMN_SIDECARS);
+       ud->listview = file_util_dialog_add_list(box, ud->flist, FALSE, ud->with_sidecars);
+       file_util_dialog_add_list_column(ud->listview, _("Sidecars"), FALSE, UTILITY_COLUMN_SIDECARS);
 
-       file_util_dialog_add_list_column(ud->listview, _("New name"), UTILITY_COLUMN_DEST_NAME);
+       file_util_dialog_add_list_column(ud->listview, _("New name"), FALSE, UTILITY_COLUMN_DEST_NAME);
 
        selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(ud->listview));
        gtk_tree_selection_set_mode(GTK_TREE_SELECTION(selection), GTK_SELECTION_SINGLE);
@@ -1300,6 +1590,56 @@ static void file_util_dialog_init_source_dest(UtilityData *ud)
        file_util_dialog_list_select(ud->listview, 0);
 }
 
+static void file_util_finalize_all(UtilityData *ud)
+{
+       GList *work = ud->flist;
+       
+       if (ud->phase == UTILITY_PHASE_CANCEL) return;
+       if (ud->phase == UTILITY_PHASE_DONE && !ud->finalize_func) return;
+       if (ud->phase == UTILITY_PHASE_DISCARD && !ud->discard_func) return;
+       
+       while (work)
+               {
+               FileData *fd = work->data;
+               work = work->next;
+               if (ud->phase == UTILITY_PHASE_DONE) ud->finalize_func(fd);
+               else if (ud->phase == UTILITY_PHASE_DISCARD) ud->discard_func(fd);
+               }
+}
+
+static gboolean file_util_exclude_fd(UtilityData *ud, FileData *fd)
+{
+       GtkTreeModel *store;
+       GtkTreeIter iter;
+       gboolean valid;
+
+       if (!g_list_find(ud->flist, fd)) return FALSE;
+
+       store = gtk_tree_view_get_model(GTK_TREE_VIEW(ud->listview));
+       valid = gtk_tree_model_get_iter_first(store, &iter);
+       while (valid)
+               {
+               FileData *store_fd;
+               gtk_tree_model_get(store, &iter, UTILITY_COLUMN_FD, &store_fd, -1);
+
+               if (store_fd == fd)
+                       {
+                       gtk_list_store_remove(GTK_LIST_STORE(store), &iter);
+                       break;
+                       }
+               valid = gtk_tree_model_iter_next(store, &iter);
+               }
+
+       ud->flist = g_list_remove(ud->flist, fd);
+
+       if (ud->with_sidecars)
+               file_data_sc_free_ci(fd);
+       else
+               file_data_free_ci(fd);
+       
+       file_data_unref(fd);
+       return TRUE;
+}
 
 void file_util_dialog_run(UtilityData *ud)
 {
@@ -1313,6 +1653,7 @@ void file_util_dialog_run(UtilityData *ud)
                                case UTILITY_TYPE_DELETE_LINK:
                                case UTILITY_TYPE_DELETE_FOLDER:
                                case UTILITY_TYPE_EDITOR:
+                               case UTILITY_TYPE_WRITE_METADATA:
                                        file_util_dialog_init_simple_list(ud);
                                        break;
                                case UTILITY_TYPE_RENAME:
@@ -1321,15 +1662,19 @@ void file_util_dialog_run(UtilityData *ud)
                                case UTILITY_TYPE_COPY:
                                case UTILITY_TYPE_MOVE:
                                case UTILITY_TYPE_FILTER:
-                               case UTILITY_TYPE_RENAME_FOLDER:
+                               case UTILITY_TYPE_CREATE_FOLDER:
                                        file_util_dialog_init_dest_folder(ud);
                                        break;
+                               case UTILITY_TYPE_RENAME_FOLDER:
+                                       ud->phase = UTILITY_PHASE_CANCEL; /* FIXME - not handled for now */
+                                       file_util_dialog_run(ud);
+                                       return;
                                }
                        ud->phase = UTILITY_PHASE_ENTERING;
                        break;
                case UTILITY_PHASE_ENTERING:
-                       /* FIXME use file_data_sc_check_ci_dest to detect problems and eventually go back to PHASE_START 
-                       or to PHASE_CANCEL */
+                       file_util_check_ci(ud);
+                       break;
                
                        ud->phase = UTILITY_PHASE_CHECKED;
                case UTILITY_PHASE_CHECKED:
@@ -1337,9 +1682,23 @@ void file_util_dialog_run(UtilityData *ud)
                        break;
                case UTILITY_PHASE_CANCEL:
                case UTILITY_PHASE_DONE:
-                       file_data_sc_free_ci_list(ud->flist);
+               case UTILITY_PHASE_DISCARD:
+
+                       file_util_finalize_all(ud);
+                       
+                       /* both DISCARD and DONE finishes the operation for good */
+                       if (ud->done_func)
+                               ud->done_func((ud->phase != UTILITY_PHASE_CANCEL), ud->dest_path, ud->done_data);
+                               
+                       if (ud->with_sidecars)
+                               file_data_sc_free_ci_list(ud->flist);
+                       else
+                               file_data_free_ci_list(ud->flist);
+                       
+                       /* directory content is always handled including sidecars */
                        file_data_sc_free_ci_list(ud->content_list);
-                       if (ud->dir_fd) file_data_sc_free_ci(ud->dir_fd);
+                       
+                       if (ud->dir_fd) file_data_free_ci(ud->dir_fd);
                        file_util_data_free(ud);
                        break;
                }
@@ -1353,185 +1712,524 @@ static void file_util_warn_op_in_progress(const gchar *title)
        file_util_warning_dialog(title, _("Another operation in progress.\n"), GTK_STOCK_DIALOG_ERROR, NULL);
 }
 
-static void file_util_disable_grouping_sc_list(GList *list)
+static void file_util_details_dialog_close_cb(GtkWidget *widget, gpointer data)
 {
-       GList *work = list;
-       
-       while (work)
-               {
-               FileData *fd = work->data;
-               work = work->next;
-               
-               if (fd->parent) file_data_disable_grouping(fd, TRUE);
-               }
-               
+       gtk_widget_destroy(data);
+
 }
 
-static void file_util_delete_full(FileData *source_fd, GList *source_list, GtkWidget *parent, UtilityPhase phase)
+static void file_util_details_dialog_destroy_cb(GtkWidget *widget, gpointer data)
 {
-       UtilityData *ud;
-       GList *flist = filelist_copy(source_list);
-       
-       if (source_fd)
-               flist = g_list_append(flist, file_data_ref(source_fd));
+       UtilityData *ud = data;
+       g_signal_handlers_disconnect_by_func(ud->gd->dialog, G_CALLBACK(file_util_details_dialog_close_cb), widget);
+}
 
-       file_util_disable_grouping_sc_list(flist);
-       
-       if (!file_data_sc_add_ci_delete_list(flist))
-               {
-               file_util_warn_op_in_progress(_("File deletion failed"));
-               filelist_free(flist);
-               return;
+
+static void file_util_details_dialog_ok_cb(GenericDialog *gd, gpointer data)
+{
+       /* no op */
+}
+
+static void file_util_details_dialog_exclude(GenericDialog *gd, gpointer data, gboolean discard)
+{
+       UtilityData *ud = data;
+       FileData *fd = g_object_get_data(G_OBJECT(gd->dialog), "file_data");
+       
+       if (!fd) return;
+       file_util_exclude_fd(ud, fd);
+       
+       if (discard && ud->discard_func) ud->discard_func(fd);
+       
+       /* all files were excluded, this has the same effect as pressing the cancel button in the confirmation dialog*/
+       if (!ud->flist)
+               {
+               /* both dialogs will be closed anyway, the signals would cause duplicate calls */
+               g_signal_handlers_disconnect_by_func(ud->gd->dialog, G_CALLBACK(file_util_details_dialog_close_cb), gd->dialog);
+               g_signal_handlers_disconnect_by_func(gd->dialog, G_CALLBACK(file_util_details_dialog_destroy_cb), ud);
+
+               file_util_cancel_cb(ud->gd, ud);
+               }
+}
+
+static void file_util_details_dialog_exclude_cb(GenericDialog *gd, gpointer data)
+{
+       file_util_details_dialog_exclude(gd, data, FALSE);
+}
+
+static void file_util_details_dialog_discard_cb(GenericDialog *gd, gpointer data)
+{
+       file_util_details_dialog_exclude(gd, data, TRUE);
+}
+
+static gchar *file_util_details_get_message(UtilityData *ud, FileData *fd, const gchar **stock_id)
+{
+       GString *message = g_string_new("");
+       gint error;
+       g_string_append_printf(message, _("File: '%s'\n"), fd->path);
+       
+       if (ud->with_sidecars && fd->sidecar_files)
+               {
+               GList *work = fd->sidecar_files;
+               g_string_append(message, _("with sidecar files:\n"));
+               
+               while (work)
+                       {
+                       FileData *sfd = work->data;
+                       work =work->next;
+                       g_string_append_printf(message, _(" '%s'\n"), sfd->path);
+                       }
+               }
+       
+       g_string_append(message, _("\nStatus: "));
+       
+       error = ud->with_sidecars ? file_data_sc_verify_ci(fd) : file_data_verify_ci(fd);
+
+       if (error)
+               {
+               gchar *err_msg = file_data_get_error_string(error);
+               g_string_append(message, err_msg);
+               if (stock_id) *stock_id = (error & CHANGE_ERROR_MASK) ? GTK_STOCK_DIALOG_ERROR : GTK_STOCK_DIALOG_WARNING;
+               }
+       else
+               {
+               g_string_append(message, _("no problem detected"));
+               if (stock_id) *stock_id = GTK_STOCK_DIALOG_INFO;
+               }
+
+       return g_string_free(message, FALSE);;
+}
+
+static void file_util_details_dialog(UtilityData *ud, FileData *fd)
+{
+       GenericDialog *gd;
+       GtkWidget *box;
+       gchar *message;
+       const gchar *stock_id;
+       
+       gd = file_util_gen_dlg(_("File details"), "details", ud->gd->dialog, TRUE, NULL, ud);
+       generic_dialog_add_button(gd, GTK_STOCK_CLOSE, NULL, file_util_details_dialog_ok_cb, TRUE);
+       generic_dialog_add_button(gd, GTK_STOCK_REMOVE, _("Exclude file"), file_util_details_dialog_exclude_cb, FALSE);
+
+       g_object_set_data(G_OBJECT(gd->dialog), "file_data", fd);
+
+       g_signal_connect(G_OBJECT(gd->dialog), "destroy",
+                        G_CALLBACK(file_util_details_dialog_destroy_cb), ud);
+
+       /* in case the ud->gd->dialog is closed during editing */
+       g_signal_connect(G_OBJECT(ud->gd->dialog), "destroy",
+                        G_CALLBACK(file_util_details_dialog_close_cb), gd->dialog);
+
+
+       message = file_util_details_get_message(ud, fd, &stock_id);
+
+       box = generic_dialog_add_message(gd, stock_id, _("File details"), message);
+
+       generic_dialog_add_image(gd, box, fd, NULL, NULL, NULL, FALSE);
+
+       gtk_widget_show(gd->dialog);
+       
+       g_free(message);
+}
+
+static void file_util_write_metadata_details_dialog(UtilityData *ud, FileData *fd)
+{
+       GenericDialog *gd;
+       GtkWidget *box;
+       GtkWidget *table;
+       GtkWidget *frame;
+       GtkWidget *label;
+       GList *keys = NULL;
+       GList *work;
+       gchar *message1;
+       gchar *message2;
+       gint i;
+       const gchar *stock_id;
+       
+       if (fd && fd->modified_xmp)
+               {
+               keys = g_hash_table_get_keys(fd->modified_xmp);
+               }
+       
+       g_assert(keys);
+       
+       
+       gd = file_util_gen_dlg(_("Overview of changed metadata"), "details", ud->gd->dialog, TRUE, NULL, ud);
+       generic_dialog_add_button(gd, GTK_STOCK_CLOSE, NULL, file_util_details_dialog_ok_cb, TRUE);
+       generic_dialog_add_button(gd, GTK_STOCK_REMOVE, _("Exclude file"), file_util_details_dialog_exclude_cb, FALSE);
+       generic_dialog_add_button(gd, GTK_STOCK_REVERT_TO_SAVED, _("Discard changes"), file_util_details_dialog_discard_cb, FALSE);
+
+       g_object_set_data(G_OBJECT(gd->dialog), "file_data", fd);
+
+       g_signal_connect(G_OBJECT(gd->dialog), "destroy",
+                        G_CALLBACK(file_util_details_dialog_destroy_cb), ud);
+
+       /* in case the ud->gd->dialog is closed during editing */
+       g_signal_connect(G_OBJECT(ud->gd->dialog), "destroy",
+                        G_CALLBACK(file_util_details_dialog_close_cb), gd->dialog);
+
+       message1 = file_util_details_get_message(ud, fd, &stock_id);
+
+       if (fd->change && fd->change->dest)
+               {
+               message2 = g_strdup_printf(_("The following metadata tags will be written to\n'%s'."), fd->change->dest);
+               }
+       else
+               {
+               message2 = g_strdup_printf(_("The following metadata tags will be written to the image file itself."));
+               }
+
+       box = generic_dialog_add_message(gd, stock_id, _("Overview of changed metadata"), message1);
+
+       box = pref_group_new(box, TRUE, message2, GTK_ORIENTATION_HORIZONTAL);
+
+       frame = pref_frame_new(box, TRUE, NULL, GTK_ORIENTATION_HORIZONTAL, 2);
+       table = pref_table_new(frame, 2, g_list_length(keys), FALSE, TRUE);
+
+       work = keys;
+       i = 0;
+       while (work)
+               {
+               const gchar *key = work->data;
+               gchar *title = exif_get_description_by_key(key);
+               gchar *title_f = g_strdup_printf("%s:", title);
+               gchar *value = metadata_read_string(fd, key, METADATA_FORMATTED);
+               work = work->next;
+               
+               
+               label = gtk_label_new(title_f);
+               gtk_misc_set_alignment(GTK_MISC(label), 1.0, 0.0);
+               pref_label_bold(label, TRUE, FALSE);
+               gtk_table_attach(GTK_TABLE(table), label,
+                                0, 1, i, i + 1,
+                                GTK_FILL, GTK_FILL,
+                                2, 2);
+               gtk_widget_show(label);
+
+               label = gtk_label_new(value);
+               
+               gtk_misc_set_alignment(GTK_MISC(label), 0.0, 0.0);
+               gtk_label_set_line_wrap(GTK_LABEL(label), TRUE);
+               gtk_table_attach(GTK_TABLE(table), label,
+                                1, 2, i, i + 1,
+                                GTK_FILL, GTK_FILL,
+                                2, 2);
+               gtk_widget_show(label);
+
+               g_free(title);
+               g_free(title_f);
+               g_free(value);
+               i++;
                }
 
+       generic_dialog_add_image(gd, box, fd, NULL, NULL, NULL, FALSE);
+
+       gtk_widget_set_size_request(gd->dialog, DIALOG_WIDTH, -1);
+       gtk_widget_show(gd->dialog);
+       
+       g_list_free(keys);
+       g_free(message1);
+       g_free(message2);
+}
+
+
+static void file_util_mark_ungrouped_files(GList *work)
+{
+       while (work)
+               {
+               FileData *fd = work->data;
+               file_data_set_regroup_when_finished(fd, TRUE);
+               work = work->next;
+               }
+}
+
+
+static void file_util_delete_full(FileData *source_fd, GList *flist, GtkWidget *parent, UtilityPhase phase)
+{
+       UtilityData *ud;
+       GList *ungrouped = NULL;
+       
+       if (source_fd)
+               flist = g_list_append(flist, file_data_ref(source_fd));
+
+       if (!flist) return;
+       
+       flist = file_data_process_groups_in_selection(flist, TRUE, &ungrouped);
+       
+       if (!file_data_sc_add_ci_delete_list(flist))
+               {
+               file_util_warn_op_in_progress(_("File deletion failed"));
+               file_data_disable_grouping_list(ungrouped, FALSE);
+               filelist_free(flist);
+               filelist_free(ungrouped);
+               return;
+               }
+       
+       file_util_mark_ungrouped_files(ungrouped);
+       filelist_free(ungrouped);
+
        ud = file_util_data_new(UTILITY_TYPE_DELETE);
        
        ud->phase = phase;
+       
+       ud->with_sidecars = TRUE;
 
        ud->dir_fd = NULL;
        ud->flist = flist;
        ud->content_list = NULL;
        ud->parent = parent;
        
+       ud->details_func = file_util_details_dialog;
+
        ud->messages.title = _("Delete");
        ud->messages.question = _("Delete files?");
        ud->messages.desc_flist = _("This will delete the following files");
-       ud->messages.desc_dlist = "";
        ud->messages.desc_source_fd = "";
        ud->messages.fail = _("File deletion failed");
 
        file_util_dialog_run(ud);
 }
 
-static void file_util_move_full(FileData *source_fd, GList *source_list, const gchar *dest_path, GtkWidget *parent, UtilityPhase phase)
+
+static void file_util_write_metadata_full(FileData *source_fd, GList *flist, GtkWidget *parent, UtilityPhase phase, FileUtilDoneFunc done_func, gpointer done_data)
+{
+       UtilityData *ud;
+       
+       if (source_fd)
+               flist = g_list_append(flist, file_data_ref(source_fd));
+
+       if (!flist) return;
+       
+       if (!file_data_add_ci_write_metadata_list(flist))
+               {
+               file_util_warn_op_in_progress(_("Can't write metadata"));
+               filelist_free(flist);
+               return;
+               }
+
+       ud = file_util_data_new(UTILITY_TYPE_WRITE_METADATA);
+       
+       ud->phase = phase;
+
+       ud->with_sidecars = FALSE; /* operate on individual files, not groups */
+
+       ud->dir_fd = NULL;
+       ud->flist = flist;
+       ud->content_list = NULL;
+       ud->parent = parent;
+       
+       ud->done_func = done_func;
+       ud->done_data = done_data;
+       
+       ud->details_func = file_util_write_metadata_details_dialog;
+       ud->finalize_func = metadata_write_queue_remove;
+       ud->discard_func = metadata_write_queue_remove;
+       
+       ud->messages.title = _("Write metadata");
+       ud->messages.question = _("Write metadata?");
+       ud->messages.desc_flist = _("This will write the changed metadata into the following files");
+       ud->messages.desc_source_fd = "";
+       ud->messages.fail = _("Metadata writing failed");
+
+       file_util_dialog_run(ud);
+}
+
+static void file_util_move_full(FileData *source_fd, GList *flist, const gchar *dest_path, GtkWidget *parent, UtilityPhase phase)
 {
        UtilityData *ud;
-       GList *flist = filelist_copy(source_list);
+       GList *ungrouped = NULL;
        
        if (source_fd)
                flist = g_list_append(flist, file_data_ref(source_fd));
 
-       file_util_disable_grouping_sc_list(flist);
+       if (!flist) return;
+
+       flist = file_data_process_groups_in_selection(flist, TRUE, &ungrouped);
 
        if (!file_data_sc_add_ci_move_list(flist, dest_path))
                {
                file_util_warn_op_in_progress(_("Move failed"));
+               file_data_disable_grouping_list(ungrouped, FALSE);
                filelist_free(flist);
+               filelist_free(ungrouped);
                return;
                }
+       
+       file_util_mark_ungrouped_files(ungrouped);
+       filelist_free(ungrouped);
 
        ud = file_util_data_new(UTILITY_TYPE_MOVE);
 
        ud->phase = phase;
 
+       ud->with_sidecars = TRUE;
+
        ud->dir_fd = NULL;
        ud->flist = flist;
        ud->content_list = NULL;
        ud->parent = parent;
 
-       ud->dest_path = g_strdup(dest_path ? dest_path : "");
+       if (dest_path) ud->dest_path = g_strdup(dest_path);
        
        ud->messages.title = _("Move");
        ud->messages.question = _("Move files?");
        ud->messages.desc_flist = _("This will move the following files");
-       ud->messages.desc_dlist = "";
        ud->messages.desc_source_fd = "";
        ud->messages.fail = _("Move failed");
 
        file_util_dialog_run(ud);
 }
-static void file_util_copy_full(FileData *source_fd, GList *source_list, const gchar *dest_path, GtkWidget *parent, UtilityPhase phase)
+
+static void file_util_copy_full(FileData *source_fd, GList *flist, const gchar *dest_path, GtkWidget *parent, UtilityPhase phase)
 {
        UtilityData *ud;
-       GList *flist = filelist_copy(source_list);
+       GList *ungrouped = NULL;
        
        if (source_fd)
                flist = g_list_append(flist, file_data_ref(source_fd));
 
-       file_util_disable_grouping_sc_list(flist);
+       if (!flist) return;
+
+       if (file_util_write_metadata_first(UTILITY_TYPE_COPY, phase, flist, dest_path, NULL, parent))
+               return;
+
+       flist = file_data_process_groups_in_selection(flist, TRUE, &ungrouped);
 
        if (!file_data_sc_add_ci_copy_list(flist, dest_path))
                {
                file_util_warn_op_in_progress(_("Copy failed"));
+               file_data_disable_grouping_list(ungrouped, FALSE);
                filelist_free(flist);
+               filelist_free(ungrouped);
                return;
                }
 
+       file_util_mark_ungrouped_files(ungrouped);
+       filelist_free(ungrouped);
+
        ud = file_util_data_new(UTILITY_TYPE_COPY);
 
        ud->phase = phase;
 
+       ud->with_sidecars = TRUE;
+
        ud->dir_fd = NULL;
        ud->flist = flist;
        ud->content_list = NULL;
        ud->parent = parent;
 
-       ud->dest_path = g_strdup(dest_path ? dest_path : "");
+       if (dest_path) ud->dest_path = g_strdup(dest_path);
        
        ud->messages.title = _("Copy");
        ud->messages.question = _("Copy files?");
        ud->messages.desc_flist = _("This will copy the following files");
-       ud->messages.desc_dlist = "";
        ud->messages.desc_source_fd = "";
        ud->messages.fail = _("Copy failed");
 
        file_util_dialog_run(ud);
 }
 
-static void file_util_rename_full(FileData *source_fd, GList *source_list, const gchar *dest_path, GtkWidget *parent, UtilityPhase phase)
+static void file_util_rename_full(FileData *source_fd, GList *flist, const gchar *dest_path, GtkWidget *parent, UtilityPhase phase)
 {
        UtilityData *ud;
-       GList *flist = filelist_copy(source_list);
+       GList *ungrouped = NULL;
        
        if (source_fd)
                flist = g_list_append(flist, file_data_ref(source_fd));
 
-       file_util_disable_grouping_sc_list(flist);
+       if (!flist) return;
+
+       flist = file_data_process_groups_in_selection(flist, TRUE, &ungrouped);
 
        if (!file_data_sc_add_ci_rename_list(flist, dest_path))
                {
                file_util_warn_op_in_progress(_("Rename failed"));
+               file_data_disable_grouping_list(ungrouped, FALSE);
                filelist_free(flist);
+               filelist_free(ungrouped);
                return;
                }
 
+       file_util_mark_ungrouped_files(ungrouped);
+       filelist_free(ungrouped);
+
        ud = file_util_data_new(UTILITY_TYPE_RENAME);
 
        ud->phase = phase;
 
+       ud->with_sidecars = TRUE;
+
        ud->dir_fd = NULL;
        ud->flist = flist;
        ud->content_list = NULL;
        ud->parent = parent;
+
+       ud->details_func = file_util_details_dialog;
        
        ud->messages.title = _("Rename");
        ud->messages.question = _("Rename files?");
        ud->messages.desc_flist = _("This will rename the following files");
-       ud->messages.desc_dlist = "";
        ud->messages.desc_source_fd = "";
        ud->messages.fail = _("Rename failed");
 
        file_util_dialog_run(ud);
 }
 
-static void file_util_start_editor_full(gint n, FileData *source_fd, GList *source_list, const gchar *dest_path, GtkWidget *parent, UtilityPhase phase)
+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)
 {
        UtilityData *ud;
-       GList *flist = filelist_copy(source_list);
+       GList *ungrouped = NULL;
+       
+       if (editor_no_param(key))
+               {
+               gchar *file_directory = NULL;
+               if (!working_directory)
+                       {
+                       /* working directory was not specified, try to extract it from the files */
+                       if (source_fd)
+                               file_directory = remove_level_from_path(source_fd->path);
+
+                       if (!file_directory && flist)
+                               file_directory = remove_level_from_path(((FileData *)flist->data)->path);
+                       working_directory = file_directory;
+                       }
+               
+               /* just start the editor, don't care about files */
+               start_editor(key, working_directory);
+               g_free(file_directory);
+               filelist_free(flist);
+               return;
+               }
+       
        
        if (source_fd)
+               {
+               /* flist is most probably NULL
+                  operate on source_fd and it's sidecars
+               */
+               flist = g_list_concat(filelist_copy(source_fd->sidecar_files), flist);
                flist = g_list_append(flist, file_data_ref(source_fd));
+               }
+
+       if (!flist) return;
+       
+       if (file_util_write_metadata_first(UTILITY_TYPE_FILTER, phase, flist, dest_path, key, parent))
+               return;
 
-       file_util_disable_grouping_sc_list(flist);
+       flist = file_data_process_groups_in_selection(flist, TRUE, &ungrouped);
 
        if (!file_data_sc_add_ci_unspecified_list(flist, dest_path))
                {
                file_util_warn_op_in_progress(_("Can't run external editor"));
+               file_data_disable_grouping_list(ungrouped, FALSE);
                filelist_free(flist);
+               filelist_free(ungrouped);
                return;
                }
 
-       if (editor_is_filter(n))
+       file_util_mark_ungrouped_files(ungrouped);
+       filelist_free(ungrouped);
+
+       if (editor_is_filter(key))
                ud = file_util_data_new(UTILITY_TYPE_FILTER);
        else
                ud = file_util_data_new(UTILITY_TYPE_EDITOR);
@@ -1541,28 +2239,51 @@ static void file_util_start_editor_full(gint n, FileData *source_fd, GList *sour
        if (ud->type == UTILITY_TYPE_FILTER && dest_path == NULL) phase = UTILITY_PHASE_START;
        
        ud->phase = phase;
+
+       ud->with_sidecars = TRUE;
        
-       ud->external_command = n;
+       ud->external_command = g_strdup(key);
 
        ud->dir_fd = NULL;
        ud->flist = flist;
        ud->content_list = NULL;
        ud->parent = parent;
 
-       ud->dest_path = g_strdup(dest_path ? dest_path : "");
+       ud->details_func = file_util_details_dialog;
+
+       if (dest_path) ud->dest_path = g_strdup(dest_path);
        
        ud->messages.title = _("Editor");
        ud->messages.question = _("Run editor?");
        ud->messages.desc_flist = _("This will copy the following files");
-       ud->messages.desc_dlist = "";
        ud->messages.desc_source_fd = "";
        ud->messages.fail = _("External command failed");
 
        file_util_dialog_run(ud);
 }
 
-static GList *file_util_delete_dir_remaining_folders(GList *dlist);
+static GList *file_util_delete_dir_remaining_folders(GList *dlist)
+{
+       GList *rlist = NULL;
 
+       while (dlist)
+               {
+               FileData *fd;
+
+               fd = dlist->data;
+               dlist = dlist->next;
+
+               if (!fd->name ||
+                   (strcmp(fd->name, THUMB_FOLDER_GLOBAL) != 0 &&
+                    strcmp(fd->name, THUMB_FOLDER_LOCAL) != 0 &&
+                    strcmp(fd->name, GQ_CACHE_LOCAL_METADATA) != 0) )
+                       {
+                       rlist = g_list_prepend(rlist, fd);
+                       }
+               }
+
+       return g_list_reverse(rlist);
+}
 
 static gboolean file_util_delete_dir_empty_path(UtilityData *ud, FileData *fd, gint level)
 {
@@ -1590,7 +2311,8 @@ static gboolean file_util_delete_dir_empty_path(UtilityData *ud, FileData *fd, g
 
        if (ok)
                {
-               if ((ok = file_data_sc_add_ci_delete(fd)))
+               ok = file_data_sc_add_ci_delete(fd);
+               if (ok)
                        {
                        ud->content_list = g_list_prepend(ud->content_list, fd);
                        }
@@ -1606,7 +2328,6 @@ static gboolean file_util_delete_dir_empty_path(UtilityData *ud, FileData *fd, g
                work = work->next;
 
                ok = file_util_delete_dir_empty_path(ud, lfd, level);
-
                }
 
        work = flist;
@@ -1619,7 +2340,8 @@ static gboolean file_util_delete_dir_empty_path(UtilityData *ud, FileData *fd, g
 
                DEBUG_1("deltree child: %s", lfd->path);
 
-               if ((ok = file_data_sc_add_ci_delete(lfd)))
+               ok = file_data_sc_add_ci_delete(lfd);
+               if (ok)
                        {
                        ud->content_list = g_list_prepend(ud->content_list, lfd);
                        }
@@ -1697,6 +2419,7 @@ static void file_util_delete_dir_full(FileData *fd, GtkWidget *parent, UtilityPh
                ud = file_util_data_new(UTILITY_TYPE_DELETE_LINK);
 
                ud->phase = phase;
+               ud->with_sidecars = TRUE;
                ud->dir_fd = file_data_ref(fd);
                ud->content_list = NULL;
                ud->flist = NULL;
@@ -1706,9 +2429,8 @@ static void file_util_delete_dir_full(FileData *fd, GtkWidget *parent, UtilityPh
                ud->messages.title = _("Delete folder");
                ud->messages.question = _("Delete symbolic link?");
                ud->messages.desc_flist = "";
-               ud->messages.desc_dlist = "";
                ud->messages.desc_source_fd = _("This will delete the symbolic link.\n"
-                                               "The folder this link points to will not be deleted.");
+                                               "The folder this link points to will not be deleted.");
                ud->messages.fail = _("Link deletion failed");
 
                file_util_dialog_run(ud);
@@ -1745,7 +2467,7 @@ static void file_util_delete_dir_full(FileData *fd, GtkWidget *parent, UtilityPh
                GtkWidget *box;
                gchar *text;
 
-               gd = file_util_gen_dlg(_("Folder contains subfolders"), GQ_WMCLASS, "dlg_warning",
+               gd = file_util_gen_dlg(_("Folder contains subfolders"), "dlg_warning",
                                        parent, TRUE, NULL, NULL);
                generic_dialog_add_button(gd, GTK_STOCK_CLOSE, NULL, NULL, TRUE);
 
@@ -1760,7 +2482,7 @@ static void file_util_delete_dir_full(FileData *fd, GtkWidget *parent, UtilityPh
                box = pref_group_new(box, TRUE, _("Subfolders:"), GTK_ORIENTATION_VERTICAL);
 
                rlist = filelist_sort_path(rlist);
-               file_util_dialog_add_list(box, rlist, FALSE);
+               file_util_dialog_add_list(box, rlist, FALSE, FALSE);
 
                gtk_widget_show(gd->dialog);
                }
@@ -1770,6 +2492,7 @@ static void file_util_delete_dir_full(FileData *fd, GtkWidget *parent, UtilityPh
                ud = file_util_data_new(UTILITY_TYPE_DELETE_FOLDER);
 
                ud->phase = phase;
+               ud->with_sidecars = TRUE;
                ud->dir_fd = file_data_ref(fd);
                ud->content_list = NULL; /* will be filled by file_util_delete_dir_prepare */
                ud->flist = flist = filelist_sort_path(flist);
@@ -1780,7 +2503,7 @@ static void file_util_delete_dir_full(FileData *fd, GtkWidget *parent, UtilityPh
                ud->messages.question = _("Delete folder?");
                ud->messages.desc_flist = _("The folder contains these files:");
                ud->messages.desc_source_fd = _("This will delete the folder.\n"
-                                               "The contents of this folder will also be deleted.");
+                                               "The contents of this folder will also be deleted.");
                ud->messages.fail = _("File deletion failed");
                
                if (!file_util_delete_dir_prepare(ud, flist, dlist))
@@ -1839,7 +2562,7 @@ static gboolean file_util_rename_dir_scan(UtilityData *ud, FileData *fd)
        return ok;
 }
 
-static gboolean file_util_rename_dir_prepare(UtilityData *ud, const char *new_path)
+static gboolean file_util_rename_dir_prepare(UtilityData *ud, const gchar *new_path)
 {
        gboolean ok;
        GList *work;
@@ -1859,7 +2582,7 @@ static gboolean file_util_rename_dir_prepare(UtilityData *ud, const char *new_pa
                
                g_assert(strncmp(fd->path, ud->dir_fd->path, orig_len) == 0);
                
-               np = g_strdup_printf("%s%s", new_path, fd->path + orig_len);
+               np = g_strconcat(new_path, fd->path + orig_len, NULL);
                
                ok = file_data_sc_add_ci_rename(fd, np);
                
@@ -1884,27 +2607,33 @@ static gboolean file_util_rename_dir_prepare(UtilityData *ud, const char *new_pa
                        file_data_sc_free_ci(fd);
                        }
                }
+
        return ok;
-}      
+}
        
 
-static void file_util_rename_dir_full(FileData *fd, const char *new_path, GtkWidget *parent, UtilityPhase phase)
+static void file_util_rename_dir_full(FileData *fd, const gchar *new_path, GtkWidget *parent, UtilityPhase phase, FileUtilDoneFunc done_func, gpointer done_data)
 {
        UtilityData *ud;
 
        ud = file_util_data_new(UTILITY_TYPE_RENAME_FOLDER);
 
        ud->phase = phase;
+       ud->with_sidecars = TRUE; /* does not matter, the directory should not have sidecars
+                                   and the content must be handled including sidecars */
 
        ud->dir_fd = file_data_ref(fd);
        ud->flist = NULL;
        ud->content_list = NULL;
        ud->parent = parent;
+
+       ud->done_func = done_func;
+       ud->done_data = done_data;
+       ud->dest_path = g_strdup(new_path);
        
        ud->messages.title = _("Rename");
        ud->messages.question = _("Rename folder?");
        ud->messages.desc_flist = _("The folder contains the following files");
-       ud->messages.desc_dlist = "";
        ud->messages.desc_source_fd = "";
        ud->messages.fail = _("Rename failed");
 
@@ -1920,22 +2649,142 @@ static void file_util_rename_dir_full(FileData *fd, const char *new_path, GtkWid
        file_util_dialog_run(ud);
 }
 
-/* FIXME: */
-void file_util_create_dir(FileData *dir_fd, GtkWidget *parent)
+static void file_util_create_dir_full(FileData *fd, const gchar *dest_path, GtkWidget *parent, UtilityPhase phase, FileUtilDoneFunc done_func, gpointer done_data)
+{
+       UtilityData *ud;
+
+       ud = file_util_data_new(UTILITY_TYPE_CREATE_FOLDER);
+
+       ud->phase = phase;
+       ud->with_sidecars = TRUE;
+
+       ud->dir_fd = NULL;
+       ud->flist = NULL;
+       ud->content_list = NULL;
+       ud->parent = parent;
+
+       if (dest_path)
+               {
+               ud->dest_path = g_strdup(dest_path);
+               }
+       else
+               {
+               gchar *buf = g_build_filename(fd->path, _("New folder"), NULL);
+               ud->dest_path = unique_filename(buf, NULL, " ", FALSE);
+               g_free(buf);
+               }
+       
+       ud->dir_fd = file_data_new_dir(ud->dest_path);
+
+       ud->done_func = done_func;
+       ud->done_data = done_data;
+       
+       ud->messages.title = _("Create Folder");
+       ud->messages.question = _("Create folder?");
+       ud->messages.desc_flist = "";
+       ud->messages.desc_source_fd = "";
+       ud->messages.fail = _("Can't create folder");
+
+       file_util_dialog_run(ud);
+}
+
+
+static gboolean file_util_write_metadata_first_after_done(gpointer data)
 {
+       UtilityDelayData *dd = data;
+       
+       /* start the delayed operation with original arguments */
+       switch (dd->type)
+               {
+               case UTILITY_TYPE_FILTER:
+               case UTILITY_TYPE_EDITOR:
+                       file_util_start_editor_full(dd->editor_key, NULL, dd->flist, dd->dest_path, NULL, dd->parent, dd->phase);
+                       break;
+               case UTILITY_TYPE_COPY:
+                       file_util_copy_full(NULL, dd->flist, dd->dest_path, dd->parent, dd->phase);
+                       break;
+               default:
+                       g_warning("unsupported type");
+               }
+       g_free(dd->dest_path);
+       g_free(dd->editor_key);
+       g_free(dd);
+       return FALSE;
 }
 
-gint file_util_rename_dir(FileData *source_fd, const gchar *new_path, GtkWidget *parent)
+static void file_util_write_metadata_first_done(gboolean success, const gchar *done_path, gpointer data)
 {
-       file_util_rename_dir_full(source_fd, new_path, parent, UTILITY_PHASE_ENTERING);
+       UtilityDelayData *dd = data;
+
+       if (success)
+               {
+               dd->idle_id = g_idle_add(file_util_write_metadata_first_after_done, dd);
+               return;
+               }
+       
+       /* the operation was cancelled */
+       filelist_free(dd->flist);
+       g_free(dd->dest_path);
+       g_free(dd->editor_key);
+       g_free(dd);
 }
 
+static gboolean file_util_write_metadata_first(UtilityType type, UtilityPhase phase, GList *flist, const gchar *dest_path, const gchar *editor_key, GtkWidget *parent)
+{
+       GList *unsaved = NULL;
+       UtilityDelayData *dd;
+       
+       GList *work;
+       
+       work = flist;
+       while (work)
+               {
+               FileData *fd = work->data;
+               work = work->next;
+               
+               if (fd->change)
+                       {
+                       filelist_free(unsaved);
+                       return FALSE; /* another op. in progress, let the caller handle it */
+                       }
+               
+               if (fd->modified_xmp) /* has unsaved metadata */
+                       {
+                       unsaved = g_list_prepend(unsaved, file_data_ref(fd));
+                       }
+               }
+       
+       if (!unsaved) return FALSE;
+       
+       /* save arguments of the original operation */
+       
+       dd = g_new0(UtilityDelayData, 1);
+       
+       dd->type = type;
+       dd->phase = phase;
+       dd->flist = flist;
+       dd->dest_path = g_strdup(dest_path);
+       dd->editor_key = g_strdup(editor_key);
+       dd->parent = parent;
+       
+       file_util_write_metadata(NULL, unsaved, parent, FALSE, file_util_write_metadata_first_done, dd);
+       return TRUE;
+}
+
+
 /* full-featured entry points
 */
 
 void file_util_delete(FileData *source_fd, GList *source_list, GtkWidget *parent)
 {
-       file_util_delete_full(source_fd, source_list, parent, UTILITY_PHASE_START);
+       file_util_delete_full(source_fd, source_list, parent, options->file_ops.confirm_delete ? UTILITY_PHASE_START : UTILITY_PHASE_ENTERING);
+}
+
+void file_util_write_metadata(FileData *source_fd, GList *source_list, GtkWidget *parent, gboolean force_dialog, FileUtilDoneFunc done_func, gpointer done_data)
+{
+       file_util_write_metadata_full(source_fd, source_list, parent,
+                                     ((options->metadata.save_in_image_file && options->metadata.confirm_write) || force_dialog) ? UTILITY_PHASE_START : UTILITY_PHASE_ENTERING,
+                                     done_func, done_data);
 }
 
 void file_util_copy(FileData *source_fd, GList *source_list, const gchar *dest_path, GtkWidget *parent)
@@ -1972,24 +2821,24 @@ void file_util_rename_simple(FileData *fd, const gchar *dest_path, GtkWidget *pa
 }
 
 
-void file_util_start_editor_from_file(gint n, FileData *fd, GtkWidget *parent)
+void file_util_start_editor_from_file(const gchar *key, FileData *fd, GtkWidget *parent)
 {
-       file_util_start_editor_full(n, fd, NULL, NULL, parent, UTILITY_PHASE_ENTERING);
+       file_util_start_editor_full(key, fd, NULL, NULL, NULL, parent, UTILITY_PHASE_ENTERING);
 }
 
-void file_util_start_editor_from_filelist(gint n, GList *list, GtkWidget *parent)
+void file_util_start_editor_from_filelist(const gchar *key, GList *list, const gchar *working_directory, GtkWidget *parent)
 {
-       file_util_start_editor_full(n, NULL, list, NULL, parent, UTILITY_PHASE_ENTERING);
+       file_util_start_editor_full(key, NULL, list, NULL, working_directory, parent, UTILITY_PHASE_ENTERING);
 }
 
-void file_util_start_filter_from_file(gint n, FileData *fd, const gchar *dest_path, GtkWidget *parent)
+void file_util_start_filter_from_file(const gchar *key, FileData *fd, const gchar *dest_path, GtkWidget *parent)
 {
-       file_util_start_editor_full(n, fd, NULL, dest_path, parent, UTILITY_PHASE_ENTERING);
+       file_util_start_editor_full(key, fd, NULL, dest_path, NULL, parent, UTILITY_PHASE_ENTERING);
 }
 
-void file_util_start_filter_from_filelist(gint n, GList *list, const gchar *dest_path, GtkWidget *parent)
+void file_util_start_filter_from_filelist(const gchar *key, GList *list, const gchar *dest_path, GtkWidget *parent)
 {
-       file_util_start_editor_full(n, NULL, list, dest_path, parent, UTILITY_PHASE_ENTERING);
+       file_util_start_editor_full(key, NULL, list, dest_path, NULL, parent, UTILITY_PHASE_ENTERING);
 }
 
 void file_util_delete_dir(FileData *fd, GtkWidget *parent)
@@ -1997,39 +2846,14 @@ void file_util_delete_dir(FileData *fd, GtkWidget *parent)
        file_util_delete_dir_full(fd, parent, UTILITY_PHASE_START);
 }
 
-
-/*
- *--------------------------------------------------------------------------
- * Delete directory routines
- *--------------------------------------------------------------------------
- */
-
-// FIXME
-
-
-
-
-static GList *file_util_delete_dir_remaining_folders(GList *dlist)
+void file_util_create_dir(FileData *dir_fd, GtkWidget *parent, FileUtilDoneFunc done_func, gpointer done_data)
 {
-       GList *rlist = NULL;
-
-       while (dlist)
-               {
-               FileData *fd;
-
-               fd = dlist->data;
-               dlist = dlist->next;
-
-               if (!fd->name ||
-                   (strcmp(fd->name, THUMB_FOLDER_GLOBAL) != 0 &&
-                    strcmp(fd->name, THUMB_FOLDER_LOCAL) != 0 &&
-                    strcmp(fd->name, GQ_CACHE_LOCAL_METADATA) != 0) )
-                       {
-                       rlist = g_list_prepend(rlist, fd);
-                       }
-               }
+       file_util_create_dir_full(dir_fd, NULL, parent, UTILITY_PHASE_ENTERING, done_func, done_data);
+}
 
-       return g_list_reverse(rlist);
+void file_util_rename_dir(FileData *source_fd, const gchar *new_path, GtkWidget *parent, FileUtilDoneFunc done_func, gpointer done_data)
+{
+       file_util_rename_dir_full(source_fd, new_path, parent, UTILITY_PHASE_ENTERING, done_func, done_data);
 }
 
 
@@ -2065,4 +2889,6 @@ void file_util_copy_path_list_to_clipboard(GList *list)
        
        gtk_clipboard_set_text(clipboard, new->str, new->len);
        g_string_free(new, TRUE);
+       filelist_free(list);
 }
+/* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */