From 0f0d10e3f2b50ae280c3b670a409d7d07ae25b60 Mon Sep 17 00:00:00 2001 From: Evangelos Ribeiro Tzaras Date: Wed, 17 Jun 2020 13:44:11 +0200 Subject: [PATCH] Make CallsContacts a singleton Since passing a CallsContacts pointer down to every class it needs, started to become laborous - especially since the intermediates classes don't need the reference themselves - it was made a singleton * src/calls-contacts.c: Added calls_contacts_get_default () function and removed calls_contacts_new () * src/calls-contacts.h: Added _get_default () prototype and removed the _new () prototype * src/calls-application.c: Use calls_contacts_get_default () now * src/calls-history-box.c: Removed self->contacts completely * src/calls-history-box.h: Got rid of CallsContacts argument in _new() * src/calls-main-window.c: Removed self->contacts completely * src/calls-main-window.h: Got rid of CallsContacts argument in _new() * src/calls-call-record-row.c: Use calls_contacts_get_default () now * src/calls-call-record-row.h: Got rid of CallsContacts argument in _new() * src/calls-call-holder.c: Use calls_contacts_get_default () now * src/calls-call-holder.h: Got rid of CallsContacts argument in _new() * src/calls-call-window.c: Removed self->contacts completely * src/calls-call-window.h: Got rid of CallsContacts argument in _new() * src/calls-notifier.c: Use calls_contacts_get_default () now * src/calls-notifier.h: Got rid of CallsContacts argument in _new() --- src/calls-application.c | 8 +++---- src/calls-call-record-row.c | 23 +++--------------- src/calls-call-record-row.h | 4 +--- src/calls-contacts.c | 10 ++++++-- src/calls-contacts.h | 2 +- src/calls-history-box.c | 22 ++--------------- src/calls-history-box.h | 4 +--- src/calls-main-window.c | 23 ++---------------- src/calls-main-window.h | 5 +--- src/calls-notifier.c | 47 ++++--------------------------------- src/calls-notifier.h | 4 +--- 11 files changed, 28 insertions(+), 124 deletions(-) diff --git a/src/calls-application.c b/src/calls-application.c index b5fd94e..c9a9251 100644 --- a/src/calls-application.c +++ b/src/calls-application.c @@ -339,16 +339,15 @@ start_proper (CallsApplication *self) self->record_store = calls_record_store_new (); g_assert (self->record_store != NULL); - self->contacts = calls_contacts_new (); + self->contacts = calls_contacts_get_default (); g_assert (self->contacts != NULL); - self->notifier = calls_notifier_new (self->contacts); + self->notifier = calls_notifier_new (); g_assert (CALLS_IS_NOTIFIER (self->notifier)); self->main_window = calls_main_window_new (gtk_app, - G_LIST_MODEL (self->record_store), - self->contacts); + G_LIST_MODEL (self->record_store)); g_assert (self->main_window != NULL); self->call_window = calls_call_window_new (gtk_app); @@ -477,6 +476,7 @@ finalize (GObject *object) g_clear_object (&self->call_window); g_clear_object (&self->main_window); g_clear_object (&self->record_store); + g_clear_object (&self->contacts); g_clear_object (&self->ringer); g_clear_object (&self->notifier); diff --git a/src/calls-call-record-row.c b/src/calls-call-record-row.c index 2bd3731..9321af2 100644 --- a/src/calls-call-record-row.c +++ b/src/calls-call-record-row.c @@ -24,6 +24,7 @@ #include "calls-call-record-row.h" #include "calls-best-match.h" +#include "calls-contacts.h" #include "contrib/hdy-avatar.h" #include "util.h" @@ -57,7 +58,6 @@ struct _CallsCallRecordRow gulong end_notify_handler_id; guint date_change_timeout; - CallsContacts *contacts; CallsBestMatch *contact; }; @@ -67,7 +67,6 @@ G_DEFINE_TYPE (CallsCallRecordRow, calls_call_record_row, GTK_TYPE_LIST_BOX_ROW) enum { PROP_0, PROP_RECORD, - PROP_CONTACTS, PROP_LAST_PROP, }; static GParamSpec *props[PROP_LAST_PROP]; @@ -386,10 +385,9 @@ setup_contact (CallsCallRecordRow *self) // Look up the best match object self->contact = calls_contacts_lookup_phone_number - (self->contacts, phone_number); + (calls_contacts_get_default (), phone_number); g_assert (self->contact != NULL); g_object_ref (self->contact); - g_clear_object (&self->contacts); e_phone_number_free (phone_number); g_signal_connect_swapped (self->contact, @@ -452,11 +450,6 @@ set_property (GObject *object, CALLS_CALL_RECORD (g_value_get_object (value))); break; - case PROP_CONTACTS: - g_set_object (&self->contacts, - CALLS_CONTACTS (g_value_get_object (value))); - break; - default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); break; @@ -525,7 +518,6 @@ dispose (GObject *object) CallsCallRecordRow *self = CALLS_CALL_RECORD_ROW (object); g_clear_object (&self->contact); - g_clear_object (&self->contacts); g_clear_object (&self->action_map); g_clear_object (&self->gesture); @@ -559,13 +551,6 @@ calls_call_record_row_class_init (CallsCallRecordRowClass *klass) CALLS_TYPE_CALL_RECORD, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY); - props[PROP_CONTACTS] = - g_param_spec_object ("contacts", - "Contacts", - "Interface for libfolks", - CALLS_TYPE_CONTACTS, - G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY); - g_object_class_install_properties (object_class, PROP_LAST_PROP, props); @@ -632,12 +617,10 @@ calls_call_record_row_init (CallsCallRecordRow *self) CallsCallRecordRow * -calls_call_record_row_new (CallsCallRecord *record, - CallsContacts *contacts) +calls_call_record_row_new (CallsCallRecord *record) { return g_object_new (CALLS_TYPE_CALL_RECORD_ROW, "record", record, - "contacts", contacts, NULL); } diff --git a/src/calls-call-record-row.h b/src/calls-call-record-row.h index eb3b5e2..99f171b 100644 --- a/src/calls-call-record-row.h +++ b/src/calls-call-record-row.h @@ -26,7 +26,6 @@ #define CALLS_CALL_RECORD_ROW_H__ #include "calls-call-record.h" -#include "calls-contacts.h" #include "calls-new-call-box.h" #include @@ -38,8 +37,7 @@ G_BEGIN_DECLS G_DECLARE_FINAL_TYPE (CallsCallRecordRow, calls_call_record_row, CALLS, CALL_RECORD_ROW, GtkListBoxRow) -CallsCallRecordRow *calls_call_record_row_new (CallsCallRecord *record, - CallsContacts *contacts); +CallsCallRecordRow *calls_call_record_row_new (CallsCallRecord *record); CallsCallRecord * calls_call_record_row_get_record (CallsCallRecordRow *self); G_END_DECLS diff --git a/src/calls-contacts.c b/src/calls-contacts.c index 36b1c85..6039543 100644 --- a/src/calls-contacts.c +++ b/src/calls-contacts.c @@ -141,9 +141,15 @@ calls_contacts_init (CallsContacts *self) CallsContacts * -calls_contacts_new () +calls_contacts_get_default () { - return g_object_new (CALLS_TYPE_CONTACTS, NULL); + static CallsContacts *instance; + if (instance == NULL) + { + instance = g_object_new (CALLS_TYPE_CONTACTS, NULL); + g_object_add_weak_pointer (G_OBJECT (instance), (gpointer *) &instance); + } + return instance; } diff --git a/src/calls-contacts.h b/src/calls-contacts.h index e3081ff..9a03953 100644 --- a/src/calls-contacts.h +++ b/src/calls-contacts.h @@ -37,7 +37,7 @@ G_BEGIN_DECLS G_DECLARE_FINAL_TYPE (CallsContacts, calls_contacts, CALLS, CONTACTS, GObject); -CallsContacts * calls_contacts_new (); +CallsContacts * calls_contacts_get_default (); CallsBestMatch * calls_contacts_lookup_phone_number (CallsContacts *self, EPhoneNumber *number); diff --git a/src/calls-history-box.c b/src/calls-history-box.c index 4d6bac6..bc28bca 100644 --- a/src/calls-history-box.c +++ b/src/calls-history-box.c @@ -43,7 +43,6 @@ struct _CallsHistoryBox GListModel *model; gulong model_changed_handler_id; - CallsContacts *contacts; }; G_DEFINE_TYPE (CallsHistoryBox, calls_history_box, GTK_TYPE_STACK); @@ -52,7 +51,6 @@ G_DEFINE_TYPE (CallsHistoryBox, calls_history_box, GTK_TYPE_STACK); enum { PROP_0, PROP_MODEL, - PROP_CONTACTS, PROP_LAST_PROP, }; static GParamSpec *props[PROP_LAST_PROP]; @@ -139,8 +137,7 @@ create_row_cb (CallsCallRecord *record, CallsHistoryBox *self) { GtkWidget *row_widget; - row_widget = GTK_WIDGET (calls_call_record_row_new (record, - self->contacts)); + row_widget = GTK_WIDGET (calls_call_record_row_new (record)); g_signal_connect (record, "call-delete", @@ -165,11 +162,6 @@ set_property (GObject *object, G_LIST_MODEL (g_value_get_object (value))); break; - case PROP_CONTACTS: - g_set_object (&self->contacts, - CALLS_CONTACTS (g_value_get_object (value))); - break; - default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); break; @@ -211,7 +203,6 @@ dispose (GObject *object) { CallsHistoryBox *self = CALLS_HISTORY_BOX (object); - g_clear_object (&self->contacts); g_clear_object (&self->model); G_OBJECT_CLASS (calls_history_box_parent_class)->dispose (object); @@ -235,13 +226,6 @@ calls_history_box_class_init (CallsHistoryBoxClass *klass) G_TYPE_LIST_MODEL, G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY); - props[PROP_CONTACTS] = - g_param_spec_object ("contacts", - "Contacts", - "Interface for libfolks", - CALLS_TYPE_CONTACTS, - G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY); - g_object_class_install_properties (object_class, PROP_LAST_PROP, props); @@ -258,11 +242,9 @@ calls_history_box_init (CallsHistoryBox *self) CallsHistoryBox * -calls_history_box_new (GListModel *model, - CallsContacts *contacts) +calls_history_box_new (GListModel *model) { return g_object_new (CALLS_TYPE_HISTORY_BOX, "model", model, - "contacts", contacts, NULL); } diff --git a/src/calls-history-box.h b/src/calls-history-box.h index 53613bb..67018ae 100644 --- a/src/calls-history-box.h +++ b/src/calls-history-box.h @@ -26,7 +26,6 @@ #define CALLS_HISTORY_BOX_H__ #include "calls-new-call-box.h" -#include "calls-contacts.h" #include @@ -39,8 +38,7 @@ G_BEGIN_DECLS G_DECLARE_FINAL_TYPE (CallsHistoryBox, calls_history_box, CALLS, HISTORY_BOX, GtkStack); -CallsHistoryBox * calls_history_box_new (GListModel *model, - CallsContacts *contacts); +CallsHistoryBox * calls_history_box_new (GListModel *model); G_END_DECLS diff --git a/src/calls-main-window.c b/src/calls-main-window.c index f344f1d..719de74 100644 --- a/src/calls-main-window.c +++ b/src/calls-main-window.c @@ -45,7 +45,6 @@ struct _CallsMainWindow GtkApplicationWindow parent_instance; GListModel *record_store; - CallsContacts *contacts; CallsInAppNotification *in_app_notification; @@ -67,7 +66,6 @@ G_DEFINE_TYPE (CallsMainWindow, calls_main_window, GTK_TYPE_APPLICATION_WINDOW); enum { PROP_0, PROP_RECORD_STORE, - PROP_CONTACTS, PROP_LAST_PROP, }; static GParamSpec *props[PROP_LAST_PROP]; @@ -153,11 +151,6 @@ set_property (GObject *object, G_LIST_MODEL (g_value_get_object (value))); break; - case PROP_CONTACTS: - g_set_object (&self->contacts, - CALLS_CONTACTS (g_value_get_object (value))); - break; - default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); break; @@ -220,8 +213,7 @@ constructed (GObject *object) NULL); // Add call records - history = calls_history_box_new (self->record_store, - self->contacts); + history = calls_history_box_new (self->record_store); widget = GTK_WIDGET (history); gtk_stack_add_titled (self->main_stack, widget, "recent", _("Recent")); @@ -272,7 +264,6 @@ dispose (GObject *object) { CallsMainWindow *self = CALLS_MAIN_WINDOW (object); - g_clear_object (&self->contacts); g_clear_object (&self->record_store); G_OBJECT_CLASS (calls_main_window_parent_class)->dispose (object); @@ -313,13 +304,6 @@ calls_main_window_class_init (CallsMainWindowClass *klass) G_TYPE_LIST_MODEL, G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY); - props[PROP_CONTACTS] = - g_param_spec_object ("contacts", - "Contacts", - "Interface for libfolks", - CALLS_TYPE_CONTACTS, - G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY); - g_object_class_install_properties (object_class, PROP_LAST_PROP, props); @@ -347,17 +331,14 @@ calls_main_window_init (CallsMainWindow *self) CallsMainWindow * calls_main_window_new (GtkApplication *application, - GListModel *record_store, - CallsContacts *contacts) + GListModel *record_store) { g_return_val_if_fail (GTK_IS_APPLICATION (application), NULL); g_return_val_if_fail (G_IS_LIST_MODEL (record_store), NULL); - g_return_val_if_fail (CALLS_IS_CONTACTS (contacts), NULL); return g_object_new (CALLS_TYPE_MAIN_WINDOW, "application", application, "record-store", record_store, - "contacts", contacts, NULL); } diff --git a/src/calls-main-window.h b/src/calls-main-window.h index aa0d63e..2627704 100644 --- a/src/calls-main-window.h +++ b/src/calls-main-window.h @@ -27,8 +27,6 @@ #include -#include "calls-contacts.h" - G_BEGIN_DECLS #define CALLS_TYPE_MAIN_WINDOW (calls_main_window_get_type ()) @@ -36,8 +34,7 @@ G_BEGIN_DECLS G_DECLARE_FINAL_TYPE (CallsMainWindow, calls_main_window, CALLS, MAIN_WINDOW, GtkApplicationWindow); CallsMainWindow *calls_main_window_new (GtkApplication *application, - GListModel *record_store, - CallsContacts *contacts); + GListModel *record_store); void calls_main_window_dial (CallsMainWindow *self, const gchar *target); diff --git a/src/calls-notifier.c b/src/calls-notifier.c index dd4b2e9..0362240 100644 --- a/src/calls-notifier.c +++ b/src/calls-notifier.c @@ -24,26 +24,18 @@ #include "calls-notifier.h" #include "calls-manager.h" +#include "calls-contacts.h" #include "config.h" #include #include -enum { - PROP_0, - PROP_CONTACTS, - PROP_LAST_PROP, -}; -static GParamSpec *props[PROP_LAST_PROP]; - struct _CallsNotifier { GObject parent_instance; GListStore *unanswered; GList *notifications; - - CallsContacts *contacts; }; G_DEFINE_TYPE (CallsNotifier, calls_notifier, G_TYPE_OBJECT); @@ -75,7 +67,7 @@ notify (CallsNotifier *self, CallsCall *call) goto done; } - match = calls_contacts_lookup_phone_number (self->contacts, phone_number); + match = calls_contacts_lookup_phone_number (calls_contacts_get_default (), phone_number); if (!match) goto done; @@ -172,31 +164,10 @@ calls_notifier_dispose (GObject *object) g_list_store_remove_all (self->unanswered); g_clear_object (&self->unanswered); - g_clear_object (&self->contacts); G_OBJECT_CLASS (calls_notifier_parent_class)->dispose (object); } -static void -calls_notifier_set_property (GObject *object, - guint property_id, - const GValue *value, - GParamSpec *pspec) -{ - CallsNotifier *self = CALLS_NOTIFIER (object); - - switch (property_id) - { - case PROP_CONTACTS: - g_set_object (&self->contacts, - CALLS_CONTACTS (g_value_get_object (value))); - break; - - default: - G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); - break; - } -} static void calls_notifier_class_init (CallsNotifierClass *klass) @@ -205,20 +176,10 @@ calls_notifier_class_init (CallsNotifierClass *klass) object_class->constructed = calls_notifier_constructed; object_class->dispose = calls_notifier_dispose; - object_class->set_property = calls_notifier_set_property; - - props[PROP_CONTACTS] = - g_param_spec_object ("contacts", - "Contacts", - "Interface for libfolks", - CALLS_TYPE_CONTACTS, - G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY); - - g_object_class_install_properties (object_class, PROP_LAST_PROP, props); } CallsNotifier * -calls_notifier_new (CallsContacts *contacts) +calls_notifier_new () { - return g_object_new (CALLS_TYPE_NOTIFIER, "contacts", contacts, NULL); + return g_object_new (CALLS_TYPE_NOTIFIER, NULL); } diff --git a/src/calls-notifier.h b/src/calls-notifier.h index b440387..647e3b5 100644 --- a/src/calls-notifier.h +++ b/src/calls-notifier.h @@ -25,8 +25,6 @@ #ifndef CALLS_NOTIFIER_H__ #define CALLS_NOTIFIER_H__ -#include "calls-contacts.h" - #include G_BEGIN_DECLS @@ -35,7 +33,7 @@ G_BEGIN_DECLS G_DECLARE_FINAL_TYPE (CallsNotifier, calls_notifier, CALLS, NOTIFIER, GObject); -CallsNotifier *calls_notifier_new (CallsContacts *contacts); +CallsNotifier *calls_notifier_new (); G_END_DECLS