libaccounts-qt  1.1
utils.cpp
00001 /* vi: set et sw=4 ts=4 cino=t0,(0: */
00002 /*
00003  * This file is part of libaccounts-qt
00004  *
00005  * Copyright (C) 2012 Canonical Ltd.
00006  *
00007  * Contact: Alberto Mardegan <alberto.mardegan@canonical.com>
00008  *
00009  * This library is free software; you can redistribute it and/or
00010  * modify it under the terms of the GNU Lesser General Public License
00011  * version 2.1 as published by the Free Software Foundation.
00012  *
00013  * This library is distributed in the hope that it will be useful, but
00014  * WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00016  * Lesser General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU Lesser General Public
00019  * License along with this library; if not, write to the Free Software
00020  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
00021  * 02110-1301 USA
00022  */
00023 
00024 #include "accountscommon.h"
00025 #include "utils.h"
00026 
00027 namespace Accounts {
00028 
00029 QVariant gvalueToVariant(const GValue *value)
00030 {
00031     GType type = G_VALUE_TYPE(value);
00032 
00033     QVariant variant;
00034 
00035     switch (type)
00036     {
00037     case G_TYPE_STRING:
00038         variant = UTF8(g_value_get_string(value));
00039         break;
00040     case G_TYPE_INT:
00041         variant = g_value_get_int(value);
00042         break;
00043     case G_TYPE_UINT:
00044         variant = g_value_get_uint(value);
00045         break;
00046     case G_TYPE_INT64:
00047         variant = qint64(g_value_get_int64(value));
00048         break;
00049     case G_TYPE_UINT64:
00050         variant = quint64(g_value_get_uint64(value));
00051         break;
00052     case G_TYPE_BOOLEAN:
00053         variant = bool(g_value_get_boolean(value));
00054         break;
00055     default:
00056         qWarning() << "Unsupported type" << UTF8(G_VALUE_TYPE_NAME(value));
00057         break;
00058     }
00059 
00060     return variant;
00061 }
00062 
00063 bool variantToGValue(const QVariant &variant, GValue *value)
00064 {
00065     QByteArray tmpvalue;
00066 
00067     switch (variant.type())
00068     {
00069     case QVariant::String:
00070         g_value_init(value, G_TYPE_STRING);
00071         tmpvalue = variant.toString().toUtf8();
00072         g_value_set_string(value, tmpvalue.constData());
00073         break;
00074     case QVariant::Int:
00075         g_value_init(value, G_TYPE_INT);
00076         g_value_set_int(value, variant.toInt());
00077         break;
00078     case QVariant::UInt:
00079         g_value_init(value, G_TYPE_UINT);
00080         g_value_set_uint(value, variant.toUInt());
00081         break;
00082     case QVariant::LongLong:
00083         g_value_init(value, G_TYPE_INT64);
00084         g_value_set_int64(value, variant.toLongLong());
00085         break;
00086     case QVariant::ULongLong:
00087         g_value_init(value, G_TYPE_UINT64);
00088         g_value_set_uint64(value, variant.toULongLong());
00089         break;
00090     case QVariant::Bool:
00091         g_value_init(value, G_TYPE_BOOLEAN);
00092         g_value_set_boolean(value, variant.toBool());
00093         break;
00094     default:
00095         qWarning() << "Unsupported datatype" << variant.typeName();
00096         return false;
00097     }
00098 
00099     return true;
00100 }
00101 
00102 }; // namespace