[nautilus/wip/antoniof/new-list-view-continuation: 2/20] tag-manager: Use regular initialization




commit d64445665490640c4661627070e3822f4a2cfb56
Author: António Fernandes <antoniof gnome org>
Date:   Sat Apr 2 19:19:05 2022 +0100

    tag-manager: Use regular initialization
    
    Somehow we have ended up in a situation where the singleton object
    is not initialized on creation but instead when requires the
    NautilusApplication code to pass a GCancellable instance in order to
    complete the initialization.
    
    The passed cancellable pointer is stored by the application object
    in order to cancel it on finalization, but there is no other usage
    for this cancellabled from outside of the NautilusTagManager code.
    
    So, we can just cancel it in NautilusTagManager::finalize, such that
    there is no need for NautilusApplication to pass or store it.
    
    This way, we can move the initalization from ::set_cancellable()
    into ::init instead.

 src/nautilus-application.c                         |  8 -----
 src/nautilus-tag-manager.c                         | 37 +++++++++-------------
 src/nautilus-tag-manager.h                         |  3 --
 .../displayless/test-file-operations-copy-files.c  |  3 ++
 .../displayless/test-file-operations-move-files.c  |  3 ++
 .../test-file-operations-trash-or-delete.c         |  3 ++
 6 files changed, 24 insertions(+), 33 deletions(-)
---
diff --git a/src/nautilus-application.c b/src/nautilus-application.c
index 06255587d..2b9880b84 100644
--- a/src/nautilus-application.c
+++ b/src/nautilus-application.c
@@ -84,7 +84,6 @@ typedef struct
     NautilusFileUndoManager *undo_manager;
 
     NautilusTagManager *tag_manager;
-    GCancellable *tag_manager_cancellable;
 
     guint previewer_selection_id;
 } NautilusApplicationPrivate;
@@ -587,9 +586,6 @@ nautilus_application_finalize (GObject *object)
 
     g_clear_object (&priv->tag_manager);
 
-    g_cancellable_cancel (priv->tag_manager_cancellable);
-    g_clear_object (&priv->tag_manager_cancellable);
-
     G_OBJECT_CLASS (nautilus_application_parent_class)->finalize (object);
 }
 
@@ -1101,11 +1097,7 @@ nautilus_application_init (NautilusApplication *self)
                                                  NULL);
 
     priv->undo_manager = nautilus_file_undo_manager_new ();
-
-    priv->tag_manager_cancellable = g_cancellable_new ();
     priv->tag_manager = nautilus_tag_manager_get ();
-    nautilus_tag_manager_set_cancellable (priv->tag_manager,
-                                          priv->tag_manager_cancellable);
 
     g_application_add_main_option_entries (G_APPLICATION (self), options);
 
diff --git a/src/nautilus-tag-manager.c b/src/nautilus-tag-manager.c
index 32a9230ae..b3a7e4278 100644
--- a/src/nautilus-tag-manager.c
+++ b/src/nautilus-tag-manager.c
@@ -300,8 +300,6 @@ nautilus_tag_manager_query_starred_files (NautilusTagManager *self,
         return;
     }
 
-    self->cancellable = cancellable;
-
     tracker_sparql_statement_execute_async (self->query_starred_files,
                                             cancellable,
                                             on_get_starred_files_query_callback,
@@ -533,6 +531,8 @@ nautilus_tag_manager_finalize (GObject *object)
                                               self);
     }
 
+    g_cancellable_cancel (self->cancellable);
+    g_clear_object (&self->cancellable);
     g_clear_object (&self->notifier);
     g_clear_object (&self->db);
     g_clear_object (&self->query_file_is_starred);
@@ -647,15 +647,20 @@ setup_database (NautilusTagManager  *self,
     return TRUE;
 }
 
-/* Initialize the tag mananger. */
-void
-nautilus_tag_manager_set_cancellable (NautilusTagManager *self,
-                                      GCancellable       *cancellable)
+static void
+nautilus_tag_manager_init (NautilusTagManager *self)
 {
     g_autoptr (GError) error = NULL;
 
-    self->database_ok = setup_database (self, cancellable, &error);
+    self->starred_file_uris = g_hash_table_new_full (g_str_hash,
+                                                     g_str_equal,
+                                                     (GDestroyNotify) g_free,
+                                                     /* values are keys */
+                                                     NULL);
+    self->home = g_file_new_for_path (g_get_home_dir ());
 
+    self->cancellable = g_cancellable_new ();
+    self->database_ok = setup_database (self, self->cancellable, &error);
     if (error)
     {
         g_warning ("Unable to initialize tag manager: %s", error->message);
@@ -664,7 +669,7 @@ nautilus_tag_manager_set_cancellable (NautilusTagManager *self,
 
     self->notifier = tracker_sparql_connection_create_notifier (self->db);
 
-    nautilus_tag_manager_query_starred_files (self, cancellable);
+    nautilus_tag_manager_query_starred_files (self, self->cancellable);
 
     g_signal_connect (self->notifier,
                       "events",
@@ -672,26 +677,15 @@ nautilus_tag_manager_set_cancellable (NautilusTagManager *self,
                       self);
 }
 
-static void
-nautilus_tag_manager_init (NautilusTagManager *self)
-{
-    self->starred_file_uris = g_hash_table_new_full (g_str_hash,
-                                                     g_str_equal,
-                                                     (GDestroyNotify) g_free,
-                                                     /* values are keys */
-                                                     NULL);
-    self->home = g_file_new_for_path (g_get_home_dir ());
-}
-
 gboolean
-nautilus_tag_manager_can_star_contents (NautilusTagManager *tag_manager,
+nautilus_tag_manager_can_star_contents (NautilusTagManager *self,
                                         GFile              *directory)
 {
     /* We only allow files to be starred inside the home directory for now.
      * This avoids the starred files database growing too big.
      * See https://gitlab.gnome.org/GNOME/nautilus/-/merge_requests/553#note_903108
      */
-    return g_file_has_prefix (directory, tag_manager->home) || g_file_equal (directory, tag_manager->home);
+    return g_file_has_prefix (directory, self->home) || g_file_equal (directory, self->home);
 }
 
 static void
@@ -713,7 +707,6 @@ update_moved_uris_callback (GObject      *object,
     else
     {
         g_autolist (NautilusFile) updated_files = NULL;
-        g_autoptr (NautilusTagManager) tag_manager = nautilus_tag_manager_get ();
 
         for (guint i = 0; i < new_uris->len; i++)
         {
diff --git a/src/nautilus-tag-manager.h b/src/nautilus-tag-manager.h
index 1ba8a48dd..e2f1d6747 100644
--- a/src/nautilus-tag-manager.h
+++ b/src/nautilus-tag-manager.h
@@ -30,9 +30,6 @@ G_DECLARE_FINAL_TYPE (NautilusTagManager, nautilus_tag_manager, NAUTILUS, TAG_MA
 
 NautilusTagManager* nautilus_tag_manager_get                (void);
 
-void                nautilus_tag_manager_set_cancellable    (NautilusTagManager *tag_manager,
-                                                             GCancellable *cancellable);
-
 GList*              nautilus_tag_manager_get_starred_files (NautilusTagManager *self);
 
 void                nautilus_tag_manager_star_files         (NautilusTagManager  *self,
diff --git a/test/automated/displayless/test-file-operations-copy-files.c 
b/test/automated/displayless/test-file-operations-copy-files.c
index fc3aa3987..601b0090d 100644
--- a/test/automated/displayless/test-file-operations-copy-files.c
+++ b/test/automated/displayless/test-file-operations-copy-files.c
@@ -1,4 +1,5 @@
 #include "test-utilities.h"
+#include <src/nautilus-tag-manager.h>
 
 static void
 test_copy_one_file (void)
@@ -1304,9 +1305,11 @@ main (int   argc,
       char *argv[])
 {
     g_autoptr (NautilusFileUndoManager) undo_manager = NULL;
+    g_autoptr (NautilusTagManager) tag_manager = NULL;
     int ret;
 
     undo_manager = nautilus_file_undo_manager_new ();
+    tag_manager = nautilus_tag_manager_new ();
     g_test_init (&argc, &argv, NULL);
     g_test_set_nonfatal_assertions ();
     nautilus_ensure_extension_points ();
diff --git a/test/automated/displayless/test-file-operations-move-files.c 
b/test/automated/displayless/test-file-operations-move-files.c
index c034a0a74..fc528c486 100644
--- a/test/automated/displayless/test-file-operations-move-files.c
+++ b/test/automated/displayless/test-file-operations-move-files.c
@@ -1,4 +1,5 @@
 #include "test-utilities.h"
+#include <src/nautilus-tag-manager.h>
 
 static void
 test_move_one_file (void)
@@ -1935,9 +1936,11 @@ main (int   argc,
       char *argv[])
 {
     g_autoptr (NautilusFileUndoManager) undo_manager = NULL;
+    g_autoptr (NautilusTagManager) tag_manager = NULL;
     int ret;
 
     undo_manager = nautilus_file_undo_manager_new ();
+    tag_manager = nautilus_tag_manager_new ();
     g_test_init (&argc, &argv, NULL);
     g_test_set_nonfatal_assertions ();
     nautilus_ensure_extension_points ();
diff --git a/test/automated/displayless/test-file-operations-trash-or-delete.c 
b/test/automated/displayless/test-file-operations-trash-or-delete.c
index 71dc26571..7f9934375 100644
--- a/test/automated/displayless/test-file-operations-trash-or-delete.c
+++ b/test/automated/displayless/test-file-operations-trash-or-delete.c
@@ -1,4 +1,5 @@
 #include "test-utilities.h"
+#include <src/nautilus-tag-manager.h>
 
 static void
 test_trash_one_file (void)
@@ -570,12 +571,14 @@ main (int   argc,
       char *argv[])
 {
     g_autoptr (NautilusFileUndoManager) undo_manager = NULL;
+    g_autoptr (NautilusTagManager) tag_manager = NULL;
     int ret;
 
     g_test_init (&argc, &argv, NULL);
     g_test_set_nonfatal_assertions ();
     nautilus_ensure_extension_points ();
     undo_manager = nautilus_file_undo_manager_new ();
+    tag_manager = nautilus_tag_manager_new ();
 
     setup_test_suite ();
 


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