| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|---|
| 4 | ** Contact: Qt Software Information (qt-info@nokia.com)
|
|---|
| 5 | **
|
|---|
| 6 | ** This file is part of the QtDBus module of the Qt Toolkit.
|
|---|
| 7 | **
|
|---|
| 8 | ** $QT_BEGIN_LICENSE:LGPL$
|
|---|
| 9 | ** Commercial Usage
|
|---|
| 10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
|---|
| 11 | ** accordance with the Qt Commercial License Agreement provided with the
|
|---|
| 12 | ** Software or, alternatively, in accordance with the terms contained in
|
|---|
| 13 | ** a written agreement between you and Nokia.
|
|---|
| 14 | **
|
|---|
| 15 | ** GNU Lesser General Public License Usage
|
|---|
| 16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
|---|
| 17 | ** General Public License version 2.1 as published by the Free Software
|
|---|
| 18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
|---|
| 19 | ** packaging of this file. Please review the following information to
|
|---|
| 20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
|---|
| 21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|---|
| 22 | **
|
|---|
| 23 | ** In addition, as a special exception, Nokia gives you certain
|
|---|
| 24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
|---|
| 25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
|---|
| 26 | ** 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 are unsure which license is appropriate for your use, please
|
|---|
| 37 | ** contact the sales department at qt-sales@nokia.com.
|
|---|
| 38 | ** $QT_END_LICENSE$
|
|---|
| 39 | **
|
|---|
| 40 | ****************************************************************************/
|
|---|
| 41 |
|
|---|
| 42 | #include "qdbusabstractinterface.h"
|
|---|
| 43 | #include "qdbusabstractinterface_p.h"
|
|---|
| 44 |
|
|---|
| 45 | #include "qdbusargument.h"
|
|---|
| 46 | #include "qdbuspendingcall.h"
|
|---|
| 47 | #include "qdbusmetaobject_p.h"
|
|---|
| 48 | #include "qdbusmetatype_p.h"
|
|---|
| 49 | #include "qdbusutil_p.h"
|
|---|
| 50 |
|
|---|
| 51 | #include <qdebug.h>
|
|---|
| 52 |
|
|---|
| 53 | QT_BEGIN_NAMESPACE
|
|---|
| 54 |
|
|---|
| 55 | QDBusAbstractInterfacePrivate::QDBusAbstractInterfacePrivate(const QString &serv,
|
|---|
| 56 | const QString &p,
|
|---|
| 57 | const QString &iface,
|
|---|
| 58 | const QDBusConnection& con,
|
|---|
| 59 | bool isDynamic)
|
|---|
| 60 | : connection(con), service(serv), path(p), interface(iface), isValid(true)
|
|---|
| 61 | {
|
|---|
| 62 | if (isDynamic) {
|
|---|
| 63 | // QDBusInterface: service and object path can't be empty, but interface can
|
|---|
| 64 | #if 0
|
|---|
| 65 | Q_ASSERT_X(QDBusUtil::isValidBusName(service),
|
|---|
| 66 | "QDBusInterface::QDBusInterface", "Invalid service name");
|
|---|
| 67 | Q_ASSERT_X(QDBusUtil::isValidObjectPath(path),
|
|---|
| 68 | "QDBusInterface::QDBusInterface", "Invalid object path given");
|
|---|
| 69 | Q_ASSERT_X(interface.isEmpty() || QDBusUtil::isValidInterfaceName(interface),
|
|---|
| 70 | "QDBusInterface::QDBusInterface", "Invalid interface name");
|
|---|
| 71 | #else
|
|---|
| 72 | if (!QDBusUtil::isValidBusName(service)) {
|
|---|
| 73 | lastError = QDBusError(QDBusError::Disconnected,
|
|---|
| 74 | QLatin1String("Invalid service name"));
|
|---|
| 75 | isValid = false;
|
|---|
| 76 | } else if (!QDBusUtil::isValidObjectPath(path)) {
|
|---|
| 77 | lastError = QDBusError(QDBusError::Disconnected,
|
|---|
| 78 | QLatin1String("Invalid object name given"));
|
|---|
| 79 | isValid = false;
|
|---|
| 80 | } else if (!interface.isEmpty() && !QDBusUtil::isValidInterfaceName(interface)) {
|
|---|
| 81 | lastError = QDBusError(QDBusError::Disconnected,
|
|---|
| 82 | QLatin1String("Invalid interface name"));
|
|---|
| 83 | isValid = false;
|
|---|
| 84 | }
|
|---|
| 85 | #endif
|
|---|
| 86 | } else {
|
|---|
| 87 | // all others: service and path can be empty here, but interface can't
|
|---|
| 88 | #if 0
|
|---|
| 89 | Q_ASSERT_X(service.isEmpty() || QDBusUtil::isValidBusName(service),
|
|---|
| 90 | "QDBusAbstractInterface::QDBusAbstractInterface", "Invalid service name");
|
|---|
| 91 | Q_ASSERT_X(path.isEmpty() || QDBusUtil::isValidObjectPath(path),
|
|---|
| 92 | "QDBusAbstractInterface::QDBusAbstractInterface", "Invalid object path given");
|
|---|
| 93 | Q_ASSERT_X(QDBusUtil::isValidInterfaceName(interface),
|
|---|
| 94 | "QDBusAbstractInterface::QDBusAbstractInterface", "Invalid interface class!");
|
|---|
| 95 | #else
|
|---|
| 96 | if (!service.isEmpty() && !QDBusUtil::isValidBusName(service)) {
|
|---|
| 97 | lastError = QDBusError(QDBusError::Disconnected,
|
|---|
| 98 | QLatin1String("Invalid service name"));
|
|---|
| 99 | isValid = false;
|
|---|
| 100 | } else if (!path.isEmpty() && !QDBusUtil::isValidObjectPath(path)) {
|
|---|
| 101 | lastError = QDBusError(QDBusError::Disconnected,
|
|---|
| 102 | QLatin1String("Invalid object path given"));
|
|---|
| 103 | isValid = false;
|
|---|
| 104 | } else if (!QDBusUtil::isValidInterfaceName(interface)) {
|
|---|
| 105 | lastError = QDBusError(QDBusError::Disconnected,
|
|---|
| 106 | QLatin1String("Invalid interface class"));
|
|---|
| 107 | isValid = false;
|
|---|
| 108 | }
|
|---|
| 109 | #endif
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 | if (!isValid)
|
|---|
| 113 | return;
|
|---|
| 114 |
|
|---|
| 115 | if (!connection.isConnected()) {
|
|---|
| 116 | lastError = QDBusError(QDBusError::Disconnected,
|
|---|
| 117 | QLatin1String("Not connected to D-Bus server"));
|
|---|
| 118 | isValid = false;
|
|---|
| 119 | } else if (!service.isEmpty()) {
|
|---|
| 120 | currentOwner = connectionPrivate()->getNameOwner(service); // verify the name owner
|
|---|
| 121 | if (currentOwner.isEmpty()) {
|
|---|
| 122 | isValid = false;
|
|---|
| 123 | lastError = connectionPrivate()->lastError;
|
|---|
| 124 | }
|
|---|
| 125 | }
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | QVariant QDBusAbstractInterfacePrivate::property(const QMetaProperty &mp) const
|
|---|
| 129 | {
|
|---|
| 130 | if (!connection.isConnected()) // not connected
|
|---|
| 131 | return QVariant();
|
|---|
| 132 |
|
|---|
| 133 | // is this metatype registered?
|
|---|
| 134 | int mid;
|
|---|
| 135 | const char *expectedSignature;
|
|---|
| 136 | if (mp.type() == QVariant::LastType) {
|
|---|
| 137 | // We're asking to read a QVariant
|
|---|
| 138 | mid = qMetaTypeId<QDBusVariant>();
|
|---|
| 139 | expectedSignature = "v";
|
|---|
| 140 | } else {
|
|---|
| 141 | mid = QMetaType::type(mp.typeName());
|
|---|
| 142 | expectedSignature = QDBusMetaType::typeToSignature(mid);
|
|---|
| 143 | if (expectedSignature == 0) {
|
|---|
| 144 | qWarning("QDBusAbstractInterface: type %s must be registered with QtDBus before it can be "
|
|---|
| 145 | "used to read property %s.%s",
|
|---|
| 146 | mp.typeName(), qPrintable(interface), mp.name());
|
|---|
| 147 | return QVariant();
|
|---|
| 148 | }
|
|---|
| 149 | }
|
|---|
| 150 |
|
|---|
| 151 | // try to read this property
|
|---|
| 152 | QDBusMessage msg = QDBusMessage::createMethodCall(service, path,
|
|---|
| 153 | QLatin1String(DBUS_INTERFACE_PROPERTIES),
|
|---|
| 154 | QLatin1String("Get"));
|
|---|
| 155 | msg << interface << QString::fromUtf8(mp.name());
|
|---|
| 156 | QDBusMessage reply = connection.call(msg, QDBus::Block);
|
|---|
| 157 |
|
|---|
| 158 | if (reply.type() != QDBusMessage::ReplyMessage) {
|
|---|
| 159 | lastError = reply;
|
|---|
| 160 | return QVariant();
|
|---|
| 161 | }
|
|---|
| 162 | if (reply.signature() != QLatin1String("v")) {
|
|---|
| 163 | QString errmsg = QLatin1String("Invalid signature `%1' in return from call to "
|
|---|
| 164 | DBUS_INTERFACE_PROPERTIES);
|
|---|
| 165 | lastError = QDBusError(QDBusError::InvalidSignature, errmsg.arg(reply.signature()));
|
|---|
| 166 | return QVariant();
|
|---|
| 167 | }
|
|---|
| 168 |
|
|---|
| 169 | QByteArray foundSignature;
|
|---|
| 170 | const char *foundType = 0;
|
|---|
| 171 | QVariant value = qvariant_cast<QDBusVariant>(reply.arguments().at(0)).variant();
|
|---|
| 172 |
|
|---|
| 173 | if (value.userType() == mid)
|
|---|
| 174 | return value; // simple match
|
|---|
| 175 |
|
|---|
| 176 | if (value.userType() == qMetaTypeId<QDBusArgument>()) {
|
|---|
| 177 | QDBusArgument arg = qvariant_cast<QDBusArgument>(value);
|
|---|
| 178 |
|
|---|
| 179 | foundType = "user type";
|
|---|
| 180 | foundSignature = arg.currentSignature().toLatin1();
|
|---|
| 181 | if (foundSignature == expectedSignature) {
|
|---|
| 182 | void *null = 0;
|
|---|
| 183 | QVariant result(mid, null);
|
|---|
| 184 | QDBusMetaType::demarshall(arg, mid, result.data());
|
|---|
| 185 |
|
|---|
| 186 | if (mp.type() == QVariant::LastType)
|
|---|
| 187 | // special case: QVariant
|
|---|
| 188 | return qvariant_cast<QDBusVariant>(result).variant();
|
|---|
| 189 | return result;
|
|---|
| 190 | }
|
|---|
| 191 | } else {
|
|---|
| 192 | foundType = value.typeName();
|
|---|
| 193 | foundSignature = QDBusMetaType::typeToSignature(value.userType());
|
|---|
| 194 | }
|
|---|
| 195 |
|
|---|
| 196 | // there was an error...
|
|---|
| 197 | QString errmsg = QLatin1String("Unexpected `%1' (%2) when retrieving property `%3.%4' "
|
|---|
| 198 | "(expected type `%5' (%6))");
|
|---|
| 199 | lastError = QDBusError(QDBusError::InvalidSignature,
|
|---|
| 200 | errmsg.arg(QString::fromLatin1(foundType),
|
|---|
| 201 | QString::fromLatin1(foundSignature),
|
|---|
| 202 | interface,
|
|---|
| 203 | QString::fromUtf8(mp.name()),
|
|---|
| 204 | QString::fromLatin1(mp.typeName()),
|
|---|
| 205 | QString::fromLatin1(expectedSignature)));
|
|---|
| 206 | return QVariant();
|
|---|
| 207 | }
|
|---|
| 208 |
|
|---|
| 209 | void QDBusAbstractInterfacePrivate::setProperty(const QMetaProperty &mp, const QVariant &value)
|
|---|
| 210 | {
|
|---|
| 211 | if (!connection.isConnected()) // not connected
|
|---|
| 212 | return;
|
|---|
| 213 |
|
|---|
| 214 | // send the value
|
|---|
| 215 | QDBusMessage msg = QDBusMessage::createMethodCall(service, path,
|
|---|
| 216 | QLatin1String(DBUS_INTERFACE_PROPERTIES),
|
|---|
| 217 | QLatin1String("Set"));
|
|---|
| 218 | msg << interface << QString::fromUtf8(mp.name()) << qVariantFromValue(QDBusVariant(value));
|
|---|
| 219 | QDBusMessage reply = connection.call(msg, QDBus::Block);
|
|---|
| 220 |
|
|---|
| 221 | if (reply.type() != QDBusMessage::ReplyMessage)
|
|---|
| 222 | lastError = reply;
|
|---|
| 223 | }
|
|---|
| 224 |
|
|---|
| 225 | void QDBusAbstractInterfacePrivate::_q_serviceOwnerChanged(const QString &name,
|
|---|
| 226 | const QString &oldOwner,
|
|---|
| 227 | const QString &newOwner)
|
|---|
| 228 | {
|
|---|
| 229 | Q_UNUSED(oldOwner);
|
|---|
| 230 | //qDebug() << "QDBusAbstractInterfacePrivate serviceOwnerChanged" << name << oldOwner << newOwner;
|
|---|
| 231 | if (name == service) {
|
|---|
| 232 | currentOwner = newOwner;
|
|---|
| 233 | isValid = !newOwner.isEmpty();
|
|---|
| 234 | }
|
|---|
| 235 | }
|
|---|
| 236 |
|
|---|
| 237 |
|
|---|
| 238 | /*!
|
|---|
| 239 | \class QDBusAbstractInterface
|
|---|
| 240 | \inmodule QtDBus
|
|---|
| 241 | \since 4.2
|
|---|
| 242 |
|
|---|
| 243 | \brief The QDBusAbstractInterface class is the base class for all D-Bus interfaces in the QtDBus binding, allowing access to remote interfaces
|
|---|
| 244 |
|
|---|
| 245 | Generated-code classes also derive from QDBusAbstractInterface,
|
|---|
| 246 | all methods described here are also valid for generated-code
|
|---|
| 247 | classes. In addition to those described here, generated-code
|
|---|
| 248 | classes provide member functions for the remote methods, which
|
|---|
| 249 | allow for compile-time checking of the correct parameters and
|
|---|
| 250 | return values, as well as property type-matching and signal
|
|---|
| 251 | parameter-matching.
|
|---|
| 252 |
|
|---|
| 253 | \sa {qdbusxml2cpp.html}{The QDBus compiler}, QDBusInterface
|
|---|
| 254 | */
|
|---|
| 255 |
|
|---|
| 256 | /*!
|
|---|
| 257 | \internal
|
|---|
| 258 | This is the constructor called from QDBusInterface::QDBusInterface.
|
|---|
| 259 | */
|
|---|
| 260 | QDBusAbstractInterface::QDBusAbstractInterface(QDBusAbstractInterfacePrivate &d, QObject *parent)
|
|---|
| 261 | : QObject(d, parent)
|
|---|
| 262 | {
|
|---|
| 263 | // keep track of the service owner
|
|---|
| 264 | if (d_func()->isValid)
|
|---|
| 265 | QObject::connect(d_func()->connectionPrivate(), SIGNAL(serviceOwnerChanged(QString,QString,QString)),
|
|---|
| 266 | this, SLOT(_q_serviceOwnerChanged(QString,QString,QString)));
|
|---|
| 267 | }
|
|---|
| 268 |
|
|---|
| 269 | /*!
|
|---|
| 270 | \internal
|
|---|
| 271 | This is the constructor called from static classes derived from
|
|---|
| 272 | QDBusAbstractInterface (i.e., those generated by dbusxml2cpp).
|
|---|
| 273 | */
|
|---|
| 274 | QDBusAbstractInterface::QDBusAbstractInterface(const QString &service, const QString &path,
|
|---|
| 275 | const char *interface, const QDBusConnection &con,
|
|---|
| 276 | QObject *parent)
|
|---|
| 277 | : QObject(*new QDBusAbstractInterfacePrivate(service, path, QString::fromLatin1(interface),
|
|---|
| 278 | con, false), parent)
|
|---|
| 279 | {
|
|---|
| 280 | // keep track of the service owner
|
|---|
| 281 | if (d_func()->connection.isConnected())
|
|---|
| 282 | QObject::connect(d_func()->connectionPrivate(), SIGNAL(serviceOwnerChanged(QString,QString,QString)),
|
|---|
| 283 | this, SLOT(_q_serviceOwnerChanged(QString,QString,QString)));
|
|---|
| 284 | }
|
|---|
| 285 |
|
|---|
| 286 | /*!
|
|---|
| 287 | Releases this object's resources.
|
|---|
| 288 | */
|
|---|
| 289 | QDBusAbstractInterface::~QDBusAbstractInterface()
|
|---|
| 290 | {
|
|---|
| 291 | }
|
|---|
| 292 |
|
|---|
| 293 | /*!
|
|---|
| 294 | Returns true if this is a valid reference to a remote object. It returns false if
|
|---|
| 295 | there was an error during the creation of this interface (for instance, if the remote
|
|---|
| 296 | application does not exist).
|
|---|
| 297 |
|
|---|
| 298 | Note: when dealing with remote objects, it is not always possible to determine if it
|
|---|
| 299 | exists when creating a QDBusInterface.
|
|---|
| 300 | */
|
|---|
| 301 | bool QDBusAbstractInterface::isValid() const
|
|---|
| 302 | {
|
|---|
| 303 | return d_func()->isValid;
|
|---|
| 304 | }
|
|---|
| 305 |
|
|---|
| 306 | /*!
|
|---|
| 307 | Returns the connection this interface is assocated with.
|
|---|
| 308 | */
|
|---|
| 309 | QDBusConnection QDBusAbstractInterface::connection() const
|
|---|
| 310 | {
|
|---|
| 311 | return d_func()->connection;
|
|---|
| 312 | }
|
|---|
| 313 |
|
|---|
| 314 | /*!
|
|---|
| 315 | Returns the name of the service this interface is associated with.
|
|---|
| 316 | */
|
|---|
| 317 | QString QDBusAbstractInterface::service() const
|
|---|
| 318 | {
|
|---|
| 319 | return d_func()->service;
|
|---|
| 320 | }
|
|---|
| 321 |
|
|---|
| 322 | /*!
|
|---|
| 323 | Returns the object path that this interface is associated with.
|
|---|
| 324 | */
|
|---|
| 325 | QString QDBusAbstractInterface::path() const
|
|---|
| 326 | {
|
|---|
| 327 | return d_func()->path;
|
|---|
| 328 | }
|
|---|
| 329 |
|
|---|
| 330 | /*!
|
|---|
| 331 | Returns the name of this interface.
|
|---|
| 332 | */
|
|---|
| 333 | QString QDBusAbstractInterface::interface() const
|
|---|
| 334 | {
|
|---|
| 335 | return d_func()->interface;
|
|---|
| 336 | }
|
|---|
| 337 |
|
|---|
| 338 | /*!
|
|---|
| 339 | Returns the error the last operation produced, or an invalid error if the last operation did not
|
|---|
| 340 | produce an error.
|
|---|
| 341 | */
|
|---|
| 342 | QDBusError QDBusAbstractInterface::lastError() const
|
|---|
| 343 | {
|
|---|
| 344 | return d_func()->lastError;
|
|---|
| 345 | }
|
|---|
| 346 |
|
|---|
| 347 | /*!
|
|---|
| 348 | Places a call to the remote method specified by \a method on this interface, using \a args as
|
|---|
| 349 | arguments. This function returns the message that was received as a reply, which can be a normal
|
|---|
| 350 | QDBusMessage::ReplyMessage (indicating success) or QDBusMessage::ErrorMessage (if the call
|
|---|
| 351 | failed). The \a mode parameter specifies how this call should be placed.
|
|---|
| 352 |
|
|---|
| 353 | If the call succeeds, lastError() will be cleared; otherwise, it will contain the error this
|
|---|
| 354 | call produced.
|
|---|
| 355 |
|
|---|
| 356 | Normally, you should place calls using call().
|
|---|
| 357 |
|
|---|
| 358 | \warning If you use \c UseEventLoop, your code must be prepared to deal with any reentrancy:
|
|---|
| 359 | other method calls and signals may be delivered before this function returns, as well
|
|---|
| 360 | as other Qt queued signals and events.
|
|---|
| 361 |
|
|---|
| 362 | \threadsafe
|
|---|
| 363 | */
|
|---|
| 364 | QDBusMessage QDBusAbstractInterface::callWithArgumentList(QDBus::CallMode mode,
|
|---|
| 365 | const QString& method,
|
|---|
| 366 | const QList<QVariant>& args)
|
|---|
| 367 | {
|
|---|
| 368 | Q_D(QDBusAbstractInterface);
|
|---|
| 369 |
|
|---|
| 370 | QString m = method;
|
|---|
| 371 | // split out the signature from the method
|
|---|
| 372 | int pos = method.indexOf(QLatin1Char('.'));
|
|---|
| 373 | if (pos != -1)
|
|---|
| 374 | m.truncate(pos);
|
|---|
| 375 |
|
|---|
| 376 | if (mode == QDBus::AutoDetect) {
|
|---|
| 377 | // determine if this a sync or async call
|
|---|
| 378 | mode = QDBus::Block;
|
|---|
| 379 | const QMetaObject *mo = metaObject();
|
|---|
| 380 | QByteArray match = m.toLatin1() + '(';
|
|---|
| 381 |
|
|---|
| 382 | for (int i = staticMetaObject.methodCount(); i < mo->methodCount(); ++i) {
|
|---|
| 383 | QMetaMethod mm = mo->method(i);
|
|---|
| 384 | if (QByteArray(mm.signature()).startsWith(match)) {
|
|---|
| 385 | // found a method with the same name as what we're looking for
|
|---|
| 386 | // hopefully, nobody is overloading asynchronous and synchronous methods with
|
|---|
| 387 | // the same name
|
|---|
| 388 |
|
|---|
| 389 | QList<QByteArray> tags = QByteArray(mm.tag()).split(' ');
|
|---|
| 390 | if (tags.contains("Q_NOREPLY"))
|
|---|
| 391 | mode = QDBus::NoBlock;
|
|---|
| 392 |
|
|---|
| 393 | break;
|
|---|
| 394 | }
|
|---|
| 395 | }
|
|---|
| 396 | }
|
|---|
| 397 |
|
|---|
| 398 | // qDebug() << "QDBusAbstractInterface" << "Service" << service() << "Path:" << path();
|
|---|
| 399 | QDBusMessage msg = QDBusMessage::createMethodCall(service(), path(), interface(), m);
|
|---|
| 400 | msg.setArguments(args);
|
|---|
| 401 |
|
|---|
| 402 | QDBusMessage reply = d->connection.call(msg, mode);
|
|---|
| 403 | d->lastError = reply; // will clear if reply isn't an error
|
|---|
| 404 |
|
|---|
| 405 | // ensure that there is at least one element
|
|---|
| 406 | if (reply.arguments().isEmpty())
|
|---|
| 407 | reply << QVariant();
|
|---|
| 408 |
|
|---|
| 409 | return reply;
|
|---|
| 410 | }
|
|---|
| 411 |
|
|---|
| 412 | /*!
|
|---|
| 413 | \since 4.5
|
|---|
| 414 | Places a call to the remote method specified by \a method on this
|
|---|
| 415 | interface, using \a args as arguments. This function returns a
|
|---|
| 416 | QDBusPendingCall object that can be used to track the status of the
|
|---|
| 417 | reply and access its contents once it has arrived.
|
|---|
| 418 |
|
|---|
| 419 | Normally, you should place calls using asyncCall().
|
|---|
| 420 |
|
|---|
| 421 | \threadsafe
|
|---|
| 422 | */
|
|---|
| 423 | QDBusPendingCall QDBusAbstractInterface::asyncCallWithArgumentList(const QString& method,
|
|---|
| 424 | const QList<QVariant>& args)
|
|---|
| 425 | {
|
|---|
| 426 | Q_D(QDBusAbstractInterface);
|
|---|
| 427 |
|
|---|
| 428 | QDBusMessage msg = QDBusMessage::createMethodCall(service(), path(), interface(), method);
|
|---|
| 429 | msg.setArguments(args);
|
|---|
| 430 | return d->connection.asyncCall(msg);
|
|---|
| 431 | }
|
|---|
| 432 |
|
|---|
| 433 | /*!
|
|---|
| 434 | Places a call to the remote method specified by \a method
|
|---|
| 435 | on this interface, using \a args as arguments. This function
|
|---|
| 436 | returns immediately after queueing the call. The reply from
|
|---|
| 437 | the remote function is delivered to the \a returnMethod on
|
|---|
| 438 | object \a receiver. If an error occurs, the \a errorMethod
|
|---|
| 439 | on object \a receiver is called instead.
|
|---|
| 440 |
|
|---|
| 441 | This function returns true if the queueing succeeds. It does
|
|---|
| 442 | not indicate that the executed call succeeded. If it fails,
|
|---|
| 443 | the \a errorMethod is called.
|
|---|
| 444 |
|
|---|
| 445 | The \a returnMethod must have as its parameters the types returned
|
|---|
| 446 | by the function call. Optionally, it may have a QDBusMessage
|
|---|
| 447 | parameter as its last or only parameter. The \a errorMethod must
|
|---|
| 448 | have a QDBusError as its only parameter.
|
|---|
| 449 |
|
|---|
| 450 | \since 4.3
|
|---|
| 451 | \sa QDBusError, QDBusMessage
|
|---|
| 452 | */
|
|---|
| 453 | bool QDBusAbstractInterface::callWithCallback(const QString &method,
|
|---|
| 454 | const QList<QVariant> &args,
|
|---|
| 455 | QObject *receiver,
|
|---|
| 456 | const char *returnMethod,
|
|---|
| 457 | const char *errorMethod)
|
|---|
| 458 | {
|
|---|
| 459 | Q_D(QDBusAbstractInterface);
|
|---|
| 460 |
|
|---|
| 461 | QDBusMessage msg = QDBusMessage::createMethodCall(service(),
|
|---|
| 462 | path(),
|
|---|
| 463 | interface(),
|
|---|
| 464 | method);
|
|---|
| 465 | msg.setArguments(args);
|
|---|
| 466 |
|
|---|
| 467 | d->lastError = 0;
|
|---|
| 468 | return d->connection.callWithCallback(msg,
|
|---|
| 469 | receiver,
|
|---|
| 470 | returnMethod,
|
|---|
| 471 | errorMethod);
|
|---|
| 472 | }
|
|---|
| 473 |
|
|---|
| 474 | /*!
|
|---|
| 475 | \overload
|
|---|
| 476 |
|
|---|
| 477 | This function is deprecated. Please use the overloaded version.
|
|---|
| 478 |
|
|---|
| 479 | Places a call to the remote method specified by \a method
|
|---|
| 480 | on this interface, using \a args as arguments. This function
|
|---|
| 481 | returns immediately after queueing the call. The reply from
|
|---|
| 482 | the remote function or any errors emitted by it are delivered
|
|---|
| 483 | to the \a slot slot on object \a receiver.
|
|---|
| 484 |
|
|---|
| 485 | This function returns true if the queueing succeeded: it does
|
|---|
| 486 | not indicate that the call succeeded. If it failed, the slot
|
|---|
| 487 | will be called with an error message. lastError() will not be
|
|---|
| 488 | set under those circumstances.
|
|---|
| 489 |
|
|---|
| 490 | \sa QDBusError, QDBusMessage
|
|---|
| 491 | */
|
|---|
| 492 | bool QDBusAbstractInterface::callWithCallback(const QString &method,
|
|---|
| 493 | const QList<QVariant> &args,
|
|---|
| 494 | QObject *receiver,
|
|---|
| 495 | const char *slot)
|
|---|
| 496 | {
|
|---|
| 497 | return callWithCallback(method, args, receiver, slot, 0);
|
|---|
| 498 | }
|
|---|
| 499 |
|
|---|
| 500 | /*!
|
|---|
| 501 | \internal
|
|---|
| 502 | Catch signal connections.
|
|---|
| 503 | */
|
|---|
| 504 | void QDBusAbstractInterface::connectNotify(const char *signal)
|
|---|
| 505 | {
|
|---|
| 506 | // we end up recursing here, so optimise away
|
|---|
| 507 | if (qstrcmp(signal + 1, "destroyed(QObject*)") == 0)
|
|---|
| 508 | return;
|
|---|
| 509 |
|
|---|
| 510 | // someone connecting to one of our signals
|
|---|
| 511 | Q_D(QDBusAbstractInterface);
|
|---|
| 512 |
|
|---|
| 513 | QDBusConnectionPrivate *conn = d->connectionPrivate();
|
|---|
| 514 | if (conn)
|
|---|
| 515 | conn->connectRelay(d->service, d->currentOwner, d->path, d->interface,
|
|---|
| 516 | this, signal);
|
|---|
| 517 | }
|
|---|
| 518 |
|
|---|
| 519 | /*!
|
|---|
| 520 | \internal
|
|---|
| 521 | Catch signal disconnections.
|
|---|
| 522 | */
|
|---|
| 523 | void QDBusAbstractInterface::disconnectNotify(const char *signal)
|
|---|
| 524 | {
|
|---|
| 525 | // someone disconnecting from one of our signals
|
|---|
| 526 | Q_D(QDBusAbstractInterface);
|
|---|
| 527 |
|
|---|
| 528 | QDBusConnectionPrivate *conn = d->connectionPrivate();
|
|---|
| 529 | if (conn)
|
|---|
| 530 | conn->disconnectRelay(d->service, d->currentOwner, d->path, d->interface,
|
|---|
| 531 | this, signal);
|
|---|
| 532 | }
|
|---|
| 533 |
|
|---|
| 534 | /*!
|
|---|
| 535 | \internal
|
|---|
| 536 | Get the value of the property \a propname.
|
|---|
| 537 | */
|
|---|
| 538 | QVariant QDBusAbstractInterface::internalPropGet(const char *propname) const
|
|---|
| 539 | {
|
|---|
| 540 | // assume this property exists and is readable
|
|---|
| 541 | // we're only called from generated code anyways
|
|---|
| 542 |
|
|---|
| 543 | int idx = metaObject()->indexOfProperty(propname);
|
|---|
| 544 | if (idx != -1)
|
|---|
| 545 | return d_func()->property(metaObject()->property(idx));
|
|---|
| 546 | qWarning("QDBusAbstractInterface::internalPropGet called with unknown property '%s'", propname);
|
|---|
| 547 | return QVariant(); // error
|
|---|
| 548 | }
|
|---|
| 549 |
|
|---|
| 550 | /*!
|
|---|
| 551 | \internal
|
|---|
| 552 | Set the value of the property \a propname to \a value.
|
|---|
| 553 | */
|
|---|
| 554 | void QDBusAbstractInterface::internalPropSet(const char *propname, const QVariant &value)
|
|---|
| 555 | {
|
|---|
| 556 | Q_D(QDBusAbstractInterface);
|
|---|
| 557 |
|
|---|
| 558 | // assume this property exists and is writeable
|
|---|
| 559 | // we're only called from generated code anyways
|
|---|
| 560 |
|
|---|
| 561 | int idx = metaObject()->indexOfProperty(propname);
|
|---|
| 562 | if (idx != -1)
|
|---|
| 563 | d->setProperty(metaObject()->property(idx), value);
|
|---|
| 564 | else
|
|---|
| 565 | qWarning("QDBusAbstractInterface::internalPropGet called with unknown property '%s'", propname);
|
|---|
| 566 | }
|
|---|
| 567 |
|
|---|
| 568 | /*!
|
|---|
| 569 | Calls the method \a method on this interface and passes the parameters to this function to the
|
|---|
| 570 | method.
|
|---|
| 571 |
|
|---|
| 572 | The parameters to \c call are passed on to the remote function via D-Bus as input
|
|---|
| 573 | arguments. Output arguments are returned in the QDBusMessage reply. If the reply is an error
|
|---|
| 574 | reply, lastError() will also be set to the contents of the error message.
|
|---|
| 575 |
|
|---|
| 576 | This function can be used with up to 8 parameters, passed in arguments \a arg1, \a arg2,
|
|---|
| 577 | \a arg3, \a arg4, \a arg5, \a arg6, \a arg7 and \a arg8. If you need more than 8
|
|---|
| 578 | parameters or if you have a variable number of parameters to be passed, use
|
|---|
| 579 | callWithArgumentList().
|
|---|
| 580 |
|
|---|
| 581 | It can be used the following way:
|
|---|
| 582 |
|
|---|
| 583 | \snippet doc/src/snippets/code/src_qdbus_qdbusabstractinterface.cpp 0
|
|---|
| 584 |
|
|---|
| 585 | This example illustrates function calling with 0, 1 and 2 parameters and illustrates different
|
|---|
| 586 | parameter types passed in each (the first call to \c "ProcessWorkUnicode" will contain one
|
|---|
| 587 | Unicode string, the second call to \c "ProcessWork" will contain one string and one byte array).
|
|---|
| 588 | */
|
|---|
| 589 | QDBusMessage QDBusAbstractInterface::call(const QString &method, const QVariant &arg1,
|
|---|
| 590 | const QVariant &arg2,
|
|---|
| 591 | const QVariant &arg3,
|
|---|
| 592 | const QVariant &arg4,
|
|---|
| 593 | const QVariant &arg5,
|
|---|
| 594 | const QVariant &arg6,
|
|---|
| 595 | const QVariant &arg7,
|
|---|
| 596 | const QVariant &arg8)
|
|---|
| 597 | {
|
|---|
| 598 | return call(QDBus::AutoDetect, method, arg1, arg2, arg3, arg4, arg5, arg6, arg7, arg8);
|
|---|
| 599 | }
|
|---|
| 600 |
|
|---|
| 601 | /*!
|
|---|
| 602 | \overload
|
|---|
| 603 |
|
|---|
| 604 | Calls the method \a method on this interface and passes the
|
|---|
| 605 | parameters to this function to the method. If \a mode is \c
|
|---|
| 606 | NoWaitForReply, then this function will return immediately after
|
|---|
| 607 | placing the call, without waiting for a reply from the remote
|
|---|
| 608 | method. Otherwise, \a mode indicates whether this function should
|
|---|
| 609 | activate the Qt Event Loop while waiting for the reply to arrive.
|
|---|
| 610 |
|
|---|
| 611 | This function can be used with up to 8 parameters, passed in arguments \a arg1, \a arg2,
|
|---|
| 612 | \a arg3, \a arg4, \a arg5, \a arg6, \a arg7 and \a arg8. If you need more than 8
|
|---|
| 613 | parameters or if you have a variable number of parameters to be passed, use
|
|---|
| 614 | callWithArgumentList().
|
|---|
| 615 |
|
|---|
| 616 | If this function reenters the Qt event loop in order to wait for the
|
|---|
| 617 | reply, it will exclude user input. During the wait, it may deliver
|
|---|
| 618 | signals and other method calls to your application. Therefore, it
|
|---|
| 619 | must be prepared to handle a reentrancy whenever a call is placed
|
|---|
| 620 | with call().
|
|---|
| 621 | */
|
|---|
| 622 | QDBusMessage QDBusAbstractInterface::call(QDBus::CallMode mode, const QString &method,
|
|---|
| 623 | const QVariant &arg1,
|
|---|
| 624 | const QVariant &arg2,
|
|---|
| 625 | const QVariant &arg3,
|
|---|
| 626 | const QVariant &arg4,
|
|---|
| 627 | const QVariant &arg5,
|
|---|
| 628 | const QVariant &arg6,
|
|---|
| 629 | const QVariant &arg7,
|
|---|
| 630 | const QVariant &arg8)
|
|---|
| 631 | {
|
|---|
| 632 | QList<QVariant> argList;
|
|---|
| 633 | int count = 0 + arg1.isValid() + arg2.isValid() + arg3.isValid() + arg4.isValid() +
|
|---|
| 634 | arg5.isValid() + arg6.isValid() + arg7.isValid() + arg8.isValid();
|
|---|
| 635 |
|
|---|
| 636 | switch (count) {
|
|---|
| 637 | case 8:
|
|---|
| 638 | argList.prepend(arg8);
|
|---|
| 639 | case 7:
|
|---|
| 640 | argList.prepend(arg7);
|
|---|
| 641 | case 6:
|
|---|
| 642 | argList.prepend(arg6);
|
|---|
| 643 | case 5:
|
|---|
| 644 | argList.prepend(arg5);
|
|---|
| 645 | case 4:
|
|---|
| 646 | argList.prepend(arg4);
|
|---|
| 647 | case 3:
|
|---|
| 648 | argList.prepend(arg3);
|
|---|
| 649 | case 2:
|
|---|
| 650 | argList.prepend(arg2);
|
|---|
| 651 | case 1:
|
|---|
| 652 | argList.prepend(arg1);
|
|---|
| 653 | }
|
|---|
| 654 |
|
|---|
| 655 | return callWithArgumentList(mode, method, argList);
|
|---|
| 656 | }
|
|---|
| 657 |
|
|---|
| 658 |
|
|---|
| 659 | /*!
|
|---|
| 660 | \since 4.5
|
|---|
| 661 | Calls the method \a method on this interface and passes the parameters to this function to the
|
|---|
| 662 | method.
|
|---|
| 663 |
|
|---|
| 664 | The parameters to \c call are passed on to the remote function via D-Bus as input
|
|---|
| 665 | arguments. The returned QDBusPendingCall object can be used to find out information about
|
|---|
| 666 | the reply.
|
|---|
| 667 |
|
|---|
| 668 | This function can be used with up to 8 parameters, passed in arguments \a arg1, \a arg2,
|
|---|
| 669 | \a arg3, \a arg4, \a arg5, \a arg6, \a arg7 and \a arg8. If you need more than 8
|
|---|
| 670 | parameters or if you have a variable number of parameters to be passed, use
|
|---|
| 671 | asyncCallWithArgumentList().
|
|---|
| 672 |
|
|---|
| 673 | It can be used the following way:
|
|---|
| 674 |
|
|---|
| 675 | \snippet doc/src/snippets/code/src_qdbus_qdbusabstractinterface.cpp 1
|
|---|
| 676 |
|
|---|
| 677 | This example illustrates function calling with 0, 1 and 2 parameters and illustrates different
|
|---|
| 678 | parameter types passed in each (the first call to \c "ProcessWorkUnicode" will contain one
|
|---|
| 679 | Unicode string, the second call to \c "ProcessWork" will contain one string and one byte array).
|
|---|
| 680 | */
|
|---|
| 681 | QDBusPendingCall QDBusAbstractInterface::asyncCall(const QString &method, const QVariant &arg1,
|
|---|
| 682 | const QVariant &arg2,
|
|---|
| 683 | const QVariant &arg3,
|
|---|
| 684 | const QVariant &arg4,
|
|---|
| 685 | const QVariant &arg5,
|
|---|
| 686 | const QVariant &arg6,
|
|---|
| 687 | const QVariant &arg7,
|
|---|
| 688 | const QVariant &arg8)
|
|---|
| 689 | {
|
|---|
| 690 | QList<QVariant> argList;
|
|---|
| 691 | int count = 0 + arg1.isValid() + arg2.isValid() + arg3.isValid() + arg4.isValid() +
|
|---|
| 692 | arg5.isValid() + arg6.isValid() + arg7.isValid() + arg8.isValid();
|
|---|
| 693 |
|
|---|
| 694 | switch (count) {
|
|---|
| 695 | case 8:
|
|---|
| 696 | argList.prepend(arg8);
|
|---|
| 697 | case 7:
|
|---|
| 698 | argList.prepend(arg7);
|
|---|
| 699 | case 6:
|
|---|
| 700 | argList.prepend(arg6);
|
|---|
| 701 | case 5:
|
|---|
| 702 | argList.prepend(arg5);
|
|---|
| 703 | case 4:
|
|---|
| 704 | argList.prepend(arg4);
|
|---|
| 705 | case 3:
|
|---|
| 706 | argList.prepend(arg3);
|
|---|
| 707 | case 2:
|
|---|
| 708 | argList.prepend(arg2);
|
|---|
| 709 | case 1:
|
|---|
| 710 | argList.prepend(arg1);
|
|---|
| 711 | }
|
|---|
| 712 |
|
|---|
| 713 | return asyncCallWithArgumentList(method, argList);
|
|---|
| 714 | }
|
|---|
| 715 |
|
|---|
| 716 | /*!
|
|---|
| 717 | \internal
|
|---|
| 718 | */
|
|---|
| 719 | QDBusMessage QDBusAbstractInterface::internalConstCall(QDBus::CallMode mode,
|
|---|
| 720 | const QString &method,
|
|---|
| 721 | const QList<QVariant> &args) const
|
|---|
| 722 | {
|
|---|
| 723 | // ### move the code here, and make the other functions call this
|
|---|
| 724 | return const_cast<QDBusAbstractInterface*>(this)->callWithArgumentList(mode, method, args);
|
|---|
| 725 | }
|
|---|
| 726 |
|
|---|
| 727 | QT_END_NAMESPACE
|
|---|
| 728 |
|
|---|
| 729 | #include "moc_qdbusabstractinterface.cpp"
|
|---|