1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
|
// This is the implementation of the various Chimera helpers.
//
// Copyright (c) 2018 Riverbank Computing Limited <info@riverbankcomputing.com>
//
// This file is part of PyQt5.
//
// This file may be used under the terms of the GNU General Public License
// version 3.0 as published by the Free Software Foundation and appearing in
// the file LICENSE included in the packaging of this file. Please review the
// following information to ensure the GNU General Public License version 3.0
// requirements will be met: http://www.gnu.org/copyleft/gpl.html.
//
// If you do not wish to use this file under the terms of the GPL version 3.0
// then you may purchase a commercial license. For more information contact
// info@riverbankcomputing.com.
//
// This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
// WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
#include <Python.h>
#include <QDBusArgument>
#include <QDBusObjectPath>
#include <QDBusSignature>
#include <QDBusVariant>
#include <QMetaType>
#include "qpydbus_chimera_helpers.h"
#include "sipAPIQtDBus.h"
// Forward declarations.
static PyObject *from_variant_type(const QDBusArgument &arg);
static PyObject *from_array_type(const QDBusArgument &arg);
static PyObject *from_structure_type(const QDBusArgument &arg);
static PyObject *from_map_type(const QDBusArgument &arg);
static PyObject *from_qstring(const QString &arg);
static PyObject *from_qvariant(const QVariant &arg);
// Convert a QVariant to a Python object.
bool qpydbus_from_qvariant_convertor(const QVariant &var, PyObject **objp)
{
// Handle QDBusObjectPath.
if (var.userType() == qMetaTypeId<QDBusObjectPath>())
{
*objp = from_qstring(var.value<QDBusObjectPath>().path());
return true;
}
// Handle QDBusSignature.
if (var.userType() == qMetaTypeId<QDBusSignature>())
{
*objp = from_qstring(var.value<QDBusSignature>().signature());
return true;
}
// Handle QDBusVariant.
if (var.userType() == qMetaTypeId<QDBusVariant>())
{
*objp = from_qvariant(var.value<QDBusVariant>().variant());
return true;
}
// Anything else must be a QDBusArgument.
if (var.userType() != qMetaTypeId<QDBusArgument>())
return false;
QDBusArgument arg = var.value<QDBusArgument>();
switch (arg.currentType())
{
case QDBusArgument::BasicType:
*objp = from_qvariant(arg.asVariant());
break;
case QDBusArgument::VariantType:
*objp = from_variant_type(arg);
break;
case QDBusArgument::ArrayType:
*objp = from_array_type(arg);
break;
case QDBusArgument::StructureType:
*objp = from_structure_type(arg);
break;
case QDBusArgument::MapType:
*objp = from_map_type(arg);
break;
default:
PyErr_Format(PyExc_TypeError, "unsupported DBus argument type %d",
(int)arg.currentType());
*objp = 0;
}
return true;
}
// Convert a QDBusArgument variant type to a Python object.
static PyObject *from_variant_type(const QDBusArgument &arg)
{
QDBusVariant dbv;
arg >> dbv;
return from_qvariant(dbv.variant());
}
// Convert a QDBusArgument array type to a Python object.
static PyObject *from_array_type(const QDBusArgument &arg)
{
QVariantList vl;
arg.beginArray();
while (!arg.atEnd())
vl.append(arg.asVariant());
arg.endArray();
PyObject *obj = PyList_New(vl.count());
if (!obj)
return 0;
for (int i = 0; i < vl.count(); ++i)
{
PyObject *itm = from_qvariant(vl.at(i));
if (!itm)
{
Py_DECREF(obj);
return 0;
}
PyList_SetItem(obj, i, itm);
}
return obj;
}
// Convert a QDBusArgument structure type to a Python object.
static PyObject *from_structure_type(const QDBusArgument &arg)
{
QVariantList vl;
arg.beginStructure();
while (!arg.atEnd())
vl.append(arg.asVariant());
arg.endStructure();
PyObject *obj = PyTuple_New(vl.count());
if (!obj)
return 0;
for (int i = 0; i < vl.count(); ++i)
{
PyObject *itm = from_qvariant(vl.at(i));
if (!itm)
{
Py_DECREF(obj);
return 0;
}
PyTuple_SetItem(obj, i, itm);
}
return obj;
}
// Convert a QDBusArgument map type to a Python object.
static PyObject *from_map_type(const QDBusArgument &arg)
{
PyObject *obj = PyDict_New();
if (!obj)
return 0;
arg.beginMap();
while (!arg.atEnd())
{
arg.beginMapEntry();
PyObject *key = from_qvariant(arg.asVariant());
PyObject *value = from_qvariant(arg.asVariant());
arg.endMapEntry();
if (!key || !value)
{
Py_XDECREF(key);
Py_XDECREF(value);
Py_DECREF(obj);
return 0;
}
int rc = PyDict_SetItem(obj, key, value);
Py_DECREF(key);
Py_DECREF(value);
if (rc < 0)
{
Py_DECREF(obj);
return 0;
}
}
arg.endMap();
return obj;
}
// Convert a QString to a Python object.
static PyObject *from_qstring(const QString &arg)
{
QString *heap = new QString(arg);
PyObject *obj = sipConvertFromNewType(heap, sipType_QString, 0);
if (!obj)
delete heap;
return obj;
}
// Convert a QVariant to a Python object.
static PyObject *from_qvariant(const QVariant &arg)
{
QVariant *heap = new QVariant(arg);
PyObject *obj = sipConvertFromNewType(heap, sipType_QVariant, 0);
if (!obj)
delete heap;
return obj;
}
|