libmm-common: let the simple properties object be built from a dictionary

This commit is contained in:
Aleksander Morgado
2011-12-28 14:30:20 +01:00
parent 0edf403734
commit 1745d8c9eb
2 changed files with 67 additions and 0 deletions

View File

@@ -168,6 +168,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 * MMCommonSimpleProperties *
mm_common_simple_properties_new (void) mm_common_simple_properties_new (void)
{ {

View File

@@ -52,6 +52,9 @@ struct _MMCommonSimplePropertiesClass {
GType mm_common_simple_properties_get_type (void); GType mm_common_simple_properties_get_type (void);
MMCommonSimpleProperties *mm_common_simple_properties_new (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); MMModemState mm_common_simple_properties_get_state (MMCommonSimpleProperties *self);
guint32 mm_common_simple_properties_get_signal_quality (MMCommonSimpleProperties *self, guint32 mm_common_simple_properties_get_signal_quality (MMCommonSimpleProperties *self,