diff --git a/solid-device-automounter/kcm/DeviceAutomounterKCM.cpp b/solid-device-automounter/kcm/DeviceAutomounterKCM.cpp index 2dc3e300b..cdd5a977a 100644 --- a/solid-device-automounter/kcm/DeviceAutomounterKCM.cpp +++ b/solid-device-automounter/kcm/DeviceAutomounterKCM.cpp @@ -1,221 +1,222 @@ /************************************************************************** * Copyright (C) 2009-2010 Trever Fischer * * Copyright (C) 2015 Kai UWe Broulik * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . * ***************************************************************************/ #include "DeviceAutomounterKCM.h" #include #include #include #include #include #include #include #include #include #include #include #include "AutomounterSettings.h" #include "LayoutSettings.h" #include "DeviceModel.h" K_PLUGIN_FACTORY(DeviceAutomounterKCMFactory, registerPlugin();) 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"), QStringLiteral("2.0"), QString(), KAboutLicense::GPL_V2, i18n("(c) 2009 Trever Fischer, (c) 2015 Kai Uwe Broulik")); about->addAuthor(i18n("Trever Fischer"), i18n("Original Author")); about->addAuthor(i18n("Kai Uwe Broulik"), i18n("Plasma 5 Port"), QStringLiteral("kde@privat.broulik.de")); setAboutData(about); setupUi(this); - addConfig(AutomounterSettings::self(), this); + addConfig(m_settings, this); deviceView->setModel(m_devices); deviceView->header()->setSectionResizeMode(QHeaderView::ResizeToContents); deviceView->header()->setSectionResizeMode(0, QHeaderView::Stretch); auto emitChanged = [this] { m_devices->setAutomaticMountOnLogin(kcfg_AutomountOnLogin->isChecked()); m_devices->setAutomaticMountOnPlugin(kcfg_AutomountOnPlugin->isChecked()); emit markAsChanged(); }; connect(m_devices, &DeviceModel::dataChanged, this, emitChanged); connect(deviceView->selectionModel(), &QItemSelectionModel::selectionChanged, this, &DeviceAutomounterKCM::updateForgetDeviceButton); connect(forgetDevice, &QAbstractButton::clicked, this, &DeviceAutomounterKCM::forgetSelectedDevices); forgetDevice->setEnabled(false); } DeviceAutomounterKCM::~DeviceAutomounterKCM() { saveLayout(); } void DeviceAutomounterKCM::updateForgetDeviceButton() { const auto selectedIndex = deviceView->selectionModel()->selectedIndexes(); for (const QModelIndex &idx : selectedIndex) { if (idx.data(DeviceModel::TypeRole) == DeviceModel::Detatched) { forgetDevice->setEnabled(true); return; } } forgetDevice->setEnabled(false); } void DeviceAutomounterKCM::forgetSelectedDevices() { QItemSelectionModel *selected = deviceView->selectionModel(); int offset = 0; while (!selected->selectedIndexes().isEmpty() && 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()); } } emit markAsChanged(); } 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(); } void DeviceAutomounterKCM::save() { KCModule::save(); saveLayout(); const bool enabled = kcfg_AutomountEnabled->isChecked(); QStringList validDevices; for (int i = 0; i < m_devices->rowCount(); ++i) { const QModelIndex &idx = m_devices->index(i, 0); for (int j = 0; j < m_devices->rowCount(idx); ++j) { QModelIndex dev = m_devices->index(j, 1, idx); const QString device = dev.data(DeviceModel::UdiRole).toString(); 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(); QDBusMessage msg = QDBusMessage::createMethodCall(QStringLiteral("org.kde.kded5"), QStringLiteral("/kded"), QStringLiteral("org.kde.kded5"), QStringLiteral("setModuleAutoloading")); msg.setArguments({QVariant(QStringLiteral("device_automounter")), QVariant(enabled)}); dbus.call(msg, QDBus::NoBlock); // Load or unload right away msg = QDBusMessage::createMethodCall(QStringLiteral("org.kde.kded5"), QStringLiteral("/kded"), QStringLiteral("org.kde.kded5"), enabled ? QStringLiteral("loadModule") : QStringLiteral("unloadModule")); msg.setArguments({QVariant(QStringLiteral("device_automounter"))}); dbus.call(msg, QDBus::NoBlock); } void DeviceAutomounterKCM::saveLayout() { QList widths; const int nbColumn = m_devices->columnCount(); widths.reserve(nbColumn); 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))); LayoutSettings::setDetatchedExpanded(deviceView->isExpanded(m_devices->index(1,0))); LayoutSettings::self()->save(); } void DeviceAutomounterKCM::loadLayout() { LayoutSettings::self()->load(); //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) { deviceView->resizeColumnToContents(i); } QList 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()); } #include "DeviceAutomounterKCM.moc" diff --git a/solid-device-automounter/kcm/DeviceAutomounterKCM.h b/solid-device-automounter/kcm/DeviceAutomounterKCM.h index c2eba31ec..edeb28064 100644 --- a/solid-device-automounter/kcm/DeviceAutomounterKCM.h +++ b/solid-device-automounter/kcm/DeviceAutomounterKCM.h @@ -1,54 +1,55 @@ /************************************************************************** * Copyright (C) 2009-2010 Trever Fischer * * Copyright (C) 2015 Kai Uwe Broulik * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . * ***************************************************************************/ #ifndef DEVICEAUTOMOUNTERKCM_H #define DEVICEAUTOMOUNTERKCM_H #include #include "ui_DeviceAutomounterKCM.h" class DeviceModel; class AutomounterSettings; class DeviceAutomounterKCM : public KCModule, public Ui::DeviceAutomounterKCM { Q_OBJECT public: explicit DeviceAutomounterKCM(QWidget *parent = nullptr, const QVariantList &args = QVariantList()); ~DeviceAutomounterKCM() override; public slots: void load() override; void save() override; private slots: void updateForgetDeviceButton(); void forgetSelectedDevices(); private: void saveLayout(); void loadLayout(); + AutomounterSettings *m_settings; DeviceModel *m_devices; }; #endif diff --git a/solid-device-automounter/kcm/DeviceModel.cpp b/solid-device-automounter/kcm/DeviceModel.cpp index be6f3348a..834a9d08c 100644 --- a/solid-device-automounter/kcm/DeviceModel.cpp +++ b/solid-device-automounter/kcm/DeviceModel.cpp @@ -1,388 +1,389 @@ /************************************************************************** * Copyright (C) 2009-2010 Trever Fischer * * Copyright (C) 2015 Kai Uwe Broulik * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . * ***************************************************************************/ #include "DeviceModel.h" #include #include #include #include #include #include #include "AutomounterSettings.h" -DeviceModel::DeviceModel(QObject *parent) +DeviceModel::DeviceModel(AutomounterSettings *m_settings, QObject *parent) : QAbstractItemModel(parent) + , m_settings(m_settings) { reload(); connect(Solid::DeviceNotifier::instance(), &Solid::DeviceNotifier::deviceAdded, this, &DeviceModel::deviceAttached); connect(Solid::DeviceNotifier::instance(), &Solid::DeviceNotifier::deviceRemoved, this, &DeviceModel::deviceRemoved); } void DeviceModel::forgetDevice(const QString &udi) { if (m_disconnected.contains(udi)) { const int deviceIndex = m_disconnected.indexOf(udi); beginRemoveRows(index(1, 0), deviceIndex, deviceIndex); m_disconnected.removeOne(udi); endRemoveRows(); } else if (m_attached.contains(udi)) { const int deviceIndex = m_attached.indexOf(udi); beginRemoveRows(index(0, 0), deviceIndex, deviceIndex); m_attached.removeOne(udi); endRemoveRows(); } m_loginForced.remove(udi); m_attachedForced.remove(udi); } QVariant DeviceModel::headerData(int section, Qt::Orientation orientation, int role) const { if (orientation == Qt::Horizontal && role == Qt::DisplayRole) { switch(section) { case 0: return i18n("Device"); case 1: return i18nc("As in automount on login", "On Login"); case 2: return i18nc("As in automoount on attach", "On Attach"); } } return QVariant(); } void DeviceModel::deviceAttached(const QString &udi) { const Solid::Device devive(udi); auto volume = devive.as(); if (volume && !volume->isIgnored()) { if (m_disconnected.contains(udi)) { const int deviceIndex = m_disconnected.indexOf(udi); beginRemoveRows(index(1, 0), deviceIndex, deviceIndex); m_disconnected.removeOne(udi); endRemoveRows(); } addNewDevice(udi); } } void DeviceModel::deviceRemoved(const QString &udi) { if (m_attached.contains(udi)) { const int deviceIndex = m_attached.indexOf(udi); beginRemoveRows(index(0, 0), deviceIndex, deviceIndex); m_attached.removeOne(udi); endRemoveRows(); // NOTE the device is not moved to the "Disconnected" section // when removing it while the KCM is opened because we need to check // whether the device that just got detached is ignored // (don't show partition tables and other garbage) but this information // is no longer available when the device is gone } } 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); if (dev.isValid()) { if (dev.is()) { const Solid::StorageAccess *access = dev.as(); if (!access->isIgnored() || !access->isAccessible()) { beginInsertRows(index(0, 0), m_attached.size(), m_attached.size()); m_attached << udi; endInsertRows(); } } } else { beginInsertRows(index(1, 0), m_disconnected.size(), m_disconnected.size()); m_disconnected << udi; endInsertRows(); } } void DeviceModel::reload() { beginResetModel(); m_loginForced.clear(); m_attachedForced.clear(); 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(); } 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) { return createIndex(row, column, static_cast(0)); } } else if (parent.row() == 1) { 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); } } return QModelIndex(); } QModelIndex DeviceModel::parent(const QModelIndex &index) const { if (index.isValid()) { if (index.internalId() == 3) return QModelIndex(); return createIndex(index.internalId(), 0, 3); } return QModelIndex(); } Qt::ItemFlags DeviceModel::flags(const QModelIndex &index) const { if (!index.isValid()) { return Qt::NoItemFlags; } if (!index.parent().isValid()) { // first child section elements if (m_automaticLogin && m_automaticAttached) { return Qt::NoItemFlags; } else { return Qt::ItemIsEnabled; } } switch (index.column()) { case 0: // first column if (m_automaticLogin && m_automaticAttached) { return Qt::NoItemFlags; } else { return Qt::ItemIsEnabled; } case 1: // on login column if (m_automaticLogin) { // automount on login was checked return Qt::ItemIsSelectable | Qt::ItemIsUserCheckable; } return Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsUserCheckable; case 2: // on attached column if (m_automaticAttached) { // automount on attach was checked return Qt::ItemIsSelectable | Qt::ItemIsUserCheckable; } return Qt::ItemIsSelectable | Qt::ItemIsEnabled | Qt::ItemIsUserCheckable; default: Q_UNREACHABLE(); } } bool DeviceModel::setData(const QModelIndex &index, const QVariant &value, int role) { if (index.isValid() && role == Qt::CheckStateRole && index.column() > 0) { const QString &udi = index.data(Qt::UserRole).toString(); switch(index.column()) { case 1: m_loginForced[udi] = (value.toInt() == Qt::Checked) ? true : false; break; case 2: m_attachedForced[udi] = (value.toInt() == Qt::Checked) ? true : false; break; } emit dataChanged(index, index); return true; } return false; } QVariant DeviceModel::data(const QModelIndex &index, int role) const { if (index.isValid() && index.parent().isValid()) { if (index.parent().row() == 0) { if (role == TypeRole) { return Attached; } const QString &udi = m_attached.at(index.row()); Solid::Device dev(udi); if (role == Qt::UserRole) { return udi; } if (index.column() == 0) { switch(role) { case Qt::DisplayRole: return dev.description(); case Qt::ToolTipRole: return i18n("UDI: %1", udi); case Qt::DecorationRole: return QIcon::fromTheme(dev.icon()); } } 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."); } } else if (index.column() == 2) { switch(role) { 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."); } } } else if (index.parent().row() == 1) { if (role == TypeRole) { return Detatched; } const QString &udi = m_disconnected[index.row()]; if (role == Qt::UserRole) { return udi; } 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."); } } else if (index.column() == 2) { switch(role) { 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."); } } } } else if (index.isValid()) { if (role == Qt::DisplayRole && index.column() == 0) { if (index.row() == 0) { return i18n("Attached Devices"); } else if (index.row() == 1) { return i18n("Disconnected Devices"); } } } return QVariant(); } int DeviceModel::rowCount(const QModelIndex &parent) const { if (parent.isValid()) { if (parent.internalId() < 3 || parent.column() > 0) { return 0; } if (parent.row() == 0) { return m_attached.size(); } return m_disconnected.size(); } return 2; } int DeviceModel::columnCount(const QModelIndex &parent) const { - Q_UNUSED(parent); + Q_UNUSED(parent) return 3; } void DeviceModel::setAutomaticMountOnLogin(bool automaticLogin) { if (m_automaticLogin != automaticLogin) { m_automaticLogin = automaticLogin; for (int parent = 0; parent < rowCount(); parent++) { const auto parentIndex = index(parent, 0); emit dataChanged(index(0, 1, parentIndex), index(rowCount(parentIndex), 1, parentIndex)); } } } void DeviceModel::setAutomaticMountOnPlugin(bool automaticAttached) { if (m_automaticAttached != automaticAttached) { m_automaticAttached = automaticAttached; for (int parent = 0; parent < rowCount(); parent++) { const auto parentIndex = index(parent, 0); emit dataChanged(index(0, 2, parentIndex), index(rowCount(parentIndex), 2, parentIndex)); } } } diff --git a/solid-device-automounter/kcm/DeviceModel.h b/solid-device-automounter/kcm/DeviceModel.h index 1a69a6c05..28f1ee5cf 100644 --- a/solid-device-automounter/kcm/DeviceModel.h +++ b/solid-device-automounter/kcm/DeviceModel.h @@ -1,82 +1,85 @@ /************************************************************************** * Copyright (C) 2009-2010 Trever Fischer * * Copyright (C) 2015 Kai Uwe Broulik * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . * ***************************************************************************/ #ifndef DEVICEMODEL_H #define DEVICEMODEL_H #include #include #include #include #include +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 { Attached, Detatched }; enum { UdiRole = Qt::UserRole, TypeRole }; Qt::ItemFlags flags(const QModelIndex &index) const override; QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override; QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override; int rowCount(const QModelIndex &parent = QModelIndex()) const override; int columnCount(const QModelIndex &parent = QModelIndex()) const override; QModelIndex index(int row, int column, const QModelIndex &parent = QModelIndex()) const override; QModelIndex parent(const QModelIndex &index) const override; bool setData(const QModelIndex &index, const QVariant &value, int role = Qt::EditRole) override; void setAutomaticMountOnLogin(bool automaticLogin); void setAutomaticMountOnPlugin(bool automaticAttached); 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 m_attached; QList m_disconnected; QHash m_loginForced; QHash m_attachedForced; bool m_automaticLogin; bool m_automaticAttached; + AutomounterSettings *m_settings; }; #endif diff --git a/solid-device-automounter/kded/DeviceAutomounter.cpp b/solid-device-automounter/kded/DeviceAutomounter.cpp index 604c0765d..e571136a5 100644 --- a/solid-device-automounter/kded/DeviceAutomounter.cpp +++ b/solid-device-automounter/kded/DeviceAutomounter.cpp @@ -1,122 +1,123 @@ /*************************************************************************** * Copyright (C) 2009 by Trever Fischer * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . * ***************************************************************************/ #include "DeviceAutomounter.h" #include #include #include #include #include #include #include #include K_PLUGIN_FACTORY_WITH_JSON(DeviceAutomounterFactory, "device_automounter.json", registerPlugin();) 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); } 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"), QStringLiteral("/kded"), QStringLiteral("org.kde.kded5"), QStringLiteral("setModuleAutoloading")); msg.setArguments({QVariant(QStringLiteral("device_automounter")), QVariant(false)}); dbus.call(msg, QDBus::NoBlock); // Unload right away msg = QDBusMessage::createMethodCall(QStringLiteral("org.kde.kded5"), QStringLiteral("/kded"), QStringLiteral("org.kde.kded5"), QStringLiteral("unloadModule")); msg.setArguments({QVariant(QStringLiteral("device_automounter"))}); dbus.call(msg, QDBus::NoBlock); return; } connect(Solid::DeviceNotifier::instance(), &Solid::DeviceNotifier::deviceAdded, this, &DeviceAutomounter::deviceAdded); const QList volumes = Solid::Device::listFromType(Solid::DeviceInterface::StorageVolume); for (Solid::Device volume : volumes) { // sa can be 0 (e.g. for the swap partition) if (const Solid::StorageAccess *sa = volume.as()) { 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) { if (dev.is() && dev.is()) { Solid::StorageAccess *sa = dev.as(); - 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(); if (!sa->isAccessible() && !sv->isIgnored()) { sa->setup(); } } } } 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 *sa = dev.as(); if (sa) { connect(sa, &Solid::StorageAccess::accessibilityChanged, this, &DeviceAutomounter::deviceMountChanged); } } } #include "DeviceAutomounter.moc" diff --git a/solid-device-automounter/kded/DeviceAutomounter.h b/solid-device-automounter/kded/DeviceAutomounter.h index 4a5668f11..be46a370c 100644 --- a/solid-device-automounter/kded/DeviceAutomounter.h +++ b/solid-device-automounter/kded/DeviceAutomounter.h @@ -1,43 +1,44 @@ /*************************************************************************** * Copyright (C) 2009 by Trever Fischer * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . * ***************************************************************************/ #ifndef DEVICEAUTOMOUNTER_H #define DEVICEAUTOMOUNTER_H #include #include #include "AutomounterSettings.h" class DeviceAutomounter : public KDEDModule { Q_OBJECT public: explicit DeviceAutomounter(QObject *parent = nullptr, const QVariantList &args = QVariantList()); ~DeviceAutomounter() override; private slots: void init(); void deviceAdded(const QString &udi); void deviceMountChanged(bool accessible, const QString &udi); private: void automountDevice(Solid::Device &dev, AutomounterSettings::AutomountType type); + AutomounterSettings *m_settings; }; #endif diff --git a/solid-device-automounter/lib/AutomounterSettings.cpp b/solid-device-automounter/lib/AutomounterSettings.cpp index de30e3a82..22f6d649b 100644 --- a/solid-device-automounter/lib/AutomounterSettings.cpp +++ b/solid-device-automounter/lib/AutomounterSettings.cpp @@ -1,106 +1,106 @@ /*************************************************************************** * Copyright (C) 2009 by Trever Fischer * * Copyright (C) 2015 by Kai Uwe Broulik * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . * ***************************************************************************/ #include "AutomounterSettings.h" 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) { switch (type) { case Login: return deviceSettings(udi).readEntry("ForceLoginAutomount", false); case Attach: return deviceSettings(udi).readEntry("ForceAttachAutomount", false); } return false; } bool AutomounterSettings::shouldAutomountDevice(const QString &udi, AutomountType type) { // We auto-mount the device, if: // 1.) auto-mounting is forced, or // 2.) auto-mounting is enabled // and auto-mounting on login/attach is configured // and the device is known, has been seen mounted last, or is unknown to us bool known = deviceIsKnown(udi); bool enabled = automountEnabled(); bool automountUnknown = automountUnknownDevices(); bool deviceAutomount = deviceAutomountIsForced(udi, type); bool lastSeenMounted = deviceSettings(udi).readEntry("LastSeenMounted", false); bool typeCondition = false; switch(type) { case Login: typeCondition = automountOnLogin(); break; case Attach: typeCondition = automountOnPlugin(); break; } bool shouldAutomount = deviceAutomount || (enabled && typeCondition && (known || lastSeenMounted || automountUnknown)); return shouldAutomount; } void AutomounterSettings::setDeviceLastSeenMounted(const QString &udi, bool mounted) { if (mounted) { deviceSettings(udi).writeEntry("EverMounted", true); } deviceSettings(udi).writeEntry("LastSeenMounted", mounted); } QString AutomounterSettings::getDeviceName(const QString &udi) { return deviceSettings(udi).readEntry("LastNameSeen"); } void AutomounterSettings::saveDevice(const Solid::Device &dev) { KConfigGroup settings = deviceSettings(dev.udi()); settings.writeEntry("LastNameSeen", dev.description()); settings.writeEntry("Icon", dev.icon()); } bool AutomounterSettings::getDeviceForcedAutomount(const QString &udi) { return deviceSettings(udi).readEntry("ForceAutomount", false); } QString AutomounterSettings::getDeviceIcon(const QString &udi) { return deviceSettings(udi).readEntry("Icon"); } diff --git a/solid-device-automounter/lib/AutomounterSettings.h b/solid-device-automounter/lib/AutomounterSettings.h index e70e0526e..500f39519 100644 --- a/solid-device-automounter/lib/AutomounterSettings.h +++ b/solid-device-automounter/lib/AutomounterSettings.h @@ -1,47 +1,48 @@ /*************************************************************************** * Copyright (C) 2009 by Trever Fischer * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU General Public License as published by * * the Free Software Foundation; either version 2 of the License, or * * (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU General Public License * * along with this program; if not, write to the * * Free Software Foundation, Inc., * * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA . * ***************************************************************************/ #ifndef AUTOMOUNTERSETTINGS_H #define AUTOMOUNTERSETTINGS_H #include "AutomounterSettingsBase.h" #include #include 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 diff --git a/solid-device-automounter/lib/AutomounterSettingsBase.kcfgc b/solid-device-automounter/lib/AutomounterSettingsBase.kcfgc index 0f9e175f5..12cc5ddbe 100644 --- a/solid-device-automounter/lib/AutomounterSettingsBase.kcfgc +++ b/solid-device-automounter/lib/AutomounterSettingsBase.kcfgc @@ -1,4 +1,4 @@ ClassName=AutomounterSettingsBase File=AutomounterSettingsBase.kcfg Mutators=true -Singleton=true \ No newline at end of file +ParentInConstructor=true