Commit ca02af65 authored by Méven Car's avatar Méven Car

Solid-device-automounter/kcm: Get rid of singleton for AutomounterSettings

Summary: Depends on D27127

Reviewers: ervin, crossi, bport, #plasma

Reviewed By: ervin, bport

Subscribers: plasma-devel

Tags: #plasma

Differential Revision: https://phabricator.kde.org/D27480
parent 2a6abf7e
...@@ -41,7 +41,8 @@ K_PLUGIN_FACTORY(DeviceAutomounterKCMFactory, registerPlugin<DeviceAutomounterKC ...@@ -41,7 +41,8 @@ K_PLUGIN_FACTORY(DeviceAutomounterKCMFactory, registerPlugin<DeviceAutomounterKC
DeviceAutomounterKCM::DeviceAutomounterKCM(QWidget *parent, const QVariantList &args) DeviceAutomounterKCM::DeviceAutomounterKCM(QWidget *parent, const QVariantList &args)
: KCModule(parent, args)//DeviceAutomounterKCMFactory::componentData(), parent) : KCModule(parent, args)//DeviceAutomounterKCMFactory::componentData(), parent)
, m_devices(new DeviceModel(this)) , m_settings(new AutomounterSettings(this))
, m_devices(new DeviceModel(m_settings, this))
{ {
KAboutData *about = new KAboutData(QStringLiteral("kcm_device_automounter"), KAboutData *about = new KAboutData(QStringLiteral("kcm_device_automounter"),
i18n("Device Automounter"), i18n("Device Automounter"),
...@@ -55,7 +56,7 @@ DeviceAutomounterKCM::DeviceAutomounterKCM(QWidget *parent, const QVariantList & ...@@ -55,7 +56,7 @@ DeviceAutomounterKCM::DeviceAutomounterKCM(QWidget *parent, const QVariantList &
setAboutData(about); setAboutData(about);
setupUi(this); setupUi(this);
addConfig(AutomounterSettings::self(), this); addConfig(m_settings, this);
deviceView->setModel(m_devices); deviceView->setModel(m_devices);
...@@ -113,11 +114,11 @@ void DeviceAutomounterKCM::load() ...@@ -113,11 +114,11 @@ void DeviceAutomounterKCM::load()
{ {
KCModule::load(); KCModule::load();
bool automountEnabled = AutomounterSettings::self()->automountEnabled(); bool automountEnabled = m_settings->automountEnabled();
kcfg_AutomountUnknownDevices->setEnabled(automountEnabled); kcfg_AutomountUnknownDevices->setEnabled(m_settings->automountEnabled());
kcfg_AutomountOnLogin->setEnabled(automountEnabled); kcfg_AutomountOnLogin->setEnabled(m_settings->automountEnabled());
kcfg_AutomountOnPlugin->setEnabled(automountEnabled); kcfg_AutomountOnPlugin->setEnabled(m_settings->automountEnabled());
m_devices->reload(); m_devices->reload();
loadLayout(); loadLayout();
...@@ -140,29 +141,29 @@ void DeviceAutomounterKCM::save() ...@@ -140,29 +141,29 @@ void DeviceAutomounterKCM::save()
validDevices << device; validDevices << device;
if (dev.data(Qt::CheckStateRole).toInt() == Qt::Checked) { if (dev.data(Qt::CheckStateRole).toInt() == Qt::Checked) {
AutomounterSettings::deviceSettings(device).writeEntry("ForceLoginAutomount", true); m_settings->deviceSettings(device).writeEntry("ForceLoginAutomount", true);
} else { } else {
AutomounterSettings::deviceSettings(device).writeEntry("ForceLoginAutomount", false); m_settings->deviceSettings(device).writeEntry("ForceLoginAutomount", false);
} }
dev = dev.sibling(j, 2); dev = dev.sibling(j, 2);
if (dev.data(Qt::CheckStateRole).toInt() == Qt::Checked) { if (dev.data(Qt::CheckStateRole).toInt() == Qt::Checked) {
AutomounterSettings::deviceSettings(device).writeEntry("ForceAttachAutomount", true); m_settings->deviceSettings(device).writeEntry("ForceAttachAutomount", true);
} else { } else {
AutomounterSettings::deviceSettings(device).writeEntry("ForceAttachAutomount", false); m_settings->deviceSettings(device).writeEntry("ForceAttachAutomount", false);
} }
} }
} }
const auto knownDevices = AutomounterSettings::knownDevices(); const auto knownDevices = m_settings->knownDevices();
for (const QString &possibleDevice : knownDevices) { for (const QString &possibleDevice : knownDevices) {
if (!validDevices.contains(possibleDevice)) { if (!validDevices.contains(possibleDevice)) {
AutomounterSettings::deviceSettings(possibleDevice).deleteGroup(); m_settings->deviceSettings(possibleDevice).deleteGroup();
} }
} }
AutomounterSettings::self()->save(); m_settings->save();
// Now tell kded to automatically load the module if loaded // Now tell kded to automatically load the module if loaded
QDBusConnection dbus = QDBusConnection::sessionBus(); QDBusConnection dbus = QDBusConnection::sessionBus();
......
...@@ -48,6 +48,7 @@ private: ...@@ -48,6 +48,7 @@ private:
void saveLayout(); void saveLayout();
void loadLayout(); void loadLayout();
AutomounterSettings *m_settings;
DeviceModel *m_devices; DeviceModel *m_devices;
}; };
......
...@@ -30,8 +30,9 @@ ...@@ -30,8 +30,9 @@
#include "AutomounterSettings.h" #include "AutomounterSettings.h"
DeviceModel::DeviceModel(QObject *parent) DeviceModel::DeviceModel(AutomounterSettings *m_settings, QObject *parent)
: QAbstractItemModel(parent) : QAbstractItemModel(parent)
, m_settings(m_settings)
{ {
reload(); reload();
...@@ -104,13 +105,13 @@ void DeviceModel::deviceRemoved(const QString &udi) ...@@ -104,13 +105,13 @@ void DeviceModel::deviceRemoved(const QString &udi)
void DeviceModel::addNewDevice(const QString &udi) void DeviceModel::addNewDevice(const QString &udi)
{ {
AutomounterSettings::self()->load(); m_settings->load();
if (!m_loginForced.contains(udi)) { if (!m_loginForced.contains(udi)) {
m_loginForced[udi] = AutomounterSettings::deviceAutomountIsForced(udi, AutomounterSettings::Login); m_loginForced[udi] = m_settings->deviceAutomountIsForced(udi, AutomounterSettings::Login);
} }
if (!m_attachedForced.contains(udi)) { if (!m_attachedForced.contains(udi)) {
m_loginForced[udi] = AutomounterSettings::deviceAutomountIsForced(udi, AutomounterSettings::Attach); m_loginForced[udi] = m_settings->deviceAutomountIsForced(udi, AutomounterSettings::Attach);
} }
const Solid::Device dev(udi); const Solid::Device dev(udi);
...@@ -138,17 +139,17 @@ void DeviceModel::reload() ...@@ -138,17 +139,17 @@ void DeviceModel::reload()
m_attached.clear(); m_attached.clear();
m_disconnected.clear(); m_disconnected.clear();
m_automaticLogin = AutomounterSettings::automountOnLogin(); m_automaticLogin = m_settings->automountOnLogin();
m_automaticAttached = AutomounterSettings::automountOnPlugin(); m_automaticAttached = m_settings->automountOnPlugin();
const auto knownDevices = AutomounterSettings::knownDevices(); const auto knownDevices = m_settings->knownDevices();
for (const QString &dev : knownDevices) { for (const QString &dev : knownDevices) {
addNewDevice(dev); addNewDevice(dev);
} }
const auto keys = m_loginForced.keys(); const auto keys = m_loginForced.keys();
for (const QString &udi : keys) { for (const QString &udi : keys) {
m_loginForced[udi] = AutomounterSettings::deviceAutomountIsForced(udi, AutomounterSettings::Login); m_loginForced[udi] = m_settings->deviceAutomountIsForced(udi, AutomounterSettings::Login);
m_attachedForced[udi] = AutomounterSettings::deviceAutomountIsForced(udi, AutomounterSettings::Attach); m_attachedForced[udi] = m_settings->deviceAutomountIsForced(udi, AutomounterSettings::Attach);
} }
endResetModel(); endResetModel();
} }
...@@ -277,7 +278,7 @@ QVariant DeviceModel::data(const QModelIndex &index, int role) const ...@@ -277,7 +278,7 @@ QVariant DeviceModel::data(const QModelIndex &index, int role) const
case Qt::CheckStateRole: case Qt::CheckStateRole:
return m_loginForced[udi] ? Qt::Checked : Qt::Unchecked; return m_loginForced[udi] ? Qt::Checked : Qt::Unchecked;
case Qt::ToolTipRole: case Qt::ToolTipRole:
if (m_loginForced[udi] || AutomounterSettings::shouldAutomountDevice(udi, AutomounterSettings::Login)) if (m_loginForced[udi] || m_settings->shouldAutomountDevice(udi, AutomounterSettings::Login))
return i18n("This device will be automatically mounted at login."); return i18n("This device will be automatically mounted at login.");
return i18n("This device will not be automatically mounted at login."); return i18n("This device will not be automatically mounted at login.");
} }
...@@ -286,7 +287,7 @@ QVariant DeviceModel::data(const QModelIndex &index, int role) const ...@@ -286,7 +287,7 @@ QVariant DeviceModel::data(const QModelIndex &index, int role) const
case Qt::CheckStateRole: case Qt::CheckStateRole:
return m_attachedForced[udi] ? Qt::Checked : Qt::Unchecked; return m_attachedForced[udi] ? Qt::Checked : Qt::Unchecked;
case Qt::ToolTipRole: case Qt::ToolTipRole:
if (m_attachedForced[udi] || AutomounterSettings::shouldAutomountDevice(udi, AutomounterSettings::Attach)) if (m_attachedForced[udi] || m_settings->shouldAutomountDevice(udi, AutomounterSettings::Attach))
return i18n("This device will be automatically mounted when attached."); return i18n("This device will be automatically mounted when attached.");
return i18n("This device will not be automatically mounted when attached."); return i18n("This device will not be automatically mounted when attached.");
} }
...@@ -305,18 +306,18 @@ QVariant DeviceModel::data(const QModelIndex &index, int role) const ...@@ -305,18 +306,18 @@ QVariant DeviceModel::data(const QModelIndex &index, int role) const
if (index.column() == 0) { if (index.column() == 0) {
switch(role) { switch(role) {
case Qt::DisplayRole: case Qt::DisplayRole:
return AutomounterSettings::getDeviceName(udi); return m_settings->getDeviceName(udi);
case Qt::ToolTipRole: case Qt::ToolTipRole:
return i18n("UDI: %1", udi); return i18n("UDI: %1", udi);
case Qt::DecorationRole: case Qt::DecorationRole:
return QIcon::fromTheme(AutomounterSettings::getDeviceIcon(udi)); return QIcon::fromTheme(m_settings->getDeviceIcon(udi));
} }
} else if (index.column() == 1) { } else if (index.column() == 1) {
switch(role) { switch(role) {
case Qt::CheckStateRole: case Qt::CheckStateRole:
return m_loginForced[udi] ? Qt::Checked : Qt::Unchecked; return m_loginForced[udi] ? Qt::Checked : Qt::Unchecked;
case Qt::ToolTipRole: case Qt::ToolTipRole:
if (m_loginForced[udi] || AutomounterSettings::shouldAutomountDevice(udi, AutomounterSettings::Login)) if (m_loginForced[udi] || m_settings->shouldAutomountDevice(udi, AutomounterSettings::Login))
return i18n("This device will be automatically mounted at login."); return i18n("This device will be automatically mounted at login.");
return i18n("This device will not be automatically mounted at login."); return i18n("This device will not be automatically mounted at login.");
} }
...@@ -325,7 +326,7 @@ QVariant DeviceModel::data(const QModelIndex &index, int role) const ...@@ -325,7 +326,7 @@ QVariant DeviceModel::data(const QModelIndex &index, int role) const
case Qt::CheckStateRole: case Qt::CheckStateRole:
return m_attachedForced[udi] ? Qt::Checked : Qt::Unchecked; return m_attachedForced[udi] ? Qt::Checked : Qt::Unchecked;
case Qt::ToolTipRole: case Qt::ToolTipRole:
if (m_attachedForced[udi] || AutomounterSettings::shouldAutomountDevice(udi, AutomounterSettings::Attach)) if (m_attachedForced[udi] || m_settings->shouldAutomountDevice(udi, AutomounterSettings::Attach))
return i18n("This device will be automatically mounted when attached."); return i18n("This device will be automatically mounted when attached.");
return i18n("This device will not be automatically mounted when attached."); return i18n("This device will not be automatically mounted when attached.");
} }
...@@ -361,7 +362,7 @@ int DeviceModel::rowCount(const QModelIndex &parent) const ...@@ -361,7 +362,7 @@ int DeviceModel::rowCount(const QModelIndex &parent) const
int DeviceModel::columnCount(const QModelIndex &parent) const int DeviceModel::columnCount(const QModelIndex &parent) const
{ {
Q_UNUSED(parent); Q_UNUSED(parent)
return 3; return 3;
} }
......
...@@ -27,12 +27,14 @@ ...@@ -27,12 +27,14 @@
#include <QList> #include <QList>
#include <QHash> #include <QHash>
class AutomounterSettings;
class DeviceModel : public QAbstractItemModel class DeviceModel : public QAbstractItemModel
{ {
Q_OBJECT Q_OBJECT
public: public:
explicit DeviceModel(QObject *parent = nullptr); explicit DeviceModel(AutomounterSettings *m_settings, QObject *parent = nullptr);
~DeviceModel() override = default; ~DeviceModel() override = default;
enum DeviceType { enum DeviceType {
...@@ -77,6 +79,7 @@ private: ...@@ -77,6 +79,7 @@ private:
QHash<QString, bool> m_attachedForced; QHash<QString, bool> m_attachedForced;
bool m_automaticLogin; bool m_automaticLogin;
bool m_automaticAttached; bool m_automaticAttached;
AutomounterSettings *m_settings;
}; };
#endif #endif
...@@ -37,8 +37,9 @@ K_PLUGIN_FACTORY_WITH_JSON(DeviceAutomounterFactory, ...@@ -37,8 +37,9 @@ K_PLUGIN_FACTORY_WITH_JSON(DeviceAutomounterFactory,
DeviceAutomounter::DeviceAutomounter(QObject *parent, const QVariantList &args) DeviceAutomounter::DeviceAutomounter(QObject *parent, const QVariantList &args)
: KDEDModule(parent) : KDEDModule(parent)
, m_settings(new AutomounterSettings(this))
{ {
Q_UNUSED(args); Q_UNUSED(args)
QTimer::singleShot(0, this, &DeviceAutomounter::init); QTimer::singleShot(0, this, &DeviceAutomounter::init);
} }
...@@ -48,7 +49,7 @@ DeviceAutomounter::~DeviceAutomounter() ...@@ -48,7 +49,7 @@ DeviceAutomounter::~DeviceAutomounter()
void DeviceAutomounter::init() void DeviceAutomounter::init()
{ {
if (!AutomounterSettings::automountEnabled()) { if (!m_settings->automountEnabled()) {
// Automounting is disabled, no point in hanging around. // Automounting is disabled, no point in hanging around.
QDBusConnection dbus = QDBusConnection::sessionBus(); QDBusConnection dbus = QDBusConnection::sessionBus();
QDBusMessage msg = QDBusMessage::createMethodCall(QStringLiteral("org.kde.kded5"), QDBusMessage msg = QDBusMessage::createMethodCall(QStringLiteral("org.kde.kded5"),
...@@ -75,15 +76,15 @@ void DeviceAutomounter::init() ...@@ -75,15 +76,15 @@ void DeviceAutomounter::init()
if (const Solid::StorageAccess *sa = volume.as<Solid::StorageAccess>()) { if (const Solid::StorageAccess *sa = volume.as<Solid::StorageAccess>()) {
connect(sa, &Solid::StorageAccess::accessibilityChanged, this, &DeviceAutomounter::deviceMountChanged); connect(sa, &Solid::StorageAccess::accessibilityChanged, this, &DeviceAutomounter::deviceMountChanged);
} }
automountDevice(volume, AutomounterSettings::Login); automountDevice(volume, m_settings->Login);
} }
AutomounterSettings::self()->save(); m_settings->save();
} }
void DeviceAutomounter::deviceMountChanged(bool accessible, const QString &udi) void DeviceAutomounter::deviceMountChanged(bool accessible, const QString &udi)
{ {
AutomounterSettings::setDeviceLastSeenMounted(udi, accessible); m_settings->setDeviceLastSeenMounted(udi, accessible);
AutomounterSettings::self()->save(); m_settings->save();
} }
void DeviceAutomounter::automountDevice(Solid::Device &dev, AutomounterSettings::AutomountType type) void DeviceAutomounter::automountDevice(Solid::Device &dev, AutomounterSettings::AutomountType type)
...@@ -91,10 +92,10 @@ void DeviceAutomounter::automountDevice(Solid::Device &dev, AutomounterSettings: ...@@ -91,10 +92,10 @@ void DeviceAutomounter::automountDevice(Solid::Device &dev, AutomounterSettings:
if (dev.is<Solid::StorageVolume>() && dev.is<Solid::StorageAccess>()) { if (dev.is<Solid::StorageVolume>() && dev.is<Solid::StorageAccess>()) {
Solid::StorageAccess *sa = dev.as<Solid::StorageAccess>(); Solid::StorageAccess *sa = dev.as<Solid::StorageAccess>();
AutomounterSettings::setDeviceLastSeenMounted(dev.udi(), sa->isAccessible()); m_settings->setDeviceLastSeenMounted(dev.udi(), sa->isAccessible());
AutomounterSettings::saveDevice(dev); m_settings->saveDevice(dev);
if (AutomounterSettings::shouldAutomountDevice(dev.udi(), type)) { if (m_settings->shouldAutomountDevice(dev.udi(), type)) {
Solid::StorageVolume *sv = dev.as<Solid::StorageVolume>(); Solid::StorageVolume *sv = dev.as<Solid::StorageVolume>();
if (!sa->isAccessible() && !sv->isIgnored()) { if (!sa->isAccessible() && !sv->isIgnored()) {
sa->setup(); sa->setup();
...@@ -105,11 +106,11 @@ void DeviceAutomounter::automountDevice(Solid::Device &dev, AutomounterSettings: ...@@ -105,11 +106,11 @@ void DeviceAutomounter::automountDevice(Solid::Device &dev, AutomounterSettings:
void DeviceAutomounter::deviceAdded(const QString &udi) void DeviceAutomounter::deviceAdded(const QString &udi)
{ {
AutomounterSettings::self()->load(); m_settings->load();
Solid::Device dev(udi); Solid::Device dev(udi);
automountDevice(dev, AutomounterSettings::Attach); automountDevice(dev, AutomounterSettings::Attach);
AutomounterSettings::self()->save(); m_settings->save();
if (dev.is<Solid::StorageAccess>()) { if (dev.is<Solid::StorageAccess>()) {
Solid::StorageAccess *sa = dev.as<Solid::StorageAccess>(); Solid::StorageAccess *sa = dev.as<Solid::StorageAccess>();
......
...@@ -38,6 +38,7 @@ private slots: ...@@ -38,6 +38,7 @@ private slots:
private: private:
void automountDevice(Solid::Device &dev, AutomounterSettings::AutomountType type); void automountDevice(Solid::Device &dev, AutomounterSettings::AutomountType type);
AutomounterSettings *m_settings;
}; };
#endif #endif
...@@ -22,17 +22,17 @@ ...@@ -22,17 +22,17 @@
KConfigGroup AutomounterSettings::deviceSettings(const QString &udi) KConfigGroup AutomounterSettings::deviceSettings(const QString &udi)
{ {
return self()->config()->group("Devices").group(udi); return config()->group("Devices").group(udi);
} }
QStringList AutomounterSettings::knownDevices() QStringList AutomounterSettings::knownDevices()
{ {
return self()->config()->group("Devices").groupList(); return config()->group("Devices").groupList();
} }
bool AutomounterSettings::deviceIsKnown(const QString &udi) bool AutomounterSettings::deviceIsKnown(const QString &udi)
{ {
return self()->config()->group("Devices").group(udi).readEntry("EverMounted", false); return config()->group("Devices").group(udi).readEntry("EverMounted", false);
} }
bool AutomounterSettings::deviceAutomountIsForced(const QString &udi, AutomountType type) bool AutomounterSettings::deviceAutomountIsForced(const QString &udi, AutomountType type)
......
...@@ -28,20 +28,21 @@ ...@@ -28,20 +28,21 @@
class AutomounterSettings : public AutomounterSettingsBase { class AutomounterSettings : public AutomounterSettingsBase {
public: public:
using AutomounterSettingsBase::AutomounterSettingsBase;
enum AutomountType { enum AutomountType {
Login, Login,
Attach Attach
}; };
static KConfigGroup deviceSettings(const QString &udi); KConfigGroup deviceSettings(const QString &udi);
static QStringList knownDevices(); QStringList knownDevices();
static bool deviceIsKnown(const QString &udi); bool deviceIsKnown(const QString &udi);
static bool shouldAutomountDevice(const QString &udi, AutomountType type); bool shouldAutomountDevice(const QString &udi, AutomountType type);
static void setDeviceLastSeenMounted(const QString &udi, bool mounted); void setDeviceLastSeenMounted(const QString &udi, bool mounted);
static bool deviceAutomountIsForced(const QString &udi, AutomountType type); bool deviceAutomountIsForced(const QString &udi, AutomountType type);
static QString getDeviceName(const QString &udi); QString getDeviceName(const QString &udi);
static bool getDeviceForcedAutomount(const QString &udi); bool getDeviceForcedAutomount(const QString &udi);
static QString getDeviceIcon(const QString &udi); QString getDeviceIcon(const QString &udi);
static void saveDevice(const Solid::Device &dev); void saveDevice(const Solid::Device &dev);
}; };
#endif #endif
ClassName=AutomounterSettingsBase ClassName=AutomounterSettingsBase
File=AutomounterSettingsBase.kcfg File=AutomounterSettingsBase.kcfg
Mutators=true Mutators=true
Singleton=true ParentInConstructor=true
\ No newline at end of file
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