accounts-qt  1.2
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 namespace Accounts {
28 
29 QVariant gvalueToVariant(const GValue *value)
30 {
31  GType type = G_VALUE_TYPE(value);
32 
33  QVariant variant;
34 
35  switch (type)
36  {
37  case G_TYPE_STRING:
38  variant = UTF8(g_value_get_string(value));
39  break;
40  case G_TYPE_INT:
41  variant = g_value_get_int(value);
42  break;
43  case G_TYPE_UINT:
44  variant = g_value_get_uint(value);
45  break;
46  case G_TYPE_INT64:
47  variant = qint64(g_value_get_int64(value));
48  break;
49  case G_TYPE_UINT64:
50  variant = quint64(g_value_get_uint64(value));
51  break;
52  case G_TYPE_BOOLEAN:
53  variant = bool(g_value_get_boolean(value));
54  break;
55  default:
56  qWarning() << "Unsupported type" << UTF8(G_VALUE_TYPE_NAME(value));
57  break;
58  }
59 
60  return variant;
61 }
62 
63 bool variantToGValue(const QVariant &variant, GValue *value)
64 {
65  QByteArray tmpvalue;
66 
67  switch (variant.type())
68  {
69  case QVariant::String:
70  g_value_init(value, G_TYPE_STRING);
71  tmpvalue = variant.toString().toUtf8();
72  g_value_set_string(value, tmpvalue.constData());
73  break;
74  case QVariant::Int:
75  g_value_init(value, G_TYPE_INT);
76  g_value_set_int(value, variant.toInt());
77  break;
78  case QVariant::UInt:
79  g_value_init(value, G_TYPE_UINT);
80  g_value_set_uint(value, variant.toUInt());
81  break;
82  case QVariant::LongLong:
83  g_value_init(value, G_TYPE_INT64);
84  g_value_set_int64(value, variant.toLongLong());
85  break;
86  case QVariant::ULongLong:
87  g_value_init(value, G_TYPE_UINT64);
88  g_value_set_uint64(value, variant.toULongLong());
89  break;
90  case QVariant::Bool:
91  g_value_init(value, G_TYPE_BOOLEAN);
92  g_value_set_boolean(value, variant.toBool());
93  break;
94  default:
95  qWarning() << "Unsupported datatype" << variant.typeName();
96  return false;
97  }
98 
99  return true;
100 }
101 
102 }; // namespace