diff options
author | Marco Trevisan (Treviño) <mail@3v1n0.net> | 2016-10-25 16:50:59 +0200 |
---|---|---|
committer | Marco Trevisan (Treviño) <mail@3v1n0.net> | 2016-10-25 16:50:59 +0200 |
commit | 6d2076c889b4d914672e2bbfd1c218252b9ed1cf (patch) | |
tree | 26a0a54cda65dfcc755b303cfd241b42c6069f18 | |
parent | d25887f16168be0d55e1b9618fe89815c3edf26c (diff) | |
download | qmenumodel-6d2076c889b4d914672e2bbfd1c218252b9ed1cf.tar.gz qmenumodel-6d2076c889b4d914672e2bbfd1c218252b9ed1cf.tar.bz2 qmenumodel-6d2076c889b4d914672e2bbfd1c218252b9ed1cf.zip |
Converter: add support to String Arrays
-rw-r--r-- | libqmenumodel/src/converter.cpp | 23 | ||||
-rw-r--r-- | tests/client/convertertest.cpp | 3 |
2 files changed, 24 insertions, 2 deletions
diff --git a/libqmenumodel/src/converter.cpp b/libqmenumodel/src/converter.cpp index 1b63272..e2a31c0 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,14 @@ 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); @@ -124,7 +133,6 @@ 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_OBJECT_PATH_ARRAY */ @@ -238,6 +246,17 @@ GVariant* Converter::toGVariant(const QVariant &value) 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", s.toUtf8().data()); + } + result = g_variant_builder_end(b); + g_variant_builder_unref(b); + break; + } default: qWarning() << "QVariant type not supported:" << value.type(); } diff --git a/tests/client/convertertest.cpp b/tests/client/convertertest.cpp index 45b104e..3bd1f74 100644 --- a/tests/client/convertertest.cpp +++ b/tests/client/convertertest.cpp @@ -131,6 +131,7 @@ private Q_SLOTS: QTest::newRow("UInt64") << QVariant::fromValue<qulonglong>(42) << QGVariantType(G_VARIANT_TYPE_UINT64); QTest::newRow("Double") << QVariant((double)42.42) << QGVariantType(G_VARIANT_TYPE_DOUBLE); QTest::newRow("String") << QVariant(QString("42")) << QGVariantType(G_VARIANT_TYPE_STRING); + QTest::newRow("String List") << QVariant(QStringList({"42", "42"})) << QGVariantType(G_VARIANT_TYPE_STRING_ARRAY); QTest::newRow("ByteArray") << QVariant(QByteArray("42")) << QGVariantType(G_VARIANT_TYPE_BYTESTRING); QTest::newRow("Map") << QVariant(QVariantMap()) << QGVariantType(G_VARIANT_TYPE_VARDICT); QTest::newRow("Map Filled") << QVariant(QVariantMap({{"fooBar", 0xdeadbeef}})) << QGVariantType(G_VARIANT_TYPE_VARDICT); @@ -264,6 +265,7 @@ private Q_SLOTS: const gchar *byteArray[] = {"42", "53", NULL}; QTest::newRow("ByteArrayList") << QGVariant(g_variant_new_bytestring_array(byteArray, -1)) << (unsigned) QMetaType::QByteArrayList; + QTest::newRow("String List") << QGVariant(g_variant_new_strv(byteArray, -1)) << (unsigned) QVariant::StringList; } void testConvertToQVariant() @@ -290,6 +292,7 @@ private Q_SLOTS: QTest::newRow("Double") << "double 65" << (unsigned) QVariant::Double; QTest::newRow("String") << "string '65'" << (unsigned) QVariant::String; QTest::newRow("String simple") << "\"65\"" << (unsigned) QVariant::String; + QTest::newRow("String List") << "['foo', 'bar']" << (unsigned) QVariant::StringList; QTest::newRow("Byte string") << "b'fooo'" << (unsigned) QVariant::ByteArray; QTest::newRow("Map") << "{'foo': <65>}" << (unsigned) QVariant::Map; QTest::newRow("List") << "[65, 66]" << (unsigned) QVariant::List; |