accounts-qt: Accounts/utils.cpp Source File

accounts-qt 1.17

utils.cpp
1/* vi: set et sw=4 ts=4 cino=t0,(0: */
2/*
3 * This file is part of libaccounts-qt
4 *
5 * Copyright (C) 2012-2016 Canonical Ltd.
6 *
7 * Contact: Alberto Mardegan <alberto.mardegan@canonical.com>
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * version 2.1 as published by the Free Software Foundation.
12 *
13 * This library is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
21 * 02110-1301 USA
22 */
23
24#include "accountscommon.h"
25#include "utils.h"
26
27#include <QStringList>
28
29namespace Accounts {
30
31static QVariantMap gVariantToQVariantMap(GVariant *variant)
32{
33 QVariantMap ret;
34 GVariantIter iter;
35 gchar *key;
36 GVariant *value;
37
38 g_variant_iter_init (&iter, variant);
39 while (g_variant_iter_next (&iter, "{sv}", &key, &value)) {
40 ret.insert(UTF8(key), gVariantToQVariant(value));
41 g_variant_unref(value);
42 g_free(key);
43 }
44
45 return ret;
46}
47
48static GVariant *qStringListToGVariant(const QStringList &stringList)
49{
50 GVariantBuilder builder;
51
52 g_variant_builder_init(&builder, G_VARIANT_TYPE_STRING_ARRAY);
53 Q_FOREACH (const QString &string, stringList) {
54 g_variant_builder_add(&builder, "s", string.toUtf8().constData());
55 }
56 return g_variant_builder_end(&builder);
57}
58
59static QStringList gVariantToQStringList(GVariant *variant)
60{
61 QStringList ret;
62
63 gsize length;
64 const gchar **strings = g_variant_get_strv(variant, &length);
65 ret.reserve(length);
66 for (gsize i = 0; i < length; i++) {
67 ret.append(UTF8(strings[i]));
68 }
69
70 g_free(strings);
71 return ret;
72}
73
74QVariant gVariantToQVariant(GVariant *value)
75{
76 GVariantClass variantClass = g_variant_classify(value);
77
78 QVariant variant;
79
80 switch (variantClass)
81 {
82 case G_VARIANT_CLASS_STRING:
83 variant = UTF8(g_variant_get_string(value, NULL));
84 break;
85 case G_VARIANT_CLASS_INT32:
86 variant = g_variant_get_int32(value);
87 break;
88 case G_VARIANT_CLASS_UINT32:
89 variant = g_variant_get_uint32(value);
90 break;
91 case G_VARIANT_CLASS_INT64:
92 variant = qint64(g_variant_get_int64(value));
93 break;
94 case G_VARIANT_CLASS_UINT64:
95 variant = quint64(g_variant_get_uint64(value));
96 break;
97 case G_VARIANT_CLASS_BOOLEAN:
98 variant = bool(g_variant_get_boolean(value));
99 break;
100 case G_VARIANT_CLASS_ARRAY:
101 if (g_variant_is_of_type(value, G_VARIANT_TYPE_VARDICT)) {
102 variant = gVariantToQVariantMap(value);
103 break;
104 } else if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING_ARRAY)) {
105 variant = gVariantToQStringList(value);
106 break;
107 }
108 // fall through
109 default:
110 qWarning() << "Unsupported type" << UTF8(g_variant_get_type_string(value));
111 break;
112 }
113
114 return variant;
115}
116
117GVariant *qVariantToGVariant(const QVariant &variant)
118{
119 GVariant *ret = nullptr;
120 QByteArray tmpvalue;
121
122 switch (variant.type())
123 {
124 case QVariant::String:
125 tmpvalue = variant.toString().toUtf8();
126 ret = g_variant_new_string(tmpvalue.constData());
127 break;
128 case QVariant::Int:
129 ret = g_variant_new_int32(variant.toInt());
130 break;
131 case QVariant::UInt:
132 ret = g_variant_new_uint32(variant.toUInt());
133 break;
134 case QVariant::LongLong:
135 ret = g_variant_new_int64(variant.toLongLong());
136 break;
137 case QVariant::ULongLong:
138 ret = g_variant_new_uint64(variant.toULongLong());
139 break;
140 case QVariant::Bool:
141 ret = g_variant_new_boolean(variant.toBool());
142 break;
143 case QVariant::StringList:
144 ret = qStringListToGVariant(variant.toStringList());
145 break;
146 default:
147 qWarning() << "Unsupported datatype" << variant.typeName();
148 }
149
150 return ret;
151}
152
153}; // namespace