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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
|
/* qdbusmarshall.cpp
*
* Copyright (C) 2005 Harald Fernengel <harry@kdevelop.org>
* Copyright (C) 2006 Trolltech AS. All rights reserved.
* Author: Thiago Macieira <thiago.macieira@trolltech.com>
*
* Licensed under the Academic Free License version 2.1
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include "qdbusmarshall_p.h"
#include "qdbustype_p.h"
#include "qdbustypehelper_p.h"
#include <qdebug.h>
#include <qvariant.h>
#include <qlist.h>
#include <qmap.h>
#include <qstringlist.h>
#include <qvarlengtharray.h>
#include <qvector.h>
#include <dbus/dbus.h>
static QVariant qFetchParameter(DBusMessageIter *it);
template <typename T>
inline T qIterGet(DBusMessageIter *it)
{
T t;
dbus_message_iter_get_basic(it, &t);
return t;
}
template<>
inline QVariant qIterGet(DBusMessageIter *it)
{
DBusMessageIter sub;
dbus_message_iter_recurse(it, &sub);
return QDBusTypeHelper<QVariant>::toVariant(qFetchParameter(&sub));
}
template <typename DBusType, typename QtType>
inline QVariant qFetchList(DBusMessageIter *arrayIt)
{
QList<QtType> list;
DBusMessageIter it;
dbus_message_iter_recurse(arrayIt, &it);
if (dbus_message_iter_get_array_len(&it) == 0)
return QDBusTypeHelper<QList<QtType> >::toVariant(list);
do {
list.append( static_cast<QtType>( qIterGet<DBusType>(&it) ) );
} while (dbus_message_iter_next(&it));
return QDBusTypeHelper<QList<QtType> >::toVariant(list);
}
static QStringList qFetchStringList(DBusMessageIter *arrayIt)
{
QStringList list;
DBusMessageIter it;
dbus_message_iter_recurse(arrayIt, &it);
if (dbus_message_iter_get_array_len(&it) == 0)
return list;
do {
list.append(QString::fromUtf8(qIterGet<char *>(&it)));
} while (dbus_message_iter_next(&it));
return list;
}
static QVariant qFetchParameter(DBusMessageIter *it)
{
switch (dbus_message_iter_get_arg_type(it)) {
case DBUS_TYPE_BYTE:
return qVariantFromValue(qIterGet<unsigned char>(it));
case DBUS_TYPE_INT16:
return qVariantFromValue(qIterGet<dbus_int16_t>(it));
case DBUS_TYPE_UINT16:
return qVariantFromValue(qIterGet<dbus_uint16_t>(it));
case DBUS_TYPE_INT32:
return qIterGet<dbus_int32_t>(it);
case DBUS_TYPE_UINT32:
return qIterGet<dbus_uint32_t>(it);
case DBUS_TYPE_DOUBLE:
return qIterGet<double>(it);
case DBUS_TYPE_BOOLEAN:
return bool(qIterGet<dbus_bool_t>(it));
case DBUS_TYPE_INT64:
return static_cast<qlonglong>(qIterGet<dbus_int64_t>(it));
case DBUS_TYPE_UINT64:
return static_cast<qulonglong>(qIterGet<dbus_uint64_t>(it));
case DBUS_TYPE_STRING:
case DBUS_TYPE_OBJECT_PATH:
case DBUS_TYPE_SIGNATURE:
return QString::fromUtf8(qIterGet<char *>(it));
case DBUS_TYPE_VARIANT:
return qIterGet<QVariant>(it);
case DBUS_TYPE_ARRAY: {
int arrayType = dbus_message_iter_get_element_type(it);
switch (arrayType)
{
case DBUS_TYPE_BYTE: {
// QByteArray
DBusMessageIter sub;
dbus_message_iter_recurse(it, &sub);
int len = dbus_message_iter_get_array_len(&sub);
char* data;
dbus_message_iter_get_fixed_array(&sub,&data,&len);
return QByteArray(data,len);
}
case DBUS_TYPE_INT16:
return qFetchList<dbus_int16_t, short>(it);
case DBUS_TYPE_UINT16:
return qFetchList<dbus_uint16_t, ushort>(it);
case DBUS_TYPE_INT32:
return qFetchList<dbus_int32_t, int>(it);
case DBUS_TYPE_UINT32:
return qFetchList<dbus_uint32_t, uint>(it);
case DBUS_TYPE_BOOLEAN:
return qFetchList<dbus_bool_t, bool>(it);
case DBUS_TYPE_DOUBLE:
return qFetchList<double, double>(it);
case DBUS_TYPE_INT64:
return qFetchList<dbus_int64_t, qlonglong>(it);
case DBUS_TYPE_UINT64:
return qFetchList<dbus_uint64_t, qulonglong>(it);
case DBUS_TYPE_STRING:
case DBUS_TYPE_OBJECT_PATH:
case DBUS_TYPE_SIGNATURE:
return qFetchStringList(it);
case DBUS_TYPE_VARIANT:
return qFetchList<QVariant, QVariant>(it);
case DBUS_TYPE_DICT_ENTRY: {
// ### support other types of maps?
QMap<QString, QVariant> map;
DBusMessageIter sub;
dbus_message_iter_recurse(it, &sub);
if (dbus_message_iter_get_array_len(&sub) == 0)
// empty map
return map;
do {
DBusMessageIter itemIter;
dbus_message_iter_recurse(&sub, &itemIter);
Q_ASSERT(dbus_message_iter_has_next(&itemIter));
QString key = qFetchParameter(&itemIter).toString();
dbus_message_iter_next(&itemIter);
map.insertMulti(key, qFetchParameter(&itemIter));
} while (dbus_message_iter_next(&sub));
return map;
}
}
}
// fall through
// common handling for structs and lists of lists (for now)
case DBUS_TYPE_STRUCT: {
QList<QVariant> list;
DBusMessageIter sub;
dbus_message_iter_recurse(it, &sub);
if (dbus_message_iter_get_array_len(&sub) == 0)
return list;
do {
list.append(qFetchParameter(&sub));
} while (dbus_message_iter_next(&sub));
return list;
}
default:
qWarning("Don't know how to handle type %d '%c'", dbus_message_iter_get_arg_type(it), dbus_message_iter_get_arg_type(it));
return QVariant();
break;
}
}
void QDBusMarshall::messageToList(QList<QVariant> &list, DBusMessage *message)
{
Q_ASSERT(message);
DBusMessageIter it;
if (!dbus_message_iter_init(message, &it))
return;
do {
list.append(qFetchParameter(&it));
} while (dbus_message_iter_next(&it));
}
// convert the variant to the given type and return true if it worked.
// if the type is not known, guess it from the variant and set.
// return false if conversion failed.
static bool checkType(QVariant &var, QDBusType &type)
{
if (!type.isValid()) {
// guess it from the variant
type = QDBusType::guessFromVariant(var);
return true;
}
int id = var.userType();
if (type.dbusType() == DBUS_TYPE_VARIANT) {
// this is a non symmetrical operation:
// nest a QVariant if we want variant and it isn't so
if (id != QDBusTypeHelper<QVariant>::id()) {
QVariant tmp = var;
var = QDBusTypeHelper<QVariant>::toVariant(tmp);
}
return true;
}
switch (id) {
case QVariant::Bool:
case QMetaType::Short:
case QMetaType::UShort:
case QMetaType::UChar:
case QVariant::Int:
case QVariant::UInt:
case QVariant::LongLong:
case QVariant::ULongLong:
case QVariant::Double:
case QVariant::String:
if (type.isBasic())
// QVariant can handle this on its own
return true;
// cannot handle this
qWarning("Invalid conversion from %s to '%s'", var.typeName(),
type.dbusSignature().constData());
var.clear();
return false;
case QVariant::ByteArray:
// make sure it's an "ARRAY of BYTE"
if (type.qvariantType() != QVariant::ByteArray) {
qWarning("Invalid conversion from %s to '%s'", var.typeName(),
type.dbusSignature().constData());
var.clear();
return false;
}
return true;
case QVariant::StringList:
// make sure it's "ARRAY of STRING"
if (type.qvariantType() != QVariant::StringList) {
qWarning("Invalid conversion from %s to '%s'", var.typeName(),
type.dbusSignature().constData());
var.clear();
return false;
}
return true;
case QVariant::List:
// could be either struct or array
if (type.dbusType() != DBUS_TYPE_ARRAY && type.dbusType() != DBUS_TYPE_STRUCT) {
qWarning("Invalid conversion from %s to '%s'", var.typeName(),
type.dbusSignature().constData());
var.clear();
return false;
}
return true;
case QVariant::Map:
if (!type.isMap()) {
qWarning("Invalid conversion from %s to '%s'", var.typeName(),
type.dbusSignature().constData());
var.clear();
return false;
}
return true;
case QVariant::Invalid: {
// create an empty variant
void *null = 0;
var = QVariant(type.qvariantType(), null);
break;
}
default:
if (id == QDBusTypeHelper<QVariant>::id()) {
// if we got here, it means the match above for DBUS_TYPE_VARIANT didn't work
qWarning("Invalid conversion from nested variant to '%s'",
type.dbusSignature().constData());
return false;
} else if (type.dbusType() == DBUS_TYPE_ARRAY) {
int subType = type.arrayElement().dbusType();
if ((id == QDBusTypeHelper<bool>::listId() && subType == DBUS_TYPE_BOOLEAN) ||
(id == QDBusTypeHelper<short>::listId() && subType == DBUS_TYPE_INT16) ||
(id == QDBusTypeHelper<ushort>::listId() && subType == DBUS_TYPE_UINT16) ||
(id == QDBusTypeHelper<int>::listId() && subType == DBUS_TYPE_INT32) ||
(id == QDBusTypeHelper<uint>::listId() && subType == DBUS_TYPE_UINT32) ||
(id == QDBusTypeHelper<qlonglong>::listId() && subType == DBUS_TYPE_INT64) ||
(id == QDBusTypeHelper<qulonglong>::listId() && subType == DBUS_TYPE_UINT64) ||
(id == QDBusTypeHelper<double>::listId() && subType == DBUS_TYPE_DOUBLE))
return true;
}
qWarning("Invalid conversion from %s to '%s'", var.typeName(),
type.dbusSignature().constData());
return false;
}
qWarning("Found unknown QVariant type %d (%s) when converting to DBus", (int)var.type(),
var.typeName());
var.clear();
return false;
}
static void qVariantToIteratorInternal(DBusMessageIter *it, const QVariant &var,
const QDBusType &type);
static void qListToIterator(DBusMessageIter *it, const QList<QVariant> &list,
const QDBusTypeList &typelist);
template<typename T>
static void qIterAppend(DBusMessageIter *it, const QDBusType &type, T arg)
{
dbus_message_iter_append_basic(it, type.dbusType(), &arg);
}
template<typename DBusType, typename QtType>
static void qAppendListToMessage(DBusMessageIter *it, const QDBusType &subType,
const QVariant &var)
{
QList<QtType> list = QDBusTypeHelper<QList<QtType> >::fromVariant(var);
foreach (const QtType &item, list)
qIterAppend(it, subType, static_cast<DBusType>(item));
}
static void qAppendArrayToMessage(DBusMessageIter *it, const QDBusType &subType,
const QVariant &var)
{
DBusMessageIter sub;
dbus_message_iter_open_container(it, DBUS_TYPE_ARRAY, subType.dbusSignature(), &sub);
switch (var.type())
{
case QVariant::StringList: {
const QStringList list = var.toStringList();
foreach (QString str, list)
qIterAppend(&sub, subType, str.toUtf8().constData());
break;
}
case QVariant::ByteArray: {
const QByteArray array = var.toByteArray();
const char* cdata = array.constData();
dbus_message_iter_append_fixed_array(&sub, DBUS_TYPE_BYTE, &cdata, array.length());
break;
}
case QVariant::Map: {
const QVariantMap map = var.toMap();
const QDBusTypeList& subTypes = subType.subTypes();
for (QMap<QString, QVariant>::const_iterator mit = map.constBegin();
mit != map.constEnd(); ++mit) {
DBusMessageIter itemIterator;
dbus_message_iter_open_container(&sub, DBUS_TYPE_DICT_ENTRY, 0, &itemIterator);
// let the string be converted to QVariant
qVariantToIteratorInternal(&itemIterator, mit.key(), subTypes[0]);
qVariantToIteratorInternal(&itemIterator, mit.value(), subTypes[1]);
dbus_message_iter_close_container(&sub, &itemIterator);
}
break;
}
case QVariant::List: {
const QVariantList list = var.toList();
foreach (QVariant v, list)
qVariantToIteratorInternal(&sub, v, subType);
break;
}
default: {
int id = var.userType();
if (id == QDBusTypeHelper<bool>::listId())
qAppendListToMessage<dbus_bool_t,bool>(&sub, subType, var);
else if (id == QDBusTypeHelper<short>::listId())
qAppendListToMessage<dbus_int16_t,short>(&sub, subType, var);
else if (id == QDBusTypeHelper<ushort>::listId())
qAppendListToMessage<dbus_uint16_t,ushort>(&sub, subType, var);
else if (id == QDBusTypeHelper<int>::listId())
qAppendListToMessage<dbus_int32_t,int>(&sub, subType, var);
else if (id == QDBusTypeHelper<uint>::listId())
qAppendListToMessage<dbus_uint32_t,uint>(&sub, subType, var);
else if (id == QDBusTypeHelper<qlonglong>::listId())
qAppendListToMessage<dbus_int64_t,qlonglong>(&sub, subType, var);
else if (id == QDBusTypeHelper<qulonglong>::listId())
qAppendListToMessage<dbus_uint64_t,qulonglong>(&sub, subType, var);
else if (id == QDBusTypeHelper<double>::listId())
qAppendListToMessage<double,double>(&sub, subType, var);
#if 0 // never reached, since QVariant::List mached
else if (id == QDBusTypeHelper<QVariant>::listId())
qAppendListToMessage<QVariant,QVariant>(&sub, subType, var);
#endif
else
qFatal("qAppendArrayToMessage got unknown type!");
break;
}
}
dbus_message_iter_close_container(it, &sub);
}
static void qAppendStructToMessage(DBusMessageIter *it, const QDBusTypeList &typeList,
const QVariantList &list)
{
DBusMessageIter sub;
dbus_message_iter_open_container(it, DBUS_TYPE_STRUCT, NULL, &sub);
qListToIterator(&sub, list, typeList);
dbus_message_iter_close_container(it, &sub);
}
static void qAppendVariantToMessage(DBusMessageIter *it, const QDBusType &type,
const QVariant &var)
{
Q_UNUSED(type); // type is 'v'
QVariant arg = var;
if (var.userType() == QDBusTypeHelper<QVariant>::id())
arg = QDBusTypeHelper<QVariant>::fromVariant(var); // extract the inner variant
QDBusType t = QDBusType::guessFromVariant(arg);
// now add this variant
DBusMessageIter sub;
dbus_message_iter_open_container(it, DBUS_TYPE_VARIANT, t.dbusSignature(), &sub);
qVariantToIteratorInternal(&sub, arg, t);
dbus_message_iter_close_container(it, &sub);
}
static void qVariantToIterator(DBusMessageIter *it, QVariant var, QDBusType type)
{
if (var.isNull() && !type.isValid())
return; // cannot add a null like this
if (!checkType(var, type))
return; // type checking failed
qVariantToIteratorInternal(it, var, type);
}
static void qVariantToIteratorInternal(DBusMessageIter *it, const QVariant &var,
const QDBusType &type)
{
switch (type.dbusType()) {
case DBUS_TYPE_BYTE:
qIterAppend( it, type, QDBusTypeHelper<uchar>::fromVariant(var) );
break;
case DBUS_TYPE_BOOLEAN:
qIterAppend( it, type, static_cast<dbus_bool_t>(var.toBool()) );
break;
case DBUS_TYPE_INT16:
qIterAppend( it, type, QDBusTypeHelper<short>::fromVariant(var) );
break;
case DBUS_TYPE_UINT16:
qIterAppend( it, type, QDBusTypeHelper<ushort>::fromVariant(var) );
break;
case DBUS_TYPE_INT32:
qIterAppend( it, type, static_cast<dbus_int32_t>(var.toInt()) );
break;
case DBUS_TYPE_UINT32:
qIterAppend( it, type, static_cast<dbus_uint32_t>(var.toUInt()) );
break;
case DBUS_TYPE_INT64:
qIterAppend( it, type, static_cast<dbus_int64_t>(var.toLongLong()) );
break;
case DBUS_TYPE_UINT64:
qIterAppend( it, type, static_cast<dbus_uint64_t>(var.toULongLong()) );
break;
case DBUS_TYPE_DOUBLE:
qIterAppend( it, type, var.toDouble() );
break;
case DBUS_TYPE_STRING:
case DBUS_TYPE_OBJECT_PATH:
case DBUS_TYPE_SIGNATURE:
qIterAppend( it, type, var.toString().toUtf8().constData() );
break;
// compound types:
case DBUS_TYPE_ARRAY:
// could be many things
qAppendArrayToMessage( it, type.arrayElement(), var );
break;
case DBUS_TYPE_VARIANT:
qAppendVariantToMessage( it, type, var );
break;
case DBUS_TYPE_STRUCT:
qAppendStructToMessage( it, type.subTypes(), var.toList() );
break;
case DBUS_TYPE_DICT_ENTRY:
qFatal("qVariantToIterator got a DICT_ENTRY!");
break;
default:
qWarning("Found unknown DBus type '%s'", type.dbusSignature().constData());
break;
}
}
void qListToIterator(DBusMessageIter *it, const QList<QVariant> &list)
{
for (int i = 0; i < list.count(); ++i)
qVariantToIterator(it, list.at(i), QDBusType());
}
void qListToIterator(DBusMessageIter *it, const QList<QVariant> &list, const QDBusTypeList &types)
{
int min = qMin(list.count(), types.count());
for (int i = 0; i < min; ++i)
qVariantToIterator(it, list.at(i), types.at(i));
for (int i = min; i < types.count(); ++i)
// we're missing a few arguments, so add default parameters
qVariantToIterator(it, QVariant(), types.at(i));
}
void QDBusMarshall::listToMessage(const QList<QVariant> &list, DBusMessage *msg,
const QString &signature)
{
Q_ASSERT(msg);
DBusMessageIter it;
dbus_message_iter_init_append(msg, &it);
if (signature.isEmpty())
qListToIterator(&it, list);
else
qListToIterator(&it, list, QDBusTypeList(signature.toUtf8()));
}
|