From 33ad4dbe8641597be3122cbab400205fd8e43934 Mon Sep 17 00:00:00 2001 From: Evangelos Ribeiro Tzaras Date: Thu, 23 Sep 2021 09:32:37 +0200 Subject: [PATCH] Simplify country code handling --- src/calls-application.c | 58 ++++------------------------------ src/calls-application.h | 6 ---- src/calls-best-match.c | 28 ++++------------- src/calls-contacts-provider.c | 56 +++++++++++---------------------- src/calls-contacts-provider.h | 4 ++- src/calls-manager.c | 59 ++++++++++------------------------- src/calls-manager.h | 2 ++ src/calls-new-call-box.c | 7 +++-- 8 files changed, 55 insertions(+), 165 deletions(-) diff --git a/src/calls-application.c b/src/calls-application.c index 7a26599..5e74074 100644 --- a/src/calls-application.c +++ b/src/calls-application.c @@ -38,7 +38,6 @@ #include "calls-call-window.h" #include "calls-main-window.h" #include "calls-manager.h" -#include "calls-settings.h" #include "calls-application.h" #include "calls-log.h" #include "version.h" @@ -61,13 +60,11 @@ struct _CallsApplication GtkApplication parent_instance; gboolean daemon; - CallsManager *manager; CallsRinger *ringer; CallsNotifier *notifier; CallsRecordStore *record_store; CallsMainWindow *main_window; CallsCallWindow *call_window; - CallsSettings *settings; CallsDBusManager *dbus_manager; char *uri; @@ -164,18 +161,19 @@ set_default_providers_action (GSimpleAction *action, GVariant *parameter, gpointer user_data) { - CallsApplication *self = CALLS_APPLICATION (user_data); + CallsManager *manager = calls_manager_get_default (); + CallsSettings *settings = calls_manager_get_settings (manager); g_auto (GStrv) plugins = NULL; /** * Only add default providers when there are none added yet, * This makes sure we're not resetting explicitly set providers */ - if (calls_manager_has_any_provider (calls_manager_get_default ())) + if (calls_manager_has_any_provider (manager)) return; - plugins = calls_settings_get_autoload_plugins (self->settings); + plugins = calls_settings_get_autoload_plugins (settings); for (guint i = 0; plugins[i] != NULL; i++) { - calls_manager_add_provider (calls_manager_get_default (), plugins[i]); + calls_manager_add_provider (manager, plugins[i]); } } @@ -360,8 +358,6 @@ startup (GApplication *application) { g_autoptr (GtkCssProvider) provider = NULL; g_autoptr (GError) error = NULL; - CallsApplication *self = CALLS_APPLICATION (application); - CallsManager *manager; G_APPLICATION_CLASS (calls_application_parent_class)->startup (application); @@ -380,14 +376,7 @@ startup (GApplication *application) G_N_ELEMENTS (actions), application); - self->settings = calls_settings_new (); - g_assert (self->settings != NULL); - - manager = calls_manager_get_default (); - g_object_bind_property (self->settings, "country-code", - manager, "country-code", - G_BINDING_SYNC_CREATE); - g_signal_connect_swapped (manager, + g_signal_connect_swapped (calls_manager_get_default (), "notify::state", G_CALLBACK (manager_state_changed_cb), application); @@ -631,7 +620,6 @@ finalize (GObject *object) g_clear_object (&self->record_store); g_clear_object (&self->ringer); g_clear_object (&self->notifier); - g_clear_object (&self->settings); g_free (self->uri); G_OBJECT_CLASS (calls_application_parent_class)->finalize (object); @@ -712,37 +700,3 @@ calls_application_new (void) "register-session", TRUE, NULL); } - -gboolean -calls_application_get_use_default_origins_setting (CallsApplication *self) -{ - g_return_val_if_fail (CALLS_IS_APPLICATION (self), FALSE); - - return calls_settings_get_use_default_origins (self->settings); -} - -void -calls_application_set_use_default_origins_setting (CallsApplication *self, - gboolean enabled) -{ - g_return_if_fail (CALLS_IS_APPLICATION (self)); - - calls_settings_set_use_default_origins (self->settings, enabled); -} - -char * -calls_application_get_country_code_setting (CallsApplication *self) -{ - g_return_val_if_fail (CALLS_IS_APPLICATION (self), FALSE); - - return calls_settings_get_country_code (self->settings); -} - -void -calls_application_set_country_code_setting (CallsApplication *self, - const char *country_code) -{ - g_return_if_fail (CALLS_IS_APPLICATION (self)); - - calls_settings_set_country_code (self->settings, country_code); -} diff --git a/src/calls-application.h b/src/calls-application.h index cf4dff6..b70e246 100644 --- a/src/calls-application.h +++ b/src/calls-application.h @@ -34,11 +34,5 @@ G_BEGIN_DECLS G_DECLARE_FINAL_TYPE (CallsApplication, calls_application, CALLS, APPLICATION, GtkApplication) CallsApplication *calls_application_new (void); -gboolean calls_application_get_use_default_origins_setting (CallsApplication *self); -void calls_application_set_use_default_origins_setting (CallsApplication *self, - gboolean enabled); -char *calls_application_get_country_code_setting (CallsApplication *self); -void calls_application_set_country_code_setting (CallsApplication *self, - const char *country_code); G_END_DECLS diff --git a/src/calls-best-match.c b/src/calls-best-match.c index 26e14db..b9d79d6 100644 --- a/src/calls-best-match.c +++ b/src/calls-best-match.c @@ -135,7 +135,6 @@ set_property (GObject *object, GParamSpec *pspec) { CallsBestMatch *self = CALLS_BEST_MATCH (object); - const char *country_code; switch (property_id) { case PROP_PHONE_NUMBER: @@ -143,15 +142,12 @@ set_property (GObject *object, break; case PROP_COUNTRY_CODE: - country_code = g_value_get_string (value); - if (country_code) { - g_free (self->country_code); - self->country_code = g_strdup (country_code); + g_free (self->country_code); + self->country_code = g_value_dup_string (value); - if (self->phone_number) { - g_autofree char *number = g_strdup (self->phone_number); - calls_best_match_set_phone_number (self, number); - } + if (self->phone_number) { + g_autofree char *number = g_strdup (self->phone_number); + calls_best_match_set_phone_number (self, number); } break; @@ -273,9 +269,6 @@ calls_best_match_class_init (CallsBestMatchClass *klass) static void calls_best_match_init (CallsBestMatch *self) { - g_object_bind_property (calls_manager_get_default (), "country-code", - self, "country-code", - G_BINDING_SYNC_CREATE); } @@ -307,24 +300,15 @@ calls_best_match_get_phone_number (CallsBestMatch *self) void calls_best_match_set_phone_number (CallsBestMatch *self, - const char *phone_number) + const char *phone_number) { g_autoptr (EPhoneNumber) number = NULL; g_autoptr (CallsPhoneNumberQuery) query = NULL; g_autoptr (GError) error = NULL; - gboolean have_country_code_now = FALSE; g_return_if_fail (CALLS_IS_BEST_MATCH (self)); g_return_if_fail (phone_number); - have_country_code_now = !!self->country_code; - - if (self->phone_number == phone_number && - self->had_country_code_last_time == have_country_code_now) - return; - - self->had_country_code_last_time = have_country_code_now; - g_clear_pointer (&self->phone_number, g_free); // Consider empty string phone numbers as NULL diff --git a/src/calls-contacts-provider.c b/src/calls-contacts-provider.c index 2a36407..0e34858 100644 --- a/src/calls-contacts-provider.c +++ b/src/calls-contacts-provider.c @@ -48,16 +48,16 @@ struct _CallsContactsProvider GObject parent_instance; FolksIndividualAggregator *folks_aggregator; + CallsSettings *settings; GHashTable *phone_number_best_matches; - gchar *country_code; }; G_DEFINE_TYPE (CallsContactsProvider, calls_contacts_provider, G_TYPE_OBJECT) enum { PROP_0, - PROP_COUNTRY_CODE, + PROP_SETTINGS, PROP_LAST_PROP }; static GParamSpec *props[PROP_LAST_PROP]; @@ -177,28 +177,8 @@ calls_contacts_provider_set_property (GObject *object, CallsContactsProvider *self = CALLS_CONTACTS_PROVIDER (object); switch (property_id) { - case PROP_COUNTRY_CODE: - g_free (self->country_code); - self->country_code = g_value_dup_string (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, - GValue *value, - GParamSpec *pspec) -{ - CallsContactsProvider *self = CALLS_CONTACTS_PROVIDER (object); - - switch (property_id) { - case PROP_COUNTRY_CODE: - g_value_set_string (value, self->country_code); + case PROP_SETTINGS: + self->settings = g_value_dup_object (value); break; default: @@ -213,8 +193,8 @@ calls_contacts_provider_finalize (GObject *object) { CallsContactsProvider *self = CALLS_CONTACTS_PROVIDER (object); - g_clear_object (&self->country_code); g_clear_object (&self->folks_aggregator); + g_clear_object (&self->settings); g_clear_pointer (&self->phone_number_best_matches, g_hash_table_unref); G_OBJECT_CLASS (calls_contacts_provider_parent_class)->finalize (object); @@ -226,7 +206,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; @@ -250,11 +229,13 @@ calls_contacts_provider_class_init (CallsContactsProviderClass *klass) 1, FOLKS_TYPE_INDIVIDUAL); - props[PROP_COUNTRY_CODE] = g_param_spec_string ("country-code", - "country code", - "The default country code to use", - NULL, - G_PARAM_READWRITE); + 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); g_object_class_install_properties (object_class, PROP_LAST_PROP, props); } @@ -287,9 +268,9 @@ calls_contacts_provider_init (CallsContactsProvider *self) } CallsContactsProvider * -calls_contacts_provider_new (void) +calls_contacts_provider_new (CallsSettings *settings) { - return g_object_new (CALLS_TYPE_CONTACTS_PROVIDER, NULL); + return g_object_new (CALLS_TYPE_CONTACTS_PROVIDER, "settings", settings, NULL); } @@ -321,7 +302,6 @@ calls_contacts_provider_lookup_phone_number (CallsContactsProvider *self, const gchar *number) { g_autoptr (CallsBestMatch) best_match = NULL; - g_autofree gchar *country_code = NULL; g_return_val_if_fail (CALLS_IS_CONTACTS_PROVIDER (self), NULL); @@ -330,15 +310,15 @@ calls_contacts_provider_lookup_phone_number (CallsContactsProvider *self, if (best_match) { g_object_ref (best_match); - g_object_get (best_match, "country-code", &country_code, NULL); - if (g_strcmp0 (country_code, self->country_code) != 0) - calls_best_match_set_phone_number (best_match, number); - return g_steal_pointer (&best_match); } best_match = calls_best_match_new (number); + g_object_bind_property (self->settings, "country-code", + best_match, "country-code", + G_BINDING_SYNC_CREATE); + g_hash_table_insert (self->phone_number_best_matches, g_strdup (number), g_object_ref (best_match)); return g_steal_pointer (&best_match); diff --git a/src/calls-contacts-provider.h b/src/calls-contacts-provider.h index e105134..ae467d0 100644 --- a/src/calls-contacts-provider.h +++ b/src/calls-contacts-provider.h @@ -27,6 +27,8 @@ #pragma once +#include "calls-settings.h" + #include #include #include @@ -48,7 +50,7 @@ typedef void (*IdleCallback) (gpointer user_data, G_DECLARE_FINAL_TYPE (CallsContactsProvider, calls_contacts_provider, CALLS, CONTACTS_PROVIDER, GObject) -CallsContactsProvider *calls_contacts_provider_new (void); +CallsContactsProvider *calls_contacts_provider_new (CallsSettings *settings); GeeCollection *calls_contacts_provider_get_individuals (CallsContactsProvider *self); CallsBestMatch *calls_contacts_provider_lookup_phone_number (CallsContactsProvider *self, const gchar *number); diff --git a/src/calls-manager.c b/src/calls-manager.c index 57a609a..24a5377 100644 --- a/src/calls-manager.c +++ b/src/calls-manager.c @@ -32,6 +32,7 @@ #include "calls-contacts-provider.h" #include "calls-manager.h" #include "calls-provider.h" +#include "calls-settings.h" #include "calls-ussd.h" #include "enum-types.h" @@ -58,7 +59,7 @@ struct _CallsManager CallsManagerState state; CallsCall *primary_call; - char *country_code; + CallsSettings *settings; }; G_DEFINE_TYPE (CallsManager, calls_manager, G_TYPE_OBJECT); @@ -66,7 +67,6 @@ G_DEFINE_TYPE (CallsManager, calls_manager, G_TYPE_OBJECT); enum { PROP_0, PROP_STATE, - PROP_COUNTRY_CODE, PROP_LAST_PROP, }; static GParamSpec *props[PROP_LAST_PROP]; @@ -258,20 +258,16 @@ update_country_code_cb (CallsOrigin *origin, GParamSpec *pspec, CallsManager *self) { - CallsApplication *app; g_autofree char *country_code = NULL; g_assert (CALLS_IS_MANAGER (self)); - app = CALLS_APPLICATION (g_application_get_default ()); g_object_get (G_OBJECT (origin), "country-code", &country_code, NULL); - if (country_code && g_strcmp0 (country_code, self->country_code) == 0) + if (!country_code || !*country_code) return; - g_free (self->country_code); - self->country_code = country_code; - calls_application_set_country_code_setting (app, country_code); + calls_settings_set_country_code (self->settings, country_code); } static void @@ -547,35 +543,12 @@ calls_manager_get_property (GObject *object, g_value_set_enum (value, calls_manager_get_state (self)); break; - case PROP_COUNTRY_CODE: - g_value_set_string (value, self->country_code); - break; - default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); break; } } -static void -calls_manager_set_property (GObject *object, - guint property_id, - const GValue *value, - GParamSpec *pspec) -{ - CallsManager *self = CALLS_MANAGER (object); - - switch (property_id) { - case PROP_COUNTRY_CODE: - g_free (self->country_code); - self->country_code = g_value_dup_string (value); - break; - - default: - G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); - break; - } -} static void calls_manager_finalize (GObject *object) @@ -584,7 +557,7 @@ calls_manager_finalize (GObject *object) g_clear_object (&self->origins); g_clear_object (&self->contacts_provider); - g_clear_pointer (&self->country_code, g_free); + 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); @@ -600,7 +573,6 @@ calls_manager_class_init (CallsManagerClass *klass) GObjectClass *object_class = G_OBJECT_CLASS (klass); object_class->get_property = calls_manager_get_property; - object_class->set_property = calls_manager_set_property; object_class->finalize = calls_manager_finalize; signals[SIGNAL_CALL_ADD] = @@ -683,12 +655,6 @@ calls_manager_class_init (CallsManagerClass *klass) CALLS_MANAGER_STATE_NO_PROVIDER, G_PARAM_READABLE | G_PARAM_EXPLICIT_NOTIFY); - props[PROP_COUNTRY_CODE] = g_param_spec_string ("country-code", - "country code", - "The default country code to use", - NULL, - G_PARAM_READWRITE); - g_object_class_install_properties (object_class, PROP_LAST_PROP, props); } @@ -714,11 +680,9 @@ calls_manager_init (CallsManager *self) self->origins = g_list_store_new (calls_origin_get_type ()); self->supported_protocols = g_ptr_array_new_full (5, g_free); + self->settings = calls_settings_new (); // Load the contacts provider - self->contacts_provider = calls_contacts_provider_new (); - g_object_bind_property (self, "country-code", - self->contacts_provider, "country-code", - G_BINDING_DEFAULT); + self->contacts_provider = calls_contacts_provider_new (self->settings); peas = peas_engine_get_default (); @@ -982,3 +946,12 @@ calls_manager_get_providers (CallsManager *self) return g_hash_table_get_values (self->providers); } + + +CallsSettings * +calls_manager_get_settings (CallsManager *self) +{ + g_return_val_if_fail (CALLS_IS_MANAGER (self), NULL); + + return self->settings; +} diff --git a/src/calls-manager.h b/src/calls-manager.h index b80f853..1eadc2e 100644 --- a/src/calls-manager.h +++ b/src/calls-manager.h @@ -27,6 +27,7 @@ #include "calls-contacts-provider.h" #include "calls-origin.h" +#include "calls-settings.h" #include @@ -50,6 +51,7 @@ typedef enum CallsManager *calls_manager_new (void); CallsManager *calls_manager_get_default (void); CallsContactsProvider *calls_manager_get_contacts_provider (CallsManager *self); +CallsSettings *calls_manager_get_settings (CallsManager *self); void calls_manager_add_provider (CallsManager *self, const char *name); void calls_manager_remove_provider (CallsManager *self, diff --git a/src/calls-new-call-box.c b/src/calls-new-call-box.c index 6709641..a192b76 100644 --- a/src/calls-new-call-box.c +++ b/src/calls-new-call-box.c @@ -24,11 +24,11 @@ #define G_LOG_DOMAIN "CallsNewCallBox" -#include "calls-application.h" #include "calls-new-call-box.h" #include "calls-ussd.h" #include "calls-main-window.h" #include "calls-manager.h" +#include "calls-settings.h" #include #include @@ -83,11 +83,12 @@ static CallsOrigin * get_origin (CallsNewCallBox *self, const char *target) { - CallsApplication *app = CALLS_APPLICATION (g_application_get_default ()); + CallsManager *manager = calls_manager_get_default (); + CallsSettings *settings = calls_manager_get_settings (manager); g_autoptr (CallsOrigin) origin = NULL; GListModel *model; gboolean auto_use_def_origin = - calls_application_get_use_default_origins_setting (app); + calls_settings_get_use_default_origins (settings); if (auto_use_def_origin) { model = calls_manager_get_suitable_origins (calls_manager_get_default (),