Commit 9ed6bd2b authored by Dan Winship's avatar Dan Winship

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.)
parent 6a4127cf
......@@ -400,12 +400,42 @@ nmc_convert_strv_to_string (const GValue *src_value, GValue *dest_value)
g_value_set_string (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 *
......
......@@ -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
......@@ -669,26 +669,16 @@ finalize (GObject *object)
G_OBJECT_CLASS (nm_setting_bond_parent_class)->finalize (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,
......
......@@ -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"
......@@ -713,20 +714,11 @@ finalize (GObject *object)
G_OBJECT_CLASS (nm_setting_vpn_parent_class)->finalize (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);
}
......@@ -706,18 +706,11 @@ finalize (GObject *object)
G_OBJECT_CLASS (nm_setting_wired_parent_class)->finalize (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);
}
......@@ -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);
......
......@@ -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)
{
......
......@@ -173,33 +173,6 @@ _nm_utils_convert_gvalue_hash_to_string (const GValue *src_value, GValue *dest_v
g_string_free (printable, FALSE);
}
static void
convert_one_string_hash_entry (gpointer key, gpointer value, gpointer user_data)
{
GString *printable = (GString *) user_data;
if (printable->len)
g_string_append_c (printable, ',');
g_string_append_printf (printable, "%s=%s", (const char *) key, (const char *) value);
}
static void
_nm_utils_convert_string_hash_to_string (const GValue *src_value, GValue *dest_value)
{
GHashTable *hash;
GString *printable;
g_return_if_fail (g_type_is_a (G_VALUE_TYPE (src_value), DBUS_TYPE_G_MAP_OF_STRING));
hash = (GHashTable *) g_value_get_boxed (src_value);
printable = g_string_new (NULL);
if (hash)
g_hash_table_foreach (hash, convert_one_string_hash_entry, printable);
g_value_take_string (dest_value, g_string_free (printable, FALSE));
}
static void
_nm_utils_convert_byte_array_to_string (const GValue *src_value, GValue *dest_value)
{
......@@ -470,9 +443,6 @@ _nm_value_transforms_register (void)
g_value_register_transform_func (DBUS_TYPE_G_MAP_OF_VARIANT,
G_TYPE_STRING,
_nm_utils_convert_gvalue_hash_to_string);
g_value_register_transform_func (DBUS_TYPE_G_MAP_OF_STRING,
G_TYPE_STRING,
_nm_utils_convert_string_hash_to_string);
g_value_register_transform_func (DBUS_TYPE_G_UCHAR_ARRAY,
G_TYPE_STRING,
_nm_utils_convert_byte_array_to_string);
......
......@@ -887,8 +887,7 @@ set_secrets_not_required (NMConnection *connection, GHashTable *hash)
* "secrets" property is actually a hash table of secrets.
*/
if ( strcmp (setting_name, NM_SETTING_VPN_SETTING_NAME) == 0
&& strcmp (key_name, NM_SETTING_VPN_SECRETS) == 0
&& G_VALUE_HOLDS (val, DBUS_TYPE_G_MAP_OF_STRING)) {
&& strcmp (key_name, NM_SETTING_VPN_SECRETS) == 0) {
GHashTableIter vpn_secret_iter;
const char *secret_name;
......
......@@ -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)) {
......
......@@ -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)) {
......
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