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
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
|
/* qdbusmessage.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 "qdbusmessage.h"
#include <qdebug.h>
#include <qstringlist.h>
#include <dbus/dbus.h>
#include "qdbuserror.h"
#include "qdbusmarshall_p.h"
#include "qdbusmessage_p.h"
#include "qdbustypehelper_p.h"
QDBusMessagePrivate::QDBusMessagePrivate()
: connection(QString()), msg(0), reply(0), type(DBUS_MESSAGE_TYPE_INVALID),
timeout(-1), ref(1), repliedTo(false)
{
}
QDBusMessagePrivate::~QDBusMessagePrivate()
{
if (msg)
dbus_message_unref(msg);
if (reply)
dbus_message_unref(reply);
}
///////////////
/*!
\class QDBusMessage
\brief Represents one message sent or received over the DBus bus.
This object can represent any of four different types of messages possible on the bus
(see MessageType)
- Method calls
- Method return values
- Signal emissions
- Error codes
Objects of this type are created with the four static functions signal, methodCall,
methodReply and error.
*/
/*!
\enum QDBusMessage::MessageType
The possible message types:
\value MethodCallMessage a message representing an outgoing or incoming method call
\value SignalMessage a message representing an outgoing or incoming signal emission
\value ReplyMessage a message representing the return values of a method call
\value ErrorMessage a message representing an error condition in response to a method call
\value InvalidMessage an invalid message: this is never set on messages received from D-Bus
*/
/*!
Constructs a new DBus message representing a signal emission. A DBus signal is emitted
from one application and is received by all applications that are listening for that signal
from that interface.
The signal will be constructed to represent a signal coming from the path \a path, interface \a
interface and signal name \a name.
The QDBusMessage object that is returned can be sent with QDBusConnection::send().
*/
QDBusMessage QDBusMessage::signal(const QString &path, const QString &interface,
const QString &name)
{
QDBusMessage message;
message.d_ptr->type = DBUS_MESSAGE_TYPE_SIGNAL;
message.d_ptr->path = path;
message.d_ptr->interface = interface;
message.d_ptr->name = name;
return message;
}
/*!
Constructs a new DBus message representing a method call. A method call always informs
its destination address (\a service, \a path, \a interface and \a method).
The DBus bus allows calling a method on a given remote object without specifying the
destination interface, if the method name is unique. However, if two interfaces on the
remote object export the same method name, the result is undefined (one of the two may be
called or an error may be returned).
When using DBus in a peer-to-peer context (i.e., not on a bus), the \a service parameter is
optional.
The QDBusObject and QDBusInterface classes provide a simpler abstraction to synchronous
method calling.
This function returns a QDBusMessage object that can be sent with QDBusConnection::send(),
QDBusConnection::sendWithReply(), or QDBusConnection::sendWithReplyAsync().
*/
QDBusMessage QDBusMessage::methodCall(const QString &service, const QString &path,
const QString &interface, const QString &method)
{
QDBusMessage message;
message.d_ptr->type = DBUS_MESSAGE_TYPE_METHOD_CALL;
message.d_ptr->service = service;
message.d_ptr->path = path;
message.d_ptr->interface = interface;
message.d_ptr->name = method;
return message;
}
/*!
Constructs a new DBus message representing the return values from a called method. The \a other
variable represents the method call that the reply will be for.
This function returns a QDBusMessage object that can be sent with QDBusConnection::send().
*/
QDBusMessage QDBusMessage::methodReply(const QDBusMessage &other)
{
Q_ASSERT(other.d_ptr->msg);
QDBusMessage message;
message.d_ptr->connection = other.d_ptr->connection;
message.d_ptr->type = DBUS_MESSAGE_TYPE_METHOD_RETURN;
message.d_ptr->reply = dbus_message_ref(other.d_ptr->msg);
other.d_ptr->repliedTo = true;
return message;
}
/*!
Constructs a DBus message representing an error condition described by the \a name
parameter. The \a msg parameter is optional and may contain a human-readable description of the
error. The \a other variable represents the method call that this error relates to.
This function returns a QDBusMessage object that can be sent with QDBusMessage::send().
*/
QDBusMessage QDBusMessage::error(const QDBusMessage &other, const QString &name,
const QString &msg)
{
Q_ASSERT(other.d_ptr->msg);
QDBusMessage message;
message.d_ptr->connection = other.d_ptr->connection;
message.d_ptr->type = DBUS_MESSAGE_TYPE_ERROR;
message.d_ptr->name = name;
message.d_ptr->message = msg;
message.d_ptr->reply = dbus_message_ref(other.d_ptr->msg);
other.d_ptr->repliedTo = true;
return message;
}
/*!
\overload
Constructs a DBus message representing an error, where \a other is the method call that
generated this error and \a error is the error code.
*/
QDBusMessage QDBusMessage::error(const QDBusMessage &other, const QDBusError &error)
{
Q_ASSERT(other.d_ptr->msg);
QDBusMessage message;
message.d_ptr->connection = other.d_ptr->connection;
message.d_ptr->type = DBUS_MESSAGE_TYPE_ERROR;
message.d_ptr->name = error.name();
message.d_ptr->message = error.message();
message.d_ptr->reply = dbus_message_ref(other.d_ptr->msg);
other.d_ptr->repliedTo = true;
return message;
}
/*!
Constructs an empty, invalid QDBusMessage object.
\sa methodCall(), methodReply(), signal(), error()
*/
QDBusMessage::QDBusMessage()
{
d_ptr = new QDBusMessagePrivate;
}
/*!
Constructs a copy of the object given by \a other.
*/
QDBusMessage::QDBusMessage(const QDBusMessage &other)
: QList<QVariant>(other)
{
d_ptr = other.d_ptr;
d_ptr->ref.ref();
}
/*!
Disposes of the object and frees any resources that were being held.
*/
QDBusMessage::~QDBusMessage()
{
if (!d_ptr->ref.deref())
delete d_ptr;
}
/*!
Copies the contents of the object given by \a other.
*/
QDBusMessage &QDBusMessage::operator=(const QDBusMessage &other)
{
QList<QVariant>::operator=(other);
qAtomicAssign(d_ptr, other.d_ptr);
return *this;
}
static inline const char *data(const QByteArray &arr)
{
return arr.isEmpty() ? 0 : arr.constData();
}
/*!
\internal
Constructs a DBusMessage object from this object. The returned value must be de-referenced
with dbus_message_unref.
*/
DBusMessage *QDBusMessage::toDBusMessage() const
{
DBusMessage *msg = 0;
switch (d_ptr->type) {
case DBUS_MESSAGE_TYPE_METHOD_CALL:
msg = dbus_message_new_method_call(data(d_ptr->service.toUtf8()), data(d_ptr->path.toUtf8()),
data(d_ptr->interface.toUtf8()), data(d_ptr->name.toUtf8()));
break;
case DBUS_MESSAGE_TYPE_SIGNAL:
msg = dbus_message_new_signal(data(d_ptr->path.toUtf8()), data(d_ptr->interface.toUtf8()),
data(d_ptr->name.toUtf8()));
break;
case DBUS_MESSAGE_TYPE_METHOD_RETURN:
msg = dbus_message_new_method_return(d_ptr->reply);
break;
case DBUS_MESSAGE_TYPE_ERROR:
msg = dbus_message_new_error(d_ptr->reply, data(d_ptr->name.toUtf8()), data(d_ptr->message.toUtf8()));
break;
}
if (!msg)
return 0;
QDBusMarshall::listToMessage(*this, msg, d_ptr->signature);
return msg;
}
/*!
\internal
Constructs a QDBusMessage by parsing the given DBusMessage object.
*/
QDBusMessage QDBusMessage::fromDBusMessage(DBusMessage *dmsg, const QDBusConnection &connection)
{
QDBusMessage message;
if (!dmsg)
return message;
message.d_ptr->connection = connection;
message.d_ptr->type = dbus_message_get_type(dmsg);
message.d_ptr->path = QString::fromUtf8(dbus_message_get_path(dmsg));
message.d_ptr->interface = QString::fromUtf8(dbus_message_get_interface(dmsg));
message.d_ptr->name = message.d_ptr->type == DBUS_MESSAGE_TYPE_ERROR ?
QString::fromUtf8(dbus_message_get_error_name(dmsg)) :
QString::fromUtf8(dbus_message_get_member(dmsg));
message.d_ptr->service = QString::fromUtf8(dbus_message_get_sender(dmsg));
message.d_ptr->signature = QString::fromUtf8(dbus_message_get_signature(dmsg));
message.d_ptr->msg = dbus_message_ref(dmsg);
QDBusMarshall::messageToList(message, dmsg);
return message;
}
/*!
Creates a QDBusMessage that represents the same error as the QDBusError object.
*/
QDBusMessage QDBusMessage::fromError(const QDBusError &error)
{
QDBusMessage message;
message.d_ptr->type = DBUS_MESSAGE_TYPE_ERROR;
message.d_ptr->name = error.name();
message << error.message();
return message;
}
/*!
Returns the path of the object that this message is being sent to (in the case of a
method call) or being received from (for a signal).
*/
QString QDBusMessage::path() const
{
return d_ptr->path;
}
/*!
Returns the interface of the method being called (in the case of a method call) or of
the signal being received from.
*/
QString QDBusMessage::interface() const
{
return d_ptr->interface;
}
/*!
Returns the name of the signal that was emitted or the name of the error that was
received.
\sa member()
*/
QString QDBusMessage::name() const
{
return d_ptr->name;
}
/*!
\fn QDBusMessage::member() const
Returns the name of the method being called.
*/
/*!
\fn QDBusMessage::method() const
\overload
Returns the name of the method being called.
*/
/*!
Returns the name of the service or the bus address of the remote method call.
*/
QString QDBusMessage::service() const
{
return d_ptr->service;
}
/*!
\fn QDBusMessage::sender() const
Returns the unique name of the remote sender.
*/
/*!
Returns the timeout (in milliseconds) for this message to be processed.
*/
int QDBusMessage::timeout() const
{
return d_ptr->timeout;
}
/*!
Sets the timeout for this message to be processed, given by \a ms, in milliseconds.
*/
void QDBusMessage::setTimeout(int ms)
{
qAtomicDetach(d_ptr);
d_ptr->timeout = ms;
}
/*!
Returns the flag that indicates if this message should see a reply or not. This is only
meaningful for MethodCall messages: any other kind of message cannot have replies and this
function will always return false for them.
*/
bool QDBusMessage::noReply() const
{
if (!d_ptr->msg)
return false;
return dbus_message_get_no_reply(d_ptr->msg);
}
/*!
Returns the unique serial number assigned to this message
or 0 if the message was not sent yet.
*/
int QDBusMessage::serialNumber() const
{
if (!d_ptr->msg)
return 0;
return dbus_message_get_serial(d_ptr->msg);
}
/*!
Returns the unique serial number assigned to the message
that triggered this reply message.
If this message is not a reply to another message, 0
is returned.
*/
int QDBusMessage::replySerialNumber() const
{
if (!d_ptr->msg)
return 0;
return dbus_message_get_reply_serial(d_ptr->msg);
}
/*!
Returns true if this is a MethodCall message and a reply for it has been generated using
QDBusMessage::methodReply or QDBusMessage::error.
*/
bool QDBusMessage::wasRepliedTo() const
{
return d_ptr->repliedTo;
}
/*!
Returns the signature of the signal that was received or for the output arguments
of a method call.
*/
QString QDBusMessage::signature() const
{
return d_ptr->signature;
}
/*!
Sets the signature for the output arguments of this method call to be the value of \a
sig. This function has no meaning in other types of messages or when dealing with received
method calls.
A message's signature indicate the type of the parameters to
be marshalled over the bus. If there are more arguments than entries in the signature, the
tailing arguments will be silently dropped and not sent. If there are less arguments,
default values will be inserted (default values are those created by QVariant::convert
when a variant of type QVariant::Invalid is converted to the type).
*/
void QDBusMessage::setSignature(const QString &sig)
{
qAtomicDetach(d_ptr);
d_ptr->signature = sig;
}
/*!
Returns the connection this message was received on or an unconnected QDBusConnection object if
this isn't a message that has been received.
*/
QDBusConnection QDBusMessage::connection() const
{
return d_ptr->connection;
}
/*!
Returns the message type.
*/
QDBusMessage::MessageType QDBusMessage::type() const
{
switch (d_ptr->type) {
case DBUS_MESSAGE_TYPE_METHOD_CALL:
return MethodCallMessage;
case DBUS_MESSAGE_TYPE_METHOD_RETURN:
return ReplyMessage;
case DBUS_MESSAGE_TYPE_ERROR:
return ErrorMessage;
case DBUS_MESSAGE_TYPE_SIGNAL:
return SignalMessage;
default:
return InvalidMessage;
}
}
// Document QDBusReply here
/*!
\class QDBusReply
\brief The reply for a method call to a remote object.
A QDBusReply object is a subset of the QDBusMessage object that represents a method call's
reply. It contains only the first output argument or the error code and is used by
QDBusInterface-derived classes to allow returning the error code as the function's return
argument.
It can be used in the following manner:
\code
QDBusReply<QString> reply = interface->call("RemoteMethod");
if (reply.isSuccess())
// use the returned value
useValue(reply.value());
else
// call failed. Show an error condition.
showError(reply.error());
\endcode
If the remote method call cannot fail, you can skip the error checking:
\code
QString reply = interface->call("RemoteMethod");
\endcode
However, if it does fail under those conditions, the value returned by QDBusReply::value() is
undefined. It may be undistinguishable from a valid return value.
QDBusReply objects are used for remote calls that have no output arguments or return values
(i.e., they have a "void" return type). In this case, you can only test if the reply succeeded
or not, by calling isError() and isSuccess(), and inspecting the error condition by calling
error(). You cannot call value().
\sa QDBusMessage, QDBusInterface
*/
/*!
\fn QDBusReply::QDBusReply(const QDBusMessage &reply)
Automatically construct a QDBusReply object from the reply message \a reply, extracting the
first return value from it if it is a success reply.
*/
/*!
\fn QDBusReply::QDBusReply(const QDBusError &error)
Constructs an error reply from the D-Bus error code given by \a error.
*/
/*!
\fn QDBusReply::isError() const
Returns true if this reply is an error reply. You can extract the error contents using the
error() function.
*/
/*!
\fn QDBusReply::isSuccess() const
Returns true if this reply is a normal error reply (not an error). You can extract the returned
value with value()
*/
/*!
\fn QDBusReply::error()
Returns the error code that was returned from the remote function call. If the remote call did
not return an error (i.e., if it succeeded), then the QDBusError object that is returned will
not be a valid error code (QDBusError::isValid() will return false).
*/
/*!
\fn QDBusReply::value()
Returns the remote function's calls return value. If the remote call returned with an error,
the return value of this function is undefined and may be undistinguishable from a valid return
value.
This function is not available if the remote call returns \c void.
*/
/*!
\fn QDBusReply::operator Type()
Returns the same as value().
This function is not available if the remote call returns \c void.
*/
/*!
\internal
\fn QDBusReply::fromVariant(const QDBusReply<QVariant> &variantReply)
Converts the QDBusReply<QVariant> object to this type by converting the variant contained in
\a variantReply to the template's type and copying the error condition.
If the QVariant in variantReply is not convertible to this type, it will assume an undefined
value.
*/
#ifndef QT_NO_DEBUG_STREAM
QDebug operator<<(QDebug dbg, QDBusMessage::MessageType t)
{
switch (t)
{
case QDBusMessage::MethodCallMessage:
return dbg << "MethodCall";
case QDBusMessage::ReplyMessage:
return dbg << "MethodReturn";
case QDBusMessage::SignalMessage:
return dbg << "Signal";
case QDBusMessage::ErrorMessage:
return dbg << "Error";
default:
return dbg << "Invalid";
}
}
static void debugVariantList(QDebug dbg, const QVariantList &list);
static void debugVariantMap(QDebug dbg, const QVariantMap &map);
static void debugVariant(QDebug dbg, const QVariant &v)
{
dbg.nospace() << v.typeName() << "(";
switch (v.userType())
{
case QVariant::Bool:
dbg.nospace() << v.toBool();
break;
case QMetaType::UChar:
dbg.nospace() << qvariant_cast<uchar>(v);
break;
case QMetaType::Short:
dbg.nospace() << qvariant_cast<short>(v);
break;
case QMetaType::UShort:
dbg.nospace() << qvariant_cast<ushort>(v);
break;
case QVariant::Int:
dbg.nospace() << v.toInt();
break;
case QVariant::UInt:
dbg.nospace() << v.toUInt();
break;
case QVariant::LongLong:
dbg.nospace() << v.toLongLong();
break;
case QVariant::ULongLong:
dbg.nospace() << v.toULongLong();
break;
case QVariant::Double:
dbg.nospace() << v.toDouble();
break;
case QVariant::String:
dbg.nospace() << v.toString();
break;
case QVariant::ByteArray:
dbg.nospace() << v.toByteArray();
break;
case QVariant::StringList:
dbg.nospace() << v.toStringList();
break;
case QVariant::List:
debugVariantList(dbg, v.toList());
break;
case QVariant::Map:
debugVariantMap(dbg, v.toMap());
break;
default: {
int id = v.userType();
if (id == QDBusTypeHelper<QVariant>::id())
debugVariant(dbg, QDBusTypeHelper<QVariant>::fromVariant(v));
else if (id == QDBusTypeHelper<bool>::listId())
dbg.nospace() << QDBusTypeHelper<QList<bool> >::fromVariant(v);
else if (id == QDBusTypeHelper<short>::listId())
dbg.nospace() << QDBusTypeHelper<QList<short> >::fromVariant(v);
else if (id == QDBusTypeHelper<ushort>::listId())
dbg.nospace() << QDBusTypeHelper<QList<ushort> >::fromVariant(v);
else if (id == QDBusTypeHelper<int>::listId())
dbg.nospace() << QDBusTypeHelper<QList<int> >::fromVariant(v);
else if (id == QDBusTypeHelper<uint>::listId())
dbg.nospace() << QDBusTypeHelper<QList<uint> >::fromVariant(v);
else if (id == QDBusTypeHelper<qlonglong>::listId())
dbg.nospace() << QDBusTypeHelper<QList<qlonglong> >::fromVariant(v);
else if (id == QDBusTypeHelper<qulonglong>::listId())
dbg.nospace() << QDBusTypeHelper<QList<qulonglong> >::fromVariant(v);
else if (id == QDBusTypeHelper<double>::listId())
dbg.nospace() << QDBusTypeHelper<QList<double> >::fromVariant(v);
else
dbg.nospace() << "unknown";
}
}
dbg.nospace() << ")";
}
static void debugVariantList(QDebug dbg, const QVariantList &list)
{
bool first = true;
QVariantList::ConstIterator it = list.constBegin();
QVariantList::ConstIterator end = list.constEnd();
for ( ; it != end; ++it) {
if (!first)
dbg.nospace() << ", ";
debugVariant(dbg, *it);
first = false;
}
}
static void debugVariantMap(QDebug dbg, const QVariantMap &map)
{
QVariantMap::ConstIterator it = map.constBegin();
QVariantMap::ConstIterator end = map.constEnd();
for ( ; it != end; ++it) {
dbg << "(" << it.key() << ", ";
debugVariant(dbg, it.value());
dbg << ") ";
}
}
QDebug operator<<(QDebug dbg, const QDBusMessage &msg)
{
dbg.nospace() << "QDBusMessage(type=" << msg.type()
<< ", service=" << msg.service()
<< ", path=" << msg.path()
<< ", interface=" << msg.interface()
<< ", name=" << msg.name()
<< ", signature=" << msg.signature()
<< ", contents=(";
debugVariantList(dbg, msg);
dbg.nospace() << " ) )";
return dbg.space();
}
#endif
|