solid-device-automounter: format and indent

Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
This commit is contained in:
Ivailo Monev 2023-07-29 09:46:56 +03:00
parent 449c5a5b39
commit 26847c1b3d
4 changed files with 223 additions and 186 deletions

View file

@ -19,19 +19,17 @@
#include "DeviceAutomounterKCM.h"
#include <QtGui/qstandarditemmodel.h>
#include <QStandardItemModel>
#include <QItemSelectionModel>
#include <KAboutData>
#include <KConfigGroup>
#include <KInputDialog>
#include <Solid/DeviceNotifier>
#include <Solid/StorageVolume>
#include <KPluginFactory>
#include <KGlobal>
#include <KLocale>
#include <Solid/DeviceNotifier>
#include <Solid/StorageVolume>
#include "AutomounterSettings.h"
#include "LayoutSettings.h"
@ -43,13 +41,15 @@ K_EXPORT_PLUGIN(DeviceAutomounterKCMFactory("kcm_device_automounter"))
DeviceAutomounterKCM::DeviceAutomounterKCM(QWidget *parent, const QVariantList &)
: KCModule(DeviceAutomounterKCMFactory::componentData(), parent)
{
KAboutData *about = new KAboutData("kcm_device_automounter",
0,
ki18n("Device Automounter"),
"0.1",
ki18n("Automatically mounts devices at login or when attached"),
KAboutData::License_GPL_V2,
ki18n("(c) 2009 Trever Fischer"));
KAboutData *about = new KAboutData(
"kcm_device_automounter",
0,
ki18n("Device Automounter"),
"0.1",
ki18n("Automatically mounts devices at login or when attached"),
KAboutData::License_GPL_V2,
ki18n("(c) 2009 Trever Fischer")
);
about->addAuthor(ki18n("Trever Fischer"));
setAboutData(about);
@ -66,47 +66,48 @@ DeviceAutomounterKCM::DeviceAutomounterKCM(QWidget *parent, const QVariantList &
connect(automountEnabled, SIGNAL(stateChanged(int)), this, SLOT(enabledChanged()));
connect(deviceView->selectionModel(), SIGNAL(selectionChanged(const QItemSelection, const QItemSelection)), this, SLOT(updateForgetDeviceButton()));
connect(
deviceView->selectionModel(), SIGNAL(selectionChanged(const QItemSelection, const QItemSelection)),
this, SLOT(updateForgetDeviceButton())
);
connect(forgetDevice, SIGNAL(clicked(bool)), this, SLOT(forgetSelectedDevices()));
forgetDevice->setEnabled(false);
}
void
DeviceAutomounterKCM::updateForgetDeviceButton()
void DeviceAutomounterKCM::updateForgetDeviceButton()
{
foreach(QModelIndex idx, deviceView->selectionModel()->selectedIndexes()) {
if (idx.data(DeviceModel::TypeRole) == DeviceModel::Detatched) {
forgetDevice->setEnabled(true);
return;
}
}
forgetDevice->setEnabled(false);
foreach(QModelIndex idx, deviceView->selectionModel()->selectedIndexes()) {
if (idx.data(DeviceModel::TypeRole) == DeviceModel::Detatched) {
forgetDevice->setEnabled(true);
return;
}
}
forgetDevice->setEnabled(false);
}
void
DeviceAutomounterKCM::forgetSelectedDevices()
{
QItemSelectionModel* selected = deviceView->selectionModel();
int offset = 0;
int offset = 0;
while(selected->selectedIndexes().size()>0 && selected->selectedIndexes().size() > offset) {
if (selected->selectedIndexes()[offset].data(DeviceModel::TypeRole) == DeviceModel::Attached)
offset++;
else
m_devices->forgetDevice(selected->selectedIndexes()[offset].data(DeviceModel::UdiRole).toString());
if (selected->selectedIndexes()[offset].data(DeviceModel::TypeRole) == DeviceModel::Attached) {
offset++;
} else {
m_devices->forgetDevice(selected->selectedIndexes()[offset].data(DeviceModel::UdiRole).toString());
}
}
changed();
}
void
DeviceAutomounterKCM::emitChanged()
void DeviceAutomounterKCM::emitChanged()
{
changed();
}
void
DeviceAutomounterKCM::enabledChanged()
void DeviceAutomounterKCM::enabledChanged()
{
if (automountEnabled->checkState() == Qt::Unchecked) {
automountOnLogin->setEnabled(false);
@ -121,57 +122,63 @@ DeviceAutomounterKCM::enabledChanged()
}
}
void
DeviceAutomounterKCM::load()
void DeviceAutomounterKCM::load()
{
if (AutomounterSettings::automountEnabled())
if (AutomounterSettings::automountEnabled()) {
automountEnabled->setCheckState(Qt::Checked);
else
} else {
automountEnabled->setCheckState(Qt::Unchecked);
}
if (AutomounterSettings::automountUnknownDevices())
if (AutomounterSettings::automountUnknownDevices()) {
automountUnknownDevices->setCheckState(Qt::Checked);
else
} else {
automountUnknownDevices->setCheckState(Qt::Unchecked);
}
if (AutomounterSettings::automountOnLogin())
if (AutomounterSettings::automountOnLogin()) {
automountOnLogin->setCheckState(Qt::Checked);
else
} else {
automountOnLogin->setCheckState(Qt::Unchecked);
}
if (AutomounterSettings::automountOnPlugin())
if (AutomounterSettings::automountOnPlugin()) {
automountOnPlugin->setCheckState(Qt::Checked);
else
} else {
automountOnPlugin->setCheckState(Qt::Unchecked);
}
m_devices->reload();
enabledChanged();
loadLayout();
}
void
DeviceAutomounterKCM::save()
void DeviceAutomounterKCM::save()
{
saveLayout();
if (this->automountEnabled->checkState() == Qt::Checked)
if (this->automountEnabled->checkState() == Qt::Checked) {
AutomounterSettings::setAutomountEnabled(true);
else
} else {
AutomounterSettings::setAutomountEnabled(false);
}
if (this->automountUnknownDevices->checkState() == Qt::Checked)
if (this->automountUnknownDevices->checkState() == Qt::Checked) {
AutomounterSettings::setAutomountUnknownDevices(true);
else
} else {
AutomounterSettings::setAutomountUnknownDevices(false);
}
if (this->automountOnLogin->checkState() == Qt::Checked)
if (this->automountOnLogin->checkState() == Qt::Checked) {
AutomounterSettings::setAutomountOnLogin(true);
else
} else {
AutomounterSettings::setAutomountOnLogin(false);
}
if (this->automountOnPlugin->checkState() == Qt::Checked)
if (this->automountOnPlugin->checkState() == Qt::Checked) {
AutomounterSettings::setAutomountOnPlugin(true);
else
} else {
AutomounterSettings::setAutomountOnPlugin(false);
}
QStringList validDevices;
for(int i = 0;i < m_devices->rowCount();i++) {
@ -180,21 +187,24 @@ DeviceAutomounterKCM::save()
QModelIndex dev = m_devices->index(j, 1, idx);
QString device = dev.data(DeviceModel::UdiRole).toString();
validDevices << device;
if (dev.data(Qt::CheckStateRole).toInt() == Qt::Checked)
if (dev.data(Qt::CheckStateRole).toInt() == Qt::Checked) {
AutomounterSettings::deviceSettings(device).writeEntry("ForceLoginAutomount", true);
else
} else {
AutomounterSettings::deviceSettings(device).writeEntry("ForceLoginAutomount", false);
}
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);
else
} else {
AutomounterSettings::deviceSettings(device).writeEntry("ForceAttachAutomount", false);
}
}
}
foreach(const QString &possibleDevice, AutomounterSettings::knownDevices()) {
if (!validDevices.contains(possibleDevice))
if (!validDevices.contains(possibleDevice)) {
AutomounterSettings::deviceSettings(possibleDevice).deleteGroup();
}
}
AutomounterSettings::self()->writeConfig();
@ -205,13 +215,13 @@ DeviceAutomounterKCM::~DeviceAutomounterKCM()
saveLayout();
}
void
DeviceAutomounterKCM::saveLayout()
void DeviceAutomounterKCM::saveLayout()
{
QList<int> widths;
const int nbColumn = m_devices->columnCount();
for(int i = 0;i<nbColumn;++i)
for (int i = 0; i < nbColumn; ++i) {
widths << deviceView->columnWidth(i);
}
LayoutSettings::setHeaderWidths(widths);
//Check DeviceModel.cpp, thats where the magic row numbers come from.
LayoutSettings::setAttachedExpanded(deviceView->isExpanded(m_devices->index(0,0)));
@ -219,21 +229,19 @@ DeviceAutomounterKCM::saveLayout()
LayoutSettings::self()->writeConfig();
}
void
DeviceAutomounterKCM::loadLayout()
void DeviceAutomounterKCM::loadLayout()
{
LayoutSettings::self()->readConfig();
//Reset it first, just in case there isn't any layout saved for a particular column.
int nbColumn = m_devices->columnCount();
for(int i = 0;i<nbColumn;++i)
for (int i = 0; i < nbColumn; ++i) {
deviceView->resizeColumnToContents(i);
}
QList<int> widths = LayoutSettings::headerWidths();
nbColumn = m_devices->columnCount();
for(int i = 0;i<nbColumn && i<widths.size();i++) {
deviceView->setColumnWidth(i, widths[i]);
}
deviceView->setExpanded(m_devices->index(0,0), LayoutSettings::attachedExpanded());
deviceView->setExpanded(m_devices->index(1,0), LayoutSettings::detatchedExpanded());
}

View file

@ -21,30 +21,34 @@
#define DEVICEAUTOMOUNTERKCM_H
#include <KCModule>
#include <QStandardItemModel>
#include <QStandardItem>
#include "ui_DeviceAutomounterKCM.h"
#include <QStandardItemModel>
#include <QStandardItem>
class DeviceModel;
class DeviceAutomounterKCM : public KCModule, public Ui::DeviceAutomounterKCM {
class DeviceAutomounterKCM : public KCModule, public Ui::DeviceAutomounterKCM
{
Q_OBJECT
public:
explicit DeviceAutomounterKCM(QWidget *parent = 0, const QVariantList &args = QVariantList());
virtual ~DeviceAutomounterKCM();
public slots:
void load();
void save();
private slots:
void emitChanged();
void enabledChanged();
void updateForgetDeviceButton();
void forgetSelectedDevices();
private:
DeviceModel* m_devices;
void saveLayout();
void loadLayout();
public:
explicit DeviceAutomounterKCM(QWidget *parent = 0, const QVariantList &args = QVariantList());
virtual ~DeviceAutomounterKCM();
public slots:
void load();
void save();
private slots:
void emitChanged();
void enabledChanged();
void updateForgetDeviceButton();
void forgetSelectedDevices();
private:
DeviceModel* m_devices;
void saveLayout();
void loadLayout();
};
#endif

View file

@ -1,31 +1,34 @@
#include "DeviceModel.h"
#include <KLocalizedString>
#include <KIcon>
#include <KDebug>
#include <Solid/DeviceNotifier>
#include <Solid/Device>
#include <Solid/StorageVolume>
#include <KIcon>
#include "AutomounterSettings.h"
#include <KDebug>
DeviceModel::DeviceModel(QObject *parent)
: QAbstractItemModel(parent)
{
reload();
connect(Solid::DeviceNotifier::instance(), SIGNAL(deviceAdded(const QString)), this, SLOT(deviceAttached(const QString)));
connect(Solid::DeviceNotifier::instance(), SIGNAL(deviceRemoved(const QString)), this, SLOT(deviceRemoved(const QString)));
connect(
Solid::DeviceNotifier::instance(), SIGNAL(deviceAdded(const QString)),
this, SLOT(deviceAttached(const QString))
);
connect(
Solid::DeviceNotifier::instance(), SIGNAL(deviceRemoved(const QString)),
this, SLOT(deviceRemoved(const QString))
);
}
DeviceModel::~DeviceModel()
{
}
void
DeviceModel::forgetDevice(const QString &udi)
void DeviceModel::forgetDevice(const QString &udi)
{
if (m_disconnected.contains(udi)) {
beginRemoveRows(index(1, 0), m_disconnected.indexOf(udi), m_disconnected.indexOf(udi));
@ -40,8 +43,7 @@ DeviceModel::forgetDevice(const QString &udi)
m_attachedForced.remove(udi);
}
QVariant
DeviceModel::headerData(int section, Qt::Orientation orientation, int role) const
QVariant DeviceModel::headerData(int section, Qt::Orientation orientation, int role) const
{
if (orientation == Qt::Horizontal && role == Qt::DisplayRole) {
switch(section) {
@ -56,8 +58,7 @@ DeviceModel::headerData(int section, Qt::Orientation orientation, int role) cons
return QVariant();
}
void
DeviceModel::deviceAttached(const QString &udi)
void DeviceModel::deviceAttached(const QString &udi)
{
Solid::Device dev(udi);
if (dev.is<Solid::StorageVolume>()) {
@ -72,8 +73,7 @@ DeviceModel::deviceAttached(const QString &udi)
}
}
void
DeviceModel::deviceRemoved(const QString &udi)
void DeviceModel::deviceRemoved(const QString &udi)
{
if (m_attached.contains(udi)) {
emit layoutAboutToBeChanged();
@ -85,14 +85,15 @@ DeviceModel::deviceRemoved(const QString &udi)
}
}
void
DeviceModel::addNewDevice(const QString &udi)
void DeviceModel::addNewDevice(const QString &udi)
{
AutomounterSettings::self()->readConfig();
if (!m_loginForced.contains(udi))
if (!m_loginForced.contains(udi)) {
m_loginForced[udi] = AutomounterSettings::deviceAutomountIsForced(udi, AutomounterSettings::Login);
if (!m_attachedForced.contains(udi))
}
if (!m_attachedForced.contains(udi)) {
m_loginForced[udi] = AutomounterSettings::deviceAutomountIsForced(udi, AutomounterSettings::Attach);
}
emit layoutAboutToBeChanged();
Solid::Device dev(udi);
if (dev.isValid()) {
@ -108,8 +109,7 @@ DeviceModel::addNewDevice(const QString &udi)
emit layoutChanged();
}
void
DeviceModel::reload()
void DeviceModel::reload()
{
beginResetModel();
m_loginForced.clear();
@ -126,39 +126,39 @@ DeviceModel::reload()
endResetModel();
}
QModelIndex
DeviceModel::index(int row, int column, const QModelIndex &parent) const
QModelIndex DeviceModel::index(int row, int column, const QModelIndex &parent) const
{
if (parent.isValid()) {
if (parent.column() > 0)
return QModelIndex();
if (parent.row() == 0) {
if (row >= 0 && row < m_attached.size() && column >= 0 && column <= 2)
if (row >= 0 && row < m_attached.size() && column >= 0 && column <= 2) {
return createIndex(row, column, 0);
}
} else if (parent.row() == 1) {
if (row >= 0 && row < m_disconnected.size() && column >= 0 && column <= 2)
if (row >= 0 && row < m_disconnected.size() && column >= 0 && column <= 2) {
return createIndex(row, column, 1);
}
}
} else {
if ((row == 0 || row == 1) && column >= 0 && column <= 2)
return createIndex(row, column, 3);
}
if ((row == 0 || row == 1) && column >= 0 && column <= 2) {
return createIndex(row, column, 3);
}
return QModelIndex();
}
QModelIndex
DeviceModel::parent(const QModelIndex &index) const
QModelIndex DeviceModel::parent(const QModelIndex &index) const
{
if (index.isValid()) {
if (index.internalId() == 3)
if (index.internalId() == 3) {
return QModelIndex();
}
return createIndex(index.internalId(), 0, 3);
}
return QModelIndex();
}
Qt::ItemFlags
DeviceModel::flags(const QModelIndex &index) const
Qt::ItemFlags DeviceModel::flags(const QModelIndex &index) const
{
if (index.isValid()) {
if (index.parent().isValid()) {
@ -167,9 +167,8 @@ DeviceModel::flags(const QModelIndex &index) const
} else if (index.column() == 0) {
return Qt::ItemIsSelectable | Qt::ItemIsEnabled;
}
} else {
return Qt::ItemIsEnabled;
}
return Qt::ItemIsEnabled;
}
return Qt::NoItemFlags;
}
@ -198,102 +197,124 @@ DeviceModel::data(const QModelIndex &index, int role) const
{
if (index.isValid() && index.parent().isValid()) {
if (index.parent().row() == 0) {
if (role == TypeRole)
return Attached;
if (role == TypeRole) {
return Attached;
}
QString udi = m_attached[index.row()];
Solid::Device dev(udi);
if (role == Qt::UserRole)
if (role == Qt::UserRole) {
return udi;
}
if (index.column() == 0) {
switch(role) {
case Qt::DisplayRole:
case Qt::DisplayRole: {
return dev.description();
case Qt::ToolTipRole:
}
case Qt::ToolTipRole: {
return i18n("UDI: %1", udi);
case Qt::DecorationRole:
}
case Qt::DecorationRole: {
return KIcon(dev.icon());
}
}
} else if (index.column() == 1) {
switch(role) {
case Qt::CheckStateRole:
case Qt::CheckStateRole: {
return m_loginForced[udi] ? Qt::Checked : Qt::Unchecked;
case Qt::ToolTipRole:
if (m_loginForced[udi] || AutomounterSettings::shouldAutomountDevice(udi, AutomounterSettings::Login))
}
case Qt::ToolTipRole: {
if (m_loginForced[udi] || AutomounterSettings::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.");
}
}
} else if (index.column() == 2) {
switch(role) {
case Qt::CheckStateRole:
case Qt::CheckStateRole: {
return m_attachedForced[udi] ? Qt::Checked : Qt::Unchecked;
case Qt::ToolTipRole:
if (m_attachedForced[udi] || AutomounterSettings::shouldAutomountDevice(udi, AutomounterSettings::Attach))
}
case Qt::ToolTipRole: {
if (m_attachedForced[udi] || AutomounterSettings::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.");
}
}
}
} else if (index.parent().row() == 1) {
if (role == TypeRole)
return Detatched;
if (role == TypeRole) {
return Detatched;
}
QString udi = m_disconnected[index.row()];
if (role == Qt::UserRole)
if (role == Qt::UserRole) {
return udi;
}
if (index.column() == 0) {
switch(role) {
case Qt::DisplayRole:
case Qt::DisplayRole: {
return AutomounterSettings::getDeviceName(udi);
case Qt::ToolTipRole:
}
case Qt::ToolTipRole: {
return i18n("UDI: %1", udi);
case Qt::DecorationRole:
}
case Qt::DecorationRole: {
return KIcon(AutomounterSettings::getDeviceIcon(udi));
}
}
} else if (index.column() == 1) {
switch(role) {
case Qt::CheckStateRole:
case Qt::CheckStateRole: {
return m_loginForced[udi] ? Qt::Checked : Qt::Unchecked;
case Qt::ToolTipRole:
if (m_loginForced[udi] || AutomounterSettings::shouldAutomountDevice(udi, AutomounterSettings::Login))
}
case Qt::ToolTipRole: {
if (m_loginForced[udi] || AutomounterSettings::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.");
}
}
} else if (index.column() == 2) {
switch(role) {
case Qt::CheckStateRole:
case Qt::CheckStateRole: {
return m_attachedForced[udi] ? Qt::Checked : Qt::Unchecked;
case Qt::ToolTipRole:
if (m_attachedForced[udi] || AutomounterSettings::shouldAutomountDevice(udi, AutomounterSettings::Attach))
}
case Qt::ToolTipRole: {
if (m_attachedForced[udi] || AutomounterSettings::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.");
}
}
}
}
} else if (index.isValid()) {
if (role == Qt::DisplayRole && index.column() == 0) {
if (index.row() == 0)
if (index.row() == 0) {
return i18n("Attached Devices");
else if (index.row() == 1)
} else if (index.row() == 1) {
return i18n("Disconnected Devices");
}
}
}
return QVariant();
}
int
DeviceModel::rowCount(const QModelIndex &parent) const
int DeviceModel::rowCount(const QModelIndex &parent) const
{
if (parent.isValid()) {
if (parent.internalId() < 3 || parent.column() > 0)
if (parent.internalId() < 3 || parent.column() > 0) {
return 0;
if (parent.row() == 0)
}
if (parent.row() == 0) {
return m_attached.size();
}
return m_disconnected.size();
} else {
return 2;
}
return 2;
}
int
DeviceModel::columnCount(const QModelIndex &parent) const
int DeviceModel::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return 3;

View file

@ -1,47 +1,51 @@
#ifndef DEVICEMODEL_H
#define DEVICEMODEL_H
#include <QtCore/QAbstractItemModel>
#include <QtCore/qabstractitemmodel.h>
#include <QtCore/QVariant>
#include <QtCore/QList>
#include <QtCore/QHash>
#include <QAbstractItemModel>
#include <QVariant>
#include <QList>
#include <QHash>
class DeviceModel : public QAbstractItemModel {
class DeviceModel : public QAbstractItemModel
{
Q_OBJECT
public:
enum DeviceType {
Attached,
Detatched
};
public:
enum DeviceType {
Attached,
Detatched
};
enum {
UdiRole = Qt::UserRole,
TypeRole
};
enum {
UdiRole = Qt::UserRole,
TypeRole
};
DeviceModel(QObject* parent = 0);
~DeviceModel();
Qt::ItemFlags flags(const QModelIndex &index) const;
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
int rowCount(const QModelIndex &parent = QModelIndex()) const;
int columnCount(const QModelIndex &parent = QModelIndex()) const;
QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const;
QModelIndex parent(const QModelIndex &index) const;
bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole);
public slots:
void forgetDevice(const QString &udi);
void reload();
private slots:
void deviceAttached(const QString &udi);
void deviceRemoved(const QString &udi);
private:
void addNewDevice(const QString &udi);
QList<QString> m_attached;
QList<QString> m_disconnected;
QHash<QString, bool> m_loginForced;
QHash<QString, bool> m_attachedForced;
DeviceModel(QObject* parent = 0);
~DeviceModel();
Qt::ItemFlags flags(const QModelIndex &index) const;
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const;
int rowCount(const QModelIndex &parent = QModelIndex()) const;
int columnCount(const QModelIndex &parent = QModelIndex()) const;
QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const;
QModelIndex parent(const QModelIndex &index) const;
bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole);
public slots:
void forgetDevice(const QString &udi);
void reload();
private slots:
void deviceAttached(const QString &udi);
void deviceRemoved(const QString &udi);
private:
void addNewDevice(const QString &udi);
QList<QString> m_attached;
QList<QString> m_disconnected;
QHash<QString, bool> m_loginForced;
QHash<QString, bool> m_attachedForced;
};
#endif