diff --git a/solid-device-automounter/kcm/DeviceAutomounterKCM.cpp b/solid-device-automounter/kcm/DeviceAutomounterKCM.cpp index 06388aba4..2dc3e300b 100644 --- a/solid-device-automounter/kcm/DeviceAutomounterKCM.cpp +++ b/solid-device-automounter/kcm/DeviceAutomounterKCM.cpp @@ -1,234 +1,221 @@ /************************************************************************** * 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)) { 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); - m_devices = new DeviceModel(this); + addConfig(AutomounterSettings::self(), this); + deviceView->setModel(m_devices); deviceView->header()->setSectionResizeMode(QHeaderView::ResizeToContents); deviceView->header()->setSectionResizeMode(0, QHeaderView::Stretch); auto emitChanged = [this] { - m_devices->setAutomaticMountOnLogin(automountOnLogin->isChecked()); - m_devices->setAutomaticMountOnPlugin(automountOnPlugin->isChecked()); + m_devices->setAutomaticMountOnLogin(kcfg_AutomountOnLogin->isChecked()); + m_devices->setAutomaticMountOnPlugin(kcfg_AutomountOnPlugin->isChecked()); emit markAsChanged(); }; - connect(automountOnLogin, &QCheckBox::stateChanged, this, emitChanged); - connect(automountOnPlugin, &QCheckBox::stateChanged, this, emitChanged); - connect(automountEnabled, &QCheckBox::stateChanged, this, emitChanged); - connect(automountUnknownDevices, &QCheckBox::stateChanged, this, emitChanged); connect(m_devices, &DeviceModel::dataChanged, this, emitChanged); - connect(automountEnabled, &QCheckBox::stateChanged, this, &DeviceAutomounterKCM::enabledChanged); - 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::enabledChanged() -{ - automountOnLogin->setEnabled(automountEnabled->isChecked()); - automountOnPlugin->setEnabled(automountEnabled->isChecked()); - automountUnknownDevices->setEnabled(automountEnabled->isChecked()); - deviceView->setEnabled(automountEnabled->isChecked()); -} - void DeviceAutomounterKCM::load() { - automountEnabled->setChecked(AutomounterSettings::automountEnabled()); - automountUnknownDevices->setChecked(AutomounterSettings::automountUnknownDevices()); - automountOnLogin->setChecked(AutomounterSettings::automountOnLogin()); - automountOnPlugin->setChecked(AutomounterSettings::automountOnPlugin()); + KCModule::load(); + + bool automountEnabled = AutomounterSettings::self()->automountEnabled(); + + kcfg_AutomountUnknownDevices->setEnabled(automountEnabled); + kcfg_AutomountOnLogin->setEnabled(automountEnabled); + kcfg_AutomountOnPlugin->setEnabled(automountEnabled); m_devices->reload(); - enabledChanged(); loadLayout(); } void DeviceAutomounterKCM::save() { + KCModule::save(); saveLayout(); - const bool enabled = automountEnabled->isChecked(); - AutomounterSettings::setAutomountEnabled(enabled); - AutomounterSettings::setAutomountUnknownDevices(automountUnknownDevices->isChecked()); - AutomounterSettings::setAutomountOnLogin(automountOnLogin->isChecked()); - AutomounterSettings::setAutomountOnPlugin(automountOnPlugin->isChecked()); + 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); } else { AutomounterSettings::deviceSettings(device).writeEntry("ForceLoginAutomount", false); } dev = dev.sibling(j, 2); if (dev.data(Qt::CheckStateRole).toInt() == Qt::Checked) { AutomounterSettings::deviceSettings(device).writeEntry("ForceAttachAutomount", true); } else { AutomounterSettings::deviceSettings(device).writeEntry("ForceAttachAutomount", false); } } } const auto knownDevices = AutomounterSettings::knownDevices(); for (const QString &possibleDevice : knownDevices) { if (!validDevices.contains(possibleDevice)) { AutomounterSettings::deviceSettings(possibleDevice).deleteGroup(); } } AutomounterSettings::self()->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 fd458aefd..c2eba31ec 100644 --- a/solid-device-automounter/kcm/DeviceAutomounterKCM.h +++ b/solid-device-automounter/kcm/DeviceAutomounterKCM.h @@ -1,55 +1,54 @@ /************************************************************************** * 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 enabledChanged(); void updateForgetDeviceButton(); void forgetSelectedDevices(); private: void saveLayout(); void loadLayout(); DeviceModel *m_devices; - }; #endif diff --git a/solid-device-automounter/kcm/DeviceAutomounterKCM.ui b/solid-device-automounter/kcm/DeviceAutomounterKCM.ui index 61dd271e6..704c7e878 100644 --- a/solid-device-automounter/kcm/DeviceAutomounterKCM.ui +++ b/solid-device-automounter/kcm/DeviceAutomounterKCM.ui @@ -1,153 +1,218 @@ DeviceAutomounterKCM 0 0 615 380 - + When this is unchecked, no device automounting of any kind will happen, regardless of anything selected in the "Device Overrides" section. Enable automatic mounting of removable media Qt::Horizontal QSizePolicy::Fixed 22 76 - + When this is checked, only remembered devices will be automatically mounted. A device is 'remembered' if it has ever been mounted before. For instance, plugging in a USB media player to charge is not sufficient to 'remember' it - if the files are not accessed, it will not be automatically mounted the next time it is seen. Once they have been accessed, however, the device's contents will be automatically made available to the system. Only automatically mount removable media that has been manually mounted before - + If any removable storage devices are connected to your system when you login to your desktop, their contents will automatically be made available to your system for other programs to read. - Mount all removable media at login + Automatically mount all removable media at login - + When this is checked, the contents of any storage device will automatically be made available to the system when it is plugged in or attached. - Automatically mount removable media when attached + Automatically mount all removable media when attached Qt::Horizontal 40 20 Automatic mount This list contains the storage devices known to the system. If "Automount on Login" is checked, the device will be automatically mounted even though "Mount all removable media at login" is unchecked. The same applies for "Automount on Attach". If "Enable automatic mounting of removable media" is unchecked, the overrides do not apply and no devices will be automatically mounted. QAbstractItemView::NoEditTriggers QAbstractItemView::ExtendedSelection true true true true true false false false Clicking this button causes the selected devices to be 'forgotten'. This is only useful if "Only automatically mount removable media that has been manually mounted before" is checked. Once a device is forgotten and the system is set to only automatically mount familiar devices, the device will not be automatically mounted. Forget Device - + + + kcfg_AutomountEnabled + toggled(bool) + kcfg_AutomountOnLogin + setEnabled(bool) + + + 307 + 20 + + + 308 + 81 + + + + + kcfg_AutomountEnabled + toggled(bool) + kcfg_AutomountOnPlugin + setEnabled(bool) + + + 307 + 20 + + + 308 + 111 + + + + + kcfg_AutomountEnabled + toggled(bool) + kcfg_AutomountUnknownDevices + setEnabled(bool) + + + 307 + 20 + + + 308 + 51 + + + + + kcfg_AutomountEnabled + toggled(bool) + deviceView + setEnabled(bool) + + + 307 + 20 + + + 307 + 244 + + + +