File indexing completed on 2024-12-15 04:55:36
0001 /* 0002 SPDX-FileCopyrightText: 2013-2024 Laurent Montel <montel@kde.org> 0003 0004 SPDX-License-Identifier: GPL-2.0-or-later 0005 */ 0006 0007 #include "serversievelistwidget.h" 0008 #include "readserversieveconfigjob.h" 0009 #include "serversievesettingsdialog.h" 0010 #include "sieveserversettings.h" 0011 #include <KLocalizedString> 0012 0013 #include <QListWidgetItem> 0014 #include <QPointer> 0015 0016 #include <qt6keychain/keychain.h> 0017 using namespace QKeychain; 0018 ServerSieveListWidget::ServerSieveListWidget(QWidget *parent) 0019 : QListWidget(parent) 0020 { 0021 connect(this, &ServerSieveListWidget::itemDoubleClicked, this, &ServerSieveListWidget::modifyServerConfig); 0022 } 0023 0024 ServerSieveListWidget::~ServerSieveListWidget() = default; 0025 0026 void ServerSieveListWidget::readConfig() 0027 { 0028 auto job = new ReadServerSieveConfigJob(this); 0029 connect(job, &ReadServerSieveConfigJob::finished, this, &ServerSieveListWidget::slotReadServerSieveConfigDone); 0030 job->start(); 0031 } 0032 0033 void ServerSieveListWidget::slotReadServerSieveConfigDone(const QList<SieveEditorUtil::SieveServerConfig> &lstConfig) 0034 { 0035 for (const SieveEditorUtil::SieveServerConfig &conf : lstConfig) { 0036 auto item = new ServerSieveListWidgetItem(this); 0037 item->setServerConfig(conf); 0038 } 0039 } 0040 0041 void ServerSieveListWidget::writeConfig() 0042 { 0043 QList<SieveEditorUtil::SieveServerConfig> lstServerConfig; 0044 for (int i = 0; i < count(); ++i) { 0045 auto serverSieveItem = static_cast<ServerSieveListWidgetItem *>(item(i)); 0046 if (serverSieveItem) { 0047 SieveEditorUtil::SieveServerConfig config = serverSieveItem->serverConfig(); 0048 config.enabled = (serverSieveItem->checkState() == Qt::Checked); 0049 lstServerConfig.append(config); 0050 } 0051 } 0052 SieveEditorUtil::writeServerSieveConfig(lstServerConfig); 0053 deletePasswords(); 0054 } 0055 0056 void ServerSieveListWidget::deletePasswords() 0057 { 0058 if (!mNeedToRemovePasswordInWallet.isEmpty()) { 0059 for (const QString &identifier : std::as_const(mNeedToRemovePasswordInWallet)) { 0060 auto deleteJob = new DeletePasswordJob(QStringLiteral("sieveeditor")); 0061 deleteJob->setKey(identifier); 0062 deleteJob->start(); 0063 } 0064 } 0065 } 0066 0067 void ServerSieveListWidget::modifyServerConfig() 0068 { 0069 QListWidgetItem *item = currentItem(); 0070 if (!item) { 0071 return; 0072 } 0073 0074 auto serverSieveListItem = static_cast<ServerSieveListWidgetItem *>(item); 0075 0076 QPointer<ServerSieveSettingsDialog> dlg = new ServerSieveSettingsDialog(this); 0077 dlg->setWindowTitle(i18nc("@title:window", "Modify Settings")); 0078 dlg->setServerSieveConfig(serverSieveListItem->serverConfig()); 0079 if (dlg->exec()) { 0080 serverSieveListItem->setServerConfig(dlg->serverSieveConfig()); 0081 } 0082 delete dlg; 0083 } 0084 0085 void ServerSieveListWidget::deleteServerConfig(QListWidgetItem *item) 0086 { 0087 auto serverSieveListItem = static_cast<ServerSieveListWidgetItem *>(item); 0088 SieveEditorUtil::SieveServerConfig conf = serverSieveListItem->serverConfig(); 0089 0090 mNeedToRemovePasswordInWallet.append(SieveEditorUtil::sievePasswordIdentifier(conf.sieveSettings.userName, conf.sieveSettings.serverName)); 0091 const QString imapIdentifier = 0092 SieveEditorUtil::imapPasswordIdentifier(conf.sieveImapAccountSettings.userName(), conf.sieveImapAccountSettings.serverName()); 0093 if (!imapIdentifier.isEmpty()) { 0094 mNeedToRemovePasswordInWallet.append(imapIdentifier); 0095 } 0096 } 0097 0098 void ServerSieveListWidget::addServerConfig() 0099 { 0100 QPointer<ServerSieveSettingsDialog> dlg = new ServerSieveSettingsDialog(this); 0101 if (dlg->exec()) { 0102 auto item = new ServerSieveListWidgetItem(this); 0103 item->setServerConfig(dlg->serverSieveConfig()); 0104 } 0105 delete dlg; 0106 } 0107 0108 ServerSieveListWidgetItem::ServerSieveListWidgetItem(QListWidget *parent) 0109 : QListWidgetItem(parent) 0110 { 0111 } 0112 0113 ServerSieveListWidgetItem::~ServerSieveListWidgetItem() = default; 0114 0115 SieveEditorUtil::SieveServerConfig ServerSieveListWidgetItem::serverConfig() const 0116 { 0117 return mServerConfig; 0118 } 0119 0120 void ServerSieveListWidgetItem::setServerConfig(const SieveEditorUtil::SieveServerConfig &conf) 0121 { 0122 setCheckState(conf.enabled ? Qt::Checked : Qt::Unchecked); 0123 setText(i18nc("server name (username)", "%1 (%2)", conf.sieveSettings.serverName, conf.sieveSettings.userName)); 0124 mServerConfig = conf; 0125 } 0126 0127 #include "moc_serversievelistwidget.cpp"