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