[gthumb] make copy and paste in same folder equivalent to duplicating



commit 127afd0d8c583992272ef4dcf48b101d8724c775
Author: Paolo Bacchilega <paobac src gnome org>
Date:   Sun Mar 23 15:51:41 2014 +0100

    make copy and paste in same folder equivalent to duplicating
    
    [bug #722726]

 gthumb/gio-utils.c           |   61 +++++++++++++++++++++++++++++++++++-------
 gthumb/gio-utils.h           |    3 +-
 gthumb/gth-file-source-vfs.c |    2 +-
 3 files changed, 54 insertions(+), 12 deletions(-)
---
diff --git a/gthumb/gio-utils.c b/gthumb/gio-utils.c
index 95f226f..3545e24 100644
--- a/gthumb/gio-utils.c
+++ b/gthumb/gio-utils.c
@@ -1112,6 +1112,7 @@ typedef struct {
        GFile                *current_destination;
        char                 *message;
        GthOverwriteResponse  default_response;
+       gboolean              duplicating_file;
 
        GList                *source_sidecars;  /* GFile list */
        GList                *destination_sidecars;  /* GFile list */
@@ -1284,7 +1285,16 @@ copy_file_ready_cb (GObject      *source_object,
 
        if (! g_file_copy_finish ((GFile *) source_object, result, &error)) {
                if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_EXISTS)) {
-                       if (copy_file_data->default_response != GTH_OVERWRITE_RESPONSE_ALWAYS_NO) {
+                       if (copy_file_data->duplicating_file) {
+
+                               /* the duplicated file already exists, try another one */
+
+                               GFile *new_destination = _g_file_get_duplicated 
(copy_file_data->current_destination);
+                               _g_copy_file_to_destination (copy_file_data, new_destination, 
G_FILE_COPY_NONE);
+
+                               g_object_unref (new_destination);
+                       }
+                       else if (copy_file_data->default_response != GTH_OVERWRITE_RESPONSE_ALWAYS_NO) {
                                GtkWidget *dialog;
 
                                dialog = gth_overwrite_dialog_new (copy_file_data->source->file,
@@ -1364,18 +1374,52 @@ copy_file_progress_cb (goffset  current_num_bytes,
 
 
 static void
+_g_copy_file_to_destination_call_ready_callback (gpointer user_data)
+{
+       CopyFileData *copy_file_data = user_data;
+
+       copy_file_data->ready_callback (copy_file_data->default_response, NULL, copy_file_data->user_data);
+       copy_file_data_free (copy_file_data);
+}
+
+
+static void
 _g_copy_file_to_destination (CopyFileData   *copy_file_data,
                             GFile          *destination,
                             GFileCopyFlags  flags)
 {
-       _g_object_unref (copy_file_data->current_destination);
-       copy_file_data->current_destination = g_file_dup (destination);
-
        if (copy_file_data->default_response == GTH_OVERWRITE_RESPONSE_ALWAYS_YES)
                flags |= G_FILE_COPY_OVERWRITE;
        if (copy_file_data->flags & GTH_FILE_COPY_ALL_METADATA)
                flags |= G_FILE_COPY_ALL_METADATA;
 
+       _g_object_unref (copy_file_data->current_destination);
+       copy_file_data->current_destination = g_file_dup (destination);
+
+       if (g_file_equal (copy_file_data->source->file, copy_file_data->current_destination)) {
+               GFile *old_destination;
+
+               if ((copy_file_data->move
+                   || (copy_file_data->default_response == GTH_OVERWRITE_RESPONSE_ALWAYS_YES)
+                   || ! (copy_file_data->flags & GTH_FILE_COPY_RENAME_SAME_FILE)))
+               {
+                       call_when_idle (_g_copy_file_to_destination_call_ready_callback, copy_file_data);
+                       return;
+               }
+
+               copy_file_data->duplicating_file = TRUE;
+
+               old_destination = copy_file_data->current_destination;
+               copy_file_data->current_destination = _g_file_get_duplicated (old_destination);
+               g_object_unref (old_destination);
+
+               /* duplicated files shouldn't be overwritten, if a duplicated
+                * file already exists get another duplicated file and try
+                * again. */
+               if (flags & G_FILE_COPY_OVERWRITE)
+                       flags ^= G_FILE_COPY_OVERWRITE;
+       }
+
        if (copy_file_data->progress_callback != NULL) {
                GFile *destination_parent;
                char  *destination_name;
@@ -1398,6 +1442,9 @@ _g_copy_file_to_destination (CopyFileData   *copy_file_data,
        if (g_file_info_get_file_type (copy_file_data->source->info) == G_FILE_TYPE_DIRECTORY) {
                GError *error = NULL;
 
+               /* FIXME: use the async version, available since glib 2.38 */
+               /* FIXME: handle the GTH_FILE_COPY_RENAME_SAME_FILE flag for directories */
+
                if (! g_file_make_directory (copy_file_data->current_destination, 
copy_file_data->cancellable, &error)) {
                        if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_EXISTS))
                                g_clear_error (&error);
@@ -1654,12 +1701,6 @@ copy_data__copy_current_file (CopyData *copy_data)
        }
        destination = _g_file_get_destination (source->file, copy_data->source_base, copy_data->destination);
 
-       if (g_file_equal (source->file, destination)) {
-               g_object_unref (destination);
-               call_when_idle ((DataFunc) copy_data__copy_next_file, copy_data);
-               return;
-       }
-
        _g_copy_file_async_private (source,
                                    destination,
                                    copy_data->move,
diff --git a/gthumb/gio-utils.h b/gthumb/gio-utils.h
index aee5d8c..e9d1dde 100644
--- a/gthumb/gio-utils.h
+++ b/gthumb/gio-utils.h
@@ -48,7 +48,8 @@ typedef enum {
 
 typedef enum { /*< skip >*/
   GTH_FILE_COPY_DEFAULT            = 0,
-  GTH_FILE_COPY_ALL_METADATA       = (1 << 1)
+  GTH_FILE_COPY_ALL_METADATA       = (1 << 1),
+  GTH_FILE_COPY_RENAME_SAME_FILE   = (1 << 2)
 } GthFileCopyFlags;
 
 typedef DirOp (*StartDirCallback)    (GFile                *directory,
diff --git a/gthumb/gth-file-source-vfs.c b/gthumb/gth-file-source-vfs.c
index 6971bd1..5fba170 100644
--- a/gthumb/gth-file-source-vfs.c
+++ b/gthumb/gth-file-source-vfs.c
@@ -380,7 +380,7 @@ gth_file_source_vfs_copy (GthFileSource    *file_source,
        _g_copy_files_async (file_list,
                             destination->file,
                             move,
-                            GTH_FILE_COPY_ALL_METADATA,
+                            GTH_FILE_COPY_ALL_METADATA | GTH_FILE_COPY_RENAME_SAME_FILE,
                             GTH_OVERWRITE_RESPONSE_UNSPECIFIED,
                             G_PRIORITY_DEFAULT,
                             gth_file_source_get_cancellable (file_source),


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]