File indexing completed on 2024-10-06 10:25:49
0001 /* 0002 Configuration page for the custom settings 0003 0004 SPDX-FileCopyrightText: 2013-2023 Alexander Reinholdt <alexander.reinholdt@kdemail.net> 0005 SPDX-License-Identifier: GPL-2.0-or-later 0006 */ 0007 0008 // application specific includes 0009 #include "smb4kconfigpagecustomsettings.h" 0010 #include "core/smb4kcustomsettings.h" 0011 #include "core/smb4kcustomsettingsmanager.h" 0012 #include "core/smb4kglobal.h" 0013 0014 // Qt includes 0015 #include <QDialogButtonBox> 0016 #include <QMenu> 0017 #include <QMouseEvent> 0018 #include <QVBoxLayout> 0019 0020 // KDE includes 0021 #include <KIconLoader> 0022 #include <KLocalizedString> 0023 #include <KMessageWidget> 0024 0025 using namespace Smb4KGlobal; 0026 0027 Smb4KConfigPageCustomSettings::Smb4KConfigPageCustomSettings(QWidget *parent) 0028 : QWidget(parent) 0029 { 0030 m_itemToEdit = nullptr; 0031 m_customSettingsChanged = false; 0032 m_savingCustomSettings = false; 0033 0034 QHBoxLayout *layout = new QHBoxLayout(this); 0035 0036 QWidget *leftWidget = new QWidget(this); 0037 QVBoxLayout *leftWidgetLayout = new QVBoxLayout(leftWidget); 0038 leftWidgetLayout->setContentsMargins(0, 0, 0, 0); 0039 0040 m_listWidget = new QListWidget(leftWidget); 0041 m_listWidget->setSelectionMode(QListWidget::SingleSelection); 0042 m_listWidget->setContextMenuPolicy(Qt::CustomContextMenu); 0043 m_listWidget->viewport()->installEventFilter(this); 0044 0045 connect(m_listWidget, &QListWidget::itemDoubleClicked, this, &Smb4KConfigPageCustomSettings::slotEditCustomItem); 0046 connect(m_listWidget, &QListWidget::itemSelectionChanged, this, &Smb4KConfigPageCustomSettings::slotItemSelectionChanged); 0047 0048 leftWidgetLayout->addWidget(m_listWidget); 0049 0050 m_editorWidget = new Smb4KCustomSettingsEditorWidget(leftWidget); 0051 m_editorWidget->setVisible(false); 0052 0053 connect(m_editorWidget, &Smb4KCustomSettingsEditorWidget::edited, this, &Smb4KConfigPageCustomSettings::slotCustomSettingsEdited); 0054 0055 leftWidgetLayout->addWidget(m_editorWidget); 0056 0057 // 0058 // The feedback message widget 0059 // 0060 m_messageWidget = new KMessageWidget(leftWidget); 0061 m_messageWidget->setCloseButtonVisible(true); 0062 m_messageWidget->setMessageType(KMessageWidget::Information); 0063 m_messageWidget->setIcon(KDE::icon(QStringLiteral("emblem-information"))); 0064 m_messageWidget->setText(i18n("All fine.")); 0065 m_messageWidget->setWordWrap(true); 0066 m_messageWidget->setVisible(false); 0067 0068 leftWidgetLayout->addWidget(m_messageWidget); 0069 0070 layout->addWidget(leftWidget); 0071 0072 // 0073 // The push buttons 0074 // 0075 QDialogButtonBox *buttonBox = new QDialogButtonBox(Qt::Vertical, this); 0076 0077 m_resetButton = buttonBox->addButton(QDialogButtonBox::Reset); 0078 m_resetButton->setEnabled(false); 0079 0080 m_editButton = buttonBox->addButton(i18n("Edit"), QDialogButtonBox::ActionRole); 0081 m_editButton->setIcon(KDE::icon(QStringLiteral("edit-rename"))); 0082 m_editButton->setEnabled(false); 0083 0084 m_removeButton = buttonBox->addButton(i18n("Remove"), QDialogButtonBox::ActionRole); 0085 m_removeButton->setIcon(KDE::icon(QStringLiteral("edit-delete"))); 0086 m_removeButton->setEnabled(false); 0087 0088 m_clearButton = buttonBox->addButton(i18n("Clear List"), QDialogButtonBox::ActionRole); 0089 m_clearButton->setIcon(KDE::icon(QStringLiteral("edit-clear"))); 0090 m_clearButton->setEnabled(!Smb4KCustomSettingsManager::self()->customSettings(true).isEmpty()); 0091 0092 connect(m_resetButton, &QPushButton::clicked, this, &Smb4KConfigPageCustomSettings::slotResetButtonClicked); 0093 connect(m_editButton, &QPushButton::clicked, this, &Smb4KConfigPageCustomSettings::slotEditButtonClicked); 0094 connect(m_removeButton, &QPushButton::clicked, this, &Smb4KConfigPageCustomSettings::slotRemoveButtonClicked); 0095 connect(m_clearButton, &QPushButton::clicked, this, &Smb4KConfigPageCustomSettings::slotClearButtonClicked); 0096 0097 layout->addWidget(buttonBox); 0098 0099 loadCustomSettings(); 0100 0101 connect(this, &Smb4KConfigPageCustomSettings::customSettingsModified, this, &Smb4KConfigPageCustomSettings::slotEnableButtons); 0102 connect(Smb4KCustomSettingsManager::self(), &Smb4KCustomSettingsManager::updated, this, &Smb4KConfigPageCustomSettings::loadCustomSettings); 0103 } 0104 0105 Smb4KConfigPageCustomSettings::~Smb4KConfigPageCustomSettings() 0106 { 0107 } 0108 0109 bool Smb4KConfigPageCustomSettings::customSettingsChanged() 0110 { 0111 return m_customSettingsChanged; 0112 } 0113 0114 void Smb4KConfigPageCustomSettings::loadCustomSettings() 0115 { 0116 if (m_savingCustomSettings) { 0117 return; 0118 } 0119 0120 if (m_listWidget->count() != 0) { 0121 m_listWidget->clear(); 0122 } 0123 0124 QList<CustomSettingsPtr> customSettings = Smb4KCustomSettingsManager::self()->customSettings(true); 0125 0126 for (const CustomSettingsPtr &option : qAsConst(customSettings)) { 0127 QVariant variant = QVariant::fromValue(*option.data()); 0128 0129 QListWidgetItem *item = new QListWidgetItem(option->displayString(), m_listWidget); 0130 item->setData(Qt::UserRole, variant); 0131 0132 if (option->type() == Host) { 0133 item->setIcon(KDE::icon(QStringLiteral("network-server"))); 0134 } else { 0135 item->setIcon(KDE::icon(QStringLiteral("folder-network"))); 0136 } 0137 } 0138 0139 m_listWidget->sortItems(Qt::AscendingOrder); 0140 0141 m_customSettingsChanged = false; 0142 Q_EMIT customSettingsModified(); 0143 } 0144 0145 void Smb4KConfigPageCustomSettings::saveCustomSettings() 0146 { 0147 if (m_customSettingsChanged) { 0148 if (m_itemToEdit) { 0149 Smb4KCustomSettings customSettings = m_editorWidget->getCustomSettings(); 0150 0151 if (customSettings.hasCustomSettings()) { 0152 Smb4KCustomSettings currentCustomSettings = m_itemToEdit->data(Qt::UserRole).value<Smb4KCustomSettings>(); 0153 currentCustomSettings.update(&customSettings); 0154 0155 QVariant variant = QVariant::fromValue(currentCustomSettings); 0156 m_itemToEdit->setData(Qt::UserRole, variant); 0157 } else { 0158 m_editorWidget->setVisible(false); 0159 m_editorWidget->clear(); 0160 0161 delete m_itemToEdit; 0162 m_itemToEdit = nullptr; 0163 0164 setRemovalMessage(customSettings); 0165 0166 if (!m_messageWidget->isVisible()) { 0167 m_messageWidget->setVisible(true); 0168 } 0169 } 0170 } 0171 0172 QList<CustomSettingsPtr> customSettingsList; 0173 0174 for (int i = 0; i < m_listWidget->count(); ++i) { 0175 CustomSettingsPtr optionsPtr = CustomSettingsPtr(new Smb4KCustomSettings(m_listWidget->item(i)->data(Qt::UserRole).value<Smb4KCustomSettings>())); 0176 0177 if (optionsPtr) { 0178 customSettingsList << optionsPtr; 0179 } 0180 } 0181 0182 m_savingCustomSettings = true; 0183 Smb4KCustomSettingsManager::self()->saveCustomSettings(customSettingsList); 0184 m_savingCustomSettings = false; 0185 0186 m_customSettingsChanged = false; 0187 Q_EMIT customSettingsModified(); 0188 } 0189 } 0190 0191 void Smb4KConfigPageCustomSettings::setRemovalMessage(const Smb4KCustomSettings &settings) 0192 { 0193 m_messageWidget->setText(i18n("The item <b>%1</b> was removed, because all custom settings were reset.", settings.displayString())); 0194 } 0195 0196 bool Smb4KConfigPageCustomSettings::eventFilter(QObject *obj, QEvent *e) 0197 { 0198 if (obj == m_listWidget->viewport()) { 0199 if (e->type() == QEvent::MouseButtonPress) { 0200 QMouseEvent *mouseEvent = static_cast<QMouseEvent *>(e); 0201 #if QT_VERSION >= QT_VERSION_CHECK(6, 0, 0) 0202 QPointF pos = m_listWidget->viewport()->mapFromGlobal(mouseEvent->globalPosition()); 0203 QListWidgetItem *item = m_listWidget->itemAt(pos.toPoint()); 0204 #else 0205 QPoint pos = m_listWidget->viewport()->mapFromGlobal(mouseEvent->globalPos()); 0206 QListWidgetItem *item = m_listWidget->itemAt(pos); 0207 #endif 0208 0209 m_editButton->setEnabled(item != nullptr); 0210 m_removeButton->setEnabled(item != nullptr); 0211 0212 if (!item) { 0213 m_listWidget->clearSelection(); 0214 } 0215 } 0216 } 0217 0218 return QObject::eventFilter(obj, e); 0219 } 0220 0221 void Smb4KConfigPageCustomSettings::slotItemSelectionChanged() 0222 { 0223 if (m_editorWidget->isVisible()) { 0224 m_editorWidget->setVisible(false); 0225 0226 Smb4KCustomSettings customSettings = m_editorWidget->getCustomSettings(); 0227 0228 if (customSettings.hasCustomSettings()) { 0229 Smb4KCustomSettings currentCustomSettings = m_itemToEdit->data(Qt::UserRole).value<Smb4KCustomSettings>(); 0230 currentCustomSettings.update(&customSettings); 0231 0232 QVariant variant = QVariant::fromValue(currentCustomSettings); 0233 m_itemToEdit->setData(Qt::UserRole, variant); 0234 0235 m_itemToEdit = nullptr; 0236 } else { 0237 delete m_itemToEdit; 0238 m_itemToEdit = nullptr; 0239 0240 setRemovalMessage(customSettings); 0241 0242 if (!m_messageWidget->isVisible()) { 0243 m_messageWidget->setVisible(true); 0244 } 0245 } 0246 0247 m_editorWidget->clear(); 0248 } 0249 } 0250 0251 void Smb4KConfigPageCustomSettings::slotEditCustomItem(QListWidgetItem *item) 0252 { 0253 m_editorWidget->setCustomSettings(item->data(Qt::UserRole).value<Smb4KCustomSettings>()); 0254 m_editorWidget->setVisible(true); 0255 m_itemToEdit = item; 0256 } 0257 0258 void Smb4KConfigPageCustomSettings::slotEditButtonClicked(bool checked) 0259 { 0260 Q_UNUSED(checked); 0261 slotEditCustomItem(m_listWidget->currentItem()); 0262 } 0263 0264 void Smb4KConfigPageCustomSettings::slotRemoveButtonClicked(bool checked) 0265 { 0266 Q_UNUSED(checked); 0267 0268 if (m_listWidget->currentItem()) { 0269 delete m_listWidget->currentItem(); 0270 m_listWidget->setCurrentItem(nullptr); 0271 m_customSettingsChanged = true; 0272 Q_EMIT customSettingsModified(); 0273 } 0274 } 0275 0276 void Smb4KConfigPageCustomSettings::slotClearButtonClicked(bool checked) 0277 { 0278 Q_UNUSED(checked); 0279 m_listWidget->clear(); 0280 m_customSettingsChanged = true; 0281 Q_EMIT customSettingsModified(); 0282 } 0283 0284 void Smb4KConfigPageCustomSettings::slotResetButtonClicked(bool checked) 0285 { 0286 // FIXME: Do not close the editor 0287 Q_UNUSED(checked); 0288 m_listWidget->clear(); 0289 m_messageWidget->setVisible(false); 0290 loadCustomSettings(); 0291 } 0292 0293 void Smb4KConfigPageCustomSettings::slotEnableButtons() 0294 { 0295 m_resetButton->setEnabled(m_customSettingsChanged); 0296 m_clearButton->setEnabled(m_listWidget->count() != 0); 0297 } 0298 0299 void Smb4KConfigPageCustomSettings::slotCustomSettingsEdited(bool changed) 0300 { 0301 m_customSettingsChanged = changed; 0302 Q_EMIT customSettingsModified(); 0303 }