diff options
author | Simon McVittie <simon.mcvittie@collabora.co.uk> | 2012-09-10 19:19:47 +0100 |
---|---|---|
committer | Simon McVittie <simon.mcvittie@collabora.co.uk> | 2012-09-10 19:19:47 +0100 |
commit | 2a647cbf905ad47a3d597499efc3eaae1397b719 (patch) | |
tree | 2b9bc8d08a901b9bc77e5512d3693d6e817e3ab8 | |
parent | 76c62bcf500acc21ef8db2064ed00d01e51b039d (diff) |
McdStorage: distinguish between parameters and attributes
Parameters are part of the Parameters property, have CM-specified types
and can be secret, whereas "attributes" (everything else) mostly stand
alone, have known types hard-coded into MC, and are all non-secret.
Signed-off-by: Simon McVittie <simon.mcvittie@collabora.co.uk>
-rw-r--r-- | src/mcd-account-addressing.c | 6 | ||||
-rw-r--r-- | src/mcd-account-conditions.c | 9 | ||||
-rw-r--r-- | src/mcd-account-manager.c | 7 | ||||
-rw-r--r-- | src/mcd-account.c | 81 | ||||
-rw-r--r-- | src/mcd-storage.c | 344 | ||||
-rw-r--r-- | src/mcd-storage.h | 52 |
6 files changed, 328 insertions, 171 deletions
diff --git a/src/mcd-account-addressing.c b/src/mcd-account-addressing.c index d5604cb2..9d9e2938 100644 --- a/src/mcd-account-addressing.c +++ b/src/mcd-account-addressing.c @@ -49,7 +49,7 @@ addressing_set_uri_scheme_association (TpSvcAccountInterfaceAddressing *iface, g_value_init (&value, G_TYPE_STRV); - if (mcd_storage_get_value (storage, account, SCHEMES, &value, NULL)) + if (mcd_storage_get_attribute (storage, account, SCHEMES, &value, NULL)) { schemes = g_value_get_boxed (&value); old_association = tp_strv_contains ((const gchar * const *) schemes, @@ -79,7 +79,7 @@ addressing_set_uri_scheme_association (TpSvcAccountInterfaceAddressing *iface, g_ptr_array_add (new_schemes, NULL); mcd_storage_set_strv (storage, account, SCHEMES, - (const gchar * const *) new_schemes->pdata, FALSE); + (const gchar * const *) new_schemes->pdata); g_ptr_array_unref (new_schemes); } @@ -100,7 +100,7 @@ addressing_get_uri_schemes (TpSvcDBusProperties *iface, g_value_init (value, G_TYPE_STRV); - if (!mcd_storage_get_value (storage, account, SCHEMES, value, NULL)) + if (!mcd_storage_get_attribute (storage, account, SCHEMES, value, NULL)) { g_value_set_boxed (value, NULL); } diff --git a/src/mcd-account-conditions.c b/src/mcd-account-conditions.c index 8a6af964..b36d6b1f 100644 --- a/src/mcd-account-conditions.c +++ b/src/mcd-account-conditions.c @@ -49,8 +49,7 @@ store_condition (gpointer key, gpointer value, gpointer userdata) gchar condition_key[256]; g_snprintf (condition_key, sizeof (condition_key), "condition-%s", name); - mcd_storage_set_string (storage, account_name, condition_key, condition, - FALSE); + mcd_storage_set_string (storage, account_name, condition_key, condition); } static gboolean @@ -84,14 +83,14 @@ set_condition (TpSvcDBusProperties *self, const gchar *name, conditions = g_value_get_boxed (value); /* first, delete existing conditions */ - keys = mcd_storage_dup_settings (storage, account_name, NULL); + keys = mcd_storage_dup_attributes (storage, account_name, NULL); for (key = keys; *key != NULL; key++) { if (strncmp (*key, "condition-", 10) != 0) continue; - mcd_storage_set_value (storage, account_name, *key, NULL, FALSE); + mcd_storage_set_attribute (storage, account_name, *key, NULL); } g_strfreev (keys); @@ -136,7 +135,7 @@ GHashTable *mcd_account_get_conditions (McdAccount *account) conditions = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free); - keys = mcd_storage_dup_settings (storage, account_name, NULL); + keys = mcd_storage_dup_attributes (storage, account_name, NULL); for (key = keys; *key != NULL; key++) { diff --git a/src/mcd-account-manager.c b/src/mcd-account-manager.c index 0f995089..01cbbd8a 100644 --- a/src/mcd-account-manager.c +++ b/src/mcd-account-manager.c @@ -906,14 +906,13 @@ _mcd_account_manager_create_account (McdAccountManager *account_manager, /* create the basic account keys */ mcd_storage_set_string (storage, unique_name, - MC_ACCOUNTS_KEY_MANAGER, manager, FALSE); + MC_ACCOUNTS_KEY_MANAGER, manager); mcd_storage_set_string (storage, unique_name, - MC_ACCOUNTS_KEY_PROTOCOL, protocol, FALSE); + MC_ACCOUNTS_KEY_PROTOCOL, protocol); if (display_name != NULL) mcd_storage_set_string (storage, unique_name, - MC_ACCOUNTS_KEY_DISPLAY_NAME, display_name, - FALSE); + MC_ACCOUNTS_KEY_DISPLAY_NAME, display_name); account = mcd_account_new (account_manager, unique_name); g_free (unique_name); diff --git a/src/mcd-account.c b/src/mcd-account.c index 68751ce9..af48434d 100644 --- a/src/mcd-account.c +++ b/src/mcd-account.c @@ -51,7 +51,6 @@ #include "_gen/gtypes.h" #include "_gen/cli-Connection_Manager_Interface_Account_Storage-body.h" -#define MAX_KEY_LENGTH (DBUS_MAXIMUM_NAME_LENGTH + 6) #define MC_OLD_AVATAR_FILENAME "avatar.bin" #define MCD_ACCOUNT_PRIV(account) (MCD_ACCOUNT (account)->priv) @@ -381,26 +380,12 @@ _mcd_account_set_parameter (McdAccount *account, const gchar *name, { McdAccountPrivate *priv = account->priv; McdStorage *storage = priv->storage; - gchar key[MAX_KEY_LENGTH]; const gchar *account_name = mcd_account_get_unique_name (account); gboolean secret = mcd_account_parameter_is_secret (account, name); - g_snprintf (key, sizeof (key), "param-%s", name); - - mcd_storage_set_value (storage, account_name, key, value, secret); + mcd_storage_set_parameter (storage, account_name, name, value, secret); } - - - - - - - - - - - static GType mc_param_type (const TpConnectionManagerParam *param); /** @@ -442,13 +427,11 @@ mcd_account_get_parameter_of_known_type (McdAccount *account, { const gchar *account_name = mcd_account_get_unique_name (account); McdStorage *storage = account->priv->storage; - gchar key[MAX_KEY_LENGTH]; GValue tmp = G_VALUE_INIT; - g_snprintf (key, sizeof (key), "param-%s", name); g_value_init (&tmp, type); - if (mcd_storage_get_value (storage, account_name, key, &tmp, error)) + if (mcd_storage_get_parameter (storage, account_name, name, &tmp, error)) { if (parameter != NULL) { @@ -1050,7 +1033,7 @@ mcd_account_set_string_val (McdAccount *account, const gchar *key, new_string = NULL; } - if (mcd_storage_set_string (storage, name, key, new_string, FALSE)) { + if (mcd_storage_set_string (storage, name, key, new_string)) { mcd_storage_commit (storage, name); mcd_account_changed_property (account, key, value); return SET_RESULT_CHANGED; @@ -1068,7 +1051,7 @@ mcd_account_get_string_val (McdAccount *account, const gchar *key, g_value_init (value, G_TYPE_STRING); - if (!mcd_storage_get_value (priv->storage, name, key, value, NULL)) + if (!mcd_storage_get_attribute (priv->storage, name, key, value, NULL)) { g_value_set_static_string (value, NULL); } @@ -1175,8 +1158,8 @@ _mcd_account_set_enabled (McdAccount *account, g_value_init (&value, G_TYPE_BOOLEAN); g_value_set_boolean (&value, enabled); - mcd_storage_set_value (priv->storage, name, - MC_ACCOUNTS_KEY_ENABLED, &value, FALSE); + mcd_storage_set_attribute (priv->storage, name, + MC_ACCOUNTS_KEY_ENABLED, &value); if (write_out) mcd_storage_commit (priv->storage, name); @@ -1457,9 +1440,9 @@ set_automatic_presence (TpSvcDBusProperties *self, g_value_init (&presence, G_TYPE_INT); g_value_set_int (&presence, type); - mcd_storage_set_value (priv->storage, account_name, - MC_ACCOUNTS_KEY_AUTO_PRESENCE_TYPE, - &presence, FALSE); + mcd_storage_set_attribute (priv->storage, account_name, + MC_ACCOUNTS_KEY_AUTO_PRESENCE_TYPE, + &presence); priv->auto_presence_type = type; changed = TRUE; } @@ -1473,7 +1456,7 @@ set_automatic_presence (TpSvcDBusProperties *self, mcd_storage_set_string (priv->storage, account_name, MC_ACCOUNTS_KEY_AUTO_PRESENCE_STATUS, - new_status, FALSE); + new_status); g_free (priv->auto_presence_status); priv->auto_presence_status = g_strdup (status); @@ -1489,7 +1472,7 @@ set_automatic_presence (TpSvcDBusProperties *self, mcd_storage_set_string (priv->storage, account_name, MC_ACCOUNTS_KEY_AUTO_PRESENCE_MESSAGE, - new_message, FALSE); + new_message); g_free (priv->auto_presence_message); priv->auto_presence_message = g_strdup (message); @@ -1562,9 +1545,9 @@ set_connect_automatically (TpSvcDBusProperties *self, if (priv->connect_automatically != connect_automatically) { const gchar *account_name = mcd_account_get_unique_name (account); - mcd_storage_set_value (priv->storage, account_name, - MC_ACCOUNTS_KEY_CONNECT_AUTOMATICALLY, - value, FALSE); + mcd_storage_set_attribute (priv->storage, account_name, + MC_ACCOUNTS_KEY_CONNECT_AUTOMATICALLY, + value); priv->connect_automatically = connect_automatically; mcd_storage_commit (priv->storage, account_name); @@ -1784,8 +1767,8 @@ set_supersedes (TpSvcDBusProperties *svc, self->priv->supersedes = g_value_dup_boxed (value); mcd_account_changed_property (self, name, value); - mcd_storage_set_value (self->priv->storage, self->priv->unique_name, - MC_ACCOUNTS_KEY_SUPERSEDES, value, FALSE); + mcd_storage_set_attribute (self->priv->storage, self->priv->unique_name, + MC_ACCOUNTS_KEY_SUPERSEDES, value); mcd_storage_commit (self->priv->storage, self->priv->unique_name); return TRUE; @@ -2008,8 +1991,8 @@ set_hidden (TpSvcDBusProperties *self, * So for now we check whether the value has changed, and violate the spec * by making this property mutable (at least with the keyfile backend). */ - if (mcd_storage_set_value (priv->storage, account_name, - MC_ACCOUNTS_KEY_HIDDEN, value, FALSE)) + if (mcd_storage_set_attribute (priv->storage, account_name, + MC_ACCOUNTS_KEY_HIDDEN, value)) { mcd_storage_commit (priv->storage, account_name); mcd_account_changed_property (account, MC_ACCOUNTS_KEY_HIDDEN, value); @@ -3068,8 +3051,8 @@ mcd_account_setup (McdAccount *account) if (priv->supersedes != NULL) g_ptr_array_unref (priv->supersedes); - if (mcd_storage_get_value (storage, name, - MC_ACCOUNTS_KEY_SUPERSEDES, &value, NULL)) + if (mcd_storage_get_attribute (storage, name, + MC_ACCOUNTS_KEY_SUPERSEDES, &value, NULL)) { priv->supersedes = g_value_dup_boxed (&value); } @@ -3769,10 +3752,8 @@ _mcd_account_set_normalized_name (McdAccount *account, const gchar *name) g_value_init (&value, G_TYPE_STRING); g_value_set_static_string (&value, name); - mcd_storage_set_value (priv->storage, - account_name, - MC_ACCOUNTS_KEY_NORMALIZED_NAME, - &value, FALSE); + mcd_storage_set_attribute (priv->storage, account_name, + MC_ACCOUNTS_KEY_NORMALIZED_NAME, &value); mcd_storage_commit (priv->storage, account_name); mcd_account_changed_property (account, MC_ACCOUNTS_KEY_NORMALIZED_NAME, &value); @@ -3790,7 +3771,7 @@ _mcd_account_set_avatar_token (McdAccount *account, const gchar *token) mcd_storage_set_string (priv->storage, account_name, MC_ACCOUNTS_KEY_AVATAR_TOKEN, - token, FALSE); + token); mcd_storage_commit (priv->storage, account_name); } @@ -3839,7 +3820,7 @@ _mcd_account_set_avatar (McdAccount *account, const GArray *avatar, mcd_storage_set_string (priv->storage, account_name, MC_ACCOUNTS_KEY_AVATAR_MIME, - mime_type, FALSE); + mime_type); if (token) { @@ -3850,7 +3831,7 @@ _mcd_account_set_avatar (McdAccount *account, const GArray *avatar, mcd_storage_set_string (priv->storage, account_name, MC_ACCOUNTS_KEY_AVATAR_TOKEN, - token, FALSE); + token); if (!prev_token || strcmp (prev_token, token) != 0) tp_svc_account_interface_avatar_emit_avatar_changed (account); @@ -3859,10 +3840,8 @@ _mcd_account_set_avatar (McdAccount *account, const GArray *avatar, } else { - mcd_storage_set_value (priv->storage, - account_name, - MC_ACCOUNTS_KEY_AVATAR_TOKEN, - NULL, FALSE); + mcd_storage_set_attribute (priv->storage, account_name, + MC_ACCOUNTS_KEY_AVATAR_TOKEN, NULL); /* this is a no-op if the connection doesn't support avatars */ if (priv->connection != NULL) @@ -4565,10 +4544,8 @@ _mcd_account_set_has_been_online (McdAccount *account) g_value_init (&value, G_TYPE_BOOLEAN); g_value_set_boolean (&value, TRUE); - mcd_storage_set_value (account->priv->storage, - account_name, - MC_ACCOUNTS_KEY_HAS_BEEN_ONLINE, - &value, FALSE); + mcd_storage_set_attribute (account->priv->storage, account_name, + MC_ACCOUNTS_KEY_HAS_BEEN_ONLINE, &value); account->priv->has_been_online = TRUE; mcd_storage_commit (account->priv->storage, account_name); mcd_account_changed_property (account, MC_ACCOUNTS_KEY_HAS_BEEN_ONLINE, diff --git a/src/mcd-storage.c b/src/mcd-storage.c index 6ae93c80..e2b84a63 100644 --- a/src/mcd-storage.c +++ b/src/mcd-storage.c @@ -44,6 +44,8 @@ # endif #endif +#define MAX_KEY_LENGTH (DBUS_MAXIMUM_NAME_LENGTH + 6) + static GList *stores = NULL; static void sort_and_cache_plugins (void); @@ -65,7 +67,8 @@ G_DEFINE_TYPE_WITH_CODE (McdStorage, mcd_storage, static void mcd_storage_init (McdStorage *self) { - self->keyfile = g_key_file_new (); + self->attributes = g_key_file_new (); + self->parameters = g_key_file_new (); self->secrets = g_key_file_new (); } @@ -76,9 +79,11 @@ storage_finalize (GObject *object) GObjectFinalizeFunc finalize = G_OBJECT_CLASS (mcd_storage_parent_class)->finalize; - g_key_file_free (self->keyfile); + g_key_file_free (self->attributes); + g_key_file_free (self->parameters); g_key_file_free (self->secrets); - self->keyfile = NULL; + self->attributes = NULL; + self->parameters = NULL; self->secrets = NULL; if (finalize != NULL) @@ -166,7 +171,11 @@ get_value (const McpAccountManager *ma, const gchar *key) { McdStorage *self = MCD_STORAGE (ma); - return g_key_file_get_value (self->keyfile, account, key, NULL); + + if (g_str_has_prefix (key, "param-")) + return g_key_file_get_value (self->parameters, account, key + 6, NULL); + else + return g_key_file_get_value (self->attributes, account, key, NULL); } static void @@ -177,10 +186,21 @@ set_value (const McpAccountManager *ma, { McdStorage *self = MCD_STORAGE (ma); - if (value != NULL) - g_key_file_set_value (self->keyfile, account, key, value); + if (g_str_has_prefix (key, "param-")) + { + if (value != NULL) + g_key_file_set_value (self->parameters, account, key + 6, value); + else + g_key_file_remove_key (self->parameters, account, key + 6, NULL); + + } else - g_key_file_remove_key (self->keyfile, account, key, NULL); + { + if (value != NULL) + g_key_file_set_value (self->attributes, account, key, value); + else + g_key_file_remove_key (self->attributes, account, key, NULL); + } } static GStrv @@ -188,8 +208,31 @@ list_keys (const McpAccountManager *ma, const gchar * account) { McdStorage *self = MCD_STORAGE (ma); + GPtrArray *ret = g_ptr_array_new (); + GStrv strv; + gchar **iter; + + strv = g_key_file_get_keys (self->attributes, account, NULL, NULL); + + for (iter = strv; iter != NULL && *iter != NULL; iter++) + { + g_ptr_array_add (ret, *iter); + } + + /* ownership of items donated to ret already */ + g_free (strv); + + strv = g_key_file_get_keys (self->parameters, account, NULL, NULL); - return g_key_file_get_keys (self->keyfile, account, NULL, NULL); + for (iter = strv; iter != NULL && *iter != NULL; iter++) + { + g_ptr_array_add (ret, g_strdup_printf ("param-%s", *iter)); + } + + g_strfreev (strv); + + g_ptr_array_add (ret, NULL); + return (GStrv) g_ptr_array_free (ret, FALSE); } static gboolean @@ -252,7 +295,7 @@ unique_name (const McpAccountManager *ma, TP_ACCOUNT_OBJECT_PATH_BASE "%s/%s/%s%u", esc_manager, esc_protocol, esc_base, i); - if (!g_key_file_has_group (self->keyfile, path + base_len) && + if (!g_key_file_has_group (self->attributes, path + base_len) && dbus_g_connection_lookup_g_object (connection, path) == NULL) { gchar *ret = g_strdup (path + base_len); @@ -414,27 +457,27 @@ mcd_storage_dup_accounts (McdStorage *self, { g_return_val_if_fail (MCD_IS_STORAGE (self), NULL); - return g_key_file_get_groups (self->keyfile, n); + return g_key_file_get_groups (self->attributes, n); } /* - * mcd_storage_dup_settings: + * mcd_storage_dup_attributes: * @storage: An object implementing the #McdStorage interface * @account: unique name of the account - * @n: place for the number of settings to be written to (or %NULL) + * @n: place for the number of attributes to be written to (or %NULL) * * Returns: a newly allocated GStrv containing the names of all the - * settings or parameters currently stored for @account. Must be + * attributes or parameters currently stored for @account. Must be * freed by the caller with g_strfreev(). */ GStrv -mcd_storage_dup_settings (McdStorage *self, +mcd_storage_dup_attributes (McdStorage *self, const gchar *account, gsize *n) { g_return_val_if_fail (MCD_IS_STORAGE (self), NULL); - return g_key_file_get_keys (self->keyfile, account, n, NULL); + return g_key_file_get_keys (self->attributes, account, n, NULL); } /* @@ -476,25 +519,27 @@ mcd_storage_get_plugin (McdStorage *self, * mcd_storage_dup_string: * @storage: An object implementing the #McdStorage interface * @account: unique name of the account - * @key: name of the setting to be retrieved + * @attribute: name of the attribute to be retrieved (which must not be a + * parameter) * * Returns: a newly allocated gchar * which must be freed with g_free(). */ gchar * mcd_storage_dup_string (McdStorage *self, const gchar *account, - const gchar *key) + const gchar *attribute) { GValue tmp = G_VALUE_INIT; gchar *ret; g_return_val_if_fail (MCD_IS_STORAGE (self), NULL); g_return_val_if_fail (account != NULL, NULL); - g_return_val_if_fail (key != NULL, NULL); + g_return_val_if_fail (attribute != NULL, NULL); + g_return_val_if_fail (!g_str_has_prefix (attribute, "param-"), NULL); g_value_init (&tmp, G_TYPE_STRING); - if (!mcd_storage_get_value (self, account, key, &tmp, NULL)) + if (!mcd_storage_get_attribute (self, account, attribute, &tmp, NULL)) return NULL; ret = g_value_dup_string (&tmp); @@ -503,16 +548,58 @@ mcd_storage_dup_string (McdStorage *self, } /* - * mcd_storage_get_value: + * mcd_storage_get_attribute: + * @storage: An object implementing the #McdStorage interface + * @account: unique name of the account + * @attribute: name of the attribute to be retrieved, e.g. 'DisplayName' + * @value: location to return the value, initialized to the right #GType + * @error: a place to store any #GError<!-- -->s that occur + */ +gboolean +mcd_storage_get_attribute (McdStorage *self, + const gchar *account, + const gchar *attribute, + GValue *value, + GError **error) +{ + g_return_val_if_fail (MCD_IS_STORAGE (self), FALSE); + + return mcd_keyfile_get_value (self->attributes, account, attribute, value, + error); +} + +/* + * mcd_storage_get_parameter: * @storage: An object implementing the #McdStorage interface * @account: unique name of the account - * @key: name of the setting to be retrieved + * @parameter: name of the parameter to be retrieved, e.g. 'account' * @value: location to return the value, initialized to the right #GType * @error: a place to store any #GError<!-- -->s that occur */ gboolean -mcd_storage_get_value (McdStorage *self, +mcd_storage_get_parameter (McdStorage *self, const gchar *account, + const gchar *parameter, + GValue *value, + GError **error) +{ + g_return_val_if_fail (MCD_IS_STORAGE (self), FALSE); + + return mcd_keyfile_get_value (self->parameters, account, parameter, + value, error); +} + +/* + * mcd_keyfile_get_value: + * @keyfile: A #GKeyFile + * @group: name of a group + * @key: name of a key + * @value: location to return the value, initialized to the right #GType + * @error: a place to store any #GError<!-- -->s that occur + */ +gboolean +mcd_keyfile_get_value (GKeyFile *keyfile, + const gchar *group, const gchar *key, GValue *value, GError **error) @@ -520,8 +607,9 @@ mcd_storage_get_value (McdStorage *self, gboolean ret = FALSE; GType type; - g_return_val_if_fail (MCD_IS_STORAGE (self), FALSE); - g_return_val_if_fail (account != NULL, FALSE); + g_return_val_if_fail (keyfile != NULL, FALSE); + g_return_val_if_fail (group != NULL, FALSE); + g_return_val_if_fail (key != NULL, FALSE); g_return_val_if_fail (G_IS_VALUE (value), FALSE); type = G_VALUE_TYPE (value); @@ -530,7 +618,7 @@ mcd_storage_get_value (McdStorage *self, { case G_TYPE_STRING: { - gchar *v_string = g_key_file_get_string (self->keyfile, account, + gchar *v_string = g_key_file_get_string (keyfile, group, key, error); if (v_string != NULL) @@ -545,7 +633,7 @@ mcd_storage_get_value (McdStorage *self, case G_TYPE_INT: { GError *e = NULL; - gint v_int = g_key_file_get_integer (self->keyfile, account, + gint v_int = g_key_file_get_integer (keyfile, group, key, &e); if (e != NULL) @@ -563,7 +651,7 @@ mcd_storage_get_value (McdStorage *self, case G_TYPE_INT64: { GError *e = NULL; - gint64 v_int = tp_g_key_file_get_int64 (self->keyfile, account, + gint64 v_int = tp_g_key_file_get_int64 (keyfile, group, key, &e); if (e != NULL) @@ -581,7 +669,7 @@ mcd_storage_get_value (McdStorage *self, case G_TYPE_UINT: { GError *e = NULL; - guint64 v_uint = tp_g_key_file_get_uint64 (self->keyfile, account, + guint64 v_uint = tp_g_key_file_get_uint64 (keyfile, group, key, &e); if (e != NULL) @@ -606,7 +694,7 @@ mcd_storage_get_value (McdStorage *self, case G_TYPE_UCHAR: { GError *e = NULL; - gint v_int = g_key_file_get_integer (self->keyfile, account, + gint v_int = g_key_file_get_integer (keyfile, group, key, &e); if (e != NULL) @@ -631,7 +719,7 @@ mcd_storage_get_value (McdStorage *self, case G_TYPE_UINT64: { GError *e = NULL; - guint64 v_uint = tp_g_key_file_get_uint64 (self->keyfile, account, + guint64 v_uint = tp_g_key_file_get_uint64 (keyfile, group, key, &e); if (e != NULL) @@ -649,7 +737,7 @@ mcd_storage_get_value (McdStorage *self, case G_TYPE_BOOLEAN: { GError *e = NULL; - gboolean v_bool = g_key_file_get_boolean (self->keyfile, account, + gboolean v_bool = g_key_file_get_boolean (keyfile, group, key, &e); if (e != NULL) @@ -667,7 +755,7 @@ mcd_storage_get_value (McdStorage *self, case G_TYPE_DOUBLE: { GError *e = NULL; - gdouble v_double = g_key_file_get_double (self->keyfile, account, + gdouble v_double = g_key_file_get_double (keyfile, group, key, &e); if (e != NULL) @@ -685,7 +773,7 @@ mcd_storage_get_value (McdStorage *self, default: if (type == G_TYPE_STRV) { - gchar **v = g_key_file_get_string_list (self->keyfile, account, + gchar **v = g_key_file_get_string_list (keyfile, group, key, NULL, error); if (v != NULL) @@ -696,7 +784,7 @@ mcd_storage_get_value (McdStorage *self, } else if (type == DBUS_TYPE_G_OBJECT_PATH) { - gchar *v_string = g_key_file_get_string (self->keyfile, account, + gchar *v_string = g_key_file_get_string (keyfile, group, key, error); if (v_string == NULL) @@ -718,7 +806,7 @@ mcd_storage_get_value (McdStorage *self, } else if (type == TP_ARRAY_TYPE_OBJECT_PATH_LIST) { - gchar **v = g_key_file_get_string_list (self->keyfile, account, + gchar **v = g_key_file_get_string_list (keyfile, group, key, NULL, error); if (v != NULL) @@ -732,7 +820,7 @@ mcd_storage_get_value (McdStorage *self, { g_set_error (error, MCD_ACCOUNT_ERROR, MCD_ACCOUNT_ERROR_GET_PARAMETER, - "Invalid object path %s stored in account", *iter); + "Invalid object path %s stored in keyfile", *iter); g_strfreev (v); v = NULL; break; @@ -756,9 +844,9 @@ mcd_storage_get_value (McdStorage *self, else { gchar *message = - g_strdup_printf ("cannot get property %s on account %s, " + g_strdup_printf ("cannot get key %s from group %s: " "unknown type %s", - key, account, g_type_name (type)); + key, group, g_type_name (type)); g_warning ("%s: %s", G_STRFUNC, message); g_set_error (error, MCD_ACCOUNT_ERROR, @@ -775,24 +863,25 @@ mcd_storage_get_value (McdStorage *self, * mcd_storage_get_boolean: * @storage: An object implementing the #McdStorage interface * @account: unique name of the account - * @key: name of the setting to be retrieved + * @key: name of the attribute to be retrieved * * Returns: a #gboolean. Unset/unparseable values are returned as %FALSE */ gboolean mcd_storage_get_boolean (McdStorage *self, const gchar *account, - const gchar *key) + const gchar *attribute) { GValue tmp = G_VALUE_INIT; g_return_val_if_fail (MCD_IS_STORAGE (self), FALSE); g_return_val_if_fail (account != NULL, FALSE); - g_return_val_if_fail (key != NULL, FALSE); + g_return_val_if_fail (attribute != NULL, FALSE); + g_return_val_if_fail (!g_str_has_prefix (attribute, "param-"), FALSE); g_value_init (&tmp, G_TYPE_BOOLEAN); - if (!mcd_storage_get_value (self, account, key, &tmp, NULL)) + if (!mcd_storage_get_attribute (self, account, attribute, &tmp, NULL)) return FALSE; return g_value_get_boolean (&tmp); @@ -802,24 +891,25 @@ mcd_storage_get_boolean (McdStorage *self, * mcd_storage_get_integer: * @storage: An object implementing the #McdStorage interface * @account: unique name of the account - * @key: name of the setting to be retrieved + * @attribute: name of the attribute to be retrieved * * Returns: a #gint. Unset or non-numeric values are returned as 0 */ gint mcd_storage_get_integer (McdStorage *self, const gchar *account, - const gchar *key) + const gchar *attribute) { GValue tmp = G_VALUE_INIT; g_return_val_if_fail (MCD_IS_STORAGE (self), 0); g_return_val_if_fail (account != NULL, 0); - g_return_val_if_fail (key != NULL, 0); + g_return_val_if_fail (attribute != NULL, 0); + g_return_val_if_fail (!g_str_has_prefix (attribute, "param-"), 0); g_value_init (&tmp, G_TYPE_INT); - if (!mcd_storage_get_value (self, account, key, &tmp, NULL)) + if (!mcd_storage_get_attribute (self, account, attribute, &tmp, NULL)) return FALSE; return g_value_get_int (&tmp); @@ -842,7 +932,10 @@ update_storage (McdStorage *self, /* don't unescape the value here, we're flushing it to storage * * everywhere else should handle escaping on the way in and unescaping * * on the way out of the keyfile, but not here: */ - val = g_key_file_get_value (self->keyfile, account, key, NULL); + if (g_str_has_prefix (key, "param-")) + val = g_key_file_get_value (self->parameters, account, key + 6, NULL); + else + val = g_key_file_get_value (self->attributes, account, key, NULL); /* we're deleting, which is unconditional, no need to check if anyone * * claims this setting for themselves */ @@ -874,10 +967,8 @@ update_storage (McdStorage *self, * mcd_storage_set_string: * @storage: An object implementing the #McdStorage interface * @account: the unique name of an account - * @key: the key (name) of the parameter or setting + * @key: the name of the attribute * @value: the value to be stored (or %NULL to erase it) - * @secret: whether the value is confidential (might get stored in the - * keyring, for example) * * Copies and stores the supplied @value (or removes it if %NULL) to the * internal cache. @@ -891,19 +982,19 @@ update_storage (McdStorage *self, gboolean mcd_storage_set_string (McdStorage *self, const gchar *account, - const gchar *key, - const gchar *val, - gboolean secret) + const gchar *attribute, + const gchar *val) { gboolean updated; g_return_val_if_fail (MCD_IS_STORAGE (self), FALSE); g_return_val_if_fail (account != NULL, FALSE); - g_return_val_if_fail (key != NULL, FALSE); + g_return_val_if_fail (attribute != NULL, FALSE); + g_return_val_if_fail (!g_str_has_prefix (attribute, "param-"), FALSE); if (val == NULL) { - updated = mcd_storage_set_value (self, account, key, NULL, secret); + updated = mcd_storage_set_attribute (self, account, attribute, NULL); } else { @@ -911,7 +1002,7 @@ mcd_storage_set_string (McdStorage *self, g_value_init (&tmp, G_TYPE_STRING); g_value_set_string (&tmp, val); - updated = mcd_storage_set_value (self, account, key, &tmp, secret); + updated = mcd_storage_set_attribute (self, account, attribute, &tmp); g_value_unset (&tmp); } @@ -919,15 +1010,52 @@ mcd_storage_set_string (McdStorage *self, } /* - * mcd_storage_set_value: + * mcd_storage_set_attribute: + * @storage: An object implementing the #McdStorage interface + * @account: the unique name of an account + * @attribute: the name of the attribute, e.g. "DisplayName" + * @value: the value to be stored (or %NULL to erase it) + * + * Copies and stores the supplied @value (or removes it if %NULL) in the + * internal cache. + * + * Returns: a #gboolean indicating whether the cache actually required an + * update (so that the caller can decide whether to request a commit to + * long term storage or not). %TRUE indicates the cache was updated and + * may not be in sync with the store any longer, %FALSE indicates we already + * held the value supplied. + */ +gboolean +mcd_storage_set_attribute (McdStorage *self, + const gchar *account, + const gchar *attribute, + const GValue *value) +{ + gboolean updated; + + g_return_val_if_fail (MCD_IS_STORAGE (self), FALSE); + g_return_val_if_fail (account != NULL, FALSE); + g_return_val_if_fail (attribute != NULL, FALSE); + g_return_val_if_fail (!g_str_has_prefix (attribute, "param-"), FALSE); + + updated = mcd_keyfile_set_value (self->attributes, account, attribute, value); + + if (updated) + update_storage (self, account, attribute, FALSE); + + return updated; +} + +/* + * mcd_storage_set_parameter: * @storage: An object implementing the #McdStorage interface * @account: the unique name of an account - * @key: the key (name) of the parameter or setting + * @parameter: the name of the parameter, e.g. "account" * @value: the value to be stored (or %NULL to erase it) * @secret: whether the value is confidential (might get stored in the * keyring, for example) * - * Copies and stores the supplied @value (or removes it if %NULL) to the + * Copies and stores the supplied @value (or removes it if %NULL) in the * internal cache. * * Returns: a #gboolean indicating whether the cache actually required an @@ -937,40 +1065,78 @@ mcd_storage_set_string (McdStorage *self, * held the value supplied. */ gboolean -mcd_storage_set_value (McdStorage *self, - const gchar *name, - const gchar *key, +mcd_storage_set_parameter (McdStorage *self, + const gchar *account, + const gchar *parameter, const GValue *value, gboolean secret) { + gboolean updated; + g_return_val_if_fail (MCD_IS_STORAGE (self), FALSE); + g_return_val_if_fail (account != NULL, FALSE); + g_return_val_if_fail (parameter != NULL, FALSE); + + updated = mcd_keyfile_set_value (self->parameters, account, parameter, + value); + + if (updated) + { + gchar key[MAX_KEY_LENGTH]; + + g_snprintf (key, sizeof (key), "param-%s", parameter); + + update_storage (self, account, key, secret); + } + + return updated; +} + +/* + * mcd_keyfile_set_value: + * @keyfile: a keyfile + * @name: the name of a group + * @key: the key in the group + * @value: the value to be stored (or %NULL to erase it) + * + * Copies and stores the supplied @value (or removes it if %NULL) to the + * internal cache. + * + * Returns: a #gboolean indicating whether the cache actually required an + * update (so that the caller can decide whether to request a commit to + * long term storage or not). %TRUE indicates the cache was updated and + * may not be in sync with the store any longer, %FALSE indicates we already + * held the value supplied. + */ +gboolean +mcd_keyfile_set_value (GKeyFile *keyfile, + const gchar *name, + const gchar *key, + const GValue *value) +{ g_return_val_if_fail (name != NULL, FALSE); g_return_val_if_fail (key != NULL, FALSE); if (value == NULL) { - gchar *old = g_key_file_get_value (self->keyfile, name, key, NULL); + gchar *old = g_key_file_get_value (keyfile, name, key, NULL); gboolean updated = (old != NULL); g_free (old); - g_key_file_remove_key (self->keyfile, name, key, NULL); - - if (updated) - update_storage (self, name, key, secret); - + g_key_file_remove_key (keyfile, name, key, NULL); return updated; } else { gboolean updated = FALSE; - gchar *old = g_key_file_get_value (self->keyfile, name, key, NULL); + gchar *old = g_key_file_get_value (keyfile, name, key, NULL); gchar *new = NULL; gchar *buf = NULL; switch (G_VALUE_TYPE (value)) { case G_TYPE_STRING: - g_key_file_set_string (self->keyfile, name, key, + g_key_file_set_string (keyfile, name, key, g_value_get_string (value)); break; @@ -979,12 +1145,12 @@ mcd_storage_set_value (McdStorage *self, break; case G_TYPE_INT: - g_key_file_set_integer (self->keyfile, name, key, + g_key_file_set_integer (keyfile, name, key, g_value_get_int (value)); break; case G_TYPE_BOOLEAN: - g_key_file_set_boolean (self->keyfile, name, key, + g_key_file_set_boolean (keyfile, name, key, g_value_get_boolean (value)); break; @@ -1003,7 +1169,7 @@ mcd_storage_set_value (McdStorage *self, break; case G_TYPE_DOUBLE: - g_key_file_set_double (self->keyfile, name, key, + g_key_file_set_double (keyfile, name, key, g_value_get_double (value)); break; @@ -1012,20 +1178,20 @@ mcd_storage_set_value (McdStorage *self, { gchar **strings = g_value_get_boxed (value); - g_key_file_set_string_list (self->keyfile, name, key, + g_key_file_set_string_list (keyfile, name, key, (const gchar **)strings, g_strv_length (strings)); } else if (G_VALUE_HOLDS (value, DBUS_TYPE_G_OBJECT_PATH)) { - g_key_file_set_string (self->keyfile, name, key, + g_key_file_set_string (keyfile, name, key, g_value_get_boxed (value)); } else if (G_VALUE_HOLDS (value, TP_ARRAY_TYPE_OBJECT_PATH_LIST)) { GPtrArray *arr = g_value_get_boxed (value); - g_key_file_set_string_list (self->keyfile, name, key, + g_key_file_set_string_list (keyfile, name, key, (const gchar * const *) arr->pdata, arr->len); } else @@ -1037,15 +1203,12 @@ mcd_storage_set_value (McdStorage *self, } if (buf != NULL) - g_key_file_set_string (self->keyfile, name, key, buf); + g_key_file_set_string (keyfile, name, key, buf); - new = g_key_file_get_value (self->keyfile, name, key, NULL); + new = g_key_file_get_value (keyfile, name, key, NULL); if (tp_strdiff (old, new)) - { - update_storage (self, name, key, secret); - updated = TRUE; - } + updated = TRUE; g_free (new); g_free (buf); @@ -1186,7 +1349,8 @@ mcd_storage_delete_account (McdStorage *self, g_return_if_fail (MCD_IS_STORAGE (self)); g_return_if_fail (account != NULL); - g_key_file_remove_group (self->keyfile, account, NULL); + g_key_file_remove_group (self->attributes, account, NULL); + g_key_file_remove_group (self->parameters, account, NULL); for (store = stores; store != NULL; store = g_list_next (store)) { @@ -1234,11 +1398,9 @@ mcd_storage_commit (McdStorage *self, const gchar *account) * mcd_storage_set_strv: * @storage: An object implementing the #McdStorage interface * @account: the unique name of an account - * @key: the key (name) of the parameter or setting + * @attribute: the name of the attribute * @strv: the string vector to be stored (where %NULL is treated as equivalent * to an empty vector) - * @secret: whether the value is confidential (might get stored in the - * keyring, for example) * * Copies and stores the supplied string vector to the internal cache. * @@ -1251,9 +1413,8 @@ mcd_storage_commit (McdStorage *self, const gchar *account) gboolean mcd_storage_set_strv (McdStorage *storage, const gchar *account, - const gchar *key, - const gchar * const *strv, - gboolean secret) + const gchar *attribute, + const gchar * const *strv) { GValue v = G_VALUE_INIT; static const gchar * const *empty = { NULL }; @@ -1261,11 +1422,12 @@ mcd_storage_set_strv (McdStorage *storage, g_return_val_if_fail (MCD_IS_STORAGE (storage), FALSE); g_return_val_if_fail (account != NULL, FALSE); - g_return_val_if_fail (key != NULL, FALSE); + g_return_val_if_fail (attribute != NULL, FALSE); + g_return_val_if_fail (!g_str_has_prefix (attribute, "param-"), FALSE); g_value_init (&v, G_TYPE_STRV); g_value_set_static_boxed (&v, strv == NULL ? empty : strv); - ret = mcd_storage_set_value (storage, account, key, &v, secret); + ret = mcd_storage_set_attribute (storage, account, attribute, &v); g_value_unset (&v); return ret; } diff --git a/src/mcd-storage.h b/src/mcd-storage.h index 619e7b69..0c99e3f4 100644 --- a/src/mcd-storage.h +++ b/src/mcd-storage.h @@ -1,5 +1,5 @@ /* Mission Control storage API - interface which provides access to account - * parameter/setting storage + * parameter/attribute storage * * Copyright © 2010 Nokia Corporation * Copyright © 2010 Collabora Ltd. @@ -30,7 +30,8 @@ G_BEGIN_DECLS typedef struct { GObject parent; TpDBusDaemon *dbusd; - GKeyFile *keyfile; + GKeyFile *attributes; + GKeyFile *parameters; GKeyFile *secrets; } McdStorage; @@ -66,25 +67,28 @@ void mcd_storage_load (McdStorage *storage); GStrv mcd_storage_dup_accounts (McdStorage *storage, gsize *n); -GStrv mcd_storage_dup_settings (McdStorage *storage, +GStrv mcd_storage_dup_attributes (McdStorage *storage, const gchar *account, gsize *n); gboolean mcd_storage_set_string (McdStorage *storage, const gchar *account, - const gchar *key, - const gchar *value, - gboolean secret); + const gchar *attribute, + const gchar *value); gboolean mcd_storage_set_strv (McdStorage *storage, const gchar *account, - const gchar *key, - const gchar * const *strv, - gboolean secret); + const gchar *attribute, + const gchar * const *strv); -gboolean mcd_storage_set_value (McdStorage *storage, +gboolean mcd_storage_set_attribute (McdStorage *storage, const gchar *account, - const gchar *key, + const gchar *attribute, + const GValue *value); + +gboolean mcd_storage_set_parameter (McdStorage *storage, + const gchar *account, + const gchar *parameter, const GValue *value, gboolean secret); @@ -101,21 +105,27 @@ void mcd_storage_commit (McdStorage *storage, const gchar *account); gchar *mcd_storage_dup_string (McdStorage *storage, const gchar *account, - const gchar *key); + const gchar *attribute); -gboolean mcd_storage_get_value (McdStorage *storage, +gboolean mcd_storage_get_attribute (McdStorage *storage, const gchar *account, - const gchar *key, + const gchar *attribute, + GValue *value, + GError **error); + +gboolean mcd_storage_get_parameter (McdStorage *storage, + const gchar *account, + const gchar *parameter, GValue *value, GError **error); gboolean mcd_storage_get_boolean (McdStorage *storage, const gchar *account, - const gchar *key); + const gchar *attribute); gint mcd_storage_get_integer (McdStorage *storage, const gchar *account, - const gchar *key); + const gchar *attribute); McpAccountStorage * mcd_storage_get_plugin (McdStorage *storage, const gchar *account); @@ -126,6 +136,16 @@ gboolean mcd_storage_add_account_from_plugin (McdStorage *storage, McpAccountStorage *plugin, const gchar *account); +gboolean mcd_keyfile_get_value (GKeyFile *keyfile, + const gchar *group, + const gchar *key, + GValue *value, + GError **error); +gboolean mcd_keyfile_set_value (GKeyFile *keyfile, + const gchar *name, + const gchar *key, + const GValue *value); + G_END_DECLS #endif /* MCD_STORAGE_H */ |