[calls/wip/ui-manage-accounts: 21/26] account-overview: Hook up account state and GtkSwitch




commit 065dfbaf4577979e4a464b03d0cbdcf13f2b3a23
Author: Evangelos Ribeiro Tzaras <evangelos tzaras puri sm>
Date:   Fri Jun 25 17:08:58 2021 +0200

    account-overview: Hook up account state and GtkSwitch

 src/calls-account-overview.c | 88 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 88 insertions(+)
---
diff --git a/src/calls-account-overview.c b/src/calls-account-overview.c
index feb91958..25cb360c 100644
--- a/src/calls-account-overview.c
+++ b/src/calls-account-overview.c
@@ -219,6 +219,29 @@ clear_credentials (CallsAccountOverview *self)
 }
 
 
+static CallsAccountRow *
+find_row_with_uuid (CallsAccountOverview *self,
+                    const char           *uuid)
+{
+  guint n_items;
+
+  g_assert (CALLS_IS_ACCOUNT_OVERVIEW (self));
+  g_assert (uuid);
+
+  n_items = g_list_model_get_n_items (calls_account_manager_get_credentials_list (self->manager));
+
+  for (gint i = 0; i < n_items; i++) {
+    GtkListBoxRow *row =
+      gtk_list_box_get_row_at_index (GTK_LIST_BOX (self->overview), i);
+    CallsAccountRow *acc_row = CALLS_ACCOUNT_ROW (row);
+
+    if (g_strcmp0 (calls_account_row_get_uuid (acc_row), uuid) == 0)
+      return acc_row;
+  }
+  return NULL;
+}
+
+
 static gboolean
 find_protocol (CallsAccountOverview *self,
                const char           *protocol,
@@ -462,6 +485,61 @@ on_manager_state_changed (CallsAccountOverview *self)
 }
 
 
+static void
+on_account_state_updated (CallsAccountOverview *self,
+                          const char           *uuid,
+                          CallsAccountState     state)
+{
+  CallsAccountRow *row;
+
+  row = find_row_with_uuid (self, uuid);
+  g_assert (row);
+
+  if (state == CALLS_ACCOUNT_CONNECTING ||
+      state == CALLS_ACCOUNT_AUTHENTICATING)
+    self->connecting = TRUE;
+  else
+    self->connecting = FALSE;
+
+  /* Update spinner */
+  update_header_visibility (self);
+
+  if (state == CALLS_ACCOUNT_ONLINE)
+    calls_account_row_set_online (row, TRUE);
+  else
+    calls_account_row_set_online (row, FALSE);
+}
+
+
+static void
+on_online_switched (CallsAccountRow      *row,
+                    const char           *uuid,
+                    gboolean              online,
+                    CallsAccountOverview *self)
+{
+  g_autoptr (CallsCredentials)  credentials;
+  CallsAccount                 *account;
+  CallsAccountState             state;
+
+  credentials = calls_account_manager_get_credentials (self->manager, uuid);
+
+  account = calls_credentials_get_account (credentials);
+  if (account == NULL) {
+    g_warning ("Cannot go online with credentials %s because there is no active account",
+               calls_credentials_get_name (credentials));
+    return;
+  }
+  state = calls_account_get_state (account);
+
+  if (state == CALLS_ACCOUNT_ONLINE && online)
+    return;
+
+  if (state == CALLS_ACCOUNT_OFFLINE && !online)
+    return;
+
+  calls_account_go_online (account, online);
+}
+
 static void
 on_credentials_items_changed (GListModel           *credentials,
                               guint                 position,
@@ -484,6 +562,11 @@ on_credentials_items_changed (GListModel           *credentials,
 
     creds = g_list_model_get_item (credentials, position + i);
     cred_row = calls_account_row_new (creds);
+    g_signal_connect_object (cred_row,
+                             "online-switch-changed",
+                             G_CALLBACK (on_online_switched),
+                             self,
+                             G_CONNECT_AFTER);
     gtk_list_box_insert (GTK_LIST_BOX (self->overview),
                          GTK_WIDGET (cred_row),
                          position + i);
@@ -525,6 +608,11 @@ calls_account_overview_constructed (GObject *object)
                             G_CALLBACK (on_manager_state_changed),
                             self);
 
+  g_signal_connect_swapped (self->manager,
+                            "account-state-updated",
+                            G_CALLBACK (on_account_state_updated),
+                            self);
+
   credentials = calls_account_manager_get_credentials_list (self->manager);
   g_signal_connect (credentials,
                     "items-changed",


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