[calls/wip/ui-manage-accounts: 7/26] credentials: Add update_from_credentials() and is_ready() API




commit 98b751774dc791c4f1c7254c9eb56068180c4fee
Author: Evangelos Ribeiro Tzaras <evangelos tzaras puri sm>
Date:   Tue Jun 1 04:48:16 2021 +0200

    credentials: Add update_from_credentials() and is_ready() API
    
    These will be useful in the CallsAccountManager and CallsAccountOverview.

 src/calls-credentials.c | 100 ++++++++++++++++++++++++++++++++++++++++++++++++
 src/calls-credentials.h |   3 ++
 2 files changed, 103 insertions(+)
---
diff --git a/src/calls-credentials.c b/src/calls-credentials.c
index 202ad9ee..853118ba 100644
--- a/src/calls-credentials.c
+++ b/src/calls-credentials.c
@@ -491,3 +491,103 @@ calls_credentials_get_account (CallsCredentials *self)
 
   return self->account;
 }
+
+
+/**
+ * calls_credentials_update_from_credentials:
+ * @self: A #CallsCredentials to update
+ * @other: The source from which to update
+ *
+ * Update the credentials @self with the values from @other.
+ * However, the provider fields in both credentials must match.
+ * And we don't allow ready credentials to update from non ready ones.
+ * If @other has no provider set, we will not update the provider in @self.
+ *
+ * Returns: %FALSE if both credentials are used with different #CallsAccountProvider's,
+ * %TRUE otherwise.
+ */
+gboolean
+calls_credentials_update_from_credentials (CallsCredentials *self,
+                                           CallsCredentials *other)
+{
+  g_return_val_if_fail (CALLS_IS_CREDENTIALS (self), FALSE);
+
+  if (calls_credentials_is_ready (self) && !calls_credentials_is_ready (other)) {
+    g_debug ("Refusing to update ready credentials from non ready credentials");
+    return FALSE;
+  }
+
+  if (self->credentials_type != CALLS_CREDENTIALS_TYPE_NULL &&
+      other->credentials_type != CALLS_CREDENTIALS_TYPE_NULL &&
+      self->credentials_type != other->credentials_type) {
+    g_autofree char *credentials_type =
+      g_enum_to_string (CALLS_TYPE_CREDENTIALS_TYPE, self->credentials_type);
+    g_autofree char *other_credentials_type =
+      g_enum_to_string (CALLS_TYPE_CREDENTIALS_TYPE, other->credentials_type);
+
+    g_info ("Refusing to update credentials from different providers:\n"
+            "our '%s' and their '%s'", credentials_type, other_credentials_type);
+    return FALSE;
+  }
+
+  /* Update the provider from other if it hasn't been set yet in self */
+  if (self->credentials_type == CALLS_CREDENTIALS_TYPE_NULL &&
+      other->credentials_type != CALLS_CREDENTIALS_TYPE_NULL)
+    self->credentials_type = other->credentials_type;
+
+  /* Update the rest of the credentials */
+  calls_credentials_set_name (self, other->name);
+
+  g_clear_pointer (&self->host, g_free);
+  if (other->host)
+    self->host = g_strdup (other->host);
+
+  g_clear_pointer (&self->display_name, g_free);
+  if (other->display_name)
+    self->display_name = g_strdup (other->display_name);
+
+  g_clear_pointer (&self->user, g_free);
+  if (other->user)
+    self->user = g_strdup (other->user);
+
+  g_clear_pointer (&self->password, g_free);
+  if (other->password)
+    self->password = g_strdup (other->password);
+
+  g_clear_pointer (&self->transport_protocol, g_free);
+  if (other->transport_protocol)
+    self->transport_protocol = g_strdup (other->transport_protocol);
+
+  self->port = other->port;
+
+  g_signal_emit (self, signals[SIGNAL_ACCOUNT_UPDATED], 0);
+  return TRUE;
+}
+
+
+/**
+ * calls_credentials_is_ready:
+ * @self: A #CallsCredentials
+ *
+ * At the bare minimum it checks if there is a host name set.
+ * If provided, a sanity check on the port number is performed,
+ * but that is about it. Additional checks should be provided by
+ * the #CallsAccountProvider in the future.
+ *
+ * Returns: %TRUE if basic checks are passed, %FALSE otherwise.
+ */
+gboolean
+calls_credentials_is_ready (CallsCredentials *self)
+{
+  g_return_val_if_fail (CALLS_IS_CREDENTIALS (self), FALSE);
+
+  if (self->host && g_strcmp0 (self->host, "") != 0 &&
+      self->transport_protocol && g_strcmp0 (self->transport_protocol, "") != 0) {
+    if (self->port > 0 && self->port < 1025 && self->port > 65535)
+      return FALSE;
+
+    return TRUE;
+  }
+
+  return FALSE;
+}
diff --git a/src/calls-credentials.h b/src/calls-credentials.h
index 378016eb..61fbd1f9 100644
--- a/src/calls-credentials.h
+++ b/src/calls-credentials.h
@@ -59,6 +59,9 @@ CallsCredentialsType    calls_credentials_get_credentials_type    (CallsCredenti
 void                    calls_credentials_set_account             (CallsCredentials *self,
                                                                    CallsAccount     *account);
 CallsAccount           *calls_credentials_get_account             (CallsCredentials *self);
+gboolean                calls_credentials_update_from_credentials (CallsCredentials *self,
+                                                                   CallsCredentials *other);
+gboolean                calls_credentials_is_ready                (CallsCredentials *self);
 
 G_END_DECLS
 


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