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

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)
{
m_container = new SensorContainer("power", i18nc("@title", "Power"), this);
const auto batteries = Solid::Device::listFromType(Solid::DeviceInterface::Battery);
for (const auto &battery : batteries) {
new Battery(battery.as<Solid::Battery>(), battery.displayName(), m_container);
for (const auto &device : batteries) {
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) {
const Solid::Device device(udi);
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) {
const Solid::Device device(udi);
if (device.isDeviceInterface(Solid::DeviceInterface::Battery)) {
const auto object = m_container->object(device.as<Solid::Battery>()->serial());
if (object) {
m_container->removeObject(object);
}
if (m_batteriesByUdi.contains(udi)) {
m_container->removeObject(m_batteriesByUdi[udi]);
m_batteriesByUdi.remove(udi);
}
});
}
......
......@@ -25,6 +25,7 @@ namespace Solid {
class Device;
}
class Battery;
class SensorContainer;
class PowerPlugin : public SensorPlugin {
......@@ -33,6 +34,7 @@ public:
PowerPlugin(QObject *parent, const QVariantList &args);
private:
SensorContainer *m_container;
QHash<QString, Battery*> m_batteriesByUdi;
};
#endif
Markdown is supported
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