libnm-core: reorganize _nm_setting_new_from_dbus()

Reorganize _nm_setting_new_from_dbus() to create an empty NMSetting
first and then set each of its properties, rather than passing all of
the properties to g_object_newv(). We don't need to pass them at
construct time since no NMSetting properties are CONSTRUCT_ONLY, and
organizing the function this way is a prereq for some later
functionality (being able to run code when a property *isn't* present
in the hash).
This commit is contained in:
Dan Winship
2014-08-04 11:39:33 -04:00
parent 773d3f0ab6
commit c191c38a5f

View File

@@ -462,14 +462,13 @@ _nm_setting_to_dbus (NMSetting *setting, NMConnectionSerializationFlags flags)
NMSetting * NMSetting *
_nm_setting_new_from_dbus (GType setting_type, GHashTable *hash) _nm_setting_new_from_dbus (GType setting_type, GHashTable *hash)
{ {
GHashTableIter iter;
NMSetting *setting; NMSetting *setting;
const char *prop_name;
GValue *src_value;
GObjectClass *class; GObjectClass *class;
guint n_params = 0; GHashTableIter iter;
GParameter *params; const char *prop_name;
int i; GParamSpec **property_specs;
guint n_property_specs;
guint i;
g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (setting_type), NULL); g_return_val_if_fail (G_TYPE_IS_INSTANTIATABLE (setting_type), NULL);
g_return_val_if_fail (hash != NULL, NULL); g_return_val_if_fail (hash != NULL, NULL);
@@ -478,35 +477,36 @@ _nm_setting_new_from_dbus (GType setting_type, GHashTable *hash)
* already been used. * already been used.
*/ */
class = g_type_class_ref (setting_type); class = g_type_class_ref (setting_type);
params = g_new0 (GParameter, g_hash_table_size (hash));
/* Check for invalid properties first. */
g_hash_table_iter_init (&iter, hash); g_hash_table_iter_init (&iter, hash);
while (g_hash_table_iter_next (&iter, (gpointer) &prop_name, (gpointer) &src_value)) { while (g_hash_table_iter_next (&iter, (gpointer) &prop_name, NULL)) {
GValue *dst_value = &params[n_params].value; if (!g_object_class_find_property (class, prop_name)) {
GParamSpec *param_spec;
param_spec = g_object_class_find_property (class, prop_name);
if (!param_spec) {
/* Oh, we're so nice and only warn, maybe it should be a fatal error? */ /* Oh, we're so nice and only warn, maybe it should be a fatal error? */
g_warning ("Ignoring invalid property '%s'", prop_name); g_warning ("Ignoring invalid property '%s'", prop_name);
continue; continue;
} }
/* 'name' doesn't get deserialized */
if (strcmp (g_param_spec_get_name (param_spec), NM_SETTING_NAME) == 0)
continue;
g_value_init (dst_value, G_VALUE_TYPE (src_value));
g_value_copy (src_value, dst_value);
params[n_params++].name = prop_name;
} }
setting = (NMSetting *) g_object_newv (setting_type, n_params, params); /* Now build the setting object from the legitimate properties */
setting = (NMSetting *) g_object_new (setting_type, NULL);
for (i = 0; i < n_params; i++) property_specs = g_object_class_list_properties (G_OBJECT_GET_CLASS (setting), &n_property_specs);
g_value_unset (&params[i].value); for (i = 0; i < n_property_specs; i++) {
GParamSpec *prop_spec = property_specs[i];
GValue *value;
g_free (params); /* 'name' doesn't get deserialized */
if (strcmp (prop_spec->name, NM_SETTING_NAME) == 0)
continue;
value = g_hash_table_lookup (hash, prop_spec->name);
if (!value)
continue;
g_object_set_property (G_OBJECT (setting), prop_spec->name, value);
}
g_free (property_specs);
g_type_class_unref (class); g_type_class_unref (class);
return setting; return setting;