Commit a5f21af1 authored by David Redondo's avatar David Redondo 🏎
Browse files

Do not crash when reconnecting a battery

If the device is removed, we cannot create a Device from the udi anymore.
parent 4769707c
...@@ -105,23 +105,22 @@ PowerPlugin::PowerPlugin(QObject *parent, const QVariantList &args) ...@@ -105,23 +105,22 @@ PowerPlugin::PowerPlugin(QObject *parent, const QVariantList &args)
{ {
m_container = new SensorContainer("power", i18nc("@title", "Power"), this); m_container = new SensorContainer("power", i18nc("@title", "Power"), this);
const auto batteries = Solid::Device::listFromType(Solid::DeviceInterface::Battery); const auto batteries = Solid::Device::listFromType(Solid::DeviceInterface::Battery);
for (const auto &battery : batteries) { for (const auto &device : batteries) {
new Battery(battery.as<Solid::Battery>(), battery.displayName(), m_container); auto battery = new Battery(device.as<Solid::Battery>(), device.displayName(), m_container);
m_batteriesByUdi.insert(device.udi(), battery);
} }
connect(Solid::DeviceNotifier::instance(), &Solid::DeviceNotifier::deviceAdded, this, [this] (const QString &udi) { connect(Solid::DeviceNotifier::instance(), &Solid::DeviceNotifier::deviceAdded, this, [this] (const QString &udi) {
const Solid::Device device(udi); const Solid::Device device(udi);
if (device.isDeviceInterface(Solid::DeviceInterface::Battery)) { if (device.isDeviceInterface(Solid::DeviceInterface::Battery)) {
new Battery(device.as<Solid::Battery>(), device.displayName(), m_container); auto battery = new Battery(device.as<Solid::Battery>(), device.displayName(), m_container);
m_batteriesByUdi.insert(udi, battery);
} }
}); });
connect(Solid::DeviceNotifier::instance(), &Solid::DeviceNotifier::deviceRemoved, this, [this] (const QString &udi) { connect(Solid::DeviceNotifier::instance(), &Solid::DeviceNotifier::deviceRemoved, this, [this] (const QString &udi) {
const Solid::Device device(udi); if (m_batteriesByUdi.contains(udi)) {
if (device.isDeviceInterface(Solid::DeviceInterface::Battery)) { m_container->removeObject(m_batteriesByUdi[udi]);
const auto object = m_container->object(device.as<Solid::Battery>()->serial()); m_batteriesByUdi.remove(udi);
if (object) {
m_container->removeObject(object);
}
} }
}); });
} }
......
...@@ -25,6 +25,7 @@ namespace Solid { ...@@ -25,6 +25,7 @@ namespace Solid {
class Device; class Device;
} }
class Battery;
class SensorContainer; class SensorContainer;
class PowerPlugin : public SensorPlugin { class PowerPlugin : public SensorPlugin {
...@@ -33,6 +34,7 @@ public: ...@@ -33,6 +34,7 @@ public:
PowerPlugin(QObject *parent, const QVariantList &args); PowerPlugin(QObject *parent, const QVariantList &args);
private: private:
SensorContainer *m_container; SensorContainer *m_container;
QHash<QString, Battery*> m_batteriesByUdi;
}; };
#endif #endif
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment