2011-04-27 10:05:43 +00:00
|
|
|
/****************************************************************************
|
|
|
|
**
|
2016-01-15 07:08:27 +00:00
|
|
|
** Copyright (C) 2016 The Qt Company Ltd.
|
|
|
|
** Contact: https://www.qt.io/licensing/
|
2011-04-27 10:05:43 +00:00
|
|
|
**
|
|
|
|
** This file is part of the QtDBus module of the Qt Toolkit.
|
|
|
|
**
|
2016-01-15 07:08:27 +00:00
|
|
|
** $QT_BEGIN_LICENSE:LGPL$
|
2012-09-19 12:28:29 +00:00
|
|
|
** Commercial License Usage
|
|
|
|
** Licensees holding valid commercial Qt licenses may use this file in
|
|
|
|
** accordance with the commercial license agreement provided with the
|
|
|
|
** Software or, alternatively, in accordance with the terms contained in
|
2015-01-28 08:44:43 +00:00
|
|
|
** a written agreement between you and The Qt Company. For licensing terms
|
2016-01-15 07:08:27 +00:00
|
|
|
** and conditions see https://www.qt.io/terms-conditions. For further
|
|
|
|
** information use the contact form at https://www.qt.io/contact-us.
|
2012-09-19 12:28:29 +00:00
|
|
|
**
|
2011-04-27 10:05:43 +00:00
|
|
|
** GNU Lesser General Public License Usage
|
2012-09-19 12:28:29 +00:00
|
|
|
** Alternatively, this file may be used under the terms of the GNU Lesser
|
2016-01-15 07:08:27 +00:00
|
|
|
** General Public License version 3 as published by the Free Software
|
|
|
|
** Foundation and appearing in the file LICENSE.LGPL3 included in the
|
|
|
|
** packaging of this file. Please review the following information to
|
|
|
|
** ensure the GNU Lesser General Public License version 3 requirements
|
|
|
|
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
|
2011-04-27 10:05:43 +00:00
|
|
|
**
|
2016-01-15 07:08:27 +00:00
|
|
|
** GNU General Public License Usage
|
|
|
|
** Alternatively, this file may be used under the terms of the GNU
|
|
|
|
** General Public License version 2.0 or (at your option) the GNU General
|
|
|
|
** Public license version 3 or any later version approved by the KDE Free
|
|
|
|
** Qt Foundation. The licenses are as published by the Free Software
|
|
|
|
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
|
|
|
|
** included in the packaging of this file. Please review the following
|
|
|
|
** information to ensure the GNU General Public License requirements will
|
|
|
|
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
|
|
|
|
** https://www.gnu.org/licenses/gpl-3.0.html.
|
2011-04-27 10:05:43 +00:00
|
|
|
**
|
|
|
|
** $QT_END_LICENSE$
|
|
|
|
**
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "qdbusinterface.h"
|
2012-09-18 22:06:49 +00:00
|
|
|
#include "qdbusinterface_p.h"
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
#include "qdbus_symbols_p.h"
|
|
|
|
#include <QtCore/qpointer.h>
|
|
|
|
#include <QtCore/qstringlist.h>
|
|
|
|
|
|
|
|
#include "qdbusmetatype_p.h"
|
|
|
|
#include "qdbusconnection_p.h"
|
|
|
|
|
|
|
|
#ifndef QT_NO_DBUS
|
|
|
|
|
|
|
|
QT_BEGIN_NAMESPACE
|
|
|
|
|
|
|
|
static void copyArgument(void *to, int id, const QVariant &arg)
|
|
|
|
{
|
2020-09-09 11:42:35 +00:00
|
|
|
if (id == arg.metaType().id()) {
|
2011-04-27 10:05:43 +00:00
|
|
|
switch (id) {
|
2019-12-02 16:54:48 +00:00
|
|
|
case QMetaType::Bool:
|
2011-04-27 10:05:43 +00:00
|
|
|
*reinterpret_cast<bool *>(to) = arg.toBool();
|
|
|
|
return;
|
|
|
|
|
|
|
|
case QMetaType::UChar:
|
2019-12-06 12:19:37 +00:00
|
|
|
*reinterpret_cast<uchar *>(to) = qvariant_cast<uchar>(arg);
|
2011-04-27 10:05:43 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
case QMetaType::Short:
|
2019-12-06 12:19:37 +00:00
|
|
|
*reinterpret_cast<short *>(to) = qvariant_cast<short>(arg);
|
2011-04-27 10:05:43 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
case QMetaType::UShort:
|
2019-12-06 12:19:37 +00:00
|
|
|
*reinterpret_cast<ushort *>(to) = qvariant_cast<ushort>(arg);
|
2011-04-27 10:05:43 +00:00
|
|
|
return;
|
|
|
|
|
2019-12-02 16:54:48 +00:00
|
|
|
case QMetaType::Int:
|
2011-04-27 10:05:43 +00:00
|
|
|
*reinterpret_cast<int *>(to) = arg.toInt();
|
|
|
|
return;
|
|
|
|
|
2019-12-02 16:54:48 +00:00
|
|
|
case QMetaType::UInt:
|
2011-04-27 10:05:43 +00:00
|
|
|
*reinterpret_cast<uint *>(to) = arg.toUInt();
|
|
|
|
return;
|
|
|
|
|
2019-12-02 16:54:48 +00:00
|
|
|
case QMetaType::LongLong:
|
2011-04-27 10:05:43 +00:00
|
|
|
*reinterpret_cast<qlonglong *>(to) = arg.toLongLong();
|
|
|
|
return;
|
|
|
|
|
2019-12-02 16:54:48 +00:00
|
|
|
case QMetaType::ULongLong:
|
2011-04-27 10:05:43 +00:00
|
|
|
*reinterpret_cast<qulonglong *>(to) = arg.toULongLong();
|
|
|
|
return;
|
|
|
|
|
2019-12-02 16:54:48 +00:00
|
|
|
case QMetaType::Double:
|
2011-04-27 10:05:43 +00:00
|
|
|
*reinterpret_cast<double *>(to) = arg.toDouble();
|
|
|
|
return;
|
|
|
|
|
2019-12-02 16:54:48 +00:00
|
|
|
case QMetaType::QString:
|
2011-04-27 10:05:43 +00:00
|
|
|
*reinterpret_cast<QString *>(to) = arg.toString();
|
|
|
|
return;
|
|
|
|
|
2019-12-02 16:54:48 +00:00
|
|
|
case QMetaType::QByteArray:
|
2011-04-27 10:05:43 +00:00
|
|
|
*reinterpret_cast<QByteArray *>(to) = arg.toByteArray();
|
|
|
|
return;
|
|
|
|
|
2019-12-02 16:54:48 +00:00
|
|
|
case QMetaType::QStringList:
|
2011-04-27 10:05:43 +00:00
|
|
|
*reinterpret_cast<QStringList *>(to) = arg.toStringList();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-09-09 11:42:35 +00:00
|
|
|
if (id == QDBusMetaTypeId::variant().id()) {
|
2019-12-06 12:19:37 +00:00
|
|
|
*reinterpret_cast<QDBusVariant *>(to) = qvariant_cast<QDBusVariant>(arg);
|
2011-04-27 10:05:43 +00:00
|
|
|
return;
|
2020-09-09 11:42:35 +00:00
|
|
|
} else if (id == QDBusMetaTypeId::objectpath().id()) {
|
2019-12-06 12:19:37 +00:00
|
|
|
*reinterpret_cast<QDBusObjectPath *>(to) = qvariant_cast<QDBusObjectPath>(arg);
|
2011-04-27 10:05:43 +00:00
|
|
|
return;
|
2020-09-09 11:42:35 +00:00
|
|
|
} else if (id == QDBusMetaTypeId::signature().id()) {
|
2019-12-06 12:19:37 +00:00
|
|
|
*reinterpret_cast<QDBusSignature *>(to) = qvariant_cast<QDBusSignature>(arg);
|
2011-04-27 10:05:43 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// those above are the only types possible
|
|
|
|
// the demarshaller code doesn't demarshall anything else
|
|
|
|
qFatal("Found a decoded basic type in a D-Bus reply that shouldn't be there");
|
|
|
|
}
|
|
|
|
|
|
|
|
// if we got here, it's either an un-dermarshalled type or a mismatch
|
2020-09-09 11:42:35 +00:00
|
|
|
if (arg.metaType() != QDBusMetaTypeId::argument()) {
|
2011-04-27 10:05:43 +00:00
|
|
|
// it's a mismatch
|
|
|
|
//qWarning?
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// is this type registered?
|
2020-09-09 11:42:35 +00:00
|
|
|
const char *userSignature = QDBusMetaType::typeToSignature(QMetaType(id));
|
2011-04-27 10:05:43 +00:00
|
|
|
if (!userSignature || !*userSignature) {
|
|
|
|
// type not registered
|
|
|
|
//qWarning?
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// is it the same signature?
|
2019-12-06 12:19:37 +00:00
|
|
|
QDBusArgument dbarg = qvariant_cast<QDBusArgument>(arg);
|
2011-04-27 10:05:43 +00:00
|
|
|
if (dbarg.currentSignature() != QLatin1String(userSignature)) {
|
|
|
|
// not the same signature, another mismatch
|
|
|
|
//qWarning?
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// we can demarshall
|
2020-09-09 10:30:11 +00:00
|
|
|
QDBusMetaType::demarshall(dbarg, QMetaType(id), to);
|
2011-04-27 10:05:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QDBusInterfacePrivate::QDBusInterfacePrivate(const QString &serv, const QString &p,
|
|
|
|
const QString &iface, const QDBusConnection &con)
|
2019-11-22 13:46:58 +00:00
|
|
|
: QDBusAbstractInterfacePrivate(serv, p, iface, con, true), metaObject(nullptr)
|
2011-04-27 10:05:43 +00:00
|
|
|
{
|
|
|
|
// QDBusAbstractInterfacePrivate's constructor checked the parameters for us
|
|
|
|
if (connection.isConnected()) {
|
|
|
|
metaObject = connectionPrivate()->findMetaObject(service, path, interface, lastError);
|
|
|
|
|
|
|
|
if (!metaObject) {
|
|
|
|
// creation failed, somehow
|
|
|
|
// most common causes are that the service doesn't exist or doesn't support introspection
|
|
|
|
// those are not fatal errors, so we continue working
|
|
|
|
|
|
|
|
if (!lastError.isValid())
|
|
|
|
lastError = QDBusError(QDBusError::InternalError, QLatin1String("Unknown error"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QDBusInterfacePrivate::~QDBusInterfacePrivate()
|
|
|
|
{
|
|
|
|
if (metaObject && !metaObject->cached)
|
|
|
|
delete metaObject;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\class QDBusInterface
|
|
|
|
\inmodule QtDBus
|
|
|
|
\since 4.2
|
|
|
|
|
|
|
|
\brief The QDBusInterface class is a proxy for interfaces on remote objects.
|
|
|
|
|
|
|
|
QDBusInterface is a generic accessor class that is used to place calls to remote objects,
|
|
|
|
connect to signals exported by remote objects and get/set the value of remote properties. This
|
|
|
|
class is useful for dynamic access to remote objects: that is, when you do not have a generated
|
|
|
|
code that represents the remote interface.
|
|
|
|
|
|
|
|
Calls are usually placed by using the call() function, which constructs the message, sends it
|
|
|
|
over the bus, waits for the reply and decodes the reply. Signals are connected to by using the
|
|
|
|
normal QObject::connect() function. Finally, properties are accessed using the
|
|
|
|
QObject::property() and QObject::setProperty() functions.
|
|
|
|
|
|
|
|
The following code snippet demonstrates how to perform a
|
|
|
|
mathematical operation of \tt{"2 + 2"} in a remote application
|
|
|
|
called \c com.example.Calculator, accessed via the session bus.
|
|
|
|
|
2012-05-07 12:03:02 +00:00
|
|
|
\snippet code/src_qdbus_qdbusinterface.cpp 0
|
2011-04-27 10:05:43 +00:00
|
|
|
|
2012-12-28 18:09:39 +00:00
|
|
|
\sa {Qt D-Bus XML compiler (qdbusxml2cpp)}
|
2011-04-27 10:05:43 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Creates a dynamic QDBusInterface object associated with the
|
|
|
|
interface \a interface on object at path \a path on service \a
|
|
|
|
service, using the given \a connection. If \a interface is an
|
|
|
|
empty string, the object created will refer to the merging of all
|
2019-11-24 20:11:25 +00:00
|
|
|
interfaces found by introspecting that object. Otherwise if
|
|
|
|
\a interface is not empty, the QDBusInterface object will be cached
|
|
|
|
to speedup further creations of the same interface.
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
\a parent is passed to the base class constructor.
|
|
|
|
|
|
|
|
If the remote service \a service is not present or if an error
|
|
|
|
occurs trying to obtain the description of the remote interface
|
|
|
|
\a interface, the object created will not be valid (see
|
|
|
|
isValid()).
|
|
|
|
*/
|
|
|
|
QDBusInterface::QDBusInterface(const QString &service, const QString &path, const QString &interface,
|
|
|
|
const QDBusConnection &connection, QObject *parent)
|
|
|
|
: QDBusAbstractInterface(*new QDBusInterfacePrivate(service, path, interface, connection),
|
|
|
|
parent)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Destroy the object interface and frees up any resource used.
|
|
|
|
*/
|
|
|
|
QDBusInterface::~QDBusInterface()
|
|
|
|
{
|
|
|
|
// resources are freed in QDBusInterfacePrivate::~QDBusInterfacePrivate()
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\internal
|
|
|
|
Overrides QObject::metaObject to return our own copy.
|
|
|
|
*/
|
|
|
|
const QMetaObject *QDBusInterface::metaObject() const
|
|
|
|
{
|
|
|
|
return d_func()->metaObject ? d_func()->metaObject : &QDBusAbstractInterface::staticMetaObject;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\internal
|
|
|
|
Override QObject::qt_metacast to catch the interface name too.
|
|
|
|
*/
|
|
|
|
void *QDBusInterface::qt_metacast(const char *_clname)
|
|
|
|
{
|
2019-11-22 13:46:58 +00:00
|
|
|
if (!_clname) return nullptr;
|
2011-04-27 10:05:43 +00:00
|
|
|
if (!strcmp(_clname, "QDBusInterface"))
|
|
|
|
return static_cast<void*>(const_cast<QDBusInterface*>(this));
|
|
|
|
if (d_func()->interface.toLatin1() == _clname)
|
|
|
|
return static_cast<void*>(const_cast<QDBusInterface*>(this));
|
|
|
|
return QDBusAbstractInterface::qt_metacast(_clname);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
\internal
|
|
|
|
Dispatch the call through the private.
|
|
|
|
*/
|
|
|
|
int QDBusInterface::qt_metacall(QMetaObject::Call _c, int _id, void **_a)
|
|
|
|
{
|
|
|
|
_id = QDBusAbstractInterface::qt_metacall(_c, _id, _a);
|
|
|
|
if (_id < 0 || !d_func()->isValid || !d_func()->metaObject)
|
|
|
|
return _id;
|
|
|
|
return d_func()->metacall(_c, _id, _a);
|
|
|
|
}
|
|
|
|
|
|
|
|
int QDBusInterfacePrivate::metacall(QMetaObject::Call c, int id, void **argv)
|
|
|
|
{
|
|
|
|
Q_Q(QDBusInterface);
|
|
|
|
|
|
|
|
if (c == QMetaObject::InvokeMetaMethod) {
|
|
|
|
int offset = metaObject->methodOffset();
|
|
|
|
QMetaMethod mm = metaObject->method(id + offset);
|
|
|
|
|
|
|
|
if (mm.methodType() == QMetaMethod::Signal) {
|
|
|
|
// signal relay from D-Bus world to Qt world
|
|
|
|
QMetaObject::activate(q, metaObject, id, argv);
|
|
|
|
|
|
|
|
} else if (mm.methodType() == QMetaMethod::Slot || mm.methodType() == QMetaMethod::Method) {
|
|
|
|
// method call relay from Qt world to D-Bus world
|
|
|
|
// get D-Bus equivalent signature
|
2012-02-19 12:25:04 +00:00
|
|
|
QString methodName = QString::fromLatin1(mm.name());
|
2011-04-27 10:05:43 +00:00
|
|
|
const int *inputTypes = metaObject->inputTypesForMethod(id);
|
|
|
|
int inputTypesCount = *inputTypes;
|
|
|
|
|
|
|
|
// we will assume that the input arguments were passed correctly
|
|
|
|
QVariantList args;
|
2015-06-22 17:12:40 +00:00
|
|
|
args.reserve(inputTypesCount);
|
2011-04-27 10:05:43 +00:00
|
|
|
int i = 1;
|
|
|
|
for ( ; i <= inputTypesCount; ++i)
|
2020-06-15 07:09:08 +00:00
|
|
|
args << QVariant(QMetaType(inputTypes[i]), argv[i]);
|
2011-04-27 10:05:43 +00:00
|
|
|
|
|
|
|
// make the call
|
|
|
|
QDBusMessage reply = q->callWithArgumentList(QDBus::Block, methodName, args);
|
|
|
|
|
|
|
|
if (reply.type() == QDBusMessage::ReplyMessage) {
|
|
|
|
// attempt to demarshall the return values
|
|
|
|
args = reply.arguments();
|
|
|
|
QVariantList::ConstIterator it = args.constBegin();
|
|
|
|
const int *outputTypes = metaObject->outputTypesForMethod(id);
|
|
|
|
int outputTypesCount = *outputTypes++;
|
|
|
|
|
2012-03-20 18:45:00 +00:00
|
|
|
if (mm.returnType() != QMetaType::UnknownType && mm.returnType() != QMetaType::Void) {
|
2011-04-27 10:05:43 +00:00
|
|
|
// this method has a return type
|
|
|
|
if (argv[0] && it != args.constEnd())
|
|
|
|
copyArgument(argv[0], *outputTypes++, *it);
|
|
|
|
|
|
|
|
// skip this argument even if we didn't copy it
|
|
|
|
--outputTypesCount;
|
|
|
|
++it;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int j = 0; j < outputTypesCount && it != args.constEnd(); ++i, ++j, ++it) {
|
|
|
|
copyArgument(argv[i], outputTypes[j], *it);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// done
|
2012-03-06 21:23:18 +00:00
|
|
|
lastError = QDBusError(reply);
|
2011-04-27 10:05:43 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
QT_END_NAMESPACE
|
|
|
|
|
|
|
|
#endif // QT_NO_DBUS
|