Commit c6b50001 authored by Nicolas Fella's avatar Nicolas Fella
Browse files

[kcms/tablet] Fix crash when opening KCM for the second time

DevicesModel is a global singleton, so it is still alive by the time we open the KCM for the second time

However, the model is not initialized a second time, so no devices are loaded and any attempt to access data will crash

To avoid this have the model as a member of the KCM class so it gets properly destroyed when the KCM is closed and recreated when opened again

BUG: 451233
(cherry picked from commit 813aeeb7)
parent bc6c5fee
Pipeline #197552 passed with stage
in 3 minutes and 48 seconds
......@@ -11,12 +11,6 @@
#include "logging.h"
DevicesModel *DevicesModel::self()
{
static DevicesModel s_self;
return &s_self;
}
DevicesModel::DevicesModel(QObject *parent)
: QAbstractListModel(parent)
{
......
......@@ -26,7 +26,6 @@ public:
void defaults();
bool isSaveNeeded() const;
bool isDefaults() const;
static DevicesModel *self();
private Q_SLOTS:
void onDeviceAdded(const QString &sysName);
......
......@@ -100,18 +100,15 @@ public:
Tablet::Tablet(QObject *parent, const QVariantList &list)
: ManagedConfigModule(parent, list)
, m_devicesModel(new DevicesModel(this))
{
qmlRegisterSingletonType<DevicesModel>("org.kde.plasma.tablet.kcm", 1, 0, "DevicesModel", [](QQmlEngine *engine, QJSEngine * /*scriptEngine*/) {
engine->setObjectOwnership(DevicesModel::self(), QQmlEngine::CppOwnership);
return DevicesModel::self();
});
qmlRegisterType<OutputsModel>("org.kde.plasma.tablet.kcm", 1, 0, "OutputsModel");
qmlRegisterType<OrientationsModel>("org.kde.plasma.tablet.kcm", 1, 0, "OrientationsModel");
qmlRegisterAnonymousType<InputDevice>("org.kde.plasma.tablet.kcm", 1);
setAboutData(new KAboutData(QStringLiteral("kcm_tablet"), i18n("Tablet"), QStringLiteral("1.0"), i18n("Configure drawing tablets"), KAboutLicense::LGPL));
connect(DevicesModel::self(), &DevicesModel::needsSaveChanged, this, &Tablet::refreshNeedsSave);
connect(m_devicesModel, &DevicesModel::needsSaveChanged, this, &Tablet::refreshNeedsSave);
}
Tablet::~Tablet() = default;
......@@ -123,25 +120,30 @@ void Tablet::refreshNeedsSave()
bool Tablet::isSaveNeeded() const
{
return DevicesModel::self()->isSaveNeeded();
return m_devicesModel->isSaveNeeded();
}
bool Tablet::isDefaults() const
{
return DevicesModel::self()->isDefaults();
return m_devicesModel->isDefaults();
}
void Tablet::load()
{
DevicesModel::self()->load();
m_devicesModel->load();
}
void Tablet::save()
{
DevicesModel::self()->save();
m_devicesModel->save();
}
void Tablet::defaults()
{
DevicesModel::self()->defaults();
m_devicesModel->defaults();
}
DevicesModel *Tablet::devicesModel() const
{
return m_devicesModel;
}
#include "kcmtablet.moc"
......@@ -10,12 +10,15 @@
#include <KSharedConfig>
#include "devicesmodel.h"
class TabletSettings;
class TabletData;
class Tablet : public KQuickAddons::ManagedConfigModule
{
Q_OBJECT
Q_PROPERTY(DevicesModel *devicesModel READ devicesModel CONSTANT)
public:
explicit Tablet(QObject *parent = nullptr, const QVariantList &list = {});
......@@ -27,5 +30,10 @@ public:
bool isSaveNeeded() const override;
bool isDefaults() const override;
DevicesModel *devicesModel() const;
private:
void refreshNeedsSave();
DevicesModel *m_devicesModel;
};
......@@ -32,10 +32,10 @@ SimpleKCM {
QQC2.ComboBox {
id: combo
Kirigami.FormData.label: i18nd("kcmtablet", "Device:")
model: DevicesModel
model: kcm.devicesModel
onCurrentIndexChanged: {
parent.device = DevicesModel.deviceAt(combo.currentIndex)
parent.device = kcm.devicesModel.deviceAt(combo.currentIndex)
}
}
......
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