File indexing completed on 2025-01-19 04:46:43

0001 /*
0002    SPDX-FileCopyrightText: 2016-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "confirmaddressinterface.h"
0008 #include "confirmaddresscheckjob.h"
0009 #include "confirmaddressconfigurewidget.h"
0010 #include "confirmaddressdialog.h"
0011 
0012 #include <KConfigGroup>
0013 #include <KSharedConfig>
0014 #include <QPointer>
0015 
0016 #include <KIdentityManagementCore/Identity>
0017 #include <KIdentityManagementCore/IdentityManager>
0018 #include <MessageComposer/AliasesExpandJob>
0019 #include <PimCommon/PimUtil>
0020 namespace
0021 {
0022 static const char myConfigGroupName[] = "Confirm Address";
0023 }
0024 ConfirmAddressInterface::ConfirmAddressInterface(QObject *parent)
0025     : MessageComposer::PluginEditorCheckBeforeSendInterface(parent)
0026 {
0027 }
0028 
0029 ConfirmAddressInterface::~ConfirmAddressInterface() = default;
0030 
0031 bool ConfirmAddressInterface::exec(const MessageComposer::PluginEditorCheckBeforeSendParams &params)
0032 {
0033     QStringList lst;
0034     if (!params.ccAddresses().trimmed().isEmpty()) {
0035         lst << params.ccAddresses();
0036     }
0037     if (!params.bccAddresses().trimmed().isEmpty()) {
0038         lst << params.bccAddresses();
0039     }
0040     if (!params.toAddresses().trimmed().isEmpty()) {
0041         lst << params.toAddresses();
0042     }
0043     if (lst.isEmpty()) {
0044         return true;
0045     }
0046     // not configurated => validate it.
0047     const ConfirmAddressSettings settings = mHashSettings.value(params.identity());
0048     if (settings.mDomains.isEmpty() && settings.mWhiteLists.isEmpty()) {
0049         return true;
0050     }
0051     const QStringList emails = PimCommon::Util::generateEmailList(lst);
0052 #if 0
0053     MessageComposer::AliasesExpandJob job(params.addresses().join(QLatin1StringView(", ")), params.defaultDomain(), this);
0054     if (job.exec()) {
0055         emails = job.emailAddressOnly();
0056     }
0057 #endif
0058     ConfirmAddressCheckJob job;
0059     job.setCheckSettings(settings.mDomains, settings.mWhiteLists, settings.mRejectedDomain);
0060     job.setAddressList(emails);
0061     job.start();
0062 
0063     const QStringList invalidEmails = job.invalidEmails();
0064     const QStringList validEmails = job.validEmails();
0065 
0066     if (!invalidEmails.isEmpty()) {
0067         QPointer<ConfirmAddressDialog> dlg = new ConfirmAddressDialog(parentWidget());
0068         connect(dlg.data(), &ConfirmAddressDialog::addWhileListEmails, this, &ConfirmAddressInterface::slotAddWhiteListEmails);
0069         dlg->setCurrentIdentity(params.identity());
0070         dlg->setValidAddresses(validEmails);
0071         dlg->setInvalidAddresses(invalidEmails);
0072         if (dlg->exec()) {
0073             delete dlg;
0074             return true;
0075         } else {
0076             delete dlg;
0077             return false;
0078         }
0079     } else {
0080         return true;
0081     }
0082 }
0083 
0084 void ConfirmAddressInterface::slotAddWhiteListEmails(const QStringList &lst, uint currentIdentity)
0085 {
0086     KConfigGroup grp(KSharedConfig::openConfig(), QLatin1StringView(myConfigGroupName));
0087     KConfigGroup identityGroup = grp.group(QStringLiteral("Confirm Address %1").arg(currentIdentity));
0088     QStringList oldWhiteList = identityGroup.readEntry("Emails", QStringList());
0089     for (const QString &email : lst) {
0090         if (!oldWhiteList.contains(email)) {
0091             oldWhiteList.append(email);
0092         }
0093     }
0094     identityGroup.writeEntry("Emails", oldWhiteList);
0095     identityGroup.sync();
0096     Q_EMIT forceReloadConfig();
0097 }
0098 
0099 void ConfirmAddressInterface::reloadConfig()
0100 {
0101     KConfigGroup grp(KSharedConfig::openConfig(), QLatin1StringView(myConfigGroupName));
0102     mHashSettings.clear();
0103 
0104     KIdentityManagementCore::IdentityManager *im = KIdentityManagementCore::IdentityManager::self();
0105     KIdentityManagementCore::IdentityManager::ConstIterator end = im->end();
0106     for (KIdentityManagementCore::IdentityManager::ConstIterator it = im->begin(); it != end; ++it) {
0107         const uint identity = (*it).uoid();
0108         KConfigGroup identityGroup = grp.group(QStringLiteral("Confirm Address %1").arg(identity));
0109         ConfirmAddressSettings settings;
0110         settings.mDomains = identityGroup.readEntry("Domains", QStringList());
0111         settings.mWhiteLists = identityGroup.readEntry("Emails", QStringList());
0112         settings.mRejectedDomain = identityGroup.readEntry("RejectDomain", false);
0113 
0114         mHashSettings.insert(identity, settings);
0115     }
0116 }
0117 
0118 #include "moc_confirmaddressinterface.cpp"