[nautilus/wip/antoniof/try-admin-backend-for-file-ops: 4/11] file-operations: Remove duplicated code




commit 9857b021ce44cbda41a6def90a4ac2a7053bfcac
Author: Sachin Daluja <30343-sachindaluja users noreply gitlab gnome org>
Date:   Sat Jun 12 10:44:51 2021 -0400

    file-operations: Remove duplicated code
    
    The outermost code for handling the "duplicate" file operation
    was mostly a copy of the similar code for the "copy" operation.
    Remove the duplicated code and add minor conditional logic in
    the code for "copy" operation to also handle "duplicate".

 src/nautilus-file-operations.c      | 88 ++++++++++---------------------------
 src/nautilus-file-operations.h      |  5 ---
 src/nautilus-file-undo-operations.c | 27 +++---------
 3 files changed, 30 insertions(+), 90 deletions(-)
---
diff --git a/src/nautilus-file-operations.c b/src/nautilus-file-operations.c
index 2c4f3ff13..fead1c8e7 100644
--- a/src/nautilus-file-operations.c
+++ b/src/nautilus-file-operations.c
@@ -6028,17 +6028,23 @@ nautilus_file_operations_copy (GTask        *task,
     if (!nautilus_file_undo_manager_is_operating ())
     {
         g_autoptr (GFile) src_dir = NULL;
+        NautilusFileUndoOp undo_op_type;
 
         src_dir = g_file_get_parent (job->files->data);
-        /* In the case of duplicate, the undo_info is already set, so we don't want to
-         * overwrite it wrongfully.
-         */
-        if (job->common.undo_info == NULL)
+        undo_op_type = NAUTILUS_FILE_UNDO_OP_COPY;
+
+        if (job->destination == NULL)
         {
-            job->common.undo_info = nautilus_file_undo_info_ext_new (NAUTILUS_FILE_UNDO_OP_COPY,
-                                                                     g_list_length (job->files),
-                                                                     src_dir, job->destination);
+            /* Use a distinct undo op ID for "duplicate" operation. This merely affects the user facing 
info-text about
+             * undo; the actual op logic is the same as the regular copy.
+             */
+
+            undo_op_type = NAUTILUS_FILE_UNDO_OP_DUPLICATE;
         }
+
+        job->common.undo_info = nautilus_file_undo_info_ext_new (undo_op_type,
+                                                                 g_list_length (job->files),
+                                                                 src_dir, job->destination);
     }
 
     nautilus_progress_info_start (job->common.progress);
@@ -7109,40 +7115,6 @@ nautilus_file_operations_link (GList                          *files,
     g_task_run_in_thread (task, link_task_thread_func);
 }
 
-
-void
-nautilus_file_operations_duplicate (GList                          *files,
-                                    GtkWindow                      *parent_window,
-                                    NautilusFileOperationsDBusData *dbus_data,
-                                    NautilusCopyCallback            done_callback,
-                                    gpointer                        done_callback_data)
-{
-    g_autoptr (GTask) task = NULL;
-    CopyMoveJob *job;
-
-    job = copy_job_setup (files,
-                          NULL,
-                          parent_window,
-                          dbus_data,
-                          done_callback,
-                          done_callback_data);
-
-    if (!nautilus_file_undo_manager_is_operating ())
-    {
-        g_autoptr (GFile) src_dir = NULL;
-
-        src_dir = g_file_get_parent (files->data);
-        job->common.undo_info =
-            nautilus_file_undo_info_ext_new (NAUTILUS_FILE_UNDO_OP_DUPLICATE,
-                                             g_list_length (files),
-                                             src_dir, NULL);
-    }
-
-    task = g_task_new (NULL, job->common.cancellable, copy_task_done, job);
-    g_task_set_task_data (task, job, NULL);
-    g_task_run_in_thread (task, nautilus_file_operations_copy);
-}
-
 static void
 set_permissions_task_done (GObject      *source_object,
                            GAsyncResult *res,
@@ -7375,7 +7347,7 @@ nautilus_file_operations_copy_move (const GList                    *item_uris,
 {
     GList *locations;
     GList *p;
-    GFile *dest, *src_dir;
+    GFile *dest;
     GtkWindow *parent_window;
     gboolean target_is_mapping;
     gboolean have_nonmapping_source;
@@ -7419,28 +7391,16 @@ nautilus_file_operations_copy_move (const GList                    *item_uris,
 
     if (copy_action == GDK_ACTION_COPY)
     {
-        src_dir = g_file_get_parent (locations->data);
-        if (target_dir == NULL ||
-            (src_dir != NULL &&
-             g_file_equal (src_dir, dest)))
-        {
-            nautilus_file_operations_duplicate (locations,
-                                                parent_window,
-                                                dbus_data,
-                                                done_callback, done_callback_data);
-        }
-        else
-        {
-            nautilus_file_operations_copy_async (locations,
-                                                 dest,
-                                                 parent_window,
-                                                 dbus_data,
-                                                 done_callback, done_callback_data);
-        }
-        if (src_dir)
-        {
-            g_object_unref (src_dir);
-        }
+        gboolean duplicate;
+
+        duplicate = target_dir == NULL || g_file_has_parent (locations->data, dest);
+
+        /* Duplication operation is identified by having a NULL dest */
+        nautilus_file_operations_copy_async (locations,
+                                             duplicate ? NULL : dest,
+                                             parent_window,
+                                             dbus_data,
+                                             done_callback, done_callback_data);
     }
     else if (copy_action == GDK_ACTION_MOVE)
     {
diff --git a/src/nautilus-file-operations.h b/src/nautilus-file-operations.h
index 14d664f80..24b8005d8 100644
--- a/src/nautilus-file-operations.h
+++ b/src/nautilus-file-operations.h
@@ -138,11 +138,6 @@ void nautilus_file_operations_move_async (GList                          *files,
 void nautilus_file_operations_move_sync (GList                *files,
                                          GFile                *target_dir);
 
-void nautilus_file_operations_duplicate (GList                          *files,
-                                         GtkWindow                      *parent_window,
-                                         NautilusFileOperationsDBusData *dbus_data,
-                                         NautilusCopyCallback            done_callback,
-                                         gpointer                        done_callback_data);
 void nautilus_file_operations_link      (GList                          *files,
                                          GFile                          *target_dir,
                                          GtkWindow                      *parent_window,
diff --git a/src/nautilus-file-undo-operations.c b/src/nautilus-file-undo-operations.c
index 70123dace..41abe2935 100644
--- a/src/nautilus-file-undo-operations.c
+++ b/src/nautilus-file-undo-operations.c
@@ -596,18 +596,6 @@ ext_create_link_redo_func (NautilusFileUndoInfoExt        *self,
                                    self);
 }
 
-static void
-ext_duplicate_redo_func (NautilusFileUndoInfoExt        *self,
-                         GtkWindow                      *parent_window,
-                         NautilusFileOperationsDBusData *dbus_data)
-{
-    nautilus_file_operations_duplicate (g_queue_peek_head_link (self->sources),
-                                        parent_window,
-                                        dbus_data,
-                                        file_undo_info_transfer_callback,
-                                        self);
-}
-
 static void
 ext_copy_redo_func (NautilusFileUndoInfoExt        *self,
                     GtkWindow                      *parent_window,
@@ -647,14 +635,11 @@ ext_redo_func (NautilusFileUndoInfo           *info,
     {
         ext_move_restore_redo_func (self, parent_window, dbus_data);
     }
-    else if (op_type == NAUTILUS_FILE_UNDO_OP_COPY)
+    else if (op_type == NAUTILUS_FILE_UNDO_OP_COPY ||
+             op_type == NAUTILUS_FILE_UNDO_OP_DUPLICATE)
     {
         ext_copy_redo_func (self, parent_window, dbus_data);
     }
-    else if (op_type == NAUTILUS_FILE_UNDO_OP_DUPLICATE)
-    {
-        ext_duplicate_redo_func (self, parent_window, dbus_data);
-    }
     else if (op_type == NAUTILUS_FILE_UNDO_OP_CREATE_LINK)
     {
         ext_create_link_redo_func (self, parent_window, dbus_data);
@@ -692,9 +677,9 @@ ext_move_undo_func (NautilusFileUndoInfoExt        *self,
 }
 
 static void
-ext_copy_duplicate_undo_func (NautilusFileUndoInfoExt        *self,
-                              GtkWindow                      *parent_window,
-                              NautilusFileOperationsDBusData *dbus_data)
+ext_copy_undo_func (NautilusFileUndoInfoExt        *self,
+                    GtkWindow                      *parent_window,
+                    NautilusFileOperationsDBusData *dbus_data)
 {
     GList *files;
 
@@ -720,7 +705,7 @@ ext_undo_func (NautilusFileUndoInfo           *info,
         op_type == NAUTILUS_FILE_UNDO_OP_DUPLICATE ||
         op_type == NAUTILUS_FILE_UNDO_OP_CREATE_LINK)
     {
-        ext_copy_duplicate_undo_func (self, parent_window, dbus_data);
+        ext_copy_undo_func (self, parent_window, dbus_data);
     }
     else if (op_type == NAUTILUS_FILE_UNDO_OP_MOVE)
     {


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