File indexing completed on 2024-11-10 04:50:04
0001 /* 0002 * SPDX-FileCopyrightText: 1996-1998 Stefan Taferner <taferner@kde.org> 0003 * 0004 * SPDX-License-Identifier: GPL-2.0-or-later 0005 * 0006 */ 0007 0008 #include "filteractionsetidentity.h" 0009 #include <MessageCore/StringUtil> 0010 0011 #include "filter/dialog/filteractionmissingidentitydialog.h" 0012 #include "kernel/mailkernel.h" 0013 0014 #include <KIdentityManagementCore/Identity> 0015 #include <KIdentityManagementCore/IdentityManager> 0016 #include <KIdentityManagementWidgets/IdentityCombo> 0017 0018 #include <KLocalizedString> 0019 #include <QPointer> 0020 0021 using namespace MailCommon; 0022 0023 FilterAction *FilterActionSetIdentity::newAction() 0024 { 0025 return new FilterActionSetIdentity; 0026 } 0027 0028 FilterActionSetIdentity::FilterActionSetIdentity(QObject *parent) 0029 : FilterActionWithUOID(QStringLiteral("set identity"), i18n("Set Identity To"), parent) 0030 { 0031 mParameter = KernelIf->identityManager()->defaultIdentity().uoid(); 0032 } 0033 0034 bool FilterActionSetIdentity::argsFromStringInteractive(const QString &argsStr, const QString &filterName) 0035 { 0036 bool needUpdate = false; 0037 argsFromString(argsStr); 0038 if (KernelIf->identityManager()->identityForUoid(mParameter).isNull()) { 0039 QPointer<MailCommon::FilterActionMissingIdentityDialog> dlg = new MailCommon::FilterActionMissingIdentityDialog(filterName); 0040 if (dlg->exec()) { 0041 mParameter = dlg->selectedIdentity(); 0042 needUpdate = true; 0043 } else { 0044 mParameter = -1; 0045 } 0046 delete dlg; 0047 } 0048 return needUpdate; 0049 } 0050 0051 FilterAction::ReturnCode FilterActionSetIdentity::process(ItemContext &context, bool applyOnOutbound) const 0052 { 0053 const KIdentityManagementCore::Identity &ident = KernelIf->identityManager()->identityForUoid(mParameter); 0054 0055 if (ident.isNull()) { 0056 return ErrorButGoOn; 0057 } 0058 0059 const auto msg = context.item().payload<KMime::Message::Ptr>(); 0060 uint currentId = 0; 0061 if (auto hrd = msg->headerByType("X-KMail-Identity")) { 0062 currentId = hrd->asUnicodeString().trimmed().toUInt(); 0063 } 0064 if (currentId != mParameter) { 0065 auto header = new KMime::Headers::Generic("X-KMail-Identity"); 0066 header->fromUnicodeString(QString::number(mParameter), "utf-8"); 0067 if (applyOnOutbound) { 0068 msg->from()->fromUnicodeString(ident.fullEmailAddr(), "utf-8"); 0069 if (!ident.bcc().isEmpty()) { 0070 const auto mailboxes = KMime::Types::Mailbox::listFromUnicodeString(ident.bcc()); 0071 for (const KMime::Types::Mailbox &mailbox : mailboxes) { 0072 msg->bcc()->addAddress(mailbox); 0073 } 0074 } 0075 } 0076 msg->setHeader(header); 0077 msg->assemble(); 0078 0079 context.setNeedsPayloadStore(); 0080 } 0081 0082 return GoOn; 0083 } 0084 0085 SearchRule::RequiredPart FilterActionSetIdentity::requiredPart() const 0086 { 0087 return SearchRule::CompleteMessage; 0088 } 0089 0090 QWidget *FilterActionSetIdentity::createParamWidget(QWidget *parent) const 0091 { 0092 auto comboBox = new KIdentityManagementWidgets::IdentityCombo(KernelIf->identityManager(), parent); 0093 comboBox->setObjectName(QLatin1StringView("identitycombobox")); 0094 comboBox->setCurrentIdentity(mParameter); 0095 0096 connect(comboBox, &KIdentityManagementWidgets::IdentityCombo::currentIndexChanged, this, &FilterActionSetIdentity::filterActionModified); 0097 return comboBox; 0098 } 0099 0100 void FilterActionSetIdentity::applyParamWidgetValue(QWidget *paramWidget) 0101 { 0102 const KIdentityManagementWidgets::IdentityCombo *comboBox = qobject_cast<KIdentityManagementWidgets::IdentityCombo *>(paramWidget); 0103 Q_ASSERT(comboBox); 0104 0105 mParameter = comboBox->currentIdentity(); 0106 } 0107 0108 void FilterActionSetIdentity::clearParamWidget(QWidget *paramWidget) const 0109 { 0110 auto comboBox = qobject_cast<KIdentityManagementWidgets::IdentityCombo *>(paramWidget); 0111 Q_ASSERT(comboBox); 0112 0113 comboBox->setCurrentIndex(0); 0114 } 0115 0116 void FilterActionSetIdentity::setParamWidgetValue(QWidget *paramWidget) const 0117 { 0118 auto comboBox = qobject_cast<KIdentityManagementWidgets::IdentityCombo *>(paramWidget); 0119 Q_ASSERT(comboBox); 0120 0121 comboBox->setCurrentIdentity(mParameter); 0122 } 0123 0124 #include "moc_filteractionsetidentity.cpp"