Commit be6a1ed9 authored by Evangelos Ribeiro Tzaras's avatar Evangelos Ribeiro Tzaras
Browse files

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()`
parent 4148edc3
Pipeline #68569 passed with stages
in 10 minutes and 45 seconds
......@@ -44,7 +44,10 @@ enum {
PROP_ACC_PORT,
PROP_ACC_PROTOCOL,
PROP_ACC_AUTO_CONNECT,
PROP_DEBUG,
PROP_DEBUG_LOCAL_PORT,
PROP_PROVIDER_NAME,
PROP_UUID,
PROP_LAST_PROP,
};
static GParamSpec *props[PROP_LAST_PROP];
......@@ -73,6 +76,9 @@ struct _CallsCredentials
char *uuid;
gboolean auto_connect;
gboolean debug;
gint local_port;
};
......@@ -146,10 +152,23 @@ calls_credentials_set_property (GObject *object,
self->auto_connect = g_value_get_boolean (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_PROVIDER_NAME:
calls_credentials_set_provider (self, g_value_get_string (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;
......@@ -198,10 +217,22 @@ calls_credentials_get_property (GObject *object,
g_value_set_boolean (value, self->auto_connect);
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_PROVIDER_NAME:
g_value_set_string (value, calls_credentials_get_provider (self));
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;
......@@ -292,6 +323,27 @@ calls_credentials_class_init (CallsCredentialsClass *klass)
TRUE,
G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
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);
props[PROP_PROVIDER_NAME] =
g_param_spec_string ("provider",
"Provider",
......@@ -314,7 +366,8 @@ calls_credentials_class_init (CallsCredentialsClass *klass)
static void
calls_credentials_init (CallsCredentials *self)
{
self->uuid = g_uuid_string_random ();
if (!self->uuid)
self->uuid = g_uuid_string_random ();
}
......@@ -325,43 +378,50 @@ calls_credentials_new (void)
}
/**
* 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 *provider = NULL;
g_autofree char *uuid = NULL;
gint port = 0;
gint local_port = 0;
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);
provider = g_key_file_get_string (key_file, name, "Provider", 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);
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);
......@@ -369,46 +429,55 @@ 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);
g_free (self->host);
self->host = host;
g_free (self->user);
self->user = user;
g_free (self->password);
self->password = password;
g_free (self->transport_protocol);
self->transport_protocol = protocol;
g_free (self->display_name);
self->display_name = display_name;
return g_object_new (CALLS_TYPE_CREDENTIALS,
"name", name,
"host", host,
"user", user,
"password", password,
"display-name", display_name,
"protocol", protocol,
"provider", provider,
"uuid", uuid,
"port", port,
"local-port", local_port,
"auto-connect", auto_connect,
"debug", debug,
NULL);
}
self->port = port;
self->auto_connect = auto_connect;
g_debug ("Updated credentials with name %s", name);
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_signal_emit (self, signals[SIGNAL_ACCOUNT_UPDATED], 0);
if (!self->name) {
g_warning ("Can't save credentials to keyfile: No name set.");
return;
}
return TRUE;
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, "DisplayName", self->display_name);
g_key_file_set_string (key_file, self->name, "Protocol", self->transport_protocol);
g_key_file_set_string (key_file, self->name, "Provider", self->provider_name);
g_key_file_set_string (key_file, self->name, "Uuid", self->uuid);
g_key_file_set_integer (key_file, self->name, "Port", self->port);
g_key_file_set_integer (key_file, self->name, "LocalPort", self->local_port);
g_key_file_set_boolean (key_file, self->name, "AutoConnect", self->auto_connect);
g_key_file_set_boolean (key_file, self->name, "Debug", self->debug);
}
const char *
......
......@@ -34,9 +34,10 @@ G_DECLARE_FINAL_TYPE (CallsCredentials, calls_credentials, CALLS, CREDENTIALS, G
CallsCredentials *calls_credentials_new (void);
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);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment