summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan Winship <danw@gnome.org>2014-06-24 17:40:08 -0400
committerDan Winship <danw@gnome.org>2014-09-03 11:13:19 -0400
commite2126fb5187a851c6e6147362288d2cbbc517a47 (patch)
tree9c4642e508e4822226ec89a11e9df3c8a01ea85a
parent02007c067196927b9908f008950c2840b9ae6775 (diff)
downloadNetworkManager-e2126fb5187a851c6e6147362288d2cbbc517a47.tar.gz
libnm-core: change map-of-string properties to G_TYPE_HASH_TABLE
Change all DBUS_TYPE_G_MAP_OF_STRING properties to G_TYPE_HASH_TABLE, with annotations indicating they are string->string. Not much outside libnm-core needs to changed for this, since DBUS_TYPE_G_MAP_OF_STRING was already represented as a hash table. (One change needed within libnm-core is that we now need to copy the hash tables in get_property(), or else the caller will receive a reffed copy of the object's own hash table, which we don't want.)
-rw-r--r--clients/cli/nmcli.c30
-rw-r--r--libnm-core/nm-core-internal.h2
-rw-r--r--libnm-core/nm-setting-bond.c32
-rw-r--r--libnm-core/nm-setting-vpn.c46
-rw-r--r--libnm-core/nm-setting-wired.c24
-rw-r--r--libnm-core/nm-utils-private.h5
-rw-r--r--libnm-core/nm-utils.c30
-rw-r--r--libnm-core/tests/test-general.c2
-rw-r--r--src/settings/plugins/keyfile/reader.c2
-rw-r--r--src/settings/plugins/keyfile/writer.c2
10 files changed, 116 insertions, 59 deletions
diff --git a/clients/cli/nmcli.c b/clients/cli/nmcli.c
index 87ac008442..f1d8b163a9 100644
--- a/clients/cli/nmcli.c
+++ b/clients/cli/nmcli.c
@@ -410,11 +410,41 @@ nmc_convert_strv_to_string (const GValue *src_value, GValue *dest_value)
}
static void
+nmc_convert_string_hash_to_string (const GValue *src_value, GValue *dest_value)
+{
+ GHashTable *hash;
+ GHashTableIter iter;
+ const char *key, *value;
+ GString *string;
+
+ hash = (GHashTable *) g_value_get_boxed (src_value);
+
+ string = g_string_new (NULL);
+ if (hash) {
+ g_hash_table_iter_init (&iter, hash);
+ while (g_hash_table_iter_next (&iter, (gpointer *) &key, (gpointer *) &value)) {
+ if (string->len)
+ g_string_append_c (string, ',');
+ g_string_append_printf (string, "%s=%s", key, value);
+ }
+ }
+
+ g_value_take_string (dest_value, g_string_free (string, FALSE));
+}
+
+static void
nmc_value_transforms_register (void)
{
g_value_register_transform_func (G_TYPE_STRV,
G_TYPE_STRING,
nmc_convert_strv_to_string);
+
+ /* This depends on the fact that all of the hash-table-valued properties
+ * in libnm-core are string->string.
+ */
+ g_value_register_transform_func (G_TYPE_HASH_TABLE,
+ G_TYPE_STRING,
+ nmc_convert_string_hash_to_string);
}
static NMClient *
diff --git a/libnm-core/nm-core-internal.h b/libnm-core/nm-core-internal.h
index 1e6dd63892..b02bb69850 100644
--- a/libnm-core/nm-core-internal.h
+++ b/libnm-core/nm-core-internal.h
@@ -63,5 +63,7 @@ gboolean _nm_setting_ip4_config_add_address_with_label (NMSettingIP4Config *s
GSList * _nm_utils_hash_values_to_slist (GHashTable *hash);
+GHashTable *_nm_utils_copy_strdict (GHashTable *strdict);
+
#endif
diff --git a/libnm-core/nm-setting-bond.c b/libnm-core/nm-setting-bond.c
index 8c5ba5e3bb..22fa19f3e4 100644
--- a/libnm-core/nm-setting-bond.c
+++ b/libnm-core/nm-setting-bond.c
@@ -670,25 +670,15 @@ finalize (GObject *object)
}
static void
-copy_hash (gpointer key, gpointer value, gpointer user_data)
-{
- g_hash_table_insert ((GHashTable *) user_data, g_strdup (key), g_strdup (value));
-}
-
-static void
set_property (GObject *object, guint prop_id,
const GValue *value, GParamSpec *pspec)
{
NMSettingBondPrivate *priv = NM_SETTING_BOND_GET_PRIVATE (object);
- GHashTable *new_hash;
switch (prop_id) {
case PROP_OPTIONS:
- /* Must make a deep copy of the hash table here... */
- g_hash_table_remove_all (priv->options);
- new_hash = g_value_get_boxed (value);
- if (new_hash)
- g_hash_table_foreach (new_hash, copy_hash, priv->options);
+ g_hash_table_unref (priv->options);
+ priv->options = _nm_utils_copy_strdict (g_value_get_boxed (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
@@ -704,7 +694,7 @@ get_property (GObject *object, guint prop_id,
switch (prop_id) {
case PROP_OPTIONS:
- g_value_set_boxed (value, priv->options);
+ g_value_take_boxed (value, _nm_utils_copy_strdict (priv->options));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
@@ -733,14 +723,20 @@ nm_setting_bond_class_init (NMSettingBondClass *setting_class)
* Dictionary of key/value pairs of bonding options. Both keys and values
* must be strings. Option names must contain only alphanumeric characters
* (ie, [a-zA-Z0-9]).
+ *
+ * Type: GHashTable(utf8,utf8)
**/
g_object_class_install_property
(object_class, PROP_OPTIONS,
- g_param_spec_boxed (NM_SETTING_BOND_OPTIONS, "", "",
- DBUS_TYPE_G_MAP_OF_STRING,
- G_PARAM_READWRITE |
- NM_SETTING_PARAM_INFERRABLE |
- G_PARAM_STATIC_STRINGS));
+ g_param_spec_boxed (NM_SETTING_BOND_OPTIONS, "", "",
+ G_TYPE_HASH_TABLE,
+ G_PARAM_READWRITE |
+ NM_SETTING_PARAM_INFERRABLE |
+ G_PARAM_STATIC_STRINGS));
+ _nm_setting_class_transform_property (parent_class, NM_SETTING_BOND_OPTIONS,
+ DBUS_TYPE_G_MAP_OF_STRING,
+ _nm_utils_strdict_to_dbus,
+ _nm_utils_strdict_from_dbus);
_nm_setting_class_add_dbus_only_property (parent_class, "interface-name", G_TYPE_STRING,
_nm_setting_get_deprecated_virtual_interface_name,
diff --git a/libnm-core/nm-setting-vpn.c b/libnm-core/nm-setting-vpn.c
index 1e902e202c..74d2f8afd7 100644
--- a/libnm-core/nm-setting-vpn.c
+++ b/libnm-core/nm-setting-vpn.c
@@ -27,6 +27,7 @@
#include "nm-setting-vpn.h"
#include "nm-utils.h"
+#include "nm-utils-private.h"
#include "nm-dbus-glib-types.h"
#include "nm-setting-private.h"
@@ -513,7 +514,7 @@ update_one_secret (NMSetting *setting, const char *key, GValue *value, GError **
* for compatibility's sake.
*/
success = update_secret_string (setting, key, g_value_get_string (value), error);
- } else if (G_VALUE_HOLDS (value, DBUS_TYPE_G_MAP_OF_STRING)) {
+ } else if (G_VALUE_HOLDS (value, G_TYPE_HASH_TABLE)) {
if (strcmp (key, NM_SETTING_VPN_SECRETS) != 0) {
g_set_error (error, NM_SETTING_ERROR, NM_SETTING_ERROR_PROPERTY_NOT_SECRET,
"Property %s not a secret property", key);
@@ -714,19 +715,10 @@ finalize (GObject *object)
}
static void
-copy_hash (gpointer key, gpointer value, gpointer user_data)
-{
- g_return_if_fail (value != NULL);
- g_return_if_fail (strlen (value));
- g_hash_table_insert ((GHashTable *) user_data, g_strdup (key), g_strdup (value));
-}
-
-static void
set_property (GObject *object, guint prop_id,
const GValue *value, GParamSpec *pspec)
{
NMSettingVpnPrivate *priv = NM_SETTING_VPN_GET_PRIVATE (object);
- GHashTable *new_hash;
switch (prop_id) {
case PROP_SERVICE_TYPE:
@@ -738,18 +730,12 @@ set_property (GObject *object, guint prop_id,
priv->user_name = g_value_dup_string (value);
break;
case PROP_DATA:
- /* Must make a deep copy of the hash table here... */
- g_hash_table_remove_all (priv->data);
- new_hash = g_value_get_boxed (value);
- if (new_hash)
- g_hash_table_foreach (new_hash, copy_hash, priv->data);
+ g_hash_table_unref (priv->data);
+ priv->data = _nm_utils_copy_strdict (g_value_get_boxed (value));
break;
case PROP_SECRETS:
- /* Must make a deep copy of the hash table here... */
- g_hash_table_remove_all (priv->secrets);
- new_hash = g_value_get_boxed (value);
- if (new_hash)
- g_hash_table_foreach (new_hash, copy_hash, priv->secrets);
+ g_hash_table_unref (priv->secrets);
+ priv->secrets = _nm_utils_copy_strdict (g_value_get_boxed (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
@@ -772,10 +758,10 @@ get_property (GObject *object, guint prop_id,
g_value_set_string (value, nm_setting_vpn_get_user_name (setting));
break;
case PROP_DATA:
- g_value_set_boxed (value, priv->data);
+ g_value_take_boxed (value, _nm_utils_copy_strdict (priv->data));
break;
case PROP_SECRETS:
- g_value_set_boxed (value, priv->secrets);
+ g_value_take_boxed (value, _nm_utils_copy_strdict (priv->secrets));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
@@ -841,25 +827,37 @@ nm_setting_vpn_class_init (NMSettingVpnClass *setting_class)
*
* Dictionary of key/value pairs of VPN plugin specific data. Both keys and
* values must be strings.
+ *
+ * Type: GHashTable(utf8,utf8)
**/
g_object_class_install_property
(object_class, PROP_DATA,
g_param_spec_boxed (NM_SETTING_VPN_DATA, "", "",
- DBUS_TYPE_G_MAP_OF_STRING,
+ G_TYPE_HASH_TABLE,
G_PARAM_READWRITE |
G_PARAM_STATIC_STRINGS));
+ _nm_setting_class_transform_property (parent_class, NM_SETTING_VPN_DATA,
+ DBUS_TYPE_G_MAP_OF_STRING,
+ _nm_utils_strdict_to_dbus,
+ _nm_utils_strdict_from_dbus);
/**
* NMSettingVpn:secrets:
*
* Dictionary of key/value pairs of VPN plugin specific secrets like
* passwords or private keys. Both keys and values must be strings.
+ *
+ * Type: GHashTable(utf8,utf8)
**/
g_object_class_install_property
(object_class, PROP_SECRETS,
g_param_spec_boxed (NM_SETTING_VPN_SECRETS, "", "",
- DBUS_TYPE_G_MAP_OF_STRING,
+ G_TYPE_HASH_TABLE,
G_PARAM_READWRITE |
NM_SETTING_PARAM_SECRET |
G_PARAM_STATIC_STRINGS));
+ _nm_setting_class_transform_property (parent_class, NM_SETTING_VPN_SECRETS,
+ DBUS_TYPE_G_MAP_OF_STRING,
+ _nm_utils_strdict_to_dbus,
+ _nm_utils_strdict_from_dbus);
}
diff --git a/libnm-core/nm-setting-wired.c b/libnm-core/nm-setting-wired.c
index e115da69db..f641e8a813 100644
--- a/libnm-core/nm-setting-wired.c
+++ b/libnm-core/nm-setting-wired.c
@@ -707,17 +707,10 @@ finalize (GObject *object)
}
static void
-copy_hash (gpointer key, gpointer value, gpointer user_data)
-{
- g_hash_table_insert ((GHashTable *) user_data, g_strdup (key), g_strdup (value));
-}
-
-static void
set_property (GObject *object, guint prop_id,
const GValue *value, GParamSpec *pspec)
{
NMSettingWiredPrivate *priv = NM_SETTING_WIRED_GET_PRIVATE (object);
- GHashTable *new_hash;
switch (prop_id) {
case PROP_PORT:
@@ -759,11 +752,8 @@ set_property (GObject *object, guint prop_id,
priv->s390_nettype = g_value_dup_string (value);
break;
case PROP_S390_OPTIONS:
- /* Must make a deep copy of the hash table here... */
- g_hash_table_remove_all (priv->s390_options);
- new_hash = g_value_get_boxed (value);
- if (new_hash)
- g_hash_table_foreach (new_hash, copy_hash, priv->s390_options);
+ g_hash_table_unref (priv->s390_options);
+ priv->s390_options = _nm_utils_copy_strdict (g_value_get_boxed (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
@@ -810,7 +800,7 @@ get_property (GObject *object, guint prop_id,
g_value_set_string (value, nm_setting_wired_get_s390_nettype (setting));
break;
case PROP_S390_OPTIONS:
- g_value_set_boxed (value, priv->s390_options);
+ g_value_take_boxed (value, _nm_utils_copy_strdict (priv->s390_options));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
@@ -998,12 +988,18 @@ nm_setting_wired_class_init (NMSettingWiredClass *setting_class)
* and values must be strings. Allowed keys include "portno", "layer2",
* "portname", "protocol", among others. Key names must contain only
* alphanumeric characters (ie, [a-zA-Z0-9]).
+ *
+ * Type: GHashTable(utf8,utf8)
**/
g_object_class_install_property
(object_class, PROP_S390_OPTIONS,
g_param_spec_boxed (NM_SETTING_WIRED_S390_OPTIONS, "", "",
- DBUS_TYPE_G_MAP_OF_STRING,
+ G_TYPE_HASH_TABLE,
G_PARAM_READWRITE |
NM_SETTING_PARAM_INFERRABLE |
G_PARAM_STATIC_STRINGS));
+ _nm_setting_class_transform_property (parent_class, NM_SETTING_WIRED_S390_OPTIONS,
+ DBUS_TYPE_G_MAP_OF_STRING,
+ _nm_utils_strdict_to_dbus,
+ _nm_utils_strdict_from_dbus);
}
diff --git a/libnm-core/nm-utils-private.h b/libnm-core/nm-utils-private.h
index b4ddf6e8a0..9154a40598 100644
--- a/libnm-core/nm-utils-private.h
+++ b/libnm-core/nm-utils-private.h
@@ -39,6 +39,11 @@ void _nm_utils_hwaddr_to_dbus (const GValue *prop_value,
void _nm_utils_hwaddr_from_dbus (const GValue *dbus_value,
GValue *prop_value);
+void _nm_utils_strdict_to_dbus (const GValue *prop_value,
+ GValue *dbus_value);
+void _nm_utils_strdict_from_dbus (const GValue *dbus_value,
+ GValue *prop_value);
+
GSList * _nm_utils_strv_to_slist (char **strv);
char ** _nm_utils_slist_to_strv (GSList *slist);
diff --git a/libnm-core/nm-utils.c b/libnm-core/nm-utils.c
index 9945a79210..444a5d71e8 100644
--- a/libnm-core/nm-utils.c
+++ b/libnm-core/nm-utils.c
@@ -564,6 +564,36 @@ _nm_utils_hash_values_to_slist (GHashTable *hash)
return list;
}
+void
+_nm_utils_strdict_to_dbus (const GValue *prop_value,
+ GValue *dbus_value)
+{
+ g_value_set_boxed (dbus_value, g_value_get_boxed (prop_value));
+}
+
+void
+_nm_utils_strdict_from_dbus (const GValue *dbus_value,
+ GValue *prop_value)
+{
+ g_value_set_boxed (prop_value, g_value_get_boxed (dbus_value));
+}
+
+GHashTable *
+_nm_utils_copy_strdict (GHashTable *strdict)
+{
+ GHashTable *copy;
+ GHashTableIter iter;
+ gpointer key, value;
+
+ copy = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
+ if (strdict) {
+ g_hash_table_iter_init (&iter, strdict);
+ while (g_hash_table_iter_next (&iter, &key, &value))
+ g_hash_table_insert (copy, g_strdup (key), g_strdup (value));
+ }
+ return copy;
+}
+
GSList *
_nm_utils_strv_to_slist (char **strv)
{
diff --git a/libnm-core/tests/test-general.c b/libnm-core/tests/test-general.c
index a5f8ee6f97..2a78ae447d 100644
--- a/libnm-core/tests/test-general.c
+++ b/libnm-core/tests/test-general.c
@@ -207,7 +207,7 @@ test_setting_vpn_update_secrets (void)
g_hash_table_insert (settings, NM_SETTING_VPN_SETTING_NAME, vpn);
secrets = g_hash_table_new_full (g_str_hash, g_str_equal, NULL, NULL);
- g_value_init (&val, DBUS_TYPE_G_MAP_OF_STRING);
+ g_value_init (&val, G_TYPE_HASH_TABLE);
g_value_take_boxed (&val, secrets);
g_hash_table_insert (vpn, NM_SETTING_VPN_SECRETS, &val);
diff --git a/src/settings/plugins/keyfile/reader.c b/src/settings/plugins/keyfile/reader.c
index 46fcbc196c..25192e6079 100644
--- a/src/settings/plugins/keyfile/reader.c
+++ b/src/settings/plugins/keyfile/reader.c
@@ -1141,7 +1141,7 @@ read_one_setting_value (NMSetting *setting,
sa = nm_keyfile_plugin_kf_get_string_list (info->keyfile, setting_name, key, &length, NULL);
g_object_set (setting, key, sa, NULL);
g_strfreev (sa);
- } else if (type == DBUS_TYPE_G_MAP_OF_STRING) {
+ } else if (type == G_TYPE_HASH_TABLE) {
read_hash_of_string (info->keyfile, setting, key);
} else if (type == DBUS_TYPE_G_UINT_ARRAY) {
if (!read_array_of_uint (info->keyfile, setting, key)) {
diff --git a/src/settings/plugins/keyfile/writer.c b/src/settings/plugins/keyfile/writer.c
index 915da95875..4278397ee1 100644
--- a/src/settings/plugins/keyfile/writer.c
+++ b/src/settings/plugins/keyfile/writer.c
@@ -899,7 +899,7 @@ write_setting_value (NMSetting *setting,
array = (char **) g_value_get_boxed (value);
nm_keyfile_plugin_kf_set_string_list (info->keyfile, setting_name, key, (const gchar **const) array, g_strv_length (array));
- } else if (type == DBUS_TYPE_G_MAP_OF_STRING) {
+ } else if (type == G_TYPE_HASH_TABLE) {
write_hash_of_string (info->keyfile, setting, key, value);
} else if (type == DBUS_TYPE_G_UINT_ARRAY) {
if (!write_array_of_uint (info->keyfile, setting, key, value)) {