on 1e71a8a help sign in
unity8/plugins/Unity/variantutils.cpp
89 lines 2.9 kB history raw
1/*
2 * Copyright (C) 2013 Canonical, Ltd.
3 *
4 * Authors:
5 * Pawel Stolowski <pawel.stolowski@canonical.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 3.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
20#include "variantutils.h"
22/* Returns floating ref */
23GVariant* GVariantFromQVariant(const QVariant &var)
25 GVariant *gv = nullptr;
27 switch (var.type()) {
28 case QVariant::String:
29 gv = g_variant_new_string(var.toString().toUtf8());
30 break;
31 case QVariant::UInt:
32 gv = g_variant_new_uint32(var.toUInt());
33 break;
34 case QVariant::Int:
35 gv = g_variant_new_int32(var.toInt());
36 break;
37 case QVariant::LongLong:
38 gv = g_variant_new_int64(var.toLongLong());
39 break;
40 case QVariant::ULongLong:
41 gv = g_variant_new_uint64(var.toULongLong());
42 break;
43 case QVariant::Double:
44 gv = g_variant_new_double(var.toDouble());
45 break;
46 case QVariant::Bool:
47 gv = g_variant_new_boolean(var.toBool());
48 break;
49 case QVariant::Hash: {
50 const auto hash = var.toHash();
51 GVariant **children = new GVariant*[hash.size()];
52 int cnt = 0;
53 QHash<QString, QVariant>::const_iterator it = hash.constBegin();
54 while (it != hash.constEnd()) {
55 GVariant *key = g_variant_new_string(it.key().toUtf8());
56 children[cnt++] = g_variant_new_dict_entry(key, g_variant_new_variant(GVariantFromQVariant(it.value())));
57 ++it;
58 }
59 gv = g_variant_new_array(G_VARIANT_TYPE("{sv}"), children, hash.size());
60 delete [] children;
61 break;
62 }
63 default:
64 break;
65 }
66 return gv;
70unity::glib::HintsMap convertToHintsMap(const QHash<QString, QVariant> &val)
72 unity::glib::HintsMap hintsMap;
73 QHash<QString, QVariant>::const_iterator it = val.constBegin();
74 while (it != val.constEnd()) {
75 hintsMap[it.key().toStdString()] = GVariantFromQVariant(it.value());
76 ++it;
77 }
78 return hintsMap;
81unity::glib::HintsMap convertToHintsMap(const QVariant &var)
83 if (var.type() == QVariant::Hash) {
84 unity::glib::HintsMap hintsMap;
85 const auto hash = var.toHash();
86 return convertToHintsMap(hash);
87 }
88 return unity::glib::HintsMap();