diff options
author | Aleksander Morgado <aleksander@lanedo.com> | 2011-12-28 14:30:20 +0100 |
---|---|---|
committer | Aleksander Morgado <aleksander@lanedo.com> | 2012-03-15 14:14:39 +0100 |
commit | 1745d8c9ebf00dc06ff4f2172d754b54c2ec9fbc (patch) | |
tree | 77e66c07ae645e6f96a086b98edbe8a08fb272f0 | |
parent | 0edf4037349ab087925de1e47faf609c78f225e8 (diff) |
libmm-common: let the simple properties object be built from a dictionary
-rw-r--r-- | libmm-common/mm-common-simple-properties.c | 64 | ||||
-rw-r--r-- | libmm-common/mm-common-simple-properties.h | 3 |
2 files changed, 67 insertions, 0 deletions
diff --git a/libmm-common/mm-common-simple-properties.c b/libmm-common/mm-common-simple-properties.c index 36679395..64150b42 100644 --- a/libmm-common/mm-common-simple-properties.c +++ b/libmm-common/mm-common-simple-properties.c @@ -169,6 +169,70 @@ mm_common_simple_properties_get_dictionary (MMCommonSimpleProperties *self) /*****************************************************************************/ MMCommonSimpleProperties * +mm_common_simple_properties_new_from_dictionary (GVariant *dictionary, + GError **error) +{ + GError *inner_error = NULL; + GVariantIter iter; + gchar *key; + GVariant *value; + MMCommonSimpleProperties *properties; + + properties = mm_common_simple_properties_new (); + if (!dictionary) + return properties; + + g_variant_iter_init (&iter, dictionary); + while (!inner_error && + g_variant_iter_next (&iter, "{sv}", &key, &value)) { + /* Note: we could do a more efficient matching by checking the variant type + * and just g_object_set()-ing they specific 'key' and value, but we do want + * to check which input keys we receive, in order to propagate the error. + */ + if (g_str_equal (key, MM_COMMON_SIMPLE_PROPERTY_STATE) || + g_str_equal (key, MM_COMMON_SIMPLE_PROPERTY_ACCESS_TECHNOLOGIES) || + g_str_equal (key, MM_COMMON_SIMPLE_PROPERTY_3GPP_REGISTRATION_STATE)) { + /* uint properties */ + g_object_set (properties, + key, g_variant_get_uint32 (value), + NULL); + } else if (g_str_equal (key, MM_COMMON_SIMPLE_PROPERTY_3GPP_OPERATOR_CODE) || + g_str_equal (key, MM_COMMON_SIMPLE_PROPERTY_3GPP_OPERATOR_NAME)) { + /* string properties */ + g_object_set (properties, + key, g_variant_get_string (value, NULL), + NULL); + } else if (g_str_equal (key, MM_COMMON_SIMPLE_PROPERTY_BANDS) || + g_str_equal (key, MM_COMMON_SIMPLE_PROPERTY_SIGNAL_QUALITY)) { + /* remaining complex types, as variant */ + g_object_set (properties, + key, value, + NULL); + } else { + /* Set inner error, will stop the loop */ + inner_error = g_error_new (MM_CORE_ERROR, + MM_CORE_ERROR_INVALID_ARGS, + "Invalid properties dictionary, unexpected key '%s'", + key); + } + + g_free (key); + g_variant_unref (value); + } + + /* If error, destroy the object */ + if (inner_error) { + g_propagate_error (error, inner_error); + g_object_unref (properties); + properties = NULL; + } + + return properties; +} + +/*****************************************************************************/ + +MMCommonSimpleProperties * mm_common_simple_properties_new (void) { return (MM_COMMON_SIMPLE_PROPERTIES ( diff --git a/libmm-common/mm-common-simple-properties.h b/libmm-common/mm-common-simple-properties.h index 57df5ed5..4c507e58 100644 --- a/libmm-common/mm-common-simple-properties.h +++ b/libmm-common/mm-common-simple-properties.h @@ -52,6 +52,9 @@ struct _MMCommonSimplePropertiesClass { GType mm_common_simple_properties_get_type (void); MMCommonSimpleProperties *mm_common_simple_properties_new (void); +MMCommonSimpleProperties *mm_common_simple_properties_new_from_dictionary ( + GVariant *dictionary, + GError **error); MMModemState mm_common_simple_properties_get_state (MMCommonSimpleProperties *self); guint32 mm_common_simple_properties_get_signal_quality (MMCommonSimpleProperties *self, |