diff --git a/src/solid/devices/backends/udisks2/udisksdevicebackend.cpp b/src/solid/devices/backends/udisks2/udisksdevicebackend.cpp index b9fcd0b..2639851 100644 --- a/src/solid/devices/backends/udisks2/udisksdevicebackend.cpp +++ b/src/solid/devices/backends/udisks2/udisksdevicebackend.cpp @@ -1,247 +1,251 @@ /* Copyright 2010 Michael Zanetti Copyright 2010-2012 Lukáš Tinkl Copyright 2012 Dan Vrátil This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) version 3, or any later version accepted by the membership of KDE e.V. (or its successor approved by the membership of KDE e.V.), which shall act as a proxy defined in Section 6 of version 3 of the license. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library. If not, see . */ #include "udisksdevicebackend.h" #include #include #include #include "solid/deviceinterface.h" #include "solid/genericinterface.h" using namespace Solid::Backends::UDisks2; /* Static cache for DeviceBackends for all UDIs */ QMap DeviceBackend::s_backends; DeviceBackend *DeviceBackend::backendForUDI(const QString &udi, bool create) { DeviceBackend *backend = nullptr; if (udi.isEmpty()) { return backend; } if (s_backends.contains(udi)) { backend = s_backends.value(udi); } else if (create) { backend = new DeviceBackend(udi); s_backends.insert(udi, backend); } return backend; } void DeviceBackend::destroyBackend(const QString &udi) { if (s_backends.contains(udi)) { DeviceBackend *backend = s_backends.value(udi); s_backends.remove(udi); delete backend; } } DeviceBackend::DeviceBackend(const QString &udi) : m_udi(udi) { //qDebug() << "Creating backend for device" << m_udi; m_device = new QDBusInterface(UD2_DBUS_SERVICE, m_udi, QString(), // no interface, we aggregate them QDBusConnection::systemBus(), this); if (m_device->isValid()) { QDBusConnection::systemBus().connect(UD2_DBUS_SERVICE, m_udi, DBUS_INTERFACE_PROPS, "PropertiesChanged", this, SLOT(slotPropertiesChanged(QString,QVariantMap,QStringList))); QDBusConnection::systemBus().connect(UD2_DBUS_SERVICE, UD2_DBUS_PATH, DBUS_INTERFACE_MANAGER, "InterfacesAdded", this, SLOT(slotInterfacesAdded(QDBusObjectPath,VariantMapMap))); QDBusConnection::systemBus().connect(UD2_DBUS_SERVICE, UD2_DBUS_PATH, DBUS_INTERFACE_MANAGER, "InterfacesRemoved", this, SLOT(slotInterfacesRemoved(QDBusObjectPath,QStringList))); initInterfaces(); } } DeviceBackend::~DeviceBackend() { //qDebug() << "Destroying backend for device" << m_udi; } void DeviceBackend::initInterfaces() { m_interfaces.clear(); const QString xmlData = introspect(); if (xmlData.isEmpty()) { qDebug() << m_udi << "has no interfaces!"; return; } QDomDocument dom; dom.setContent(xmlData); QDomNodeList ifaceNodeList = dom.elementsByTagName("interface"); for (int i = 0; i < ifaceNodeList.count(); i++) { QDomElement ifaceElem = ifaceNodeList.item(i).toElement(); /* Accept only org.freedesktop.UDisks2.* interfaces so that when the device is unplugged, * m_interfaces goes empty and we can easily verify that the device is gone. */ if (!ifaceElem.isNull() && ifaceElem.attribute("name").startsWith(UD2_DBUS_SERVICE)) { m_interfaces.append(ifaceElem.attribute("name")); } } //qDebug() << m_udi << "has interfaces:" << m_interfaces; } QStringList DeviceBackend::interfaces() const { return m_interfaces; } const QString &DeviceBackend::udi() const { return m_udi; } QVariant DeviceBackend::prop(const QString &key) const { checkCache(key); return m_propertyCache.value(key); } bool DeviceBackend::propertyExists(const QString &key) const { checkCache(key); /* checkCache() will put an invalid QVariant in cache when the property * does not exist, so check for validity, not for an actual presence. */ return m_propertyCache.value(key).isValid(); } QVariantMap DeviceBackend::allProperties() const { QDBusMessage call = QDBusMessage::createMethodCall(UD2_DBUS_SERVICE, m_udi, DBUS_INTERFACE_PROPS, "GetAll"); Q_FOREACH (const QString &iface, m_interfaces) { call.setArguments(QVariantList() << iface); QDBusPendingReply reply = QDBusConnection::systemBus().call(call); if (reply.isValid()) { m_propertyCache.unite(reply.value()); } else { qWarning() << "Error getting props:" << reply.error().name() << reply.error().message(); } //qDebug() << "After iface" << iface << ", cache now contains" << m_cache.size() << "items"; } return m_propertyCache; } void DeviceBackend::invalidateProperties() { m_propertyCache.clear(); } QString DeviceBackend::introspect() const { QDBusMessage call = QDBusMessage::createMethodCall(UD2_DBUS_SERVICE, m_udi, DBUS_INTERFACE_INTROSPECT, "Introspect"); QDBusPendingReply reply = QDBusConnection::systemBus().call(call); if (reply.isValid()) { return reply.value(); } else { return QString(); } } void DeviceBackend::checkCache(const QString &key) const { if (m_propertyCache.isEmpty()) { // recreate the cache allProperties(); } if (m_propertyCache.contains(key)) { return; } - QVariant reply = m_device->property(key.toUtf8()); - m_propertyCache.insert(key, reply); - - if (!reply.isValid()) { - /* Store the item in the cache anyway so next time we don't have to - * do the DBus call to find out it does not exist but just check whether - * prop(key).isValid() */ -// qDebug() << m_udi << ": property" << key << "does not exist"; - } + QDBusMessage call = QDBusMessage::createMethodCall(UD2_DBUS_SERVICE, m_udi, DBUS_INTERFACE_PROPS, "Get"); + /* + * Interface is set to an empty string as in this QDBusInterface is a meta-object of multiple interfaces on the same path + * The DBus properties also interface supports this, and will find the appropriate interface if none is explicitly set. + * This matches what QDBusAbstractInterface would do + */ + call.setArguments(QVariantList() << QString() << key); + QDBusPendingReply reply = QDBusConnection::systemBus().call(call); + + /* We don't check for error here and store the item in the cache anyway so next time we don't have to + * do the DBus call to find out it does not exist but just check whether + * prop(key).isValid() */ + m_propertyCache.insert(key, reply.value()); } void DeviceBackend::slotPropertiesChanged(const QString &ifaceName, const QVariantMap &changedProps, const QStringList &invalidatedProps) { if (!ifaceName.startsWith(UD2_DBUS_SERVICE)) { return; } //qDebug() << m_udi << "'s interface" << ifaceName << "changed props:"; QMap changeMap; Q_FOREACH (const QString &key, invalidatedProps) { m_propertyCache.remove(key); changeMap.insert(key, Solid::GenericInterface::PropertyModified); //qDebug() << "\t invalidated:" << key; } QMapIterator i(changedProps); while (i.hasNext()) { i.next(); const QString key = i.key(); m_propertyCache.insert(key, i.value()); // replace the value changeMap.insert(key, Solid::GenericInterface::PropertyModified); //qDebug() << "\t modified:" << key << ":" << m_propertyCache.value(key); } emit propertyChanged(changeMap); emit changed(); } void DeviceBackend::slotInterfacesAdded(const QDBusObjectPath &object_path, const VariantMapMap &interfaces_and_properties) { if (object_path.path() != m_udi) { return; } Q_FOREACH (const QString &iface, interfaces_and_properties.keys()) { /* Don't store generic DBus interfaces */ if (iface.startsWith(UD2_DBUS_SERVICE)) { m_interfaces.append(iface); } } } void DeviceBackend::slotInterfacesRemoved(const QDBusObjectPath &object_path, const QStringList &interfaces) { if (object_path.path() != m_udi) { return; } Q_FOREACH (const QString &iface, interfaces) { m_interfaces.removeAll(iface); } }