accounts-qt 1.10

- Main Page
- Related Pages
- Classes
- Files

- File List

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 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
29 namespace Accounts {
30
31 static QVariantMap gVariantToQVariantMap(GVariant *variant)
32 {
33 QVariantMap ret;
34 GVariantIter iter;
35 const 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 }
42
43 return ret;
44 }
45
46 static GVariant *qStringListToGVariant(const QStringList &stringList)
47 {
48 GVariantBuilder builder;
49
50 g_variant_builder_init(&builder, G_VARIANT_TYPE_STRING_ARRAY);
51 Q_FOREACH (const QString &string, stringList) {
52 g_variant_builder_add(&builder, "s", string.toUtf8().constData());
53 }
54 return g_variant_builder_end(&builder);
55 }
56
57 static QStringList gVariantToQStringList(GVariant *variant)
58 {
59 QStringList ret;
60
61 gsize length;
62 const gchar **strings = g_variant_get_strv(variant, &length);
63 for (gsize i = 0; i < length; i++) {
64 ret.append(UTF8(strings[i]));
65 }
66
67 g_free(strings);
68 return ret;
69 }
70
71 QVariant gVariantToQVariant(GVariant *value)
72 {
73 GVariantClass variantClass = g_variant_classify(value);
74
75 QVariant variant;
76
77 switch (variantClass)
78 {
79 case G_VARIANT_CLASS_STRING:
80 variant = UTF8(g_variant_get_string(value, NULL));
81 break;
82 case G_VARIANT_CLASS_INT32:
83 variant = g_variant_get_int32(value);
84 break;
85 case G_VARIANT_CLASS_UINT32:
86 variant = g_variant_get_uint32(value);
87 break;
88 case G_VARIANT_CLASS_INT64:
89 variant = qint64(g_variant_get_int64(value));
90 break;
91 case G_VARIANT_CLASS_UINT64:
92 variant = quint64(g_variant_get_uint64(value));
93 break;
94 case G_VARIANT_CLASS_BOOLEAN:
95 variant = bool(g_variant_get_boolean(value));
96 break;
97 case G_VARIANT_CLASS_ARRAY:
98 if (g_variant_is_of_type(value, G_VARIANT_TYPE_VARDICT)) {
99 variant = gVariantToQVariantMap(value);
100 break;
101 } else if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING_ARRAY)) {
102 variant = gVariantToQStringList(value);
103 break;
104 }
105 // fall through
106 default:
107 qWarning() << "Unsupported type" << UTF8(g_variant_get_type_string(value));
108 break;
109 }
110
111 return variant;
112 }
113
114 GVariant *qVariantToGVariant(const QVariant &variant)
115 {
116 GVariant *ret = 0;
117 QByteArray tmpvalue;
118
119 switch (variant.type())
120 {
121 case QVariant::String:
122 tmpvalue = variant.toString().toUtf8();
123 ret = g_variant_new_string(tmpvalue.constData());
124 break;
125 case QVariant::Int:
126 ret = g_variant_new_int32(variant.toInt());
127 break;
128 case QVariant::UInt:
129 ret = g_variant_new_uint32(variant.toUInt());
130 break;
131 case QVariant::LongLong:
132 ret = g_variant_new_int64(variant.toLongLong());
133 break;
134 case QVariant::ULongLong:
135 ret = g_variant_new_uint64(variant.toULongLong());
136 break;
137 case QVariant::Bool:
138 ret = g_variant_new_boolean(variant.toBool());
139 break;
140 case QVariant::StringList:
141 ret = qStringListToGVariant(variant.toStringList());
142 break;
143 default:
144 qWarning() << "Unsupported datatype" << variant.typeName();
145 }
146
147 return ret;
148 }
149
150 }; // namespace