source: trunk/src/dbus/qdbusabstractadaptor.cpp@ 890

Last change on this file since 890 was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

File size: 13.4 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation ([email protected])
6**
7** This file is part of the QtDBus module of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial Usage
11** Licensees holding valid Qt Commercial licenses may use this file in
12** accordance with the Qt Commercial License Agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and Nokia.
15**
16** GNU Lesser General Public License Usage
17** Alternatively, this file may be used under the terms of the GNU Lesser
18** General Public License version 2.1 as published by the Free Software
19** Foundation and appearing in the file LICENSE.LGPL included in the
20** packaging of this file. Please review the following information to
21** ensure the GNU Lesser General Public License version 2.1 requirements
22** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
23**
24** In addition, as a special exception, Nokia gives you certain additional
25** rights. These rights are described in the Nokia Qt LGPL Exception
26** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you have questions regarding the use of this file, please contact
37** Nokia at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "qdbusabstractadaptor.h"
43
44#include <QtCore/qcoreapplication.h>
45#include <QtCore/qmetaobject.h>
46#include <QtCore/qset.h>
47#include <QtCore/qtimer.h>
48#include <QtCore/qthread.h>
49
50#include "qdbusconnection.h"
51
52#include "qdbusconnection_p.h" // for qDBusParametersForMethod
53#include "qdbusabstractadaptor_p.h"
54#include "qdbusmetatype_p.h"
55
56#ifndef QT_NO_DBUS
57
58QT_BEGIN_NAMESPACE
59
60QDBusAdaptorConnector *qDBusFindAdaptorConnector(QObject *obj)
61{
62 if (!obj)
63 return 0;
64 const QObjectList &children = obj->children();
65 QObjectList::ConstIterator it = children.constBegin();
66 QObjectList::ConstIterator end = children.constEnd();
67 for ( ; it != end; ++it) {
68 QDBusAdaptorConnector *connector = qobject_cast<QDBusAdaptorConnector *>(*it);
69 if (connector) {
70 connector->polish();
71 return connector;
72 }
73 }
74 return 0;
75}
76
77QDBusAdaptorConnector *qDBusFindAdaptorConnector(QDBusAbstractAdaptor *adaptor)
78{
79 return qDBusFindAdaptorConnector(adaptor->parent());
80}
81
82QDBusAdaptorConnector *qDBusCreateAdaptorConnector(QObject *obj)
83{
84 QDBusAdaptorConnector *connector = qDBusFindAdaptorConnector(obj);
85 if (connector)
86 return connector;
87 return new QDBusAdaptorConnector(obj);
88}
89
90QString QDBusAbstractAdaptorPrivate::retrieveIntrospectionXml(QDBusAbstractAdaptor *adaptor)
91{
92 return adaptor->d_func()->xml;
93}
94
95void QDBusAbstractAdaptorPrivate::saveIntrospectionXml(QDBusAbstractAdaptor *adaptor,
96 const QString &xml)
97{
98 adaptor->d_func()->xml = xml;
99}
100
101/*!
102 \class QDBusAbstractAdaptor
103 \inmodule QtDBus
104 \since 4.2
105
106 \brief The QDBusAbstractAdaptor class is the base class of D-Bus adaptor classes.
107
108 The QDBusAbstractAdaptor class is the starting point for all objects intending to provide
109 interfaces to the external world using D-Bus. This is accomplished by attaching a one or more
110 classes derived from QDBusAbstractAdaptor to a normal QObject and then registering that QObject
111 with QDBusConnection::registerObject. QDBusAbstractAdaptor objects are intended to be
112 light-weight wrappers, mostly just relaying calls into the real object (its parent) and the
113 signals from it.
114
115 Each QDBusAbstractAdaptor-derived class should define the D-Bus interface it is implementing
116 using the Q_CLASSINFO macro in the class definition. Note that only one interface can be
117 exposed in this way.
118
119 QDBusAbstractAdaptor uses the standard QObject mechanism of signals, slots and properties to
120 determine what signals, methods and properties to export to the bus. Any signal emitted by
121 QDBusAbstractAdaptor-derived classes will be automatically be relayed through any D-Bus
122 connections the object is registered on.
123
124 Classes derived from QDBusAbstractAdaptor must be created on the heap using the \a new operator
125 and must not be deleted by the user (they will be deleted automatically when the object they are
126 connected to is also deleted).
127
128 \sa {usingadaptors.html}{Using adaptors}, QDBusConnection
129*/
130
131/*!
132 Constructs a QDBusAbstractAdaptor with \a obj as the parent object.
133*/
134QDBusAbstractAdaptor::QDBusAbstractAdaptor(QObject* obj)
135 : QObject(*new QDBusAbstractAdaptorPrivate, obj)
136{
137 QDBusAdaptorConnector *connector = qDBusCreateAdaptorConnector(obj);
138
139 connector->waitingForPolish = true;
140 QMetaObject::invokeMethod(connector, "polish", Qt::QueuedConnection);
141}
142
143/*!
144 Destroys the adaptor.
145
146 \warning Adaptors are destroyed automatically when the real object they refer to is
147 destroyed. Do not delete the adaptors yourself.
148*/
149QDBusAbstractAdaptor::~QDBusAbstractAdaptor()
150{
151}
152
153/*!
154 Toggles automatic signal relaying from the real object (see object()).
155
156 Automatic signal relaying consists of signal-to-signal connection of the signals on the parent
157 that have the exact same method signatue in both classes.
158
159 If \a enable is set to true, connect the signals; if set to false, disconnect all signals.
160*/
161void QDBusAbstractAdaptor::setAutoRelaySignals(bool enable)
162{
163 const QMetaObject *us = metaObject();
164 const QMetaObject *them = parent()->metaObject();
165 bool connected = false;
166 for (int idx = staticMetaObject.methodCount(); idx < us->methodCount(); ++idx) {
167 QMetaMethod mm = us->method(idx);
168
169 if (mm.methodType() != QMetaMethod::Signal)
170 continue;
171
172 // try to connect/disconnect to a signal on the parent that has the same method signature
173 QByteArray sig = QMetaObject::normalizedSignature(mm.signature());
174 if (them->indexOfSignal(sig) == -1)
175 continue;
176 sig.prepend(QSIGNAL_CODE + '0');
177 parent()->disconnect(sig, this, sig);
178 if (enable)
179 connected = connect(parent(), sig, sig) || connected;
180 }
181 d_func()->autoRelaySignals = connected;
182}
183
184/*!
185 Returns true if automatic signal relaying from the real object (see object()) is enabled,
186 otherwiser returns false.
187
188 \sa setAutoRelaySignals()
189*/
190bool QDBusAbstractAdaptor::autoRelaySignals() const
191{
192 return d_func()->autoRelaySignals;
193}
194
195QDBusAdaptorConnector::QDBusAdaptorConnector(QObject *obj)
196 : QObject(obj), waitingForPolish(false)
197{
198}
199
200QDBusAdaptorConnector::~QDBusAdaptorConnector()
201{
202}
203
204void QDBusAdaptorConnector::addAdaptor(QDBusAbstractAdaptor *adaptor)
205{
206 // find the interface name
207 const QMetaObject *mo = adaptor->metaObject();
208 int ciid = mo->indexOfClassInfo(QCLASSINFO_DBUS_INTERFACE);
209 if (ciid != -1) {
210 QMetaClassInfo mci = mo->classInfo(ciid);
211 if (*mci.value()) {
212 // find out if this interface exists first
213 const char *interface = mci.value();
214 AdaptorMap::Iterator it = qLowerBound(adaptors.begin(), adaptors.end(),
215 QByteArray(interface));
216 if (it != adaptors.end() && qstrcmp(interface, it->interface) == 0) {
217 // exists. Replace it (though it's probably the same)
218 if (it->adaptor != adaptor) {
219 // reconnect the signals
220 disconnectAllSignals(it->adaptor);
221 connectAllSignals(adaptor);
222 }
223 it->adaptor = adaptor;
224 } else {
225 // create a new one
226 AdaptorData entry;
227 entry.interface = interface;
228 entry.adaptor = adaptor;
229 adaptors << entry;
230
231 // connect the adaptor's signals to our relaySlot slot
232 connectAllSignals(adaptor);
233 }
234 }
235 }
236}
237
238void QDBusAdaptorConnector::disconnectAllSignals(QObject *obj)
239{
240 QMetaObject::disconnect(obj, -1, this, metaObject()->methodOffset());
241}
242
243void QDBusAdaptorConnector::connectAllSignals(QObject *obj)
244{
245 QMetaObject::connect(obj, -1, this, metaObject()->methodOffset(), Qt::DirectConnection);
246}
247
248void QDBusAdaptorConnector::polish()
249{
250 if (!waitingForPolish)
251 return; // avoid working multiple times if multiple adaptors were added
252
253 waitingForPolish = false;
254 const QObjectList &objs = parent()->children();
255 QObjectList::ConstIterator it = objs.constBegin();
256 QObjectList::ConstIterator end = objs.constEnd();
257 for ( ; it != end; ++it) {
258 QDBusAbstractAdaptor *adaptor = qobject_cast<QDBusAbstractAdaptor *>(*it);
259 if (adaptor)
260 addAdaptor(adaptor);
261 }
262
263 // sort the adaptor list
264 qSort(adaptors);
265}
266
267void QDBusAdaptorConnector::relaySlot(void **argv)
268{
269 QObjectPrivate *d = static_cast<QObjectPrivate *>(d_ptr.data());
270 relay(d->currentSender->sender, d->currentSender->signal, argv);
271}
272
273void QDBusAdaptorConnector::relay(QObject *senderObj, int lastSignalIdx, void **argv)
274{
275 if (lastSignalIdx < QObject::staticMetaObject.methodCount())
276 // QObject signal (destroyed(QObject *)) -- ignore
277 return;
278
279 const QMetaObject *senderMetaObject = senderObj->metaObject();
280 QMetaMethod mm = senderMetaObject->method(lastSignalIdx);
281
282 QObject *realObject = senderObj;
283 if (qobject_cast<QDBusAbstractAdaptor *>(senderObj))
284 // it's an adaptor, so the real object is in fact its parent
285 realObject = realObject->parent();
286
287 // break down the parameter list
288 QList<int> types;
289 int inputCount = qDBusParametersForMethod(mm, types);
290 if (inputCount == -1)
291 // invalid signal signature
292 // qDBusParametersForMethod has already complained
293 return;
294 if (inputCount + 1 != types.count() ||
295 types.at(inputCount) == QDBusMetaTypeId::message) {
296 // invalid signal signature
297 // qDBusParametersForMethod has not yet complained about this one
298 qWarning("QDBusAbstractAdaptor: Cannot relay signal %s::%s",
299 senderMetaObject->className(), mm.signature());
300 return;
301 }
302
303 QVariantList args;
304 for (int i = 1; i < types.count(); ++i)
305 args << QVariant(types.at(i), argv[i]);
306
307 // now emit the signal with all the information
308 emit relaySignal(realObject, senderMetaObject, lastSignalIdx, args);
309}
310
311// our Meta Object
312// modify carefully: this has been hand-edited!
313// the relaySlot slot has local ID 0 (we use this when calling QMetaObject::connect)
314// it also gets called with the void** array
315
316static const uint qt_meta_data_QDBusAdaptorConnector[] = {
317 // content:
318 1, // revision
319 0, // classname
320 0, 0, // classinfo
321 3, 10, // methods
322 0, 0, // properties
323 0, 0, // enums/sets
324
325 // slots: signature, parameters, type, tag, flags
326 106, 22, 22, 22, 0x0a,
327 118, 22, 22, 22, 0x0a,
328
329 // signals: signature, parameters, type, tag, flags
330 47, 23, 22, 22, 0x05,
331
332 0 // eod
333};
334
335static const char qt_meta_stringdata_QDBusAdaptorConnector[] = {
336 "QDBusAdaptorConnector\0\0obj,metaobject,sid,args\0"
337 "relaySignal(QObject*,const QMetaObject*,int,QVariantList)\0\0relaySlot()\0"
338 "polish()\0"
339};
340
341const QMetaObject QDBusAdaptorConnector::staticMetaObject = {
342 { &QObject::staticMetaObject, qt_meta_stringdata_QDBusAdaptorConnector,
343 qt_meta_data_QDBusAdaptorConnector, 0 }
344};
345
346const QMetaObject *QDBusAdaptorConnector::metaObject() const
347{
348 return &staticMetaObject;
349}
350
351void *QDBusAdaptorConnector::qt_metacast(const char *_clname)
352{
353 if (!_clname) return 0;
354 if (!strcmp(_clname, qt_meta_stringdata_QDBusAdaptorConnector))
355 return static_cast<void*>(const_cast<QDBusAdaptorConnector*>(this));
356 return QObject::qt_metacast(_clname);
357}
358
359int QDBusAdaptorConnector::qt_metacall(QMetaObject::Call _c, int _id, void **_a)
360{
361 _id = QObject::qt_metacall(_c, _id, _a);
362 if (_id < 0)
363 return _id;
364 if (_c == QMetaObject::InvokeMetaMethod) {
365 switch (_id) {
366 case 0: relaySlot(_a); break; // HAND EDIT: add the _a parameter
367 case 1: polish(); break;
368 case 2: relaySignal((*reinterpret_cast< QObject*(*)>(_a[1])),(*reinterpret_cast< const QMetaObject*(*)>(_a[2])),(*reinterpret_cast< int(*)>(_a[3])),(*reinterpret_cast< const QVariantList(*)>(_a[4]))); break;
369 }
370 _id -= 3;
371 }
372 return _id;
373}
374
375// SIGNAL 0
376void QDBusAdaptorConnector::relaySignal(QObject * _t1, const QMetaObject * _t2, int _t3, const QVariantList & _t4)
377{
378 void *_a[] = { 0, const_cast<void*>(reinterpret_cast<const void*>(&_t1)), const_cast<void*>(reinterpret_cast<const void*>(&_t2)), const_cast<void*>(reinterpret_cast<const void*>(&_t3)), const_cast<void*>(reinterpret_cast<const void*>(&_t4)) };
379 QMetaObject::activate(this, &staticMetaObject, 2, _a);
380}
381
382QT_END_NAMESPACE
383
384#endif // QT_NO_DBUS
Note: See TracBrowser for help on using the repository browser.