diff options
author | Simon McVittie <simon.mcvittie@collabora.co.uk> | 2012-09-06 12:23:53 +0100 |
---|---|---|
committer | Simon McVittie <simon.mcvittie@collabora.co.uk> | 2012-09-10 19:21:14 +0100 |
commit | fd9f5784098d774076a12a5dd0da693f2b417cca (patch) | |
tree | 5f4a37858aefc6b04faa73f372b7116da6d022d0 | |
parent | 2a647cbf905ad47a3d597499efc3eaae1397b719 (diff) | |
download | telepathy-mission-control-fd9f5784098d774076a12a5dd0da693f2b417cca.tar.gz |
mcd_keyfile_escape_value, mcd_keyfile_unescape_value: add
Also make them available to account storage plugins.
Signed-off-by: Simon McVittie <simon.mcvittie@collabora.co.uk>
-rw-r--r-- | mission-control-plugins/account.c | 59 | ||||
-rw-r--r-- | mission-control-plugins/account.h | 10 | ||||
-rw-r--r-- | mission-control-plugins/implementation.h | 8 | ||||
-rw-r--r-- | src/mcd-storage.c | 70 | ||||
-rw-r--r-- | src/mcd-storage.h | 5 |
5 files changed, 152 insertions, 0 deletions
diff --git a/mission-control-plugins/account.c b/mission-control-plugins/account.c index a026d15c..63892ed6 100644 --- a/mission-control-plugins/account.c +++ b/mission-control-plugins/account.c @@ -237,3 +237,62 @@ mcp_account_manager_get_unique_name (McpAccountManager *mcpa, return iface->unique_name (mcpa, manager, protocol, params); } + +/** + * mcp_account_manager_escape_value_from_keyfile: + * @mcpa: a #McpAccountManager + * @value: a value with a supported #GType + * + * Escape @value so it could be passed to g_key_file_set_value(). + * For instance, escaping the boolean value TRUE returns "true", + * and escaping the string value containing one space returns "\s". + * + * It is a programming error to use an unsupported type. + * The supported types are currently %G_TYPE_STRING, %G_TYPE_BOOLEAN, + * %G_TYPE_INT, %G_TYPE_UINT, %G_TYPE_INT64, %G_TYPE_UINT64, %G_TYPE_UCHAR, + * %G_TYPE_STRV, %DBUS_TYPE_G_OBJECT_PATH and %TP_ARRAY_TYPE_OBJECT_PATH_LIST. + * + * Returns: the escaped form of @value + */ +gchar * +mcp_account_manager_escape_value_for_keyfile (const McpAccountManager *mcpa, + const GValue *value) +{ + McpAccountManagerIface *iface = MCP_ACCOUNT_MANAGER_GET_IFACE (mcpa); + + g_return_val_if_fail (iface != NULL, NULL); + g_return_val_if_fail (iface->escape_value_for_keyfile != NULL, NULL); + + return iface->escape_value_for_keyfile (mcpa, value); +} + +/** + * mcp_account_manager_unescape_value_from_keyfile: + * @mcpa: a #McpAccountManager + * @escaped: an escaped string as returned by g_key_file_get_value() + * @value: a value to populate, with a supported #GType + * @error: used to raise an error if %FALSE is returned + * + * Attempt to interpret @escaped as a value of @value's type. + * If successful, put it in @value and return %TRUE. + * + * It is a programming error to try to escape an unsupported type. + * The supported types are currently %G_TYPE_STRING, %G_TYPE_BOOLEAN, + * %G_TYPE_INT, %G_TYPE_UINT, %G_TYPE_INT64, %G_TYPE_UINT64, %G_TYPE_UCHAR, + * %G_TYPE_STRV, %DBUS_TYPE_G_OBJECT_PATH and %TP_ARRAY_TYPE_OBJECT_PATH_LIST. + * + * Returns: %TRUE if @value was filled in + */ +gboolean +mcp_account_manager_unescape_value_from_keyfile (const McpAccountManager *mcpa, + const gchar *escaped, + GValue *value, + GError **error) +{ + McpAccountManagerIface *iface = MCP_ACCOUNT_MANAGER_GET_IFACE (mcpa); + + g_return_val_if_fail (iface != NULL, FALSE); + g_return_val_if_fail (iface->unescape_value_from_keyfile != NULL, FALSE); + + return iface->unescape_value_from_keyfile (mcpa, escaped, value, error); +} diff --git a/mission-control-plugins/account.h b/mission-control-plugins/account.h index d281f625..7f670fd0 100644 --- a/mission-control-plugins/account.h +++ b/mission-control-plugins/account.h @@ -70,6 +70,16 @@ gchar * mcp_account_manager_get_unique_name (McpAccountManager *mcpa, GStrv mcp_account_manager_list_keys (const McpAccountManager *mcpa, const gchar *account); +gchar *mcp_account_manager_escape_value_for_keyfile ( + const McpAccountManager *mcpa, + const GValue *value); + +gboolean mcp_account_manager_unescape_value_from_keyfile ( + const McpAccountManager *mcpa, + const gchar *escaped, + GValue *value, + GError **error); + G_END_DECLS #endif diff --git a/mission-control-plugins/implementation.h b/mission-control-plugins/implementation.h index 918f8309..2924f441 100644 --- a/mission-control-plugins/implementation.h +++ b/mission-control-plugins/implementation.h @@ -104,6 +104,14 @@ struct _McpAccountManagerIface { GStrv (* list_keys) (const McpAccountManager *ma, const gchar *acct); + + gchar * (* escape_value_for_keyfile) (const McpAccountManager *mcpa, + const GValue *value); + + gboolean (* unescape_value_from_keyfile) (const McpAccountManager *mcpa, + const gchar *escaped, + GValue *value, + GError **error); }; G_END_DECLS diff --git a/src/mcd-storage.c b/src/mcd-storage.c index e2b84a63..36344cfe 100644 --- a/src/mcd-storage.c +++ b/src/mcd-storage.c @@ -589,6 +589,43 @@ mcd_storage_get_parameter (McdStorage *self, value, error); } +static gboolean +mcpa_unescape_value_from_keyfile (const McpAccountManager *unused G_GNUC_UNUSED, + const gchar *escaped, + GValue *value, + GError **error) +{ + return mcd_keyfile_unescape_value (escaped, value, error); +} + +/* + * @escaped: a keyfile-escaped string + * @value: a #GValue initialized with a supported #GType + * @error: used to raise an error if %FALSE is returned + * + * Try to interpret @escaped as a value of the type of @value. If we can, + * write the resulting value into @value and return %TRUE. + * + * Returns: %TRUE if @escaped could be interpreted as a value of that type + */ +gboolean +mcd_keyfile_unescape_value (const gchar *escaped, + GValue *value, + GError **error) +{ + GKeyFile *keyfile; + gboolean ret; + + g_return_val_if_fail (escaped != NULL, FALSE); + g_return_val_if_fail (G_IS_VALUE (value), FALSE); + + keyfile = g_key_file_new (); + g_key_file_set_value (keyfile, "g", "k", escaped); + ret = mcd_keyfile_get_value (keyfile, "g", "k", value, error); + g_key_file_free (keyfile); + return ret; +} + /* * mcd_keyfile_get_value: * @keyfile: A #GKeyFile @@ -1092,6 +1129,37 @@ mcd_storage_set_parameter (McdStorage *self, return updated; } +static gchar * +mcpa_escape_value_for_keyfile (const McpAccountManager *unused G_GNUC_UNUSED, + const GValue *value) +{ + return mcd_keyfile_escape_value (value); +} + +/* + * @value: a populated #GValue of a supported #GType + * + * Escape the contents of @value to go in a #GKeyFile. Return the + * value that would go in the keyfile. + * + * For instance, for a boolean value TRUE this would return "true", + * and for a string containing one space, it would return "\s". + */ +gchar * +mcd_keyfile_escape_value (const GValue *value) +{ + GKeyFile *keyfile; + gchar *ret; + + g_return_val_if_fail (G_IS_VALUE (value), NULL); + + keyfile = g_key_file_new (); + mcd_keyfile_set_value (keyfile, "g", "k", value); + ret = g_key_file_get_value (keyfile, "g", "k", NULL); + g_key_file_free (keyfile); + return ret; +} + /* * mcd_keyfile_set_value: * @keyfile: a keyfile @@ -1460,6 +1528,8 @@ plugin_iface_init (McpAccountManagerIface *iface, iface->make_secret = make_secret; iface->unique_name = unique_name; iface->list_keys = list_keys; + iface->escape_value_for_keyfile = mcpa_escape_value_for_keyfile; + iface->unescape_value_from_keyfile = mcpa_unescape_value_from_keyfile; } gboolean diff --git a/src/mcd-storage.h b/src/mcd-storage.h index 0c99e3f4..f69bc5d1 100644 --- a/src/mcd-storage.h +++ b/src/mcd-storage.h @@ -146,6 +146,11 @@ gboolean mcd_keyfile_set_value (GKeyFile *keyfile, const gchar *key, const GValue *value); +gchar *mcd_keyfile_escape_value (const GValue *value); +gboolean mcd_keyfile_unescape_value (const gchar *escaped, + GValue *value, + GError **error); + G_END_DECLS #endif /* MCD_STORAGE_H */ |