diff options
author | Robin Burchell <robin.burchell@jollamobile.com> | 2014-07-24 11:55:56 +0000 |
---|---|---|
committer | Robin Burchell <robin+qt@viroteck.net> | 2014-07-27 22:35:40 +0200 |
commit | 8d483aacad38b668940f3a4cc5d319d6c91edac6 (patch) | |
tree | f0f62ec8b2410cc2a3f58791335702d3def1b014 /src | |
parent | 34437490ad0fb018d59ea18ddc3ca98177fb3582 (diff) |
QUPowerDeviceInterface: Make every property request except the first one asynchronous.
Fetching UPower properties can take a long time (I've seen >20ms on a device
that isn't even under load), so minimizing the amount of time we wait on the
main thread for the reply makes sense.
We keep the properties we had before around, and only emit changed signals when
the properties actually change.
Change-Id: Ia3a111d16bf0149a9aa153bf08f37409663ff7be
Reviewed-by: Alex Blasche <alexander.blasche@digia.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/systeminfo/linux/qdevicekitservice_linux.cpp | 44 | ||||
-rw-r--r-- | src/systeminfo/linux/qdevicekitservice_linux_p.h | 6 |
2 files changed, 31 insertions, 19 deletions
diff --git a/src/systeminfo/linux/qdevicekitservice_linux.cpp b/src/systeminfo/linux/qdevicekitservice_linux.cpp index 7a8c62da..1e27cbcd 100644 --- a/src/systeminfo/linux/qdevicekitservice_linux.cpp +++ b/src/systeminfo/linux/qdevicekitservice_linux.cpp @@ -140,15 +140,7 @@ QUPowerDeviceInterface::QUPowerDeviceInterface(const QString &dbusPathName, QObj , QDBusConnection::systemBus() , parent) { - pMap = getProperties(); -} - -QUPowerDeviceInterface::~QUPowerDeviceInterface() -{ -} - -QVariantMap QUPowerDeviceInterface::getProperties() -{ + // We make a synchronous properties request here so we have a valid state QDBusMessage propGetMsg = QDBusMessage::createMethodCall(UPOWER_SERVICE, path(), QStringLiteral("org.freedesktop.DBus.Properties"), QLatin1String("GetAll")); QList<QVariant> arguments; arguments << QLatin1String("org.freedesktop.UPower.Device"); @@ -156,9 +148,8 @@ QVariantMap QUPowerDeviceInterface::getProperties() QDBusMessage propReply = QDBusConnection::systemBus().call(propGetMsg); if (propReply.type() == QDBusMessage::ErrorMessage) { - // don't throw away the existing map if the call fails qWarning() << "QUPowerDeviceInterface: error getting properties: " << propReply.errorMessage(); - return pMap; + return; } QList<QVariant> args = propReply.arguments(); @@ -166,11 +157,14 @@ QVariantMap QUPowerDeviceInterface::getProperties() if (!args.length()) { qWarning() << "QUPowerDeviceInterface: Got an empty property reply"; pMap = QVariantMap(); - return pMap; + return; } pMap = args[0].toMap(); - return pMap; +} + +QUPowerDeviceInterface::~QUPowerDeviceInterface() +{ } QVariant QUPowerDeviceInterface::getProperty(const QString &property) @@ -275,8 +269,28 @@ void QUPowerDeviceInterface::disconnectNotify(const QMetaMethod &signal) void QUPowerDeviceInterface::propChanged() { - QVariantMap map = pMap; - QMapIterator<QString, QVariant> i(getProperties()); + QDBusMessage propGetMsg = QDBusMessage::createMethodCall(UPOWER_SERVICE, path(), QStringLiteral("org.freedesktop.DBus.Properties"), QLatin1String("GetAll")); + QList<QVariant> arguments; + arguments << QLatin1String("org.freedesktop.UPower.Device"); + propGetMsg.setArguments(arguments); + QDBusPendingCall asyncPropReply = QDBusConnection::systemBus().asyncCall(propGetMsg); + QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(asyncPropReply, this); + + connect(watcher, &QDBusPendingCallWatcher::finished, this, &QUPowerDeviceInterface::propRequestFinished); +} + +void QUPowerDeviceInterface::propRequestFinished(QDBusPendingCallWatcher *call) +{ + QDBusPendingReply<QVariantMap> reply = *call; + if (!reply.isValid()) { + // don't throw away the existing map if the call fails + qWarning() << "QUPowerDeviceInterface: fetching properties failed: " << reply.error(); + return; + } + + QVariantMap map = pMap; // copy to compare + pMap = reply.value(); + QMapIterator<QString, QVariant> i(reply.value()); while (i.hasNext()) { i.next(); diff --git a/src/systeminfo/linux/qdevicekitservice_linux_p.h b/src/systeminfo/linux/qdevicekitservice_linux_p.h index 163a026e..8adcca35 100644 --- a/src/systeminfo/linux/qdevicekitservice_linux_p.h +++ b/src/systeminfo/linux/qdevicekitservice_linux_p.h @@ -101,12 +101,11 @@ public: qint64 timeToFull(); QString nativePath(); - QVariantMap getProperties(); + QVariantMap getProperties() { return pMap; } Q_SIGNALS: void changed(); void propertyChanged(QString,QVariant); -// void getPropertiesFinished(const QVariantMap &properties); protected: void connectNotify(const QMetaMethod &signal); @@ -118,8 +117,7 @@ private: private Q_SLOTS: void propChanged(); - //void propertiesFinished(QDBusPendingCallWatcher *watch); - + void propRequestFinished(QDBusPendingCallWatcher *call); }; |