| 1 | // -*- c++ -*- |
| 2 | |
| 3 | /*! |
| 4 | * |
| 5 | * Copyright (C) 2015 Jolla Ltd. |
| 6 | * |
| 7 | * Contact: Valerio Valerio <[email protected]> |
| 8 | * Author: Andres Gomez <[email protected]> |
| 9 | * |
| 10 | * This library is free software; you can redistribute it and/or |
| 11 | * modify it under the terms of the GNU Lesser General Public |
| 12 | * License as published by the Free Software Foundation; either |
| 13 | * version 2.1 of the License, or (at your option) any later version. |
| 14 | * |
| 15 | * This library is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 18 | * Lesser General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU Lesser General Public |
| 21 | * License along with this library; if not, write to the Free Software |
| 22 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 23 | */ |
| 24 | |
| 25 | |
| 26 | #include "dbusextendedpendingcallwatcher_p.h" |
| 27 | |
| 28 | #include <DBusExtendedAbstractInterface> |
| 29 | |
| 30 | #include <QtDBus/QDBusMetaType> |
| 31 | #include <QtDBus/QDBusMessage> |
| 32 | #include <QtDBus/QDBusPendingCall> |
| 33 | #include <QtDBus/QDBusPendingCallWatcher> |
| 34 | #include <QtDBus/QDBusPendingReply> |
| 35 | |
| 36 | #include <QtCore/QDebug> |
| 37 | #include <QtCore/QMetaProperty> |
| 38 | |
| 39 | |
| 40 | Q_GLOBAL_STATIC_WITH_ARGS(QByteArray, dBusPropertiesInterface, ("org.freedesktop.DBus.Properties")) |
| 41 | Q_GLOBAL_STATIC_WITH_ARGS(QByteArray, dBusPropertiesChangedSignal, ("PropertiesChanged")) |
| 42 | Q_GLOBAL_STATIC_WITH_ARGS(QByteArray, propertyChangedSignature, ("propertyChanged(QString,QVariant)")) |
| 43 | Q_GLOBAL_STATIC_WITH_ARGS(QByteArray, propertyInvalidatedSignature, ("propertyInvalidated(QString)")) |
| 44 | |
| 45 | |
| 46 | DBusExtendedAbstractInterface::DBusExtendedAbstractInterface(const QString &service, const QString &path, const char *interface, const QDBusConnection &connection, QObject *parent) |
| 47 | : QDBusAbstractInterface(service, path, interface, connection, parent) |
| 48 | , m_sync(false) |
| 49 | , m_useCache(false) |
| 50 | , m_getAllPendingCallWatcher(0) |
| 51 | , m_propertiesChangedConnected(false) |
| 52 | { |
| 53 | } |
| 54 | |
| 55 | DBusExtendedAbstractInterface::~DBusExtendedAbstractInterface() |
| 56 | { |
| 57 | } |
| 58 | |
| 59 | void DBusExtendedAbstractInterface::getAllProperties() |
| 60 | { |
| 61 | m_lastExtendedError = QDBusError(); |
| 62 | |
| 63 | if (!isValid()) { |
| 64 | QString errorMessage = QStringLiteral("This Extended DBus interface is not valid yet."); |
| 65 | m_lastExtendedError = QDBusMessage::createError(QDBusError::Failed, errorMessage); |
| 66 | qDebug() << Q_FUNC_INFO << errorMessage; |
| 67 | return; |
| 68 | } |
| 69 | |
| 70 | if (!m_sync && m_getAllPendingCallWatcher) { |
| 71 | // Call already in place, not repeating ... |
| 72 | return; |
| 73 | } |
| 74 | |
| 75 | QDBusMessage msg = QDBusMessage::createMethodCall(service(), path(), *dBusPropertiesInterface(), QStringLiteral("GetAll")); |
| 76 | msg << interface(); |
| 77 | |
| 78 | if (m_sync) { |
| 79 | QDBusMessage reply = connection().call(msg); |
| 80 | |
| 81 | if (reply.type() != QDBusMessage::ReplyMessage) { |
| 82 | m_lastExtendedError = QDBusError(reply); |
| 83 | qWarning() << Q_FUNC_INFO << m_lastExtendedError.message(); |
| 84 | return; |
| 85 | } |
| 86 | |
| 87 | if (reply.signature() != QLatin1String("a{sv}")) { |
| 88 | QString errorMessage = QStringLiteral("Invalid signature \"%1\" in return from call to %2") |
| 89 | .arg(reply.signature(), |
| 90 | QString(*dBusPropertiesInterface())); |
| 91 | qWarning() << Q_FUNC_INFO << errorMessage; |
| 92 | m_lastExtendedError = QDBusError(QDBusError::InvalidSignature, errorMessage); |
| 93 | return; |
| 94 | } |
| 95 | |
| 96 | QVariantMap value = reply.arguments().at(0).toMap(); |
| 97 | onPropertiesChanged(interface(), value, QStringList()); |
| 98 | } else { |
| 99 | QDBusPendingReply<QVariantMap> async = connection().asyncCall(msg); |
| 100 | m_getAllPendingCallWatcher = new QDBusPendingCallWatcher(async, this); |
| 101 | |
| 102 | connect(m_getAllPendingCallWatcher, SIGNAL(finished(QDBusPendingCallWatcher*)), this, SLOT(onAsyncGetAllPropertiesFinished(QDBusPendingCallWatcher*))); |
| 103 | return; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | void DBusExtendedAbstractInterface::connectNotify(const QMetaMethod &signal) |
| 108 | { |
| 109 | if (signal.methodType() == QMetaMethod::Signal |
| 110 | && (signal.methodSignature() == *propertyChangedSignature() |
| 111 | || signal.methodSignature() == *propertyInvalidatedSignature())) { |
| 112 | if (!m_propertiesChangedConnected) { |
| 113 | QStringList argumentMatch; |
| 114 | argumentMatch << interface(); |
| 115 | connection().connect(service(), path(), *dBusPropertiesInterface(), *dBusPropertiesChangedSignal(), |
| 116 | argumentMatch, QString(), |
| 117 | this, SLOT(onPropertiesChanged(QString, QVariantMap, QStringList))); |
| 118 | |
| 119 | m_propertiesChangedConnected = true; |
| 120 | return; |
| 121 | } |
| 122 | } else { |
| 123 | QDBusAbstractInterface::connectNotify(signal); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | void DBusExtendedAbstractInterface::disconnectNotify(const QMetaMethod &signal) |
| 128 | { |
| 129 | if (signal.methodType() == QMetaMethod::Signal |
| 130 | && (signal.methodSignature() == *propertyChangedSignature() |
| 131 | || signal.methodSignature() == *propertyInvalidatedSignature())) { |
| 132 | if (m_propertiesChangedConnected |
| 133 | && 0 == receivers(propertyChangedSignature()->constData()) |
| 134 | && 0 == receivers(propertyInvalidatedSignature()->constData())) { |
| 135 | QStringList argumentMatch; |
| 136 | argumentMatch << interface(); |
| 137 | connection().disconnect(service(), path(), *dBusPropertiesInterface(), *dBusPropertiesChangedSignal(), |
| 138 | argumentMatch, QString(), |
| 139 | this, SLOT(onPropertiesChanged(QString, QVariantMap, QStringList))); |
| 140 | |
| 141 | m_propertiesChangedConnected = false; |
| 142 | return; |
| 143 | } |
| 144 | } else { |
| 145 | QDBusAbstractInterface::disconnectNotify(signal); |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | QVariant DBusExtendedAbstractInterface::internalPropGet(const char *propname, void *propertyPtr) |
| 150 | { |
| 151 | m_lastExtendedError = QDBusError(); |
| 152 | |
| 153 | if (m_useCache) { |
| 154 | int propertyIndex = metaObject()->indexOfProperty(propname); |
| 155 | QMetaProperty metaProperty = metaObject()->property(propertyIndex); |
| 156 | return QVariant(metaProperty.type(), propertyPtr); |
| 157 | } |
| 158 | |
| 159 | if (m_sync) { |
| 160 | return property(propname); |
| 161 | } else { |
| 162 | if (!isValid()) { |
| 163 | QString errorMessage = QStringLiteral("This Extended DBus interface is not valid yet."); |
| 164 | m_lastExtendedError = QDBusMessage::createError(QDBusError::Failed, errorMessage); |
| 165 | qDebug() << Q_FUNC_INFO << errorMessage; |
| 166 | return QVariant(); |
| 167 | } |
| 168 | |
| 169 | int propertyIndex = metaObject()->indexOfProperty(propname); |
| 170 | |
| 171 | if (-1 == propertyIndex) { |
| 172 | QString errorMessage = QStringLiteral("Got unknown property \"%1\" to read") |
| 173 | .arg(QString::fromLatin1(propname)); |
| 174 | m_lastExtendedError = QDBusMessage::createError(QDBusError::Failed, errorMessage); |
| 175 | qWarning() << Q_FUNC_INFO << errorMessage; |
| 176 | return QVariant(); |
| 177 | } |
| 178 | |
| 179 | QMetaProperty metaProperty = metaObject()->property(propertyIndex); |
| 180 | |
| 181 | if (!metaProperty.isReadable()) { |
| 182 | QString errorMessage = QStringLiteral("Property \"%1\" is NOT readable") |
| 183 | .arg(QString::fromLatin1(propname)); |
| 184 | m_lastExtendedError = QDBusMessage::createError(QDBusError::Failed, errorMessage); |
| 185 | qWarning() << Q_FUNC_INFO << errorMessage; |
| 186 | return QVariant(); |
| 187 | } |
| 188 | |
| 189 | // is this metatype registered? |
| 190 | const char *expectedSignature = ""; |
| 191 | if (int(metaProperty.type()) != QMetaType::QVariant) { |
| 192 | expectedSignature = QDBusMetaType::typeToSignature(metaProperty.userType()); |
| 193 | if (0 == expectedSignature) { |
| 194 | QString errorMessage = |
| 195 | QStringLiteral("Type %1 must be registered with Qt D-Bus " |
| 196 | "before it can be used to read property " |
| 197 | "%2.%3") |
| 198 | .arg(metaProperty.typeName(), |
| 199 | interface(), |
| 200 | propname); |
| 201 | m_lastExtendedError = QDBusMessage::createError(QDBusError::Failed, errorMessage); |
| 202 | qWarning() << Q_FUNC_INFO << errorMessage; |
| 203 | return QVariant(); |
| 204 | } |
| 205 | } |
| 206 | |
| 207 | asyncProperty(propname); |
| 208 | return QVariant(metaProperty.type(), propertyPtr); |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | void DBusExtendedAbstractInterface::internalPropSet(const char *propname, const QVariant &value, void *propertyPtr) |
| 213 | { |
| 214 | m_lastExtendedError = QDBusError(); |
| 215 | |
| 216 | if (m_sync) { |
| 217 | setProperty(propname, value); |
| 218 | } else { |
| 219 | if (!isValid()) { |
| 220 | QString errorMessage = QStringLiteral("This interface is not yet valid"); |
| 221 | m_lastExtendedError = QDBusMessage::createError(QDBusError::Failed, errorMessage); |
| 222 | qDebug() << Q_FUNC_INFO << errorMessage; |
| 223 | return; |
| 224 | } |
| 225 | |
| 226 | int propertyIndex = metaObject()->indexOfProperty(propname); |
| 227 | |
| 228 | if (-1 == propertyIndex) { |
| 229 | QString errorMessage = QStringLiteral("Got unknown property \"%1\" to write") |
| 230 | .arg(QString::fromLatin1(propname)); |
| 231 | m_lastExtendedError = QDBusMessage::createError(QDBusError::Failed, errorMessage); |
| 232 | qWarning() << Q_FUNC_INFO << errorMessage; |
| 233 | return; |
| 234 | } |
| 235 | |
| 236 | QMetaProperty metaProperty = metaObject()->property(propertyIndex); |
| 237 | |
| 238 | if (!metaProperty.isWritable()) { |
| 239 | QString errorMessage = QStringLiteral("Property \"%1\" is NOT writable") |
| 240 | .arg(QString::fromLatin1(propname)); |
| 241 | m_lastExtendedError = QDBusMessage::createError(QDBusError::Failed, errorMessage); |
| 242 | qWarning() << Q_FUNC_INFO << errorMessage; |
| 243 | return; |
| 244 | } |
| 245 | |
| 246 | asyncSetProperty(propname, QVariant(metaProperty.type(), propertyPtr)); |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | QVariant DBusExtendedAbstractInterface::asyncProperty(const QString &propertyName) |
| 251 | { |
| 252 | QDBusMessage msg = QDBusMessage::createMethodCall(service(), path(), *dBusPropertiesInterface(), QStringLiteral("Get")); |
| 253 | msg << interface() << propertyName; |
| 254 | QDBusPendingReply<QVariant> async = connection().asyncCall(msg); |
| 255 | DBusExtendedPendingCallWatcher *watcher = new DBusExtendedPendingCallWatcher(async, propertyName, QVariant(), this); |
| 256 | |
| 257 | connect(watcher, SIGNAL(finished(QDBusPendingCallWatcher*)), this, SLOT(onAsyncPropertyFinished(QDBusPendingCallWatcher*))); |
| 258 | |
| 259 | return QVariant(); |
| 260 | } |
| 261 | |
| 262 | void DBusExtendedAbstractInterface::asyncSetProperty(const QString &propertyName, const QVariant &value) |
| 263 | { |
| 264 | QDBusMessage msg = QDBusMessage::createMethodCall(service(), path(), *dBusPropertiesInterface(), QStringLiteral("Set")); |
| 265 | msg << interface() << propertyName << value; |
| 266 | QDBusPendingReply<QVariant> async = connection().asyncCall(msg); |
| 267 | DBusExtendedPendingCallWatcher *watcher = new DBusExtendedPendingCallWatcher(async, propertyName, value, this); |
| 268 | |
| 269 | connect(watcher, SIGNAL(finished(QDBusPendingCallWatcher*)), this, SLOT(onAsyncSetPropertyFinished(QDBusPendingCallWatcher*))); |
| 270 | } |
| 271 | |
| 272 | void DBusExtendedAbstractInterface::onAsyncPropertyFinished(DBusExtendedPendingCallWatcher *watcher) |
| 273 | { |
| 274 | QDBusPendingReply<QVariant> reply = *watcher; |
| 275 | |
| 276 | if (reply.isError()) { |
| 277 | m_lastExtendedError = reply.error(); |
| 278 | } else { |
| 279 | int propertyIndex = metaObject()->indexOfProperty(watcher->asyncProperty().toLatin1().constData()); |
| 280 | QVariant value = demarshall(interface(), |
| 281 | metaObject()->property(propertyIndex), |
| 282 | reply.value(), |
| 283 | &m_lastExtendedError); |
| 284 | |
| 285 | if (m_lastExtendedError.isValid()) { |
| 286 | emit propertyInvalidated(watcher->asyncProperty()); |
| 287 | } else { |
| 288 | emit propertyChanged(watcher->asyncProperty(), value); |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | emit asyncPropertyFinished(watcher->asyncProperty()); |
| 293 | watcher->deleteLater(); |
| 294 | } |
| 295 | |
| 296 | void DBusExtendedAbstractInterface::onAsyncSetPropertyFinished(DBusExtendedPendingCallWatcher *watcher) |
| 297 | { |
| 298 | QDBusPendingReply<QVariant> reply = *watcher; |
| 299 | |
| 300 | if (reply.isError()) { |
| 301 | m_lastExtendedError = reply.error(); |
| 302 | } else { |
| 303 | m_lastExtendedError = QDBusError(); |
| 304 | } |
| 305 | |
| 306 | emit asyncSetPropertyFinished(watcher->asyncProperty()); |
| 307 | |
| 308 | // Resetting the property to its previous value after sending the |
| 309 | // finished signal |
| 310 | if (reply.isError()) { |
| 311 | m_lastExtendedError = QDBusError(); |
| 312 | emit propertyChanged(watcher->asyncProperty(), watcher->previousValue()); |
| 313 | } |
| 314 | |
| 315 | watcher->deleteLater(); |
| 316 | } |
| 317 | |
| 318 | void DBusExtendedAbstractInterface::onAsyncGetAllPropertiesFinished(QDBusPendingCallWatcher *watcher) |
| 319 | { |
| 320 | m_getAllPendingCallWatcher = 0; |
| 321 | |
| 322 | QDBusPendingReply<QVariantMap> reply = *watcher; |
| 323 | |
| 324 | if (reply.isError()) { |
| 325 | m_lastExtendedError = reply.error(); |
| 326 | } else { |
| 327 | m_lastExtendedError = QDBusError(); |
| 328 | } |
| 329 | |
| 330 | emit asyncGetAllPropertiesFinished(); |
| 331 | |
| 332 | if (!reply.isError()) { |
| 333 | onPropertiesChanged(interface(), reply.value(), QStringList()); |
| 334 | } |
| 335 | |
| 336 | watcher->deleteLater(); |
| 337 | } |
| 338 | |
| 339 | void DBusExtendedAbstractInterface::onPropertiesChanged(const QString& interfaceName, |
| 340 | const QVariantMap& changedProperties, |
| 341 | const QStringList& invalidatedProperties) |
| 342 | { |
| 343 | if (interfaceName == interface()) { |
| 344 | QVariantMap::const_iterator i = changedProperties.constBegin(); |
| 345 | while (i != changedProperties.constEnd()) { |
| 346 | int propertyIndex = metaObject()->indexOfProperty(i.key().toLatin1().constData()); |
| 347 | |
| 348 | if (-1 == propertyIndex) { |
| 349 | qDebug() << Q_FUNC_INFO << "Got unknown changed property" << i.key(); |
| 350 | } else { |
| 351 | QVariant value = demarshall(interface(), metaObject()->property(propertyIndex), i.value(), &m_lastExtendedError); |
| 352 | |
| 353 | if (m_lastExtendedError.isValid()) { |
| 354 | emit propertyInvalidated(i.key()); |
| 355 | } else { |
| 356 | emit propertyChanged(i.key(), value); |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | ++i; |
| 361 | } |
| 362 | |
| 363 | QStringList::const_iterator j = invalidatedProperties.constBegin(); |
| 364 | while (j != invalidatedProperties.constEnd()) { |
| 365 | if (-1 == metaObject()->indexOfProperty(j->toLatin1().constData())) { |
| 366 | qDebug() << Q_FUNC_INFO << "Got unknown invalidated property" << *j; |
| 367 | } else { |
| 368 | m_lastExtendedError = QDBusError(); |
| 369 | emit propertyInvalidated(*j); |
| 370 | } |
| 371 | |
| 372 | ++j; |
| 373 | } |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | QVariant DBusExtendedAbstractInterface::demarshall(const QString &interface, const QMetaProperty &metaProperty, const QVariant &value, QDBusError *error) |
| 378 | { |
| 379 | Q_ASSERT(metaProperty.isValid()); |
| 380 | Q_ASSERT(error != 0); |
| 381 | |
| 382 | if (value.userType() == metaProperty.userType()) { |
| 383 | // No need demarshalling. Passing back straight away ... |
| 384 | *error = QDBusError(); |
| 385 | return value; |
| 386 | } |
| 387 | |
| 388 | QVariant result = QVariant(metaProperty.userType(), (void*)0); |
| 389 | QString errorMessage; |
| 390 | const char *expectedSignature = QDBusMetaType::typeToSignature(metaProperty.userType()); |
| 391 | |
| 392 | if (value.userType() == qMetaTypeId<QDBusArgument>()) { |
| 393 | // demarshalling a DBus argument ... |
| 394 | QDBusArgument dbusArg = value.value<QDBusArgument>(); |
| 395 | |
| 396 | if (expectedSignature == dbusArg.currentSignature().toLatin1()) { |
| 397 | QDBusMetaType::demarshall(dbusArg, metaProperty.userType(), result.data()); |
| 398 | if (!result.isValid()) { |
| 399 | errorMessage = QStringLiteral("Unexpected failure demarshalling " |
| 400 | "upon PropertiesChanged signal arrival " |
| 401 | "for property `%3.%4' (expected type `%5' (%6))") |
| 402 | .arg(interface, |
| 403 | QString::fromLatin1(metaProperty.name()), |
| 404 | QString::fromLatin1(metaProperty.typeName()), |
| 405 | expectedSignature); |
| 406 | } |
| 407 | } else { |
| 408 | errorMessage = QStringLiteral("Unexpected `user type' (%2) " |
| 409 | "upon PropertiesChanged signal arrival " |
| 410 | "for property `%3.%4' (expected type `%5' (%6))") |
| 411 | .arg(dbusArg.currentSignature(), |
| 412 | interface, |
| 413 | QString::fromLatin1(metaProperty.name()), |
| 414 | QString::fromLatin1(metaProperty.typeName()), |
| 415 | QString::fromLatin1(expectedSignature)); |
| 416 | } |
| 417 | } else { |
| 418 | const char *actualSignature = QDBusMetaType::typeToSignature(value.userType()); |
| 419 | |
| 420 | errorMessage = QStringLiteral("Unexpected `%1' (%2) " |
| 421 | "upon PropertiesChanged signal arrival " |
| 422 | "for property `%3.%4' (expected type `%5' (%6))") |
| 423 | .arg(QString::fromLatin1(value.typeName()), |
| 424 | QString::fromLatin1(actualSignature), |
| 425 | interface, |
| 426 | QString::fromLatin1(metaProperty.name()), |
| 427 | QString::fromLatin1(metaProperty.typeName()), |
| 428 | QString::fromLatin1(expectedSignature)); |
| 429 | } |
| 430 | |
| 431 | if (errorMessage.isEmpty()) { |
| 432 | *error = QDBusError(); |
| 433 | } else { |
| 434 | *error = QDBusMessage::createError(QDBusError::InvalidSignature, errorMessage); |
| 435 | qDebug() << Q_FUNC_INFO << errorMessage; |
| 436 | } |
| 437 | |
| 438 | return result; |
| 439 | } |