If the file is open, there must be an engine.
[qt-netbsd.git] / src / dbus / qdbusabstractadaptor.cpp
blob4f5457092263e437e578e184df499b31baf1dd6d
1 /****************************************************************************
2 **
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the QtDBus module of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** No Commercial Usage
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
14 ** this package.
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.
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.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
38 ** $QT_END_LICENSE$
40 ****************************************************************************/
42 #include "qdbusabstractadaptor.h"
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>
50 #include "qdbusconnection.h"
52 #include "qdbusconnection_p.h" // for qDBusParametersForMethod
53 #include "qdbusabstractadaptor_p.h"
54 #include "qdbusmetatype_p.h"
56 QT_BEGIN_NAMESPACE
58 QDBusAdaptorConnector *qDBusFindAdaptorConnector(QObject *obj)
60 if (!obj)
61 return 0;
62 const QObjectList &children = obj->children();
63 QObjectList::ConstIterator it = children.constBegin();
64 QObjectList::ConstIterator end = children.constEnd();
65 for ( ; it != end; ++it) {
66 QDBusAdaptorConnector *connector = qobject_cast<QDBusAdaptorConnector *>(*it);
67 if (connector) {
68 connector->polish();
69 return connector;
72 return 0;
75 QDBusAdaptorConnector *qDBusFindAdaptorConnector(QDBusAbstractAdaptor *adaptor)
77 return qDBusFindAdaptorConnector(adaptor->parent());
80 QDBusAdaptorConnector *qDBusCreateAdaptorConnector(QObject *obj)
82 QDBusAdaptorConnector *connector = qDBusFindAdaptorConnector(obj);
83 if (connector)
84 return connector;
85 return new QDBusAdaptorConnector(obj);
88 QString QDBusAbstractAdaptorPrivate::retrieveIntrospectionXml(QDBusAbstractAdaptor *adaptor)
90 return adaptor->d_func()->xml;
93 void QDBusAbstractAdaptorPrivate::saveIntrospectionXml(QDBusAbstractAdaptor *adaptor,
94 const QString &xml)
96 adaptor->d_func()->xml = xml;
99 /*!
100 \class QDBusAbstractAdaptor
101 \inmodule QtDBus
102 \since 4.2
104 \brief The QDBusAbstractAdaptor class is the base class of D-Bus adaptor classes.
106 The QDBusAbstractAdaptor class is the starting point for all objects intending to provide
107 interfaces to the external world using D-Bus. This is accomplished by attaching a one or more
108 classes derived from QDBusAbstractAdaptor to a normal QObject and then registering that QObject
109 with QDBusConnection::registerObject. QDBusAbstractAdaptor objects are intended to be
110 light-weight wrappers, mostly just relaying calls into the real object (its parent) and the
111 signals from it.
113 Each QDBusAbstractAdaptor-derived class should define the D-Bus interface it is implementing
114 using the Q_CLASSINFO macro in the class definition. Note that only one interface can be
115 exposed in this way.
117 QDBusAbstractAdaptor uses the standard QObject mechanism of signals, slots and properties to
118 determine what signals, methods and properties to export to the bus. Any signal emitted by
119 QDBusAbstractAdaptor-derived classes will be automatically be relayed through any D-Bus
120 connections the object is registered on.
122 Classes derived from QDBusAbstractAdaptor must be created on the heap using the \a new operator
123 and must not be deleted by the user (they will be deleted automatically when the object they are
124 connected to is also deleted).
126 \sa {usingadaptors.html}{Using adaptors}, QDBusConnection
130 Constructs a QDBusAbstractAdaptor with \a obj as the parent object.
132 QDBusAbstractAdaptor::QDBusAbstractAdaptor(QObject* obj)
133 : QObject(*new QDBusAbstractAdaptorPrivate, obj)
135 QDBusAdaptorConnector *connector = qDBusCreateAdaptorConnector(obj);
137 connector->waitingForPolish = true;
138 QMetaObject::invokeMethod(connector, "polish", Qt::QueuedConnection);
142 Destroys the adaptor.
144 \warning Adaptors are destroyed automatically when the real object they refer to is
145 destroyed. Do not delete the adaptors yourself.
147 QDBusAbstractAdaptor::~QDBusAbstractAdaptor()
152 Toggles automatic signal relaying from the real object (see object()).
154 Automatic signal relaying consists of signal-to-signal connection of the signals on the parent
155 that have the exact same method signatue in both classes.
157 If \a enable is set to true, connect the signals; if set to false, disconnect all signals.
159 void QDBusAbstractAdaptor::setAutoRelaySignals(bool enable)
161 const QMetaObject *us = metaObject();
162 const QMetaObject *them = parent()->metaObject();
163 bool connected = false;
164 for (int idx = staticMetaObject.methodCount(); idx < us->methodCount(); ++idx) {
165 QMetaMethod mm = us->method(idx);
167 if (mm.methodType() != QMetaMethod::Signal)
168 continue;
170 // try to connect/disconnect to a signal on the parent that has the same method signature
171 QByteArray sig = QMetaObject::normalizedSignature(mm.signature());
172 if (them->indexOfSignal(sig) == -1)
173 continue;
174 sig.prepend(QSIGNAL_CODE + '0');
175 parent()->disconnect(sig, this, sig);
176 if (enable)
177 connected = connect(parent(), sig, sig) || connected;
179 d_func()->autoRelaySignals = connected;
183 Returns true if automatic signal relaying from the real object (see object()) is enabled,
184 otherwiser returns false.
186 \sa setAutoRelaySignals()
188 bool QDBusAbstractAdaptor::autoRelaySignals() const
190 return d_func()->autoRelaySignals;
193 QDBusAdaptorConnector::QDBusAdaptorConnector(QObject *obj)
194 : QObject(obj), waitingForPolish(false)
198 QDBusAdaptorConnector::~QDBusAdaptorConnector()
202 void QDBusAdaptorConnector::addAdaptor(QDBusAbstractAdaptor *adaptor)
204 // find the interface name
205 const QMetaObject *mo = adaptor->metaObject();
206 int ciid = mo->indexOfClassInfo(QCLASSINFO_DBUS_INTERFACE);
207 if (ciid != -1) {
208 QMetaClassInfo mci = mo->classInfo(ciid);
209 if (*mci.value()) {
210 // find out if this interface exists first
211 const char *interface = mci.value();
212 AdaptorMap::Iterator it = qLowerBound(adaptors.begin(), adaptors.end(),
213 QByteArray(interface));
214 if (it != adaptors.end() && qstrcmp(interface, it->interface) == 0) {
215 // exists. Replace it (though it's probably the same)
216 if (it->adaptor != adaptor) {
217 // reconnect the signals
218 disconnectAllSignals(it->adaptor);
219 connectAllSignals(adaptor);
221 it->adaptor = adaptor;
222 } else {
223 // create a new one
224 AdaptorData entry;
225 entry.interface = interface;
226 entry.adaptor = adaptor;
227 adaptors << entry;
229 // connect the adaptor's signals to our relaySlot slot
230 connectAllSignals(adaptor);
236 void QDBusAdaptorConnector::disconnectAllSignals(QObject *obj)
238 QMetaObject::disconnect(obj, -1, this, metaObject()->methodOffset());
241 void QDBusAdaptorConnector::connectAllSignals(QObject *obj)
243 QMetaObject::connect(obj, -1, this, metaObject()->methodOffset(), Qt::DirectConnection);
246 void QDBusAdaptorConnector::polish()
248 if (!waitingForPolish)
249 return; // avoid working multiple times if multiple adaptors were added
251 waitingForPolish = false;
252 const QObjectList &objs = parent()->children();
253 QObjectList::ConstIterator it = objs.constBegin();
254 QObjectList::ConstIterator end = objs.constEnd();
255 for ( ; it != end; ++it) {
256 QDBusAbstractAdaptor *adaptor = qobject_cast<QDBusAbstractAdaptor *>(*it);
257 if (adaptor)
258 addAdaptor(adaptor);
261 // sort the adaptor list
262 qSort(adaptors);
265 void QDBusAdaptorConnector::relaySlot(void **argv)
267 QObjectPrivate *d = static_cast<QObjectPrivate *>(d_ptr.data());
268 relay(d->currentSender->sender, d->currentSender->signal, argv);
271 void QDBusAdaptorConnector::relay(QObject *senderObj, int lastSignalIdx, void **argv)
273 if (lastSignalIdx < QObject::staticMetaObject.methodCount())
274 // QObject signal (destroyed(QObject *)) -- ignore
275 return;
277 const QMetaObject *senderMetaObject = senderObj->metaObject();
278 QMetaMethod mm = senderMetaObject->method(lastSignalIdx);
280 QObject *realObject = senderObj;
281 if (qobject_cast<QDBusAbstractAdaptor *>(senderObj))
282 // it's an adaptor, so the real object is in fact its parent
283 realObject = realObject->parent();
285 // break down the parameter list
286 QList<int> types;
287 int inputCount = qDBusParametersForMethod(mm, types);
288 if (inputCount == -1)
289 // invalid signal signature
290 // qDBusParametersForMethod has already complained
291 return;
292 if (inputCount + 1 != types.count() ||
293 types.at(inputCount) == QDBusMetaTypeId::message) {
294 // invalid signal signature
295 // qDBusParametersForMethod has not yet complained about this one
296 qWarning("QDBusAbstractAdaptor: Cannot relay signal %s::%s",
297 senderMetaObject->className(), mm.signature());
298 return;
301 QVariantList args;
302 for (int i = 1; i < types.count(); ++i)
303 args << QVariant(types.at(i), argv[i]);
305 // now emit the signal with all the information
306 emit relaySignal(realObject, senderMetaObject, lastSignalIdx, args);
309 // our Meta Object
310 // modify carefully: this has been hand-edited!
311 // the relaySlot slot has local ID 0 (we use this when calling QMetaObject::connect)
312 // it also gets called with the void** array
314 static const uint qt_meta_data_QDBusAdaptorConnector[] = {
315 // content:
316 1, // revision
317 0, // classname
318 0, 0, // classinfo
319 3, 10, // methods
320 0, 0, // properties
321 0, 0, // enums/sets
323 // slots: signature, parameters, type, tag, flags
324 106, 22, 22, 22, 0x0a,
325 118, 22, 22, 22, 0x0a,
327 // signals: signature, parameters, type, tag, flags
328 47, 23, 22, 22, 0x05,
330 0 // eod
333 static const char qt_meta_stringdata_QDBusAdaptorConnector[] = {
334 "QDBusAdaptorConnector\0\0obj,metaobject,sid,args\0"
335 "relaySignal(QObject*,const QMetaObject*,int,QVariantList)\0\0relaySlot()\0"
336 "polish()\0"
339 const QMetaObject QDBusAdaptorConnector::staticMetaObject = {
340 { &QObject::staticMetaObject, qt_meta_stringdata_QDBusAdaptorConnector,
341 qt_meta_data_QDBusAdaptorConnector, 0 }
344 const QMetaObject *QDBusAdaptorConnector::metaObject() const
346 return &staticMetaObject;
349 void *QDBusAdaptorConnector::qt_metacast(const char *_clname)
351 if (!_clname) return 0;
352 if (!strcmp(_clname, qt_meta_stringdata_QDBusAdaptorConnector))
353 return static_cast<void*>(const_cast<QDBusAdaptorConnector*>(this));
354 return QObject::qt_metacast(_clname);
357 int QDBusAdaptorConnector::qt_metacall(QMetaObject::Call _c, int _id, void **_a)
359 _id = QObject::qt_metacall(_c, _id, _a);
360 if (_id < 0)
361 return _id;
362 if (_c == QMetaObject::InvokeMetaMethod) {
363 switch (_id) {
364 case 0: relaySlot(_a); break; // HAND EDIT: add the _a parameter
365 case 1: polish(); break;
366 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;
368 _id -= 3;
370 return _id;
373 // SIGNAL 0
374 void QDBusAdaptorConnector::relaySignal(QObject * _t1, const QMetaObject * _t2, int _t3, const QVariantList & _t4)
376 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)) };
377 QMetaObject::activate(this, &staticMetaObject, 2, _a);
380 QT_END_NAMESPACE