[calls/wip/ui-manage-accounts: 22/26] account-manager: Check credentials when adding or removing account provider




commit 86b8a395359bb1fd17626c06b5e50cb3edc1de3e
Author: Evangelos Ribeiro Tzaras <evangelos tzaras puri sm>
Date:   Mon Jun 28 10:33:20 2021 +0200

    account-manager: Check credentials when adding or removing account provider
    
    And make sure to add or remove the corresponding accounts.

 src/calls-account-manager.c | 53 ++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 50 insertions(+), 3 deletions(-)
---
diff --git a/src/calls-account-manager.c b/src/calls-account-manager.c
index f840ddf4..8412d9da 100644
--- a/src/calls-account-manager.c
+++ b/src/calls-account-manager.c
@@ -312,18 +312,31 @@ gboolean
 calls_account_manager_add_account_provider (CallsAccountManager  *self,
                                             CallsAccountProvider *provider)
 {
+  guint n_credentials;
+  const char *provider_name;
+
   g_return_val_if_fail (CALLS_IS_ACCOUNT_MANAGER (self), FALSE);
   g_return_val_if_fail (CALLS_IS_ACCOUNT_PROVIDER (provider), FALSE);
 
+  provider_name = calls_provider_get_name (CALLS_PROVIDER (provider));
   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)));
+             provider_name);
     return FALSE;
   }
 
   g_ptr_array_add (self->providers, g_object_ref (provider));
 
-  /* TODO check if there are already credentials for this provider */
+  n_credentials = g_list_model_get_n_items (G_LIST_MODEL (self->credentials));
+  for (guint i = 0; i < n_credentials; i++) {
+    g_autoptr (CallsCredentials) cred =
+      g_list_model_get_item (G_LIST_MODEL (self->credentials), i);
+
+    if (calls_credentials_get_credentials_type (cred) ==
+        calls_account_provider_get_credentials_type (provider))
+      calls_account_provider_add_account (provider, cred);
+  }
+
   update_state (self);
   return TRUE;
 }
@@ -340,10 +353,44 @@ calls_account_manager_remove_account_provider (CallsAccountManager  *self,
                                                CallsAccountProvider *provider)
 {
   gboolean removed;
+  CallsAccountProvider *provider_credentials;
+  guint n_credentials;
+
   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 */
+  n_credentials = g_list_model_get_n_items (G_LIST_MODEL (self->credentials));
+  for (guint i = 0; i < n_credentials; i++) {
+    g_autoptr (CallsCredentials) cred =
+      g_list_model_get_item (G_LIST_MODEL (self->credentials), i);
+    CallsCredentialsType credentials_type =
+      calls_credentials_get_credentials_type (cred);
+
+    provider_credentials = get_provider (self, credentials_type);
+    if (provider_credentials == provider) {
+      CallsAccount *account = calls_credentials_get_account (cred);
+      CallsAccountState acc_state;
+
+      if (account == NULL) {
+        g_warning ("Could not find account on provider %s for credentials %s",
+                   calls_provider_get_name (CALLS_PROVIDER (provider)),
+                   calls_credentials_get_name (cred));
+        continue;
+      }
+      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, cred)) {
+        g_warning ("Could not remove account on provider %s for credentials %s",
+                   calls_provider_get_name (CALLS_PROVIDER (provider)),
+                   calls_credentials_get_name (cred));
+      }
+    }
+  }
+
   removed =  g_ptr_array_remove (self->providers, provider);
 
   update_state (self);


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