[calls] settings: Convert to the singleton pattern



commit 94fa13af4c9586d1986c1d46e1307ab57540e125
Author: Evangelos Ribeiro Tzaras <devrtz fortysixandtwo eu>
Date:   Mon May 9 22:57:41 2022 +0200

    settings: Convert to the singleton pattern
    
    We only have a single source of settings, so we should reflect that by
    using a singleton. This also reduces our LoC.
    
    This doesn't impair our ability to run tests because there we run with
    GSETTINGS_BACKEND=memory

 plugins/sip/calls-sip-media-manager.c |  3 +--
 src/calls-contacts-provider.c         | 44 ++++-------------------------------
 src/calls-contacts-provider.h         |  3 +--
 src/calls-manager.c                   |  5 ++--
 src/calls-settings.c                  | 15 ++++++++----
 src/calls-settings.h                  |  2 +-
 tests/mock-contacts-provider.h        |  2 +-
 tests/test-contacts.c                 |  3 +--
 tests/test-manager.c                  |  2 +-
 tests/test-ringer.c                   |  2 +-
 tests/test-ui-call.c                  |  2 +-
 11 files changed, 26 insertions(+), 57 deletions(-)
---
diff --git a/plugins/sip/calls-sip-media-manager.c b/plugins/sip/calls-sip-media-manager.c
index cc6aa56e..05d6a8cd 100644
--- a/plugins/sip/calls-sip-media-manager.c
+++ b/plugins/sip/calls-sip-media-manager.c
@@ -154,7 +154,6 @@ calls_sip_media_manager_finalize (GObject *object)
   CallsSipMediaManager *self = CALLS_SIP_MEDIA_MANAGER (object);
 
   g_list_free (self->preferred_codecs);
-  g_object_unref (self->settings);
   g_object_unref (self->pipelines);
 
   G_OBJECT_CLASS (calls_sip_media_manager_parent_class)->finalize (object);
@@ -176,7 +175,7 @@ calls_sip_media_manager_init (CallsSipMediaManager *self)
   if (!gst_is_initialized ())
     gst_init (NULL, NULL);
 
-  self->settings = calls_settings_new ();
+  self->settings = calls_settings_get_default ();
   g_signal_connect_swapped (self->settings,
                             "notify::preferred-audio-codecs",
                             G_CALLBACK (on_notify_preferred_audio_codecs),
diff --git a/src/calls-contacts-provider.c b/src/calls-contacts-provider.c
index 8279a487..a8951e6d 100644
--- a/src/calls-contacts-provider.c
+++ b/src/calls-contacts-provider.c
@@ -29,6 +29,7 @@
 
 #include "calls-contacts-provider.h"
 #include "calls-best-match.h"
+#include "calls-settings.h"
 
 #include <gee-0.8/gee.h>
 #include <folks/folks.h>
@@ -72,7 +73,6 @@ G_DEFINE_TYPE (CallsContactsProvider, calls_contacts_provider, G_TYPE_OBJECT)
 
 enum {
   PROP_0,
-  PROP_SETTINGS,
   PROP_CAN_ADD_CONTACTS,
   PROP_LAST_PROP
 };
@@ -239,26 +239,6 @@ on_contacts_appeared (GDBusConnection *connection,
 }
 
 
-static void
-calls_contacts_provider_set_property (GObject      *object,
-                                      guint         property_id,
-                                      const GValue *value,
-                                      GParamSpec   *pspec)
-{
-  CallsContactsProvider *self = CALLS_CONTACTS_PROVIDER (object);
-
-  switch (property_id) {
-  case PROP_SETTINGS:
-    self->settings = g_value_dup_object (value);
-    break;
-
-  default:
-    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
-    break;
-  }
-}
-
-
 static void
 calls_contacts_provider_get_property (GObject    *object,
                                       guint       property_id,
@@ -287,7 +267,6 @@ calls_contacts_provider_finalize (GObject *object)
   g_clear_handle_id (&self->bus_watch_id, g_bus_unwatch_name);
   g_clear_object (&self->contacts_action_group);
   g_clear_object (&self->folks_aggregator);
-  g_clear_object (&self->settings);
   g_clear_pointer (&self->best_matches, g_hash_table_unref);
 
   G_OBJECT_CLASS (calls_contacts_provider_parent_class)->finalize (object);
@@ -300,7 +279,6 @@ calls_contacts_provider_class_init (CallsContactsProviderClass *klass)
   GObjectClass *object_class = G_OBJECT_CLASS (klass);
 
   object_class->get_property = calls_contacts_provider_get_property;
-  object_class->set_property = calls_contacts_provider_set_property;
   object_class->finalize = calls_contacts_provider_finalize;
 
   /**
@@ -338,20 +316,6 @@ calls_contacts_provider_class_init (CallsContactsProviderClass *klass)
                   1,
                   FOLKS_TYPE_INDIVIDUAL);
 
-  /**
-   * CallsContactsProvider::settings:
-   *
-   * The settings are used to get the country code
-   * which is used for contact lookups.
-   */
-  props[PROP_SETTINGS] =
-    g_param_spec_object ("settings",
-                         "settings",
-                         "The settings object to use",
-                         CALLS_TYPE_SETTINGS,
-                         G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY |
-                         G_PARAM_STATIC_STRINGS);
-
   /**
    * CallsContactsProvider::can-add-contacts:
    *
@@ -372,6 +336,8 @@ static void
 calls_contacts_provider_init (CallsContactsProvider *self)
 {
   g_autoptr (GeeCollection) individuals = NULL;
+
+  self->settings = calls_settings_get_default ();
   self->folks_aggregator = folks_individual_aggregator_dup ();
 
   individuals = calls_contacts_provider_get_individuals (self);
@@ -404,9 +370,9 @@ calls_contacts_provider_init (CallsContactsProvider *self)
 
 
 CallsContactsProvider *
-calls_contacts_provider_new (CallsSettings *settings)
+calls_contacts_provider_new (void)
 {
-  return g_object_new (CALLS_TYPE_CONTACTS_PROVIDER, "settings", settings, NULL);
+  return g_object_new (CALLS_TYPE_CONTACTS_PROVIDER, NULL);
 }
 
 
diff --git a/src/calls-contacts-provider.h b/src/calls-contacts-provider.h
index 9ed13657..f6c5f9b5 100644
--- a/src/calls-contacts-provider.h
+++ b/src/calls-contacts-provider.h
@@ -28,7 +28,6 @@
 #pragma once
 
 #include "calls-best-match.h"
-#include "calls-settings.h"
 
 #include <folks/folks.h>
 #include <glib-object.h>
@@ -49,7 +48,7 @@ typedef void (*IdleCallback) (gpointer         user_data,
 
 G_DECLARE_FINAL_TYPE (CallsContactsProvider, calls_contacts_provider, CALLS, CONTACTS_PROVIDER, GObject);
 
-CallsContactsProvider *calls_contacts_provider_new                  (CallsSettings *settings);
+CallsContactsProvider *calls_contacts_provider_new                  (void);
 GeeCollection         *calls_contacts_provider_get_individuals      (CallsContactsProvider *self);
 CallsBestMatch        *calls_contacts_provider_lookup_id            (CallsContactsProvider *self,
                                                                      const char            *id);
diff --git a/src/calls-manager.c b/src/calls-manager.c
index 82e5b0c2..655bb4db 100644
--- a/src/calls-manager.c
+++ b/src/calls-manager.c
@@ -646,7 +646,6 @@ calls_manager_finalize (GObject *object)
 
   g_clear_object (&self->origins);
   g_clear_object (&self->contacts_provider);
-  g_clear_object (&self->settings);
 
   g_clear_pointer (&self->providers, g_hash_table_unref);
   g_clear_pointer (&self->origins_by_protocol, g_hash_table_unref);
@@ -795,9 +794,9 @@ calls_manager_init (CallsManager *self)
   /* This hash table only owns the value, not the key */
   self->calls = g_hash_table_new_full (NULL, NULL, NULL, g_object_unref);
 
-  self->settings = calls_settings_new ();
+  self->settings = calls_settings_get_default ();
   // Load the contacts provider
-  self->contacts_provider = calls_contacts_provider_new (self->settings);
+  self->contacts_provider = calls_contacts_provider_new ();
 
   peas = peas_engine_get_default ();
 
diff --git a/src/calls-settings.c b/src/calls-settings.c
index 3cc71750..ca34fdaf 100644
--- a/src/calls-settings.c
+++ b/src/calls-settings.c
@@ -195,14 +195,21 @@ calls_settings_init (CallsSettings *self)
 }
 
 /**
- * calls_settings_new:
+ * calls_settings_get_default:
  *
- * Returns: (transfer full): A #CallsSettings.
+ * Returns: (transfer none): A #CallsSettings.
  */
 CallsSettings *
-calls_settings_new (void)
+calls_settings_get_default (void)
 {
-  return g_object_new (CALLS_TYPE_SETTINGS, NULL);
+  static CallsSettings *instance = NULL;
+
+  if (!instance) {
+      instance = g_object_new (CALLS_TYPE_SETTINGS, NULL);
+      g_object_add_weak_pointer (G_OBJECT (instance), (gpointer *)&instance);
+    }
+
+  return instance;
 }
 
 /**
diff --git a/src/calls-settings.h b/src/calls-settings.h
index 4ce5dbb0..7728d5cb 100644
--- a/src/calls-settings.h
+++ b/src/calls-settings.h
@@ -32,7 +32,7 @@ G_BEGIN_DECLS
 
 G_DECLARE_FINAL_TYPE (CallsSettings, calls_settings, CALLS, SETTINGS, GObject);
 
-CallsSettings *calls_settings_new                            (void);
+CallsSettings *calls_settings_get_default                    (void);
 gboolean       calls_settings_get_use_default_origins        (CallsSettings *self);
 void           calls_settings_set_use_default_origins        (CallsSettings *self,
                                                               gboolean       enable);
diff --git a/tests/mock-contacts-provider.h b/tests/mock-contacts-provider.h
index ad0dbb40..c79d8010 100644
--- a/tests/mock-contacts-provider.h
+++ b/tests/mock-contacts-provider.h
@@ -11,4 +11,4 @@
 
 #include "calls-contacts-provider.h"
 
-CallsContactsProvider *__wrap_calls_contacts_provider_new (CallsSettings *settings);
+CallsContactsProvider *__wrap_calls_contacts_provider_new (void);
diff --git a/tests/test-contacts.c b/tests/test-contacts.c
index 2b43ffc2..b974d2c0 100644
--- a/tests/test-contacts.c
+++ b/tests/test-contacts.c
@@ -14,9 +14,8 @@
 static void
 test_contacts_null_contact (void)
 {
-  g_autoptr (CallsSettings) settings = calls_settings_new ();
   g_autoptr (CallsContactsProvider) contacts_provider =
-    calls_contacts_provider_new (settings);
+    calls_contacts_provider_new ();
   CallsBestMatch *best_match;
 
   best_match = calls_contacts_provider_lookup_id (contacts_provider, NULL);
diff --git a/tests/test-manager.c b/tests/test-manager.c
index 0f122560..3ef52cd3 100644
--- a/tests/test-manager.c
+++ b/tests/test-manager.c
@@ -12,7 +12,7 @@
 #include <libpeas/peas.h>
 
 CallsContactsProvider *
-__wrap_calls_contacts_provider_new (CallsSettings *settings)
+__wrap_calls_contacts_provider_new (void)
 {
   return NULL;
 }
diff --git a/tests/test-ringer.c b/tests/test-ringer.c
index b8fe5c33..33165139 100644
--- a/tests/test-ringer.c
+++ b/tests/test-ringer.c
@@ -138,7 +138,7 @@ __wrap_lfb_event_end_feedback_finish (LfbEvent     *self,
 
 
 CallsContactsProvider *
-__wrap_calls_contacts_provider_new (CallsSettings *settings)
+__wrap_calls_contacts_provider_new (void)
 {
   return NULL;
 }
diff --git a/tests/test-ui-call.c b/tests/test-ui-call.c
index 4dd73f1d..2c8cd534 100644
--- a/tests/test-ui-call.c
+++ b/tests/test-ui-call.c
@@ -14,7 +14,7 @@
 #include <gtk/gtk.h>
 
 CallsContactsProvider *
-__wrap_calls_contacts_provider_new (CallsSettings *settings)
+__wrap_calls_contacts_provider_new (void)
 {
   return NULL;
 }


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