1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267
|
/*
Copyright 2010 Michael Zanetti <mzanetti@kde.org>
Copyright 2010-2012 Lukáš Tinkl <ltinkl@redhat.com>
Copyright 2012 Dan Vrátil <dvratil@redhat.com>
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 <http://www.gnu.org/licenses/>.
*/
#include "udisksdevicebackend.h"
#include <QDBusConnection>
#include <QDBusInterface>
#include <QDomDocument>
#include "solid-lite/deviceinterface.h"
#include "solid-lite/genericinterface.h"
using namespace Solid::Backends::UDisks2;
/* Static cache for DeviceBackends for all UDIs */
QMap<QString /* UDI */, DeviceBackend*> 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, QVariantMapMap)));
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<QVariantMap> reply = QDBusConnection::systemBus().call(call);
if (reply.isValid()) {
auto props = reply.value();
// Can not use QMap<>::unite(), as it allows multiple values per key
for (auto it = props.cbegin(); it != props.cend(); ++it) {
cacheProperty(it.key(), it.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<QString> 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";
}
}
void DeviceBackend::slotPropertiesChanged(const QString& /*ifaceName*/, const QVariantMap& changedProps, const QStringList& invalidatedProps)
{
//qDebug() << m_udi << "'s interface" << ifaceName << "changed props:";
QMap<QString, int> changeMap;
Q_FOREACH (const QString& key, invalidatedProps) {
m_propertyCache.remove(key);
changeMap.insert(key, Solid::GenericInterface::PropertyRemoved);
//qDebug() << "\t invalidated:" << key;
}
QMapIterator<QString, QVariant> 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);
}
Q_EMIT propertyChanged(changeMap);
Q_EMIT changed();
}
void DeviceBackend::slotInterfacesAdded(const QDBusObjectPath& object_path, const QVariantMapMap& 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(interfaces_and_properties.keys());
}
}
}
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);
}
}
// UDisks2 sends us null terminated strings, make sure to strip the extranous \0 in favor of the implicit \0.
// Otherwise comparision becomes unnecessarily complicated because 'foo\0' != 'foo'. QByteArrays are implicitly
// terminated already.
void DeviceBackend::cacheProperty(const QString& key, const QVariant& value) const
{
if (value.metaType() == QMetaType::fromType<QByteArray>()) {
auto blob = value.toByteArray();
while (blob.endsWith('\0')) {
blob.chop(1);
}
m_propertyCache.insert(key, blob);
}
else {
m_propertyCache.insert(key, value);
}
}
|