From 67394e49c25cb273b53cd2034bb2d31b14a7402a Mon Sep 17 00:00:00 2001 From: Nick Dedekind Date: Thu, 15 Aug 2013 12:48:55 +0100 Subject: Added toGVariantWithSchema for UnityMenuModel::changeState --- libqmenumodel/src/converter.cpp | 132 +++++++++++++++++++++++++++++++++++ libqmenumodel/src/converter.h | 4 ++ libqmenumodel/src/unitymenumodel.cpp | 16 ++++- 3 files changed, 150 insertions(+), 2 deletions(-) (limited to 'libqmenumodel/src') diff --git a/libqmenumodel/src/converter.cpp b/libqmenumodel/src/converter.cpp index d9d90bd..69e6629 100644 --- a/libqmenumodel/src/converter.cpp +++ b/libqmenumodel/src/converter.cpp @@ -195,3 +195,135 @@ GVariant* Converter::toGVariant(const QVariant &value) return result; } +GVariant* Converter::toGVariantWithSchema(const QVariant &value, const char* schema) +{ + if (!g_variant_type_string_is_valid(schema)) { + return Converter::toGVariant(value); + } + + GVariant* result = NULL; + const GVariantType* schema_type; + schema_type = g_variant_type_new(schema); + + if (g_variant_type_equal(schema_type, G_VARIANT_TYPE_BOOLEAN)) { + if (value.canConvert()) { + result = g_variant_new_boolean (value.value()); + } + } else if (g_variant_type_equal(schema_type, G_VARIANT_TYPE_BYTE)) { + if (value.canConvert()) { + result = g_variant_new_byte (value.value()); + } + } else if (g_variant_type_equal(schema_type, G_VARIANT_TYPE_INT16)) { + if (value.canConvert()) { + result = g_variant_new_int16 (value.value()); + } + } else if (g_variant_type_equal(schema_type, G_VARIANT_TYPE_UINT16)) { + if (value.canConvert()) { + result = g_variant_new_uint16 (value.value()); + } + } else if (g_variant_type_equal(schema_type, G_VARIANT_TYPE_INT32)) { + if (value.canConvert()) { + result = g_variant_new_int32 (value.value()); + } + } else if (g_variant_type_equal(schema_type, G_VARIANT_TYPE_UINT32)) { + if (value.canConvert()) { + result = g_variant_new_uint32 (value.value()); + } + } else if (g_variant_type_equal(schema_type, G_VARIANT_TYPE_INT64)) { + if (value.canConvert()) { + result = g_variant_new_int64 (value.value()); + } + } else if (g_variant_type_equal(schema_type, G_VARIANT_TYPE_UINT64)) { + if (value.canConvert()) { + result = g_variant_new_uint64 (value.value()); + } + } else if (g_variant_type_equal(schema_type, G_VARIANT_TYPE_DOUBLE)) { + if (value.canConvert()) { + result = g_variant_new_double (value.value()); + } + } else if (g_variant_type_equal(schema_type, G_VARIANT_TYPE_STRING)) { + if (value.canConvert()) { + result = g_variant_new_string(value.toString().toUtf8().data()); + } + } else if (g_variant_type_equal(schema_type, G_VARIANT_TYPE_VARIANT)) { + result = Converter::toGVariant(value); + } else if (g_variant_type_equal(schema_type, G_VARIANT_TYPE_VARDICT)) { + if (value.canConvert(QVariant::Map)) { + result = Converter::toGVariant(value.toMap()); + } + } else if (g_variant_type_is_array(schema_type)) { + if (value.canConvert(QVariant::List)) { + + const GVariantType* entry_type; + GVariant* data; + entry_type = g_variant_type_element(schema_type); + gchar* entryTypeString = g_variant_type_dup_string(entry_type); + + QVariantList lst = value.toList(); + GVariant **vars = g_new(GVariant*, lst.size()); + + bool ok = true; + for (int i=0; i < lst.size(); i++) { + data = Converter::toGVariantWithSchema(lst[i], entryTypeString); + + if (data) { + vars[i] = data; + } + else { + ok = false; + qWarning() << "Failed to convert list to array with schema:" << schema; + break; + } + } + if (ok) { + result = g_variant_new_array(entry_type, vars, lst.size()); + } + g_free(entryTypeString); + g_free(vars); + } + } else if (g_variant_type_is_tuple(schema_type)) { + if (value.canConvert(QVariant::List)) { + GVariant* data; + + QVariantList lst = value.toList(); + GVariant **vars = g_new(GVariant*, lst.size()); + + const GVariantType* entry_type = g_variant_type_first(schema_type); + + bool ok = true; + for (int i=0; i < lst.size(); i++) { + + gchar* entryTypeString = g_variant_type_dup_string(entry_type); + + data = Converter::toGVariantWithSchema(lst[i], entryTypeString); + + if (data) { + vars[i] = data; + } + else { + ok = false; + qWarning() << "Failed to convert list to tuple with schema:" << schema; + g_free(entryTypeString); + break; + } + g_free(entryTypeString); + + entry_type = g_variant_type_next(entry_type); + if (!entry_type) { + break; + } + } + if (ok) { + result = g_variant_new_tuple(vars, lst.size()); + } + g_free(vars); + } + } + + // fallback to straight convert. + if (!result) { + result = Converter::toGVariant(value); + } + return result; +} + diff --git a/libqmenumodel/src/converter.h b/libqmenumodel/src/converter.h index 5f05bc7..f47c09e 100644 --- a/libqmenumodel/src/converter.h +++ b/libqmenumodel/src/converter.h @@ -28,6 +28,10 @@ class Converter public: static QVariant toQVariant(GVariant *value); static GVariant* toGVariant(const QVariant &value); + + // This converts a QVariant to a GVariant using a provided gvariant schema as + // a conversion base (it will attempt to convert to this format). + static GVariant* toGVariantWithSchema(const QVariant &value, const char* schema); }; #endif // CONVERTER_H diff --git a/libqmenumodel/src/unitymenumodel.cpp b/libqmenumodel/src/unitymenumodel.cpp index e5407ab..50eff1c 100644 --- a/libqmenumodel/src/unitymenumodel.cpp +++ b/libqmenumodel/src/unitymenumodel.cpp @@ -607,13 +607,25 @@ void UnityMenuModel::activate(int index, const QVariant& parameter) void UnityMenuModel::changeState(int index, const QVariant& parameter) { GtkMenuTrackerItem* item; + GVariant* data; + GVariant* current_state; item = (GtkMenuTrackerItem *) g_sequence_get (g_sequence_get_iter_at_pos (priv->items, index)); if (!item) return; - GVariant* data = Converter::toGVariant(parameter); + current_state = gtk_menu_tracker_item_get_action_state (item); + if (current_state) { + // Attempt to convert the parameter to the expected type + data = Converter::toGVariantWithSchema(parameter, g_variant_get_type_string(current_state)); + g_variant_unref (current_state); + } else { + data = Converter::toGVariant(parameter); + } + gtk_menu_tracker_item_change_state (item, data); - g_variant_unref(data); + if (data) { + g_variant_unref(data); + } } -- cgit v1.2.3