[calls/wip/ui-manage-accounts: 13/26] credentials: Rework loading credentials




commit 4c3c97152d126469dab1dcf3aa1fd6bb42e5db68
Author: Evangelos Ribeiro Tzaras <evangelos tzaras puri sm>
Date:   Fri Jun 11 09:40:38 2021 +0200

    credentials: Rework loading credentials
    
    This gets rid of the `calls_credentials_update_from_keyfile()`, which had to be
    used after `calls_credentials_new()` in favour of
    `calls_credentials_new_from_keyfile()`.
    
    Additionally it introduces `calls_credentials_save_to_key_file()`

 src/calls-credentials.c | 191 +++++++++++++++++++++++++++++++++++-------------
 src/calls-credentials.h |   7 +-
 2 files changed, 146 insertions(+), 52 deletions(-)
---
diff --git a/src/calls-credentials.c b/src/calls-credentials.c
index 4de88db4..9351b594 100644
--- a/src/calls-credentials.c
+++ b/src/calls-credentials.c
@@ -47,6 +47,9 @@ enum {
   PROP_ACC_AUTO_CONNECT,
   PROP_CREDENTIALS_TYPE,
   PROP_ACCOUNT,
+  PROP_DEBUG,
+  PROP_DEBUG_LOCAL_PORT,
+  PROP_UUID,
   PROP_LAST_PROP,
 };
 static GParamSpec *props[PROP_LAST_PROP];
@@ -75,6 +78,8 @@ struct _CallsCredentials
   char *uuid;
 
   gboolean auto_connect;
+  gboolean debug;
+  gint local_port;
 
   /* The account these credentials are currently used for */
   CallsAccount *account;
@@ -160,6 +165,19 @@ calls_credentials_set_property (GObject      *object,
     calls_credentials_set_account (self, g_value_get_object (value));
     break;
 
+  case PROP_DEBUG:
+    self->debug = g_value_get_boolean (value);
+    break;
+
+  case PROP_DEBUG_LOCAL_PORT:
+    self->local_port = g_value_get_int (value);
+    break;
+
+  case PROP_UUID:
+    if (g_value_get_string (value))
+      self->uuid = g_value_dup_string (value);
+    break;
+
   default:
     G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
     break;
@@ -216,6 +234,18 @@ calls_credentials_get_property (GObject    *object,
     g_value_set_object (value, calls_credentials_get_account (self));
     break;
 
+  case PROP_DEBUG:
+    g_value_set_boolean (value, self->debug);
+    break;
+
+  case PROP_DEBUG_LOCAL_PORT:
+    g_value_set_int (value, self->local_port);
+    break;
+
+  case PROP_UUID:
+    g_value_set_string (value, calls_credentials_get_uuid (self));
+    break;
+
   default:
     G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
     break;
@@ -223,6 +253,18 @@ calls_credentials_get_property (GObject    *object,
 }
 
 
+static void
+calls_credentials_constructed (GObject *object)
+{
+  CallsCredentials *self = CALLS_CREDENTIALS (object);
+
+  if (!self->uuid)
+    self->uuid = g_uuid_string_random ();
+
+  G_OBJECT_CLASS (calls_credentials_parent_class)->constructed (object);
+}
+
+
 static void
 calls_credentials_finalize (GObject *object)
 {
@@ -248,6 +290,7 @@ calls_credentials_class_init (CallsCredentialsClass *klass)
 
   object_class->set_property = calls_credentials_set_property;
   object_class->get_property = calls_credentials_get_property;
+  object_class->constructed = calls_credentials_constructed;
   object_class->finalize = calls_credentials_finalize;
 
   props[PROP_NAME] =
@@ -321,6 +364,27 @@ calls_credentials_class_init (CallsCredentialsClass *klass)
                          CALLS_TYPE_ACCOUNT,
                          G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY);
 
+  props[PROP_DEBUG] =
+    g_param_spec_boolean ("debug",
+                          "Debug",
+                          "Whether we are using debug mode",
+                          FALSE,
+                          G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
+
+  props[PROP_DEBUG_LOCAL_PORT] =
+    g_param_spec_int ("local-port",
+                      "Local port",
+                      "The local port",
+                      0, 65535, 0,
+                      G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
+
+  props[PROP_UUID] =
+    g_param_spec_string ("uuid",
+                         "uuid",
+                         "The uuid of the credentials",
+                         NULL,
+                         G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS);
+
   g_object_class_install_properties (object_class, PROP_LAST_PROP, props);
 
   signals[SIGNAL_CREDENTIALS_UPDATED] =
@@ -336,7 +400,6 @@ calls_credentials_class_init (CallsCredentialsClass *klass)
 static void
 calls_credentials_init (CallsCredentials *self)
 {
-  self->uuid = g_uuid_string_random ();
 }
 
 
@@ -353,45 +416,51 @@ calls_credentials_new (CallsCredentialsType  credentials_type,
 }
 
 /**
- * calls_credentials_update_from_keyfile:
- * @self: A #CallsCredentials
+ * calls_credentials_new_from_keyfile:
  * @key_file: A #GKeyFile
  * @name: The name of the credentials which doubles as the option group
  *
- * Updates the credentials from a given keyfile.
- *
- * Returns: %TRUE if credentials were updated, %FALSE otherwise
+ * Returns: (transfer full): A new #CallsCredentials with fields from @key_file
  */
-gboolean
-calls_credentials_update_from_keyfile (CallsCredentials *self,
-                                       GKeyFile         *key_file,
-                                       const char       *name)
+CallsCredentials*
+calls_credentials_new_from_keyfile (GKeyFile         *key_file,
+                                    const char       *name)
 {
-  char *user = NULL;
-  char *password = NULL;
-  char *host = NULL;
-  char *protocol = NULL;
-  char *display_name = NULL;
+  g_autofree char *host = NULL;
+  g_autofree char *user = NULL;
+  g_autofree char *password = NULL;
+  g_autofree char *display_name = NULL;
+  g_autofree char *protocol = NULL;
+  g_autofree char *uuid = NULL;
   gint port = 0;
+  gint local_port = 0;
   CallsCredentialsType credentials_type = CALLS_CREDENTIALS_TYPE_NULL;
   gboolean auto_connect = TRUE;
+  gboolean debug = FALSE;
 
-  g_return_val_if_fail (CALLS_IS_CREDENTIALS (self), FALSE);
-  g_return_val_if_fail (name, FALSE);
-  g_return_val_if_fail (g_key_file_has_group (key_file, name), FALSE);
+  g_return_val_if_fail (name, NULL);
+  g_return_val_if_fail (key_file, NULL);
+  g_return_val_if_fail (g_key_file_has_group (key_file, name), NULL);
+
+  if (g_key_file_has_key (key_file, name, "Debug", NULL))
+    debug = g_key_file_get_boolean (key_file, name, "Debug", NULL);
 
-  if (!check_required_keys (key_file, name)) {
+  /* For debugging we don't want strict checking */
+  if (!debug && !check_required_keys (key_file, name)) {
     g_warning ("Not all required keys found in section %s", name);
-    return FALSE;
+    return NULL;
   }
 
+  host = g_key_file_get_string (key_file, name, "Host", NULL);
   user = g_key_file_get_string (key_file, name, "User", NULL);
   password = g_key_file_get_string (key_file, name, "Password", NULL);
-  host = g_key_file_get_string (key_file, name, "Host", NULL);
+  display_name = g_key_file_get_string (key_file, name, "DisplayName", NULL);
   protocol = g_key_file_get_string (key_file, name, "Protocol", NULL);
+  uuid = g_key_file_get_string (key_file, name, "Uuid", NULL);
   port = g_key_file_get_integer (key_file, name, "Port", NULL);
   display_name = g_key_file_get_string (key_file, name, "DisplayName", NULL);
   credentials_type = g_key_file_get_integer (key_file, name, "CredentialsType", NULL);
+  local_port = g_key_file_get_integer (key_file, name, "LocalPort", NULL);
 
   if (g_key_file_has_key (key_file, name, "AutoConnect", NULL))
     auto_connect = g_key_file_get_boolean (key_file, name, "AutoConnect", NULL);
@@ -399,47 +468,68 @@ calls_credentials_update_from_keyfile (CallsCredentials *self,
   if (protocol == NULL)
     protocol = g_strdup ("UDP");
 
-  if (g_strcmp0 (host, "") == 0 ||
-      g_strcmp0 (user, "") == 0 ||
-      g_strcmp0 (password, "") == 0) {
+  if (!debug &&
+      (g_strcmp0 (host, "") == 0 ||
+       g_strcmp0 (user, "") == 0 ||
+       g_strcmp0 (password, "") == 0)) {
     g_warning ("Host, user and password must not be empty");
 
-    g_free (user);
-    g_free (password);
-    g_free (host);
-    g_free (protocol);
-    g_free (display_name);
-
-    return FALSE;
+    return NULL;
   }
 
-  g_free (self->name);
-  self->name = g_strdup (name);
+  return g_object_new (CALLS_TYPE_CREDENTIALS,
+                       "name", name,
+                       "host", host,
+                       "user", user,
+                       "password", password,
+                       "display-name", display_name,
+                       "protocol", protocol,
+                       "uuid", uuid,
+                       "port", port,
+                       "local-port", local_port,
+                       "credentials-type", credentials_type,
+                       "auto-connect", auto_connect,
+                       "debug", debug,
+                       NULL);
+}
 
-  g_free (self->host);
-  self->host = host;
 
-  g_free (self->user);
-  self->user = user;
+void
+calls_credentials_save_to_key_file (CallsCredentials *self,
+                                    GKeyFile         *key_file)
+{
+  g_return_if_fail (CALLS_IS_CREDENTIALS (self));
+  g_return_if_fail (key_file);
 
-  g_free (self->password);
-  self->password = password;
+  if (!self->name) {
+    g_warning ("Can't save credentials to keyfile: No name set.");
+    return;
+  }
 
-  g_free (self->transport_protocol);
-  self->transport_protocol = protocol;
+  g_key_file_set_string (key_file, self->name, "Host", self->host);
+  g_key_file_set_string (key_file, self->name, "User", self->user);
+  g_key_file_set_string (key_file, self->name, "Password", self->password);
+  g_key_file_set_string (key_file, self->name, "Uuid", self->uuid);
+  g_key_file_set_integer (key_file, self->name, "CredentialsType", self->credentials_type);
 
-  g_free (self->display_name);
-  self->display_name = display_name;
+  if (self->display_name)
+    g_key_file_set_string (key_file, self->name, "DisplayName", self->display_name);
 
-  self->port = port;
-  self->auto_connect = auto_connect;
-  self->credentials_type = credentials_type;
+  if (self->transport_protocol)
+    g_key_file_set_string (key_file, self->name, "Protocol", self->transport_protocol);
 
-  g_debug ("Updated credentials with name %s", name);
+  if (self->port > 0)
+    g_key_file_set_integer (key_file, self->name, "Port", self->port);
+  if (self->local_port > 0)
+    g_key_file_set_integer (key_file, self->name, "LocalPort", self->local_port);
 
-  g_signal_emit (self, signals[SIGNAL_CREDENTIALS_UPDATED], 0);
+  if (self->auto_connect)
+    g_key_file_set_boolean (key_file, self->name, "AutoConnect", self->auto_connect);
 
-  return TRUE;
+  if (self->debug)
+    g_key_file_set_boolean (key_file, self->name, "Debug", self->debug);
+
+  g_signal_emit (self, signals[SIGNAL_CREDENTIALS_UPDATED], 0);
 }
 
 const char *
@@ -589,6 +679,9 @@ calls_credentials_is_ready (CallsCredentials *self)
 {
   g_return_val_if_fail (CALLS_IS_CREDENTIALS (self), FALSE);
 
+  if (self->debug)
+    return TRUE;
+
   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)
diff --git a/src/calls-credentials.h b/src/calls-credentials.h
index d23c2467..f87dfc1c 100644
--- a/src/calls-credentials.h
+++ b/src/calls-credentials.h
@@ -50,9 +50,10 @@ typedef enum {
 
 CallsCredentials       *calls_credentials_new                     (CallsCredentialsType  credentials_type,
                                                                    const char           *name);
-gboolean                calls_credentials_update_from_keyfile     (CallsCredentials     *self,
-                                                                   GKeyFile             *key_file,
-                                                                   const char           *name);
+CallsCredentials       *calls_credentials_new_from_keyfile        (GKeyFile             *key_file,
+                                                                   const char           *group);
+void                    calls_credentials_save_to_key_file        (CallsCredentials     *self,
+                                                                   GKeyFile             *key_file);
 void                    calls_credentials_set_name                (CallsCredentials     *self,
                                                                    const char           *name);
 const char             *calls_credentials_get_name                (CallsCredentials     *self);


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