|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2009 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. |
|
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 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 #include "qdbusinterface.h" |
|
43 |
|
44 #include <qdbus_symbols_p.h> |
|
45 #include <QtCore/qpointer.h> |
|
46 #include <QtCore/qstringlist.h> |
|
47 |
|
48 #include "qdbusmetatype_p.h" |
|
49 #include "qdbusinterface_p.h" |
|
50 #include "qdbusconnection_p.h" |
|
51 |
|
52 QT_BEGIN_NAMESPACE |
|
53 |
|
54 static void copyArgument(void *to, int id, const QVariant &arg) |
|
55 { |
|
56 if (id == arg.userType()) { |
|
57 switch (id) { |
|
58 case QVariant::Bool: |
|
59 *reinterpret_cast<bool *>(to) = arg.toBool(); |
|
60 return; |
|
61 |
|
62 case QMetaType::UChar: |
|
63 *reinterpret_cast<uchar *>(to) = arg.value<uchar>(); |
|
64 return; |
|
65 |
|
66 case QMetaType::Short: |
|
67 *reinterpret_cast<short *>(to) = arg.value<short>(); |
|
68 return; |
|
69 |
|
70 case QMetaType::UShort: |
|
71 *reinterpret_cast<ushort *>(to) = arg.value<ushort>(); |
|
72 return; |
|
73 |
|
74 case QVariant::Int: |
|
75 *reinterpret_cast<int *>(to) = arg.toInt(); |
|
76 return; |
|
77 |
|
78 case QVariant::UInt: |
|
79 *reinterpret_cast<uint *>(to) = arg.toUInt(); |
|
80 return; |
|
81 |
|
82 case QVariant::LongLong: |
|
83 *reinterpret_cast<qlonglong *>(to) = arg.toLongLong(); |
|
84 return; |
|
85 |
|
86 case QVariant::ULongLong: |
|
87 *reinterpret_cast<qulonglong *>(to) = arg.toULongLong(); |
|
88 return; |
|
89 |
|
90 case QVariant::Double: |
|
91 *reinterpret_cast<double *>(to) = arg.toDouble(); |
|
92 return; |
|
93 |
|
94 case QVariant::String: |
|
95 *reinterpret_cast<QString *>(to) = arg.toString(); |
|
96 return; |
|
97 |
|
98 case QVariant::ByteArray: |
|
99 *reinterpret_cast<QByteArray *>(to) = arg.toByteArray(); |
|
100 return; |
|
101 |
|
102 case QVariant::StringList: |
|
103 *reinterpret_cast<QStringList *>(to) = arg.toStringList(); |
|
104 return; |
|
105 } |
|
106 |
|
107 if (id == QDBusMetaTypeId::variant) { |
|
108 *reinterpret_cast<QDBusVariant *>(to) = arg.value<QDBusVariant>(); |
|
109 return; |
|
110 } else if (id == QDBusMetaTypeId::objectpath) { |
|
111 *reinterpret_cast<QDBusObjectPath *>(to) = arg.value<QDBusObjectPath>(); |
|
112 return; |
|
113 } else if (id == QDBusMetaTypeId::signature) { |
|
114 *reinterpret_cast<QDBusSignature *>(to) = arg.value<QDBusSignature>(); |
|
115 return; |
|
116 } |
|
117 |
|
118 // those above are the only types possible |
|
119 // the demarshaller code doesn't demarshall anything else |
|
120 qFatal("Found a decoded basic type in a D-Bus reply that shouldn't be there"); |
|
121 } |
|
122 |
|
123 // if we got here, it's either an un-dermarshalled type or a mismatch |
|
124 if (arg.userType() != QDBusMetaTypeId::argument) { |
|
125 // it's a mismatch |
|
126 //qWarning? |
|
127 return; |
|
128 } |
|
129 |
|
130 // is this type registered? |
|
131 const char *userSignature = QDBusMetaType::typeToSignature(id); |
|
132 if (!userSignature || !*userSignature) { |
|
133 // type not registered |
|
134 //qWarning? |
|
135 return; |
|
136 } |
|
137 |
|
138 // is it the same signature? |
|
139 QDBusArgument dbarg = arg.value<QDBusArgument>(); |
|
140 if (dbarg.currentSignature() != QLatin1String(userSignature)) { |
|
141 // not the same signature, another mismatch |
|
142 //qWarning? |
|
143 return; |
|
144 } |
|
145 |
|
146 // we can demarshall |
|
147 QDBusMetaType::demarshall(dbarg, id, to); |
|
148 } |
|
149 |
|
150 QDBusInterfacePrivate::QDBusInterfacePrivate(const QString &serv, const QString &p, |
|
151 const QString &iface, const QDBusConnection &con) |
|
152 : QDBusAbstractInterfacePrivate(serv, p, iface, con, true), metaObject(0) |
|
153 { |
|
154 // QDBusAbstractInterfacePrivate's constructor checked the parameters for us |
|
155 if (connection.isConnected()) { |
|
156 metaObject = connectionPrivate()->findMetaObject(service, path, interface, lastError); |
|
157 |
|
158 if (!metaObject) { |
|
159 // creation failed, somehow |
|
160 isValid = false; |
|
161 if (!lastError.isValid()) |
|
162 lastError = QDBusError(QDBusError::InternalError, QLatin1String("Unknown error")); |
|
163 } |
|
164 } |
|
165 } |
|
166 |
|
167 QDBusInterfacePrivate::~QDBusInterfacePrivate() |
|
168 { |
|
169 if (metaObject && !metaObject->cached) |
|
170 delete metaObject; |
|
171 } |
|
172 |
|
173 |
|
174 /*! |
|
175 \class QDBusInterface |
|
176 \inmodule QtDBus |
|
177 \since 4.2 |
|
178 |
|
179 \brief The QDBusInterface class is a proxy for interfaces on remote objects. |
|
180 |
|
181 QDBusInterface is a generic accessor class that is used to place calls to remote objects, |
|
182 connect to signals exported by remote objects and get/set the value of remote properties. This |
|
183 class is useful for dynamic access to remote objects: that is, when you do not have a generated |
|
184 code that represents the remote interface. |
|
185 |
|
186 Calls are usually placed by using the call() function, which constructs the message, sends it |
|
187 over the bus, waits for the reply and decodes the reply. Signals are connected to by using the |
|
188 normal QObject::connect() function. Finally, properties are accessed using the |
|
189 QObject::property() and QObject::setProperty() functions. |
|
190 |
|
191 The following code snippet demonstrates how to perform a |
|
192 mathematical operation of \tt{"2 + 2"} in a remote application |
|
193 called \c com.example.Calculator, accessed via the session bus. |
|
194 |
|
195 \snippet doc/src/snippets/code/src_qdbus_qdbusinterface.cpp 0 |
|
196 |
|
197 \sa {QtDBus XML compiler (qdbusxml2cpp)} |
|
198 */ |
|
199 |
|
200 /*! |
|
201 Creates a dynamic QDBusInterface object associated with the |
|
202 interface \a interface on object at path \a path on service \a |
|
203 service, using the given \a connection. If \a interface is an |
|
204 empty string, the object created will refer to the merging of all |
|
205 interfaces found in that object. |
|
206 |
|
207 \a parent is passed to the base class constructor. |
|
208 |
|
209 If the remote service \a service is not present or if an error |
|
210 occurs trying to obtain the description of the remote interface |
|
211 \a interface, the object created will not be valid (see |
|
212 isValid()). |
|
213 */ |
|
214 QDBusInterface::QDBusInterface(const QString &service, const QString &path, const QString &interface, |
|
215 const QDBusConnection &connection, QObject *parent) |
|
216 : QDBusAbstractInterface(*new QDBusInterfacePrivate(service, path, interface, connection), |
|
217 parent) |
|
218 { |
|
219 } |
|
220 |
|
221 /*! |
|
222 Destroy the object interface and frees up any resource used. |
|
223 */ |
|
224 QDBusInterface::~QDBusInterface() |
|
225 { |
|
226 // resources are freed in QDBusInterfacePrivate::~QDBusInterfacePrivate() |
|
227 } |
|
228 |
|
229 /*! |
|
230 \internal |
|
231 Overrides QObject::metaObject to return our own copy. |
|
232 */ |
|
233 const QMetaObject *QDBusInterface::metaObject() const |
|
234 { |
|
235 return d_func()->isValid ? d_func()->metaObject : &QDBusAbstractInterface::staticMetaObject; |
|
236 } |
|
237 |
|
238 /*! |
|
239 \internal |
|
240 Override QObject::qt_metacast to catch the interface name too. |
|
241 */ |
|
242 void *QDBusInterface::qt_metacast(const char *_clname) |
|
243 { |
|
244 if (!_clname) return 0; |
|
245 if (!strcmp(_clname, "QDBusInterface")) |
|
246 return static_cast<void*>(const_cast<QDBusInterface*>(this)); |
|
247 if (d_func()->interface.toLatin1() == _clname) |
|
248 return static_cast<void*>(const_cast<QDBusInterface*>(this)); |
|
249 return QDBusAbstractInterface::qt_metacast(_clname); |
|
250 } |
|
251 |
|
252 /*! |
|
253 \internal |
|
254 Dispatch the call through the private. |
|
255 */ |
|
256 int QDBusInterface::qt_metacall(QMetaObject::Call _c, int _id, void **_a) |
|
257 { |
|
258 _id = QDBusAbstractInterface::qt_metacall(_c, _id, _a); |
|
259 if (_id < 0 || !d_func()->isValid) |
|
260 return _id; |
|
261 return d_func()->metacall(_c, _id, _a); |
|
262 } |
|
263 |
|
264 int QDBusInterfacePrivate::metacall(QMetaObject::Call c, int id, void **argv) |
|
265 { |
|
266 Q_Q(QDBusInterface); |
|
267 |
|
268 if (c == QMetaObject::InvokeMetaMethod) { |
|
269 int offset = metaObject->methodOffset(); |
|
270 QMetaMethod mm = metaObject->method(id + offset); |
|
271 |
|
272 if (mm.methodType() == QMetaMethod::Signal) { |
|
273 // signal relay from D-Bus world to Qt world |
|
274 QMetaObject::activate(q, metaObject, id, argv); |
|
275 |
|
276 } else if (mm.methodType() == QMetaMethod::Slot) { |
|
277 // method call relay from Qt world to D-Bus world |
|
278 // get D-Bus equivalent signature |
|
279 QString methodName = QLatin1String(metaObject->dbusNameForMethod(id)); |
|
280 const int *inputTypes = metaObject->inputTypesForMethod(id); |
|
281 int inputTypesCount = *inputTypes; |
|
282 |
|
283 // we will assume that the input arguments were passed correctly |
|
284 QVariantList args; |
|
285 int i = 1; |
|
286 for ( ; i <= inputTypesCount; ++i) |
|
287 args << QVariant(inputTypes[i], argv[i]); |
|
288 |
|
289 // make the call |
|
290 QDBusMessage reply = q->callWithArgumentList(QDBus::Block, methodName, args); |
|
291 |
|
292 if (reply.type() == QDBusMessage::ReplyMessage) { |
|
293 // attempt to demarshall the return values |
|
294 args = reply.arguments(); |
|
295 QVariantList::ConstIterator it = args.constBegin(); |
|
296 const int *outputTypes = metaObject->outputTypesForMethod(id); |
|
297 int outputTypesCount = *outputTypes++; |
|
298 |
|
299 if (*mm.typeName()) { |
|
300 // this method has a return type |
|
301 if (argv[0] && it != args.constEnd()) |
|
302 copyArgument(argv[0], *outputTypes++, *it); |
|
303 |
|
304 // skip this argument even if we didn't copy it |
|
305 --outputTypesCount; |
|
306 ++it; |
|
307 } |
|
308 |
|
309 for (int j = 0; j < outputTypesCount && it != args.constEnd(); ++i, ++j, ++it) { |
|
310 copyArgument(argv[i], outputTypes[j], *it); |
|
311 } |
|
312 } |
|
313 |
|
314 // done |
|
315 lastError = reply; |
|
316 return -1; |
|
317 } |
|
318 } |
|
319 return id; |
|
320 } |
|
321 |
|
322 QT_END_NAMESPACE |