[calls/wip/ui-manage-accounts: 25/26] account-manager: Allow saving and storing credentials




commit 48ad61f02164a7add60bd5a379906746bf9d5b44
Author: Evangelos Ribeiro Tzaras <evangelos tzaras puri sm>
Date:   Wed Jun 30 12:36:06 2021 +0200

    account-manager: Allow saving and storing credentials
    
    And use the credentials memory backend for the tests to prevent trying to read
    a file from disk which is not there.

 src/calls-account-manager.c | 67 +++++++++++++++++++++++++++++++++++++++++++++
 tests/meson.build           |  1 +
 2 files changed, 68 insertions(+)
---
diff --git a/src/calls-account-manager.c b/src/calls-account-manager.c
index bc9c8b08..918755ea 100644
--- a/src/calls-account-manager.c
+++ b/src/calls-account-manager.c
@@ -24,10 +24,13 @@
 
 #define G_LOG_DOMAIN "CallsAccountManager"
 
+#define CALLS_CREDENTIALS_FILE "credentials.cfg"
+
 #include "calls-account.h"
 #include "calls-account-manager.h"
 #include "calls-credentials.h"
 
+#include "config.h"
 #include "enum-types.h"
 #include "util.h"
 
@@ -63,6 +66,14 @@ struct _CallsAccountManager
   GPtrArray               *providers;
   GListStore              *credentials;
 
+  GKeyFile                *key_file;
+  char                    *filename;
+
+  /** Set environment variable CALLS_CREDENTIALS_IN_MEMORY
+   * if you don't want to save/load to disk
+   */
+  gboolean                 use_memory_backend;
+
   CallsAccountManagerState state;
 };
 
@@ -212,6 +223,35 @@ update_state (CallsAccountManager *self)
 }
 
 
+static gboolean
+save_to_file (CallsAccountManager *self)
+{
+  g_autoptr (GError) error = NULL;
+
+  if (!g_key_file_save_to_file (self->key_file, self->filename, &error)) {
+    g_warning ("Error saving keyfile to file %s: %s", self->filename, error->message);
+    return FALSE;
+  }
+
+  return TRUE;
+}
+
+
+static gboolean
+load_from_file (CallsAccountManager *self)
+{
+  g_autoptr (GError) error = NULL;
+
+  if (!g_key_file_load_from_file (self->key_file, self->filename, G_KEY_FILE_NONE, &error)) {
+    g_warning ("Error loading key file: %s", error->message);
+    return FALSE;
+  }
+
+  calls_account_manager_load_from_keyfile (self, self->key_file);
+  return TRUE;
+}
+
+
 static void
 calls_account_manager_get_property (GObject    *object,
                                     guint       property_id,
@@ -239,6 +279,10 @@ calls_account_manager_constructed (GObject *object)
   self->providers = g_ptr_array_new_with_free_func (g_object_unref);
   self->credentials = g_list_store_new (CALLS_TYPE_CREDENTIALS);
 
+  if (!self->use_memory_backend) {
+    load_from_file (self);
+  }
+
   G_OBJECT_CLASS (calls_account_manager_parent_class)->constructed (object);
 }
 
@@ -250,6 +294,8 @@ calls_account_manager_dispose (GObject *object)
 
   g_clear_pointer (&self->credentials, g_object_unref);
   g_clear_pointer (&self->providers, g_ptr_array_unref);
+  g_clear_pointer (&self->key_file, g_key_file_free);
+  g_clear_pointer (&self->filename, g_free);
 
   G_OBJECT_CLASS (calls_account_manager_parent_class)->dispose (object);
 }
@@ -288,6 +334,22 @@ calls_account_manager_class_init (CallsAccountManagerClass *klass)
 static void
 calls_account_manager_init (CallsAccountManager *self)
 {
+  const char *filename_env = g_getenv ("CALLS_CREDENTIALS_FILE");
+  const char *env_credentials_memory = g_getenv ("CALLS_CREDENTIALS_IN_MEMORY");
+
+  if (filename_env && filename_env[0] != '\0')
+    self->filename = g_strdup (filename_env);
+  else
+    self->filename = g_build_filename (g_get_user_config_dir (),
+                                       APP_DATA_NAME,
+                                       CALLS_CREDENTIALS_FILE,
+                                       NULL);
+
+  if (env_credentials_memory && env_credentials_memory[0] != '\0')
+    self->use_memory_backend = TRUE;
+
+  self->key_file = g_key_file_new ();
+
   self->state = CALLS_ACCOUNT_MANAGER_INIT;
 }
 
@@ -479,6 +541,11 @@ calls_account_manager_add_credentials (CallsAccountManager *self,
 
   update_state (self);
 
+  if (!self->use_memory_backend) {
+    calls_credentials_save_to_key_file (credentials, self->key_file);
+    save_to_file (self);
+  }
+
   return uuid;
 }
 
diff --git a/tests/meson.build b/tests/meson.build
index 25c493ba..f7faf6f3 100644
--- a/tests/meson.build
+++ b/tests/meson.build
@@ -10,6 +10,7 @@ test_env = [
   'NO_AT_BRIDGE=1',
   'CALLS_AUDIOSRC=audiotestsrc',
   'CALLS_AUDIOSINK=fakesink',
+  'CALLS_CREDENTIALS_IN_MEMORY=1',
 ]
 
 test_cflags = [


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