File indexing completed on 2025-02-16 04:49:30

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 "checkbeforesendinterface.h"
0008 #include "checkbeforesendupdatesmtpdialog.h"
0009 #include "duplicateemails/checkduplicateemailsdialog.h"
0010 #include "duplicateemails/checkduplicateemailsjob.h"
0011 #include "sendattachments/checkattachmentdialog.h"
0012 #include "sendattachments/checkattachmentjob.h"
0013 
0014 #include <KConfigGroup>
0015 #include <KIdentityManagementCore/Identity>
0016 #include <KIdentityManagementCore/IdentityManager>
0017 #include <KLocalizedString>
0018 #include <KMessageBox>
0019 #include <KSharedConfig>
0020 
0021 #include <QPointer>
0022 
0023 CheckBeforeSendInterface::CheckBeforeSendInterface(QObject *parent)
0024     : MessageComposer::PluginEditorCheckBeforeSendInterface(parent)
0025 {
0026 }
0027 
0028 CheckBeforeSendInterface::~CheckBeforeSendInterface() = default;
0029 
0030 bool CheckBeforeSendInterface::exec(const MessageComposer::PluginEditorCheckBeforeSendParams &params)
0031 {
0032     if (mSendPlainText) {
0033         if (params.isHtmlMail()) {
0034             const int answer = KMessageBox::questionTwoActions(parentWidget(),
0035                                                                i18n("Do you want to send the email as HTML?"),
0036                                                                i18n("Send email as plain text"),
0037                                                                KGuiItem(i18nc("@action:button", "Send As HTML")),
0038                                                                KStandardGuiItem::cancel());
0039             if (answer == KMessageBox::ButtonCode::SecondaryAction) {
0040                 return false;
0041             }
0042         }
0043     }
0044     if (mCheckMailTransport) {
0045         KIdentityManagementCore::Identity *identity = &KIdentityManagementCore::IdentityManager::self()->modifyIdentityForUoid(params.identity());
0046         if (identity->transport() != QString::number(params.transportId())) {
0047             QPointer<CheckBeforeSendUpdateSmtpDialog> dlg = new CheckBeforeSendUpdateSmtpDialog(parentWidget());
0048             if (!dlg->exec()) {
0049                 delete dlg;
0050                 return false;
0051             }
0052             if (dlg->changeSmtp()) {
0053                 identity->setTransport(QString::number(params.transportId()));
0054                 KIdentityManagementCore::IdentityManager::self()->commit();
0055             }
0056             delete dlg;
0057         }
0058     }
0059     if (mCheckDuplicateEmails) {
0060         QStringList lst;
0061         if (!params.ccAddresses().trimmed().isEmpty()) {
0062             lst << params.ccAddresses();
0063         }
0064         if (!params.bccAddresses().trimmed().isEmpty()) {
0065             lst << params.bccAddresses();
0066         }
0067         if (!params.toAddresses().trimmed().isEmpty()) {
0068             lst << params.toAddresses();
0069         }
0070         if (!lst.isEmpty()) {
0071             CheckDuplicateEmailsJob job;
0072             job.setEmails(lst);
0073             job.start();
0074             const QMap<QString, int> results = job.result();
0075             if (!results.isEmpty()) {
0076                 QPointer<CheckDuplicateEmailsDialog> dlg = new CheckDuplicateEmailsDialog(parentWidget());
0077                 dlg->setDuplicatedEmails(results);
0078                 bool result = false;
0079                 if (dlg->exec()) {
0080                     result = true;
0081                 } else {
0082                     result = false;
0083                 }
0084                 delete dlg;
0085                 if (!result) {
0086                     return result;
0087                 }
0088             }
0089         }
0090     }
0091     if (mCheckSendAttachments) {
0092         if (params.hasAttachment()) {
0093             QPointer<CheckAttachmentDialog> dlg = new CheckAttachmentDialog(parentWidget());
0094             QStringList lst;
0095             if (!params.ccAddresses().trimmed().isEmpty()) {
0096                 lst << params.ccAddresses();
0097             }
0098             if (!params.bccAddresses().trimmed().isEmpty()) {
0099                 lst << params.bccAddresses();
0100             }
0101             if (!params.toAddresses().trimmed().isEmpty()) {
0102                 lst << params.toAddresses();
0103             }
0104             CheckAttachmentJob job;
0105             job.setOriginalEmails(lst);
0106             job.start();
0107             dlg->setEmails(job.resultList());
0108             if (dlg->exec()) {
0109                 delete dlg;
0110                 return true;
0111             } else {
0112                 delete dlg;
0113                 return false;
0114             }
0115         }
0116     }
0117     return true;
0118 }
0119 
0120 void CheckBeforeSendInterface::reloadConfig()
0121 {
0122     KConfigGroup grp(KSharedConfig::openConfig(), QStringLiteral("Check Before Send"));
0123     mSendPlainText = grp.readEntry("SendPlainText", false);
0124     mCheckMailTransport = grp.readEntry("SmtpDefinedInIdentity", false);
0125     mCheckDuplicateEmails = grp.readEntry("CheckDuplicatedEmails", false);
0126     mCheckSendAttachments = grp.readEntry("CheckSendAttachment", false);
0127 }
0128 
0129 #include "moc_checkbeforesendinterface.cpp"