diff --git a/src/solid/devices/frontend/device.cpp b/src/solid/devices/frontend/device.cpp index 7e02997..17d60a0 100644 --- a/src/solid/devices/frontend/device.cpp +++ b/src/solid/devices/frontend/device.cpp @@ -1,272 +1,269 @@ /* Copyright 2005-2007 Kevin Ottens 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 "device.h" #include "device_p.h" #include "devicenotifier.h" #include "devicemanager_p.h" #include "deviceinterface_p.h" #include "soliddefs_p.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include Solid::Device::Device(const QString &udi) { DeviceManagerPrivate *manager = static_cast(Solid::DeviceNotifier::instance()); d = manager->findRegisteredDevice(udi); } Solid::Device::Device(const Device &device) : d(device.d) { } Solid::Device::~Device() { } Solid::Device &Solid::Device::operator=(const Solid::Device &device) { d = device.d; return *this; } bool Solid::Device::isValid() const { return d->backendObject() != nullptr; } QString Solid::Device::udi() const { return d->udi(); } QString Solid::Device::parentUdi() const { return_SOLID_CALL(Ifaces::Device *, d->backendObject(), QString(), parentUdi()); } Solid::Device Solid::Device::parent() const { QString udi = parentUdi(); if (udi.isEmpty()) { return Device(); } else { return Device(udi); } } QString Solid::Device::vendor() const { return_SOLID_CALL(Ifaces::Device *, d->backendObject(), QString(), vendor()); } QString Solid::Device::product() const { return_SOLID_CALL(Ifaces::Device *, d->backendObject(), QString(), product()); } QString Solid::Device::icon() const { return_SOLID_CALL(Ifaces::Device *, d->backendObject(), QString(), icon()); } QStringList Solid::Device::emblems() const { return_SOLID_CALL(Ifaces::Device *, d->backendObject(), QStringList(), emblems()); } QString Solid::Device::description() const { return_SOLID_CALL(Ifaces::Device *, d->backendObject(), QString(), description()); } bool Solid::Device::isDeviceInterface(const DeviceInterface::Type &type) const { return_SOLID_CALL(Ifaces::Device *, d->backendObject(), false, queryDeviceInterface(type)); } #define deviceinterface_cast(IfaceType, DevType, backendObject) \ (qobject_cast(backendObject) ? new DevType(backendObject) : nullptr) Solid::DeviceInterface *Solid::Device::asDeviceInterface(const DeviceInterface::Type &type) { const Solid::DeviceInterface *interface = const_cast(this)->asDeviceInterface(type); return const_cast(interface); } const Solid::DeviceInterface *Solid::Device::asDeviceInterface(const DeviceInterface::Type &type) const { Ifaces::Device *device = qobject_cast(d->backendObject()); if (device != nullptr) { DeviceInterface *iface = d->interface(type); if (iface != nullptr) { return iface; } QObject *dev_iface = device->createDeviceInterface(type); if (dev_iface != nullptr) { switch (type) { case DeviceInterface::GenericInterface: iface = deviceinterface_cast(Ifaces::GenericInterface, GenericInterface, dev_iface); break; case DeviceInterface::Processor: iface = deviceinterface_cast(Ifaces::Processor, Processor, dev_iface); break; case DeviceInterface::Block: iface = deviceinterface_cast(Ifaces::Block, Block, dev_iface); break; case DeviceInterface::StorageAccess: iface = deviceinterface_cast(Ifaces::StorageAccess, StorageAccess, dev_iface); break; case DeviceInterface::StorageDrive: iface = deviceinterface_cast(Ifaces::StorageDrive, StorageDrive, dev_iface); break; case DeviceInterface::OpticalDrive: iface = deviceinterface_cast(Ifaces::OpticalDrive, OpticalDrive, dev_iface); break; case DeviceInterface::StorageVolume: iface = deviceinterface_cast(Ifaces::StorageVolume, StorageVolume, dev_iface); break; case DeviceInterface::OpticalDisc: iface = deviceinterface_cast(Ifaces::OpticalDisc, OpticalDisc, dev_iface); break; case DeviceInterface::Camera: iface = deviceinterface_cast(Ifaces::Camera, Camera, dev_iface); break; case DeviceInterface::PortableMediaPlayer: iface = deviceinterface_cast(Ifaces::PortableMediaPlayer, PortableMediaPlayer, dev_iface); break; case DeviceInterface::Battery: iface = deviceinterface_cast(Ifaces::Battery, Battery, dev_iface); break; case DeviceInterface::NetworkShare: iface = deviceinterface_cast(Ifaces::NetworkShare, NetworkShare, dev_iface); break; case DeviceInterface::Unknown: case DeviceInterface::Last: break; } } if (iface != nullptr) { // Lie on the constness since we're simply doing caching here const_cast(this)->d->setInterface(type, iface); iface->d_ptr->setDevicePrivate(d.data()); } return iface; } else { return nullptr; } } ////////////////////////////////////////////////////////////////////// Solid::DevicePrivate::DevicePrivate(const QString &udi) : QObject(), QSharedData(), m_udi(udi) { } Solid::DevicePrivate::~DevicePrivate() { - Q_FOREACH (DeviceInterface *iface, m_ifaces) { - delete iface->d_ptr->backendObject(); - } setBackendObject(nullptr); } void Solid::DevicePrivate::_k_destroyed(QObject *object) { Q_UNUSED(object); setBackendObject(nullptr); } void Solid::DevicePrivate::setBackendObject(Ifaces::Device *object) { if (m_backendObject) { m_backendObject.data()->disconnect(this); } delete m_backendObject.data(); m_backendObject = object; if (object) { connect(object, SIGNAL(destroyed(QObject*)), this, SLOT(_k_destroyed(QObject*))); } if (!m_ifaces.isEmpty()) { Q_FOREACH (DeviceInterface *iface, m_ifaces) { delete iface; } m_ifaces.clear(); if (!ref.deref()) { deleteLater(); } } } Solid::DeviceInterface *Solid::DevicePrivate::interface(const DeviceInterface::Type &type) const { return m_ifaces[type]; } void Solid::DevicePrivate::setInterface(const DeviceInterface::Type &type, DeviceInterface *interface) { if (m_ifaces.isEmpty()) { ref.ref(); } m_ifaces[type] = interface; } diff --git a/src/solid/devices/frontend/device_p.h b/src/solid/devices/frontend/device_p.h index 56e6dbd..7ff7412 100644 --- a/src/solid/devices/frontend/device_p.h +++ b/src/solid/devices/frontend/device_p.h @@ -1,68 +1,68 @@ /* Copyright 2005-2007 Kevin Ottens 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 . */ #ifndef SOLID_DEVICE_P_H #define SOLID_DEVICE_P_H #include #include #include #include #include #if defined(Q_OS_WIN32) #undef interface #endif namespace Solid { class DevicePrivate : public QObject, public QSharedData { Q_OBJECT public: explicit DevicePrivate(const QString &udi); ~DevicePrivate(); QString udi() const { return m_udi; } Ifaces::Device *backendObject() const { return m_backendObject.data(); } void setBackendObject(Ifaces::Device *object); DeviceInterface *interface(const DeviceInterface::Type &type) const; void setInterface(const DeviceInterface::Type &type, DeviceInterface *interface); public Q_SLOTS: void _k_destroyed(QObject *object); private: QString m_udi; QPointer m_backendObject; - QMap m_ifaces; + QMap> m_ifaces; }; } #endif diff --git a/src/solid/devices/frontend/deviceinterface.cpp b/src/solid/devices/frontend/deviceinterface.cpp index 9f54b41..cd86ada 100644 --- a/src/solid/devices/frontend/deviceinterface.cpp +++ b/src/solid/devices/frontend/deviceinterface.cpp @@ -1,127 +1,128 @@ /* Copyright 2006-2007 Kevin Ottens 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 "deviceinterface.h" #include "deviceinterface_p.h" #include #include Solid::DeviceInterface::DeviceInterface(DeviceInterfacePrivate &dd, QObject *backendObject) : d_ptr(&dd) { Q_D(DeviceInterface); d->setBackendObject(backendObject); } Solid::DeviceInterface::~DeviceInterface() { + delete d_ptr->backendObject(); delete d_ptr; d_ptr = nullptr; } bool Solid::DeviceInterface::isValid() const { Q_D(const DeviceInterface); return d->backendObject() != nullptr; } QString Solid::DeviceInterface::typeToString(Type type) { int index = staticMetaObject.indexOfEnumerator("Type"); QMetaEnum metaEnum = staticMetaObject.enumerator(index); return QString(metaEnum.valueToKey((int)type)); } Solid::DeviceInterface::Type Solid::DeviceInterface::stringToType(const QString &type) { int index = staticMetaObject.indexOfEnumerator("Type"); QMetaEnum metaEnum = staticMetaObject.enumerator(index); return (Type)metaEnum.keyToValue(type.toUtf8()); } QString Solid::DeviceInterface::typeDescription(Type type) { switch (type) { case Unknown: return tr("Unknown", "Unknown device type"); case GenericInterface: return tr("Generic Interface", "Generic Interface device type"); case Processor: return tr("Processor", "Processor device type"); case Block: return tr("Block", "Block device type"); case StorageAccess: return tr("Storage Access", "Storage Access device type"); case StorageDrive: return tr("Storage Drive", "Storage Drive device type"); case OpticalDrive: return tr("Optical Drive", "Optical Drive device type"); case StorageVolume: return tr("Storage Volume", "Storage Volume device type"); case OpticalDisc: return tr("Optical Disc", "Optical Disc device type"); case Camera: return tr("Camera", "Camera device type"); case PortableMediaPlayer: return tr("Portable Media Player", "Portable Media Player device type"); case Battery: return tr("Battery", "Battery device type"); case NetworkShare: return tr("Network Share", "Network Share device type"); case Last: return QString(); } return QString(); } Solid::DeviceInterfacePrivate::DeviceInterfacePrivate() : m_devicePrivate(nullptr) { } Solid::DeviceInterfacePrivate::~DeviceInterfacePrivate() { } QObject *Solid::DeviceInterfacePrivate::backendObject() const { return m_backendObject.data(); } void Solid::DeviceInterfacePrivate::setBackendObject(QObject *object) { m_backendObject = object; } Solid::DevicePrivate *Solid::DeviceInterfacePrivate::devicePrivate() const { return m_devicePrivate; } void Solid::DeviceInterfacePrivate::setDevicePrivate(DevicePrivate *devicePrivate) { m_devicePrivate = devicePrivate; }