Debian Codegraph @

src/serviceframework/ipc/qservicemetaobject_dbus.cpp

raw symbols SCIP
1 /****************************************************************************
2 **
3 ** Copyright (C) 2018 The Qt Company Ltd and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtSystems module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39
40 #include "qservicemetaobject_dbus_p.h"
41 #include <private/qmetaobjectbuilder_p.h>
42 #include "qsignalintercepter_p.h"
43 #include <QDebug>
44
45 #include <stdlib.h>
46
47 QT_BEGIN_NAMESPACE
48
49 class ServiceMetaSignalIntercepter : public QSignalIntercepter
50 {
51
52 public:
53 ServiceMetaSignalIntercepter(QObject* sender, const QByteArray& signal, QServiceMetaObjectDBus* parent)
54 : QSignalIntercepter(sender, signal, parent), serviceDBus(parent)
55 {
56
57 }
58
59 void setMetaIndex(int index)
60 {
61 metaIndex = index;
62 }
63
64 protected:
65 void activated( const QList<QVariant>& args )
66 {
67 serviceDBus->activateMetaSignal(metaIndex, args);
68 }
69 private:
70 QServiceMetaObjectDBus* serviceDBus;
71 int metaIndex;
72 };
73
74
75 class QServiceMetaObjectDBusPrivate
76 {
77 public:
78 QObject *service;
79 const QMetaObject *serviceMeta;
80 const QMetaObject *dbusMeta;
81 };
82
83 QServiceMetaObjectDBus::QServiceMetaObjectDBus(QObject* service, bool signalsObject)
84 : QDBusAbstractAdaptor(service)
85 {
86 // Register our DBus custom type object
87 qRegisterMetaType<QServiceUserTypeDBus>();
88 qDBusRegisterMetaType<QServiceUserTypeDBus>();
89
90 // Generate our DBus meta object
91 d = new QServiceMetaObjectDBusPrivate();
92 d->service = service;
93 d->serviceMeta = service->metaObject();
94 d->dbusMeta = dbusMetaObject(signalsObject);
95
96 // Relay signals from the service to the constructed DBus meta object
97 connectMetaSignals(signalsObject);
98 }
99
100 QServiceMetaObjectDBus::~QServiceMetaObjectDBus()
101 {
102 if (d->dbusMeta)
103 free(const_cast<QMetaObject*>(d->dbusMeta));
104
105 delete d;
106 }
107
108 /*!
109 Relays all signals from the service object to the converted DBus service adaptor so
110 that when a service signal is emitted the DBus object will emit the counterpart signal
111 */
112 void QServiceMetaObjectDBus::connectMetaSignals(bool signalsObject) {
113 if (!signalsObject) {
114 // Automatically relay signals from service object to adaptor
115 setAutoRelaySignals(true);
116
117 // Connect signals with custom arguments
118 int methodCount = d->serviceMeta->methodCount();
119 for (int i = 0; i < methodCount; i++) {
120 QMetaMethod mm = d->serviceMeta->method(i);
121
122
123 if (mm.methodType() == QMetaMethod::Signal) {
124 QByteArray sig(mm.methodSignature());
125 bool customType = false;
126 const QList<QByteArray> pTypes = mm.parameterTypes();
127 const int pTypesCount = pTypes.count();
128
129 // Ignore all QObject calls
130 const QMetaObject *mo = QObject::metaObject();
131 int qobjectIndex = mo->indexOfMethod(sig);
132 if (qobjectIndex >= 0)
133 continue;
134
135 // Detects custom types as passed arguments
136 for (int arg = 0; arg < pTypesCount; arg++) {
137 const QByteArray& type = pTypes[arg];
138 int variantType = QMetaType::type(type);
139 if (variantType >= QMetaType::User || variantType == QMetaType::QVariant) {
140 sig.replace(QByteArray(type), QByteArray("QDBusVariant"));
141 customType = true;
142 }
143 }
144
145 // Connects the service signal to the corresponding DBus service signal
146 if (customType) {
147 QByteArray signal = mm.methodSignature();
148 ServiceMetaSignalIntercepter *intercept =
149 new ServiceMetaSignalIntercepter(d->service, "2"+signal, this);
150 intercept->setMetaIndex(i);
151 }
152 }
153 }
154 }
155 }
156
157 /*!
158 Relays the activation to the DBus object signal with the given id and arguments list when the
159 intercepter for signals with custom arguments has been activated. This bypasses the metacall
160 which usually does the relaying for signals with standard arguments since no pre-connection
161 conversions are required.
162 */
163 void QServiceMetaObjectDBus::activateMetaSignal(int id, const QVariantList& args)
164 {
165 QMetaMethod method = d->serviceMeta->method(id);
166
167 // Converts the argument list to values supported by the QtDBus type system
168 QVariantList convertedList = args;
169 QByteArray sig(method.methodSignature());
170 QList<QByteArray> params = method.parameterTypes();
171
172 for (int i = 0; i < params.size(); i++) {
173 QVariant dbusVariant = args[i];
174
175 // Convert custom types
176 const QByteArray& type = params[i];
177 int variantType = QMetaType::type(type);
178 if (variantType >= QMetaType::User || variantType == QMetaType::QVariant) {
179 if (variantType != QMetaType::QVariant) {
180 // Wrap custom types in a QDBusVariant of the type name and
181 // a buffer of its variant-wrapped data
182 QByteArray buffer;
183 QDataStream stream(&buffer, QIODevice::ReadWrite | QIODevice::Append);
184 stream << args[i];
185
186 QServiceUserTypeDBus customType;
187 customType.typeName = type;
188 customType.variantBuffer = buffer;
189
190 QDBusVariant replacement(QVariant::fromValue(customType));
191 convertedList.replace(i, QVariant::fromValue(replacement));
192 }
193
194 sig.replace(QByteArray(type), QByteArray("QDBusVariant"));
195 }
196 }
197
198 // Activate the DBus object signal call
199 const int numArgs = convertedList.size();
200 QVarLengthArray<void *, 32> a( numArgs+1 );
201 a[0] = 0;
202
203 const QList<QByteArray> pTypes = method.parameterTypes();
204 for ( int arg = 0; arg < numArgs; ++arg ) {
205 if (pTypes.at(arg) == "QVariant")
206 a[arg+1] = (void *)&( convertedList[arg] );
207 else
208 a[arg+1] = (void *)( convertedList[arg].data() );
209 }
210
211 int dbusIndex = d->dbusMeta->indexOfSignal(sig);
212 QMetaObject::activate(this, dbusIndex, a.data());
213 }
214
215
216 /*!
217 Build a metaobject that represents the service object as a valid service that
218 satisfies the QtDBus type system.
219 */
220 const QMetaObject* QServiceMetaObjectDBus::dbusMetaObject(bool signalsObject) const
221 {
222 // Create a meta-object to represent all the contents of our service on DBus
223 QMetaObjectBuilder builder;
224
225 builder.setClassName(d->serviceMeta->className());
226 builder.setSuperClass(d->serviceMeta->superClass()); // needed?
227
228 const QMetaObject* mo = d->serviceMeta;
229 while (mo && strcmp(mo->className(), "QObject")) {
230 // Add our methods, signals and slots
231 for (int i = mo->methodOffset(); i < mo->methodCount(); i++) {
232 QMetaMethod mm = mo->method(i);
233
234 if (signalsObject && mm.methodType() != QMetaMethod::Signal)
235 continue;
236
237 // Convert QVariant and custom return types to QDBusVariants
238 QByteArray ret(mm.typeName());
239 const QByteArray& type = mm.typeName();
240 int variantType = QMetaType::type(type);
241 if (variantType >= QMetaType::User || variantType == QMetaType::QVariant) {
242 ret = QByteArray("QDBusVariant");
243 }
244
245 // Convert QVariant and custom argument types to QDBusVariants
246 QByteArray sig(mm.methodSignature());
247 const QList<QByteArray> pTypes = mm.parameterTypes();
248 const int pTypesCount = pTypes.count();
249 for (int i=0; i < pTypesCount; i++) {
250 const QByteArray& type = pTypes[i];
251 int variantType = QMetaType::type(type);
252 if (variantType >= QMetaType::User || variantType == QMetaType::QVariant) {
253 sig.replace(QByteArray(type), QByteArray("QDBusVariant"));
254 }
255 }
256
257 // Add a MetaMethod with converted signature to our builder
258 QMetaMethodBuilder method;
259 switch (mm.methodType()) {
260 case QMetaMethod::Method:
261 method = builder.addMethod(sig);
262 break;
263 case QMetaMethod::Slot:
264 method = builder.addSlot(sig);
265 break;
266 case QMetaMethod::Signal:
267 method = builder.addSignal(sig);
268 break;
269 default:
270 break;
271 }
272
273 // Make sure our built MetaMethod is identical, excluding conversion
274 method.setReturnType(ret);
275 method.setParameterNames(mm.parameterNames());
276 method.setTag(mm.tag());
277 method.setAccess(mm.access());
278 method.setAttributes(mm.attributes());
279 }
280
281 if (signalsObject)
282 return builder.toMetaObject();
283
284 // Add our property accessor methods
285 // NOTE: required because read/reset properties over DBus require adaptors
286 // otherwise a metacall won't be invoked as QMetaObject::ReadProperty
287 // or QMetaObject::ResetProperty
288 QMetaMethodBuilder readProp;
289 readProp = builder.addMethod(QByteArray("propertyRead(QString)"));
290 readProp.setReturnType(QByteArray("QDBusVariant"));
291 QList<QByteArray> params;
292 params << QByteArray("name");
293 readProp.setParameterNames(params);
294
295 QMetaMethodBuilder resetProp;
296 resetProp = builder.addMethod(QByteArray("propertyReset(QString)"));
297 QList<QByteArray> paramsReset;
298 paramsReset << QByteArray("name");
299 resetProp.setParameterNames(paramsReset);
300
301
302 // Add our properties/enums
303 int propCount = d->serviceMeta->propertyCount();
304 for (int i=0; i<propCount; i++) {
305 QMetaProperty mp = d->serviceMeta->property(i);
306
307 QMetaPropertyBuilder property = builder.addProperty(mp.name(), mp.typeName());
308 property.setReadable(mp.isReadable());
309 property.setWritable(mp.isWritable());
310 property.setResettable(mp.isResettable());
311 property.setDesignable(mp.isDesignable());
312 property.setScriptable(mp.isScriptable());
313 property.setStored(mp.isStored());
314 property.setEditable(mp.isEditable());
315 property.setUser(mp.isUser());
316 property.setStdCppSet(mp.hasStdCppSet());
317 property.setEnumOrFlag(mp.isEnumType());
318 }
319
320 // Needs Enumerators support when QtDBus supports
321
322 mo = mo->superClass();
323 }
324
325 // return our constructed dbus metaobject
326 return builder.toMetaObject();
327 }
328
329 /*!
330 Provide custom Q_OBJECT implementation of the metaObject
331 */
332 const QMetaObject* QServiceMetaObjectDBus::metaObject() const
333 {
334 // Provide our construected DBus service metaobject
335 return d->dbusMeta;
336 }
337
338 /*!
339 Overrides metacall which relays the DBus service call to the actual service
340 meta object. Positive return will indicate that the metacall was unsuccessful
341 */
342 int QServiceMetaObjectDBus::qt_metacall(QMetaObject::Call c, int id, void **a)
343 {
344 int dbusIndex = id;
345
346 // Relay the meta-object call to the service object
347 if (c == QMetaObject::InvokeMetaMethod) {
348 // METHOD CALL
349 QMetaMethod method = d->dbusMeta->method(id);
350
351 const bool isSignal = (method.methodType() == QMetaMethod::Signal);
352
353 ///////////////////// CHECK SPECIAL PROPERTY ///////////////////////
354 // This is required because property READ/RESET doesn't function
355 // as desired over DBus without the use of adaptors. Temporary
356 // methods propertyRead and propertyReset are added to the published
357 // meta object and relay the correct property call
358 QString methodName(QLatin1String(method.methodSignature().constData()));
359 methodName.truncate(methodName.indexOf(QLatin1String("(")));
360
361 if (methodName == QLatin1String("propertyRead")) {
362 QString propertyName = *reinterpret_cast<QString*>(a[1]);
363 int index = d->dbusMeta->indexOfProperty(propertyName.toLatin1().constData());
364 id = qt_metacall(QMetaObject::ReadProperty, index, a);
365 return id;
366
367 } else if (methodName == QLatin1String("propertyReset")) {
368 QString propertyName = *reinterpret_cast<QString*>(a[1]);
369 int index = d->dbusMeta->indexOfProperty(propertyName.toLatin1().constData());
370 id = qt_metacall(QMetaObject::ResetProperty, index, a);
371 return id;
372 }
373 ////////////////////////////////////////////////////////////////////
374
375 // Find the corresponding signature to our service object
376 QByteArray sig(method.methodSignature());
377 int count = methodName.size() + 1;
378 const QList<QByteArray> xTypes = method.parameterTypes();
379 const int xTypesCount = xTypes.count();
380 for (int i=0; i < xTypesCount; i++) {
381 const QByteArray& t = xTypes[i];
382 int variantType = QMetaType::type(t);
383
384 // Check for QVariants or custom types, represented as QDBusVariants
385 if (t == "QDBusVariant") {
386 // Convert QDBusVariant to QVariant
387 QVariant convert = QVariant(variantType, a[i+1]);
388 QDBusVariant dbusVariant = qvariant_cast<QDBusVariant>(convert);
389 QVariant variant = dbusVariant.variant();
390
391 // Is a custom argument if castable to QDBusArgument
392 bool hasCustomType = variant.canConvert<QDBusArgument>();
393 QByteArray replacement("QVariant");
394
395 // Custom types will have QDBusArgument streaming operators for
396 // the QServiceUserTypeDBus object. Extract the real type name
397 // and buffered QVariant from this
398 if (hasCustomType) {
399 QDBusArgument demarshall = variant.value<QDBusArgument>();
400 QServiceUserTypeDBus userType = qdbus_cast<QServiceUserTypeDBus>(demarshall);
401 *reinterpret_cast<QVariant*>(a[i+1]) = userType.variantBuffer;
402
403 replacement = userType.typeName;
404 }
405
406 // Replace "QDBusVariant" with "QVariant" or custom type name
407 sig.replace(count, 12, replacement);
408 count += replacement.size();
409
410 } else {
411 // Supported type so skip this paramater
412 count += t.size();
413 }
414
415 // Skips the comma if not last parameter
416 if (i < xTypesCount)
417 count += 1;
418 }
419
420 // Find the corresponding method metaindex to our service object
421 id = d->serviceMeta->indexOfMethod(sig);
422 QMetaMethod mm = d->serviceMeta->method(id);
423
424 const QList<QByteArray> pTypes = mm.parameterTypes();
425 const int pTypesCount = pTypes.count();
426
427 const char* typeNames[] = {0,0,0,0,0,0,0,0,0,0};
428 const void* params[] = {0,0,0,0,0,0,0,0,0,0};
429 bool hasCustomType = false;
430
431 // Process arguments
432 for (int i=0; i < pTypesCount; i++) {
433 const QByteArray& t = pTypes[i];
434 int variantType = QMetaType::type(t);
435
436 if (variantType >= QMetaType::User) {
437 // Custom argument
438 QVariant convert = QVariant(QVariant::ByteArray, a[i+1]);
439 QByteArray buffer = convert.toByteArray();
440 QDataStream stream(&buffer, QIODevice::ReadWrite);
441
442 // Load our buffered variant-wrapped custom type
443 QVariant *customType = new QVariant(variantType, (const void*)0);
444 QMetaType::load(stream, QMetaType::QVariant, customType);
445
446 typeNames[i] = customType->typeName();
447 params[i] = customType->constData();
448 hasCustomType = true;
449 } else {
450 typeNames[i] = t.constData();
451 params[i] = a[i+1];
452 }
453 }
454
455 // Check if this is a signal emit and activate
456 if (isSignal) {
457 QMetaObject::activate(this, dbusIndex, a);
458 return id;
459 }
460
461 // Check for custom return types and make the metacall
462 const QByteArray& type = mm.typeName();
463 int retType = QMetaType::type(type);
464 if (retType >= QMetaType::User) {
465 // Invoke the object method directly for custom return types
466 bool result = false;
467 QVariant returnValue = QVariant(retType, (const void*)0);
468 QGenericReturnArgument ret(type, returnValue.data());
469 result = mm.invoke(d->service, ret,
470 QGenericArgument(typeNames[0], params[0]),
471 QGenericArgument(typeNames[1], params[1]),
472 QGenericArgument(typeNames[2], params[2]),
473 QGenericArgument(typeNames[3], params[3]),
474 QGenericArgument(typeNames[4], params[4]),
475 QGenericArgument(typeNames[5], params[5]),
476 QGenericArgument(typeNames[6], params[6]),
477 QGenericArgument(typeNames[7], params[7]),
478 QGenericArgument(typeNames[8], params[8]),
479 QGenericArgument(typeNames[9], params[9]));
480
481 if (result) {
482 // Wrap custom return type in a QDBusVariant of the type
483 // and a buffer of its variant-wrapped data
484 QByteArray buffer;
485 QDataStream stream(&buffer, QIODevice::WriteOnly | QIODevice::Append);
486 stream << returnValue;
487
488 QServiceUserTypeDBus customType;
489 customType.typeName = type;
490 customType.variantBuffer = buffer;
491
492 QDBusVariant replacement(QVariant::fromValue(customType));
493 *reinterpret_cast<QDBusVariant*>(a[0]) = replacement;
494
495 // Return negative id to say metacall was handled externally
496 return -1;
497 }
498
499 } else {
500 // Void or standard return types
501 if (hasCustomType == true) {
502 // Invoke the object method directly for custom arguments
503 bool result = false;
504 result = mm.invoke(d->service,
505 QGenericArgument(typeNames[0], params[0]),
506 QGenericArgument(typeNames[1], params[1]),
507 QGenericArgument(typeNames[2], params[2]),
508 QGenericArgument(typeNames[3], params[3]),
509 QGenericArgument(typeNames[4], params[4]),
510 QGenericArgument(typeNames[5], params[5]),
511 QGenericArgument(typeNames[6], params[6]),
512 QGenericArgument(typeNames[7], params[7]),
513 QGenericArgument(typeNames[8], params[8]),
514 QGenericArgument(typeNames[9], params[9]));
515 if (result) {
516 // Return negative id to say metacall was handled externally
517 return -1;
518 }
519 } else {
520 // Relay standard metacall to service object
521 id = d->service->qt_metacall(c, id, a);
522 }
523 }
524
525 } else {
526 // PROPERTY CALL
527
528 // Find the corresponding property metaindex of our service object
529 QMetaProperty property = d->dbusMeta->property(id);
530 QByteArray name(property.name());
531 id = d->serviceMeta->indexOfProperty(name);
532
533 if (c == QMetaObject::ReadProperty) {
534 // Convert to DBusVariant
535 QMetaProperty mp = d->serviceMeta->property(id);
536 QVariant value = mp.read(d->service);
537 QDBusVariant replacement(value);
538 *reinterpret_cast<QDBusVariant*>(a[0]) = replacement;
539
540 // Return negative id to say metacall was handled externally
541 return -1;
542 }
543
544 // Metacall our service object property
545 id = d->service->qt_metacall(c, id, a);
546 }
547
548 return id;
549 }
550
551 void *QServiceMetaObjectDBus::qt_metacast(const char* className)
552 {
553 if (!className) return 0;
554 //this object should not be castable to anything but QObject
555 return QObject::qt_metacast(className);
556 }
557
558 QDBusArgument &operator<<(QDBusArgument &argument, const QServiceUserTypeDBus &myType)
559 {
560 argument.beginStructure();
561 argument << myType.typeName << myType.variantBuffer;
562 argument.endStructure();
563 return argument;
564 }
565
566 const QDBusArgument &operator>>(const QDBusArgument &argument, QServiceUserTypeDBus &myType)
567 {
568 argument.beginStructure();
569 argument >> myType.typeName >> myType.variantBuffer;
570 argument.endStructure();
571 return argument;
572 }
573
574 QT_END_NAMESPACE