diff --git a/libnm-core/nm-utils.c b/libnm-core/nm-utils.c index 24d9dfcb43..2cfaea6dd4 100644 --- a/libnm-core/nm-utils.c +++ b/libnm-core/nm-utils.c @@ -802,53 +802,7 @@ _nm_utils_hash_values_to_slist (GHashTable *hash) static GVariant * _nm_utils_strdict_to_dbus (const GValue *prop_value) { - GHashTable *hash; - GHashTableIter iter; - const char *key, *value; - GVariantBuilder builder; - guint i, len; - - g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{ss}")); - - hash = g_value_get_boxed (prop_value); - if (!hash) - goto out; - len = g_hash_table_size (hash); - if (!len) - goto out; - - g_hash_table_iter_init (&iter, hash); - if (!g_hash_table_iter_next (&iter, (gpointer *) &key, (gpointer *) &value)) - nm_assert_not_reached (); - - if (len == 1) - g_variant_builder_add (&builder, "{ss}", key, value); - else { - gs_free NMUtilsNamedValue *idx_free = NULL; - NMUtilsNamedValue *idx; - - if (len > 300 / sizeof (NMUtilsNamedValue)) { - idx_free = g_new (NMUtilsNamedValue, len); - idx = idx_free; - } else - idx = g_alloca (sizeof (NMUtilsNamedValue) * len); - - i = 0; - do { - idx[i].name = key; - idx[i].value_str = value; - i++; - } while (g_hash_table_iter_next (&iter, (gpointer *) &key, (gpointer *) &value)); - nm_assert (i == len); - - nm_utils_named_value_list_sort (idx, len, NULL, NULL); - - for (i = 0; i < len; i++) - g_variant_builder_add (&builder, "{ss}", idx[i].name, idx[i].value_str); - } - -out: - return g_variant_builder_end (&builder); + return nm_utils_strdict_to_variant_ass (g_value_get_boxed (prop_value)); } void diff --git a/shared/nm-glib-aux/nm-shared-utils.c b/shared/nm-glib-aux/nm-shared-utils.c index 14e32956db..2b4f5b5b68 100644 --- a/shared/nm-glib-aux/nm-shared-utils.c +++ b/shared/nm-glib-aux/nm-shared-utils.c @@ -444,6 +444,62 @@ nm_utils_gbytes_to_variant_ay (GBytes *bytes) /*****************************************************************************/ +/* Convert a hash table with "char *" keys and values to an "a{ss}" GVariant. + * The keys will be sorted asciibetically. + * Returns a floating reference. + */ +GVariant * +nm_utils_strdict_to_variant_ass (GHashTable *strdict) +{ + GHashTableIter iter; + const char *key, *value; + GVariantBuilder builder; + guint i, len; + + g_variant_builder_init (&builder, G_VARIANT_TYPE ("a{ss}")); + + if (!strdict) + goto out; + len = g_hash_table_size (strdict); + if (!len) + goto out; + + g_hash_table_iter_init (&iter, strdict); + if (!g_hash_table_iter_next (&iter, (gpointer *) &key, (gpointer *) &value)) + nm_assert_not_reached (); + + if (len == 1) + g_variant_builder_add (&builder, "{ss}", key, value); + else { + gs_free NMUtilsNamedValue *idx_free = NULL; + NMUtilsNamedValue *idx; + + if (len > 300 / sizeof (NMUtilsNamedValue)) { + idx_free = g_new (NMUtilsNamedValue, len); + idx = idx_free; + } else + idx = g_alloca (sizeof (NMUtilsNamedValue) * len); + + i = 0; + do { + idx[i].name = key; + idx[i].value_str = value; + i++; + } while (g_hash_table_iter_next (&iter, (gpointer *) &key, (gpointer *) &value)); + nm_assert (i == len); + + nm_utils_named_value_list_sort (idx, len, NULL, NULL); + + for (i = 0; i < len; i++) + g_variant_builder_add (&builder, "{ss}", idx[i].name, idx[i].value_str); + } + +out: + return g_variant_builder_end (&builder); +} + +/*****************************************************************************/ + /** * nm_strquote: * @buf: the output buffer of where to write the quoted @str argument. diff --git a/shared/nm-glib-aux/nm-shared-utils.h b/shared/nm-glib-aux/nm-shared-utils.h index 1d066451b3..0d5f383de0 100644 --- a/shared/nm-glib-aux/nm-shared-utils.h +++ b/shared/nm-glib-aux/nm-shared-utils.h @@ -393,6 +393,8 @@ gboolean nm_utils_gbytes_equal_mem (GBytes *bytes, GVariant *nm_utils_gbytes_to_variant_ay (GBytes *bytes); +GVariant *nm_utils_strdict_to_variant_ass (GHashTable *strdict); + /*****************************************************************************/ GVariant *nm_utils_gvariant_vardict_filter (GVariant *src,