aboutsummaryrefslogtreecommitdiff
path: root/libqmenumodel/src/converter.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'libqmenumodel/src/converter.cpp')
-rw-r--r--libqmenumodel/src/converter.cpp170
1 files changed, 97 insertions, 73 deletions
diff --git a/libqmenumodel/src/converter.cpp b/libqmenumodel/src/converter.cpp
index 527ae85..a7c3adf 100644
--- a/libqmenumodel/src/converter.cpp
+++ b/libqmenumodel/src/converter.cpp
@@ -1,5 +1,5 @@
/*
- * Copyright 2012 Canonical Ltd.
+ * Copyright 2012-2016 Canonical Ltd.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
@@ -15,6 +15,7 @@
*
* Authors:
* Renato Araujo Oliveira Filho <renato@canonical.com>
+ * Marco Trevisan <marco.trevisan@canonical.com>
*/
extern "C" {
@@ -58,6 +59,28 @@ QVariant Converter::toQVariant(GVariant *value)
gsize size = 0;
const gchar *v = g_variant_get_string(value, &size);
result.setValue(QString::fromUtf8(v, size));
+ } else if (g_variant_type_equal(type, G_VARIANT_TYPE_STRING_ARRAY)) {
+ gsize size = 0;
+ const gchar **sa = g_variant_get_strv(value, &size);
+ QStringList list;
+ for (gsize i = 0; i < size; ++i) {
+ list << QString::fromUtf8(sa[i]);
+ }
+ result.setValue(list);
+ } else if (g_variant_type_equal(type, G_VARIANT_TYPE_BYTESTRING)) {
+ gsize size = 0;
+ gchar *bs = g_variant_dup_bytestring(value, &size);
+ result.setValue(QByteArray::fromRawData(bs, size));
+ g_free(bs);
+ } else if (g_variant_type_equal(type, G_VARIANT_TYPE_BYTESTRING_ARRAY)) {
+ gsize size = 0;
+ const gchar **bsa = g_variant_get_bytestring_array(value, &size);
+ QByteArrayList list;
+ for (gsize i = 0; i < size; ++i) {
+ list << bsa[i];
+ }
+ result.setValue(list);
+ g_free(bsa);
} else if (g_variant_type_equal(type, G_VARIANT_TYPE_VARIANT)) {
GVariant *var = g_variant_get_variant(value);
result = toQVariant(var);
@@ -110,34 +133,12 @@ QVariant Converter::toQVariant(GVariant *value)
* G_VARIANT_TYPE_UNIT
* G_VARIANT_TYPE_DICT_ENTRY
* G_VARIANT_TYPE_DICTIONARY
- * G_VARIANT_TYPE_STRING_ARRAY
- * G_VARIANT_TYPE_BYTESTRING
* G_VARIANT_TYPE_OBJECT_PATH_ARRAY
- * G_VARIANT_TYPE_BYTESTRING_ARRAY
*/
return result;
}
-static GVariant* toGVariant(const QString &typeName, const QVariant &value)
-{
- if (typeName == "uchar") {
- return g_variant_new_byte(value.value<uchar>());
- } else if (typeName == "short") {
- return g_variant_new_int16(value.value<short>());
- } else if (typeName == "ushort") {
- return g_variant_new_uint16(value.value<ushort>());
- } else if (typeName == "long") {
- return g_variant_new_int64(value.value<long>());
- } else if (typeName == "ulong") {
- return g_variant_new_uint64(value.value<ulong>());
- } else {
- qWarning() << "QVariant type not supported:" << typeName;
- }
-
- return NULL;
-}
-
QVariant Converter::toQVariantFromVariantString(const QString &variantString)
{
GVariant *gvariant;
@@ -147,7 +148,7 @@ QVariant Converter::toQVariantFromVariantString(const QString &variantString)
return QVariant();
}
- gvariant = g_variant_parse (NULL, variantString.toUtf8().data(), NULL, NULL, &error);
+ gvariant = g_variant_parse (NULL, qUtf8Printable(variantString), NULL, NULL, &error);
if (error) {
qWarning() << "Impossible to parse" << variantString << "as variant string:"<< error->message;
@@ -184,7 +185,7 @@ GVariant* Converter::toGVariant(const QVariant &value)
result = g_variant_new_int64(value.toLongLong());
break;
case QVariant::String:
- result = g_variant_new_string(value.toString().toUtf8().data());
+ result = g_variant_new_string(qUtf8Printable(value.toString()));
break;
case QVariant::UInt:
result = g_variant_new_uint32(value.toUInt());
@@ -192,6 +193,21 @@ GVariant* Converter::toGVariant(const QVariant &value)
case QVariant::ULongLong:
result = g_variant_new_uint64(value.toULongLong());
break;
+ case QMetaType::UChar:
+ result = g_variant_new_byte(value.value<uchar>());
+ break;
+ case QMetaType::Short:
+ result = g_variant_new_int16(value.value<short>());
+ break;
+ case QMetaType::UShort:
+ result = g_variant_new_uint16(value.value<ushort>());
+ break;
+ case QMetaType::Long:
+ result = g_variant_new_int64(value.value<long>());
+ break;
+ case QMetaType::ULong:
+ result = g_variant_new_uint64(value.value<ulong>());
+ break;
case QVariant::Map:
{
GVariantBuilder *b;
@@ -201,7 +217,19 @@ GVariant* Converter::toGVariant(const QVariant &value)
QMapIterator<QString, QVariant> i(value.toMap());
while (i.hasNext()) {
i.next();
- g_variant_builder_add(b, "{sv}", i.key().toUtf8().data(), toGVariant(i.value()));
+ g_variant_builder_add(b, "{sv}", qUtf8Printable(i.key()), toGVariant(i.value()));
+ }
+ result = g_variant_builder_end(b);
+ g_variant_builder_unref(b);
+ break;
+ }
+ case QMetaType::QByteArrayList:
+ {
+ const QByteArrayList &list = qvariant_cast<QByteArrayList>(value);
+ GVariantBuilder *b = g_variant_builder_new(G_VARIANT_TYPE_BYTESTRING_ARRAY);
+
+ for (const QByteArray &ba : list) {
+ g_variant_builder_add_value(b, g_variant_new_bytestring(ba));
}
result = g_variant_builder_end(b);
g_variant_builder_unref(b);
@@ -209,17 +237,28 @@ GVariant* Converter::toGVariant(const QVariant &value)
}
case QVariant::List:
{
- QVariantList lst = value.toList();
- GVariant **vars = g_new(GVariant*, lst.size());
- for (int i=0; i < lst.size(); i++) {
- vars[i] = toGVariant(lst[i]);
+ GVariantBuilder *b = g_variant_builder_new(G_VARIANT_TYPE_TUPLE);
+
+ for (const QVariant &v : value.toList()) {
+ g_variant_builder_add_value(b, toGVariant(v));
}
- result = g_variant_new_tuple(vars, lst.size());
- g_free(vars);
+ result = g_variant_builder_end(b);
+ g_variant_builder_unref(b);
+ break;
+ }
+ case QVariant::StringList:
+ {
+ GVariantBuilder *b = g_variant_builder_new(G_VARIANT_TYPE_STRING_ARRAY);
+
+ for (const QString &s : value.toStringList()) {
+ g_variant_builder_add(b, "s", qUtf8Printable(s));
+ }
+ result = g_variant_builder_end(b);
+ g_variant_builder_unref(b);
break;
}
default:
- result = ::toGVariant(value.typeName(), value);
+ qWarning() << "QVariant type not supported:" << value.type();
}
return result;
@@ -273,7 +312,7 @@ GVariant* Converter::toGVariantWithSchema(const QVariant &value, const char* sch
}
} else if (g_variant_type_equal(schema_type, G_VARIANT_TYPE_STRING)) {
if (value.canConvert<QString>()) {
- result = g_variant_new_string(value.toString().toUtf8().data());
+ result = g_variant_new_string(qUtf8Printable(value.toString()));
}
} else if (g_variant_type_equal(schema_type, G_VARIANT_TYPE_VARIANT)) {
result = Converter::toGVariant(value);
@@ -283,70 +322,55 @@ GVariant* Converter::toGVariantWithSchema(const QVariant &value, const char* sch
}
} else if (g_variant_type_is_array(schema_type)) {
if (value.canConvert(QVariant::List)) {
+ const GVariantType* entryType = g_variant_type_element(schema_type);
+ const gchar* entryTypeString = g_variant_type_peek_string(entryType);
- 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());
-
+ GVariantBuilder *b = g_variant_builder_new(G_VARIANT_TYPE_ARRAY);
bool ok = true;
- for (int i=0; i < lst.size(); i++) {
- data = Converter::toGVariantWithSchema(lst[i], entryTypeString);
+
+ for (const QVariant &v : value.toList()) {
+ GVariant *data = toGVariantWithSchema(v, entryTypeString);
if (data) {
- vars[i] = data;
- }
- else {
+ g_variant_builder_add_value(b, 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());
+ result = g_variant_builder_end(b);
}
- g_free(entryTypeString);
- g_free(vars);
+ g_variant_builder_unref(b);
}
} 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);
+ const GVariantType* entryType = g_variant_type_first(schema_type);
+ GVariantBuilder *b = g_variant_builder_new(G_VARIANT_TYPE_TUPLE);
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);
+ for (const QVariant &v : value.toList()) {
+ gchar* entryTypeString = g_variant_type_dup_string(entryType);
+ GVariant *data = toGVariantWithSchema(v, entryTypeString);
+ g_free(entryTypeString);
if (data) {
- vars[i] = data;
- }
- else {
+ g_variant_builder_add_value(b, data);
+ entryType = g_variant_type_next(entryType);
+ if (!entryType)
+ break;
+ } 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) {
+ qWarning() << "Failed to convert list to array with schema:" << schema;
break;
}
}
if (ok) {
- result = g_variant_new_tuple(vars, lst.size());
+ result = g_variant_builder_end(b);
}
- g_free(vars);
+ g_variant_builder_unref(b);
}
}