[calls/wip/ui-manage-accounts: 16/26] Introduce CallsAccountManager




commit b8bf340e52410573d4fae4e7726e31464a1d912d
Author: Evangelos Ribeiro Tzaras <evangelos tzaras puri sm>
Date:   Thu May 20 04:43:54 2021 +0200

    Introduce CallsAccountManager
    
    The account manager keeps track of available CallsAccountProviders and
    manages credentials including facilities to load accounts from the system
    via GKeyFiles.
    
    - manager.c: Also add/remove providers to/from the account manager
    - account.*: Add API to fetch the current (online) state of the account
    
    TODO
    -testing of account manager

 src/calls-account-manager.c | 550 ++++++++++++++++++++++++++++++++++++++++++++
 src/calls-account-manager.h |  72 ++++++
 src/calls-account.c         |  21 +-
 src/calls-account.h         |   1 +
 src/calls-manager.c         |  28 +++
 src/calls-manager.h         |   2 +
 src/meson.build             |   2 +
 7 files changed, 675 insertions(+), 1 deletion(-)
---
diff --git a/src/calls-account-manager.c b/src/calls-account-manager.c
new file mode 100644
index 00000000..252058d8
--- /dev/null
+++ b/src/calls-account-manager.c
@@ -0,0 +1,550 @@
+/*
+ * Copyright (C) 2021 Purism SPC
+ *
+ * This file is part of Calls.
+ *
+ * Calls is free software: you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Calls is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Calls.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Author: Evangelos Ribeiro Tzaras <evangelos tzaras puri sm>
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ *
+ */
+
+#define G_LOG_DOMAIN "CallsAccountManager"
+
+#include "calls-account.h"
+#include "calls-account-manager.h"
+#include "calls-credentials.h"
+
+#include "enum-types.h"
+#include "util.h"
+
+/**
+ * SECTION: AccountManager
+ * @short_description: Account manager
+ * @Tittle: CallsAccountManager
+ *
+ * #CallsAccountManager is a manager for the following tasks:
+ * Manage available #CallsCredentials
+ * Central place to keep track of associated #CallsAccount's
+ * Offline storage of Credentials
+ * Managing secrets in a keyring with libsecret
+ */
+
+enum {
+  PROP_0,
+  PROP_STATE,
+  PROP_LAST_PROP
+};
+static GParamSpec *props[PROP_LAST_PROP];
+
+enum {
+  ACCOUNT_STATE_UPDATED,
+  N_SIGNALS
+};
+static guint signals[N_SIGNALS];
+
+struct _CallsAccountManager
+{
+  GObject parent_instance;
+
+  GPtrArray               *providers;
+  GListStore              *credentials;
+
+  CallsAccountManagerState state;
+};
+
+G_DEFINE_TYPE (CallsAccountManager, calls_account_manager, G_TYPE_OBJECT)
+
+
+static gboolean
+find_credentials (CallsAccountManager *self,
+                  const char          *uuid,
+                  guint               *position)
+{
+  guint len;
+  g_assert (CALLS_IS_ACCOUNT_MANAGER (self));
+  g_assert (uuid);
+
+  len = g_list_model_get_n_items (G_LIST_MODEL (self->credentials));
+  for (guint i = 0; i < len; i++) {
+    g_autoptr (CallsCredentials) cred =
+      g_list_model_get_item (G_LIST_MODEL (self->credentials), i);
+
+    if (g_strcmp0 (uuid, calls_credentials_get_uuid (cred)) == 0) {
+      if (position)
+        *position = i;
+      return TRUE;
+    }
+  }
+
+  return FALSE;
+}
+
+
+static CallsCredentials *
+get_credentials (CallsAccountManager *self,
+                 const char          *uuid)
+{
+  guint index;
+
+  if (!find_credentials (self, uuid, &index))
+    return NULL;
+
+  return g_list_model_get_item (G_LIST_MODEL (self->credentials), index);
+}
+
+
+static gboolean
+find_provider (CallsAccountManager  *self,
+               CallsCredentialsType credentials_type,
+               guint                *position)
+{
+  g_assert (CALLS_ACCOUNT_MANAGER (self));
+
+  for (guint i = 0; i < self->providers->len; i++) {
+    CallsAccountProvider *provider = g_ptr_array_index (self->providers, i);
+    CallsCredentialsType provider_type =
+      calls_account_provider_get_credentials_type (provider);
+
+    if (credentials_type == provider_type) {
+      if (position)
+        *position = i;
+      return TRUE;
+    }
+  }
+
+  return FALSE;
+}
+
+
+static CallsAccountProvider *
+get_provider (CallsAccountManager *self,
+              CallsCredentialsType credentials_type)
+{
+  guint index;
+
+  g_assert (CALLS_IS_ACCOUNT_MANAGER (self));
+
+  if (!find_provider (self, credentials_type, &index))
+    return NULL;
+
+  return g_ptr_array_index (self->providers, index);
+}
+
+
+static void
+on_account_state_changed (CallsAccount        *account,
+                          const char          *uuid,
+                          CallsAccountState    account_state,
+                          CallsAccountManager *self)
+{
+  g_assert (CALLS_IS_ACCOUNT (account));
+  g_assert (CALLS_IS_ACCOUNT_MANAGER (self));
+  g_assert (uuid);
+
+  g_signal_emit (self, signals[ACCOUNT_STATE_UPDATED], 0, uuid, account_state);
+}
+
+
+static void
+on_credentials_account_changed (CallsCredentials    *credentials,
+                                GParamSpec          *pspec,
+                                CallsAccountManager *self)
+{
+  CallsAccount *account;
+
+  g_assert (CALLS_IS_CREDENTIALS (credentials));
+  g_assert (CALLS_IS_ACCOUNT_MANAGER (self));
+
+  account = calls_credentials_get_account (credentials);
+  if (account)
+    g_signal_connect (account,
+                      "account-state-changed",
+                      G_CALLBACK (on_account_state_changed),
+                      self);
+}
+
+
+static void
+update_state (CallsAccountManager *self)
+{
+  guint n_providers;
+  guint n_credentials;
+
+  g_assert (CALLS_ACCOUNT_MANAGER (self));
+
+  n_credentials = g_list_model_get_n_items (G_LIST_MODEL (self->credentials));
+  n_providers = self->providers->len;
+
+  if (n_credentials > 0 && n_providers > 0) {
+    /** TODO once we have multiple AccountProviders we need to check
+     * if credentials are suitable for the providers
+     */
+    self->state = CALLS_ACCOUNT_MANAGER_READY;
+    goto notify;
+  }
+  if (n_credentials > 0) {
+    self->state = CALLS_ACCOUNT_MANAGER_ONLY_CREDENTIALS;
+    goto notify;
+  }
+  if (n_providers > 0) {
+    self->state = CALLS_ACCOUNT_MANAGER_ONLY_PROVIDER;
+    goto notify;
+  }
+
+  self->state = CALLS_ACCOUNT_MANAGER_INIT;
+
+ notify:
+  g_object_notify_by_pspec (G_OBJECT (self), props[PROP_STATE]);
+}
+
+
+static void
+calls_account_manager_get_property (GObject    *object,
+                                    guint       property_id,
+                                    GValue     *value,
+                                    GParamSpec *pspec)
+{
+  CallsAccountManager *self = CALLS_ACCOUNT_MANAGER (object);
+
+  switch (property_id) {
+  case PROP_STATE:
+    g_value_set_enum (value, calls_account_manager_get_state (self));
+    break;
+
+  default:
+    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+    break;
+  }
+}
+
+static void
+calls_account_manager_constructed (GObject *object)
+{
+  CallsAccountManager *self = CALLS_ACCOUNT_MANAGER (object);
+
+  self->providers = g_ptr_array_new_with_free_func (g_object_unref);
+  self->credentials = g_list_store_new (CALLS_TYPE_CREDENTIALS);
+
+  G_OBJECT_CLASS (calls_account_manager_parent_class)->constructed (object);
+}
+
+
+static void
+calls_account_manager_dispose (GObject *object)
+{
+  CallsAccountManager *self = CALLS_ACCOUNT_MANAGER (object);
+
+  g_clear_pointer (&self->credentials, g_object_unref);
+  g_clear_pointer (&self->providers, g_ptr_array_unref);
+
+  G_OBJECT_CLASS (calls_account_manager_parent_class)->dispose (object);
+}
+
+
+static void
+calls_account_manager_class_init (CallsAccountManagerClass *klass)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (klass);
+
+  object_class->get_property = calls_account_manager_get_property;
+  object_class->constructed = calls_account_manager_constructed;
+  object_class->dispose = calls_account_manager_dispose;
+
+  props[PROP_STATE] = g_param_spec_enum ("state",
+                                         "State",
+                                         "The state of the account manager",
+                                         CALLS_TYPE_ACCOUNT_MANAGER_STATE,
+                                         CALLS_ACCOUNT_MANAGER_NULL,
+                                         G_PARAM_READABLE | G_PARAM_EXPLICIT_NOTIFY | 
G_PARAM_STATIC_STRINGS);
+
+  g_object_class_install_properties (object_class, PROP_LAST_PROP, props);
+
+  signals[ACCOUNT_STATE_UPDATED] =
+    g_signal_new ("account-state-updated",
+                  CALLS_TYPE_ACCOUNT_MANAGER,
+                  G_SIGNAL_RUN_LAST,
+                  0,
+                  NULL, NULL, NULL,
+                  G_TYPE_NONE,
+                  2,
+                  G_TYPE_STRING, CALLS_TYPE_ACCOUNT_STATE);
+}
+
+
+static void
+calls_account_manager_init (CallsAccountManager *self)
+{
+  self->state = CALLS_ACCOUNT_MANAGER_INIT;
+}
+
+/**
+ * calls_account_manager_new:
+ * Returns: (transfer full): A new #CallsAccountManager
+ */
+CallsAccountManager *
+calls_account_manager_new (void)
+{
+  return g_object_new (CALLS_TYPE_ACCOUNT_MANAGER, NULL);
+}
+
+/**
+ * calls_account_manager_add_account_provider:
+ * @self: A #CallsAccountManager
+ * @provider: The #CallsAccountProvider to add
+ *
+ * Returns: %TRUE if @provider was successfully added, %FALSE otherwise
+ */
+gboolean
+calls_account_manager_add_account_provider (CallsAccountManager  *self,
+                                            CallsAccountProvider *provider)
+{
+  g_return_val_if_fail (CALLS_IS_ACCOUNT_MANAGER (self), FALSE);
+  g_return_val_if_fail (CALLS_IS_ACCOUNT_PROVIDER (provider), FALSE);
+
+  if (g_ptr_array_find (self->providers, provider, NULL)) {
+    g_debug ("Trying to add account provider '%s' a second time",
+             calls_provider_get_name (CALLS_PROVIDER (provider)));
+    return FALSE;
+  }
+
+  g_ptr_array_add (self->providers, g_object_ref (provider));
+
+  /* TODO check if there are already credentials for this provider */
+  update_state (self);
+  return TRUE;
+}
+
+/**
+ * calls_account_manager_remove_account_provider:
+ * @self: A #CallsAccountManager
+ * @provider: The #CallsAccountProvider to remove
+ *
+ * Returns: %TRUE if @provider was successfully removed, %FALSE otherwise
+ */
+gboolean
+calls_account_manager_remove_account_provider (CallsAccountManager  *self,
+                                               CallsAccountProvider *provider)
+{
+  gboolean removed;
+  g_return_val_if_fail (CALLS_IS_ACCOUNT_MANAGER (self), FALSE);
+  g_return_val_if_fail (CALLS_IS_ACCOUNT_PROVIDER (provider), FALSE);
+
+  /* TODO check if there are credentials in use with this provider */
+  removed =  g_ptr_array_remove (self->providers, provider);
+
+  update_state (self);
+
+  return removed;
+}
+
+/**
+ * calls_account_manager_get_state:
+ * @self: A #CallsAccountManager
+ *
+ * Returns: The current #CallsAccountManagerState
+ */
+CallsAccountManagerState
+calls_account_manager_get_state (CallsAccountManager *self)
+{
+  g_return_val_if_fail (CALLS_IS_ACCOUNT_MANAGER (self), CALLS_ACCOUNT_MANAGER_NULL);
+
+  return self->state;
+}
+
+/**
+ * calls_account_manager_add_credentials:
+ * @self: A #CallsAccountManager
+ * @credentials: The #CallsCredentials to add
+ *
+ * The provider property of @credentials must be one of the #CallAccountProvider's
+ * known to @self - i.e. previously added with calls_account_manager_add_account_provider
+ *
+ * Returns: The uuid that represents the added #CallsCredentials
+ */
+const char *
+calls_account_manager_add_credentials (CallsAccountManager *self,
+                                       CallsCredentials    *credentials)
+{
+  const char *uuid;
+  guint index;
+  CallsCredentialsType credentials_type;
+  CallsAccountProvider *provider;
+
+  g_return_val_if_fail (CALLS_IS_ACCOUNT_MANAGER (self), NULL);
+  g_return_val_if_fail (CALLS_IS_CREDENTIALS (credentials), NULL);
+
+  g_debug ("Trying to add credentials with name '%s'",
+           calls_credentials_get_name (credentials));
+
+  uuid = calls_credentials_get_uuid (credentials);
+
+  g_return_val_if_fail (uuid, NULL);
+
+  if (find_credentials (self, uuid, NULL)) {
+    g_debug ("Credentials already in store");
+    return NULL;
+  }
+
+  credentials_type = calls_credentials_get_credentials_type (credentials);
+
+  if (credentials_type == CALLS_CREDENTIALS_TYPE_NULL) {
+    g_info ("Credentials must have a credential type set. Aborting.");
+    return NULL;
+  }
+
+  g_signal_connect (credentials,
+                    "notify::account",
+                    G_CALLBACK (on_credentials_account_changed),
+                    self);
+
+  if (!find_provider (self, credentials_type, &index)) {
+    g_autofree char *provider_type =
+      g_enum_to_string (CALLS_TYPE_CREDENTIALS_TYPE, credentials_type);
+    /* TODO we should be able to queue something up eventually but let's abort for now */
+    g_info ("Cannot find provider of type '%s'. Aborting (for now).", provider_type);
+    return NULL;
+  }
+
+  provider = g_ptr_array_index (self->providers, index);
+
+  if (!calls_account_provider_add_account (provider, credentials)) {
+    g_warning ("Cannot add credentials '%s' to provider '%s'",
+               calls_credentials_get_name (credentials),
+               calls_provider_get_name (CALLS_PROVIDER (provider)));
+    return NULL;
+  }
+
+  g_list_store_append (self->credentials, credentials);
+
+  update_state (self);
+
+  return uuid;
+}
+
+/**
+ * calls_account_manager_get_credentials:
+ * @self: A #CallsAccountManager
+ * @uuid: A uuid
+ *
+ * Returns: (transfer full): If @uuid is found in the store the
+ * corresponding #CallsCredentials is returned, %NULL otherwise.
+ */
+CallsCredentials *
+calls_account_manager_get_credentials (CallsAccountManager *self,
+                                       const char          *uuid)
+{
+  g_return_val_if_fail (CALLS_IS_ACCOUNT_MANAGER (self), NULL);
+  g_return_val_if_fail (uuid, NULL);
+
+  return get_credentials (self, uuid);
+}
+
+
+/**
+ * calls_account_manager_update_credentials:
+ * @self: A #CallsAccountManager
+ * @uuid: A uuid
+ * @new_creds: The new set of credentials
+ *
+ * Update the account represented by @uuid with the new credentials @new_creds.
+ *
+ * Returns: %TRUE if credentials were found and updated, %FALSE otherwise.
+ */
+gboolean
+calls_account_manager_update_credentials (CallsAccountManager *self,
+                                          const char          *uuid,
+                                          CallsCredentials    *new_creds)
+{
+  g_autoptr (CallsCredentials) creds = NULL;
+
+  g_return_val_if_fail (CALLS_IS_ACCOUNT_MANAGER (self), FALSE);
+  g_return_val_if_fail (uuid, FALSE);
+  g_return_val_if_fail (CALLS_IS_CREDENTIALS (new_creds), FALSE);
+
+  creds = get_credentials (self, uuid);
+
+  if (creds == NULL)
+    return FALSE;
+
+  return calls_credentials_update_from_credentials (creds, new_creds);
+}
+
+/**
+ * calls_account_manager_remove_credentials:
+ * @self: A #CallsAccountManager
+ * @uuid: A uuid
+ *
+ * Remove the credentials represented by @uuid.
+ *
+ * Returns: %TRUE if credentials were found and removed, %FALSE otherwise.
+ */
+gboolean
+calls_account_manager_remove_credentials (CallsAccountManager *self,
+                                          const char          *uuid)
+{
+  guint index;
+  CallsAccount *account;
+  CallsAccountProvider *provider;
+  g_autoptr (CallsCredentials) creds = NULL;
+  CallsAccountState acc_state = CALLS_ACCOUNT_NULL;
+
+  g_return_val_if_fail (CALLS_IS_ACCOUNT_MANAGER (self), FALSE);
+  g_return_val_if_fail (uuid, FALSE);
+
+  g_debug ("Trying to remove credentials with uuid %s", uuid);
+  if (!find_credentials (self, uuid, &index)) {
+    g_warning ("Could not find credentials for uuid %s", uuid);
+    return FALSE;
+  }
+
+  find_credentials (self, uuid, &index);
+  creds = g_list_model_get_item (G_LIST_MODEL (self->credentials), index);
+
+  provider = get_provider (self, calls_credentials_get_credentials_type (creds));
+  /* It's okay if there is no provider since the plugin could simply be unloaded */
+  if (provider) {
+    account = calls_credentials_get_account (creds);
+
+    /* Since all accounts on the provider should be correctly managed
+     * it would be an error if there are creds, the provider is available,
+     * but no account for the given credentials could be found.
+     */
+    if (!account) {
+      g_warning ("Did not find an account on provider '%s' for uuid %s",
+                 calls_provider_get_name (CALLS_PROVIDER (provider)), uuid);
+      return FALSE;
+    }
+    g_signal_handlers_disconnect_by_data (account, self);
+
+    acc_state = calls_account_get_state (account);
+    if (acc_state == CALLS_ACCOUNT_ONLINE ||
+        acc_state == CALLS_ACCOUNT_AUTHENTICATING ||
+        acc_state == CALLS_ACCOUNT_CONNECTING)
+      calls_account_go_online (account, FALSE);
+
+    if (!calls_account_provider_remove_account (provider, creds)) {
+      g_warning ("Could not remove account on provider '%s' for uuid '%s'",
+                 calls_provider_get_name (CALLS_PROVIDER (provider)), uuid);
+      return FALSE;
+    }
+  }
+
+  g_list_store_remove (self->credentials, index);
+  update_state (self);
+
+  return TRUE;
+}
diff --git a/src/calls-account-manager.h b/src/calls-account-manager.h
new file mode 100644
index 00000000..1b1ac8a9
--- /dev/null
+++ b/src/calls-account-manager.h
@@ -0,0 +1,72 @@
+/*
+ * Copyright (C) 2021 Purism SPC
+ *
+ * This file is part of Calls.
+ *
+ * Calls is free software: you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * Calls is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Calls.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Author: Evangelos Ribeiro Tzaras <evangelos tzaras puri sm>
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ *
+ */
+
+#pragma once
+
+#include "calls-account-provider.h"
+
+#include <glib-object.h>
+
+G_BEGIN_DECLS
+
+#define CALLS_TYPE_ACCOUNT_MANAGER (calls_account_manager_get_type ())
+
+G_DECLARE_FINAL_TYPE (CallsAccountManager, calls_account_manager, CALLS, ACCOUNT_MANAGER, GObject)
+
+/**
+ * CallsAccountManagerState:
+ * @CALLS_ACCOUNT_MANAGER_NULL: Uninitialized
+ * @CALLS_ACCOUNT_MANAGER_INIT: Initialized, but don't have any #CallsAccountProvider or
+ * #CallsCredentials yet
+ * @CALLS_ACCOUNT_MANAGER_ONLY_PROVIDER: Got #CallsAccountProvider, but no #CallsCredentials
+ * @CALLS_ACCOUNT_MANAGER_ONLY_CREDENTIALS: Got #CallsCredentials, but no #CallsAccountProvider
+ * @CALLS_ACCOUNT_MANAGER_READY: Got both #CallsCredentials and #CallsAccountProvider
+ *
+ * State enum values for #CallsAccountManager.
+ */
+typedef enum {
+  CALLS_ACCOUNT_MANAGER_NULL = 0,
+  CALLS_ACCOUNT_MANAGER_INIT,
+  CALLS_ACCOUNT_MANAGER_ONLY_PROVIDER,
+  CALLS_ACCOUNT_MANAGER_ONLY_CREDENTIALS,
+  CALLS_ACCOUNT_MANAGER_READY,
+} CallsAccountManagerState;
+
+CallsAccountManager      *calls_account_manager_new                      (void);
+CallsAccountManagerState  calls_account_manager_get_state                (CallsAccountManager  *self);
+gboolean                  calls_account_manager_add_account_provider     (CallsAccountManager  *self,
+                                                                          CallsAccountProvider *provider);
+gboolean                  calls_account_manager_remove_account_provider  (CallsAccountManager  *self,
+                                                                          CallsAccountProvider *provider);
+const char               *calls_account_manager_add_credentials          (CallsAccountManager  *self,
+                                                                          CallsCredentials     *credentials);
+gboolean                  calls_account_manager_remove_credentials       (CallsAccountManager  *self,
+                                                                          const char           *uuid);
+CallsCredentials         *calls_account_manager_get_credentials          (CallsAccountManager  *self,
+                                                                          const char           *uuid);
+gboolean                  calls_account_manager_update_credentials       (CallsAccountManager  *self,
+                                                                          const char           *uuid,
+                                                                          CallsCredentials     *new_creds);
+
+G_END_DECLS
diff --git a/src/calls-account.c b/src/calls-account.c
index 07954edb..848644a5 100644
--- a/src/calls-account.c
+++ b/src/calls-account.c
@@ -71,9 +71,10 @@ calls_account_default_init (CallsAccountInterface *iface)
                        G_PARAM_READABLE));
 }
 
+// TODO this could use an async version with both a cancellable and a callback function
 /**
  * calls_account_go_online:
- * @self: a #CallsAccount
+ * @self: A #CallsAccount
  * @online: %TRUE to try to go online, %FALSE to go offline
  *
  * Connect or disconnect to a server.
@@ -91,3 +92,21 @@ calls_account_go_online (CallsAccount *self,
 
   return iface->go_online (self, online);
 }
+
+/**
+ * calls_account_get_state:
+ * @self: A #CallsAccount
+ *
+ * Returns: The current #CallsAccountState of this account
+ */
+CallsAccountState
+calls_account_get_state (CallsAccount *self)
+{
+  CallsAccountState state;
+
+  g_return_val_if_fail (CALLS_IS_ACCOUNT (self), CALLS_ACCOUNT_NULL);
+
+  g_object_get (self, "account-state", &state, NULL);
+
+  return state;
+}
diff --git a/src/calls-account.h b/src/calls-account.h
index 1585f30a..b3501150 100644
--- a/src/calls-account.h
+++ b/src/calls-account.h
@@ -69,5 +69,6 @@ typedef enum {
 
 void                   calls_account_go_online          (CallsAccount *self,
                                                          gboolean      online);
+CallsAccountState      calls_account_get_state          (CallsAccount *self);
 
 G_END_DECLS
diff --git a/src/calls-manager.c b/src/calls-manager.c
index e3002a0b..396a4447 100644
--- a/src/calls-manager.c
+++ b/src/calls-manager.c
@@ -54,6 +54,7 @@ struct _CallsManager
   GHashTable *origins_by_protocol;
 
   CallsContactsProvider *contacts_provider;
+  CallsAccountManager *account_manager;
 
   CallsManagerState state;
   CallsCall *primary_call;
@@ -388,6 +389,10 @@ remove_provider (CallsManager *self,
     return;
   }
 
+  if (CALLS_IS_ACCOUNT_PROVIDER (provider))
+    calls_account_manager_remove_account_provider (self->account_manager,
+                                                   CALLS_ACCOUNT_PROVIDER (provider));
+
   g_debug ("Remove provider: %s", name);
   g_signal_handlers_disconnect_by_data (provider, self);
 
@@ -524,6 +529,9 @@ add_provider (CallsManager *self, const gchar *name)
   n_items = g_list_model_get_n_items (origins);
   origin_items_changed_cb (origins, 0, 0, n_items, self);
 
+  if (CALLS_IS_ACCOUNT_PROVIDER (provider))
+    calls_account_manager_add_account_provider (self->account_manager,
+                                                CALLS_ACCOUNT_PROVIDER (provider));
 }
 
 static void
@@ -576,6 +584,7 @@ calls_manager_finalize (GObject *object)
 
   g_clear_object (&self->origins);
   g_clear_object (&self->contacts_provider);
+  g_clear_object (&self->account_manager);
   g_clear_pointer (&self->country_code, g_free);
 
   g_clear_pointer (&self->providers, g_hash_table_unref);
@@ -703,6 +712,9 @@ calls_manager_init (CallsManager *self)
                           self->contacts_provider, "country-code",
                           G_BINDING_DEFAULT);
 
+  // Load the account manager
+  self->account_manager = calls_account_manager_new ();
+
   peas = peas_engine_get_default ();
 
   dir = g_getenv ("CALLS_PLUGIN_DIR");
@@ -1000,3 +1012,19 @@ calls_manager_get_provider_names (CallsManager *self,
 
   return (const char **) g_hash_table_get_keys_as_array (self->providers, length);
 }
+
+
+/**
+ * calls_manager_get_account_manager:
+ * @self: The #CallsManager
+ *
+ * Returns: (transfer none): The #CallsAccountManager in use by this #CallsManager.
+ * Do not free it.
+ */
+CallsAccountManager *
+calls_manager_get_account_manager (CallsManager *self)
+{
+  g_return_val_if_fail (CALLS_IS_MANAGER (self), NULL);
+
+  return self->account_manager;
+}
diff --git a/src/calls-manager.h b/src/calls-manager.h
index c2a81e16..a64c4184 100644
--- a/src/calls-manager.h
+++ b/src/calls-manager.h
@@ -24,6 +24,7 @@
 
 #pragma once
 
+#include "calls-account-manager.h"
 #include "calls-contacts-provider.h"
 #include "calls-origin.h"
 #include "calls-credentials.h"
@@ -78,5 +79,6 @@ void                   calls_manager_hang_up_all_calls        (CallsManager
 gboolean               calls_manager_has_any_provider         (CallsManager     *self);
 const char           **calls_manager_get_provider_names       (CallsManager     *self,
                                                                guint            *length);
+CallsAccountManager   *calls_manager_get_account_manager      (CallsManager     *self);
 
 G_END_DECLS
diff --git a/src/meson.build b/src/meson.build
index 39a8141c..65152bd5 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -70,6 +70,7 @@ calls_enum_headers = files(['calls-call.h',
                             'calls-ussd.h',
                             'calls-manager.h',
                             'calls-account.h',
+                            'calls-account-manager.h',
                             'calls-credentials.h',
                             ])
 calls_enum_sources = gnome.mkenums_simple('enum-types',
@@ -117,6 +118,7 @@ calls_sources = files(['calls-message-source.c', 'calls-message-source.h',
                        'calls-contacts-row.c', 'calls-contacts-row.h',
                        'calls-credentials.c', 'calls-credentials.h',
                        'calls-account.c', 'calls-account.h',
+                       'calls-account-manager.c', 'calls-account-manager.h',
                        'calls-account-provider.c', 'calls-account-provider.h',
                        'calls-settings.c', 'calls-settings.h',
                       ]) + wayland_sources + calls_generated_sources


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