File indexing completed on 2024-12-29 04:54:51

0001 /*
0002   SPDX-FileCopyrightText: 2013-2024 Laurent Montel <montel@kde.org>
0003 
0004   SPDX-License-Identifier: GPL-2.0-only
0005 */
0006 
0007 #include "custommanagesievewidget.h"
0008 #include "util/findaccountinfojob.h"
0009 #include "widgets/managesievetreeview.h"
0010 
0011 #include "kmanagesieve/sievejob.h"
0012 #include "widgets/sievetreewidgetitem.h"
0013 #include <KLocalizedString>
0014 #include <QIcon>
0015 #include <QTreeWidgetItem>
0016 
0017 using namespace KSieveUi;
0018 
0019 CustomManageSieveWidget::CustomManageSieveWidget(KSieveCore::SieveImapPasswordProvider *passwordProvider, QWidget *parent)
0020     : KSieveUi::ManageSieveWidget(parent)
0021     , mPasswordProvider(passwordProvider)
0022 {
0023 }
0024 
0025 CustomManageSieveWidget::~CustomManageSieveWidget() = default;
0026 
0027 void CustomManageSieveWidget::updateSieveSettings()
0028 {
0029     mSieveImapInstances = KSieveCore::Util::sieveImapInstances();
0030     Q_EMIT updateSieveSettingsDone();
0031 }
0032 
0033 bool CustomManageSieveWidget::refreshList()
0034 {
0035     if (mRefreshInProgress) {
0036         return false;
0037     }
0038     mRefreshInProgress = true;
0039     bool noImapFound = true;
0040     mLastSieveTreeWidgetItem = nullptr;
0041     mServerSieveInfos.clear();
0042     for (const KSieveCore::SieveImapInstance &type : std::as_const(mSieveImapInstances)) {
0043         if (type.status() == KSieveCore::SieveImapInstance::Broken) {
0044             continue;
0045         }
0046         mServerSieveInfos.insert(type.name(), type.identifier());
0047     }
0048     noImapFound = mServerSieveInfos.isEmpty();
0049     searchSieveScript();
0050     return noImapFound;
0051 }
0052 
0053 void CustomManageSieveWidget::searchSieveScript()
0054 {
0055     mSieveServerMapIterator = mServerSieveInfos.constBegin();
0056     if (mSieveServerMapIterator != mServerSieveInfos.constEnd()) {
0057         slotSearchSieveScript(mSieveServerMapIterator.key(), mSieveServerMapIterator.value());
0058     }
0059 }
0060 
0061 void CustomManageSieveWidget::searchNextServerSieve()
0062 {
0063     ++mSieveServerMapIterator;
0064     if (mSieveServerMapIterator != mServerSieveInfos.constEnd()) {
0065         slotSearchSieveScript(mSieveServerMapIterator.key(), mSieveServerMapIterator.value());
0066     } else {
0067         mLastSieveTreeWidgetItem = nullptr;
0068         mRefreshInProgress = false;
0069     }
0070 }
0071 
0072 void CustomManageSieveWidget::slotSearchSieveScript(const QString &name, const QString &identifier)
0073 {
0074     mLastSieveTreeWidgetItem = new SieveTreeWidgetItem(treeView(), mLastSieveTreeWidgetItem);
0075     mLastSieveTreeWidgetItem->setIcon(0, QIcon::fromTheme(QStringLiteral("network-server")));
0076 
0077     auto job = new KSieveCore::FindAccountInfoJob(this);
0078     connect(job, &KSieveCore::FindAccountInfoJob::findAccountInfoFinished, this, &CustomManageSieveWidget::slotFindAccountInfoFinished);
0079     job->setIdentifier(identifier);
0080     job->setProperty("serverName", name);
0081     job->setProvider(mPasswordProvider);
0082     job->start();
0083 }
0084 
0085 void CustomManageSieveWidget::slotFindAccountInfoFinished(const KSieveCore::Util::AccountInfo &info)
0086 {
0087     QString serverName = sender()->property("serverName").toString();
0088     const QUrl u = info.sieveUrl;
0089     if (u.isEmpty()) {
0090         auto item = new QTreeWidgetItem(mLastSieveTreeWidgetItem);
0091         item->setText(0, i18n("No Sieve URL configured"));
0092         item->setFlags(item->flags() & ~Qt::ItemIsEnabled);
0093         treeView()->expandItem(mLastSieveTreeWidgetItem);
0094     } else {
0095         serverName += QStringLiteral(" (%1)").arg(u.userName());
0096         KManageSieve::SieveJob *job = KManageSieve::SieveJob::list(u);
0097         // qDebug() << " SETTINGS " << info;
0098         job->setProperty("sieveimapaccountsettings", QVariant::fromValue(info.sieveImapAccountSettings));
0099         connect(job, &KManageSieve::SieveJob::gotList, this, &CustomManageSieveWidget::slotGotList);
0100         mJobs.insert(job, mLastSieveTreeWidgetItem);
0101         mUrls.insert(mLastSieveTreeWidgetItem, u);
0102         mLastSieveTreeWidgetItem->startAnimation();
0103     }
0104     mLastSieveTreeWidgetItem->setText(0, serverName);
0105     searchNextServerSieve();
0106 }
0107 
0108 #include "moc_custommanagesievewidget.cpp"