Mailcommon

filteractionsetidentity.cpp
1/*
2 * SPDX-FileCopyrightText: 1996-1998 Stefan Taferner <taferner@kde.org>
3 *
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 *
6 */
7
8#include "filteractionsetidentity.h"
9#include <MessageCore/StringUtil>
10
11#include "filter/dialog/filteractionmissingidentitydialog.h"
12#include "kernel/mailkernel.h"
13
14#include <KIdentityManagementCore/Identity>
15#include <KIdentityManagementCore/IdentityManager>
16#include <KIdentityManagementWidgets/IdentityCombo>
17
18#include <KLocalizedString>
19#include <QPointer>
20
21using namespace MailCommon;
22
23FilterAction *FilterActionSetIdentity::newAction()
24{
25 return new FilterActionSetIdentity;
26}
27
28FilterActionSetIdentity::FilterActionSetIdentity(QObject *parent)
29 : FilterActionWithUOID(QStringLiteral("set identity"), i18n("Set Identity To"), parent)
30{
31 mParameter = KernelIf->identityManager()->defaultIdentity().uoid();
32}
33
34bool FilterActionSetIdentity::argsFromStringInteractive(const QString &argsStr, const QString &filterName)
35{
36 bool needUpdate = false;
37 argsFromString(argsStr);
38 if (KernelIf->identityManager()->identityForUoid(mParameter).isNull()) {
39 QPointer<MailCommon::FilterActionMissingIdentityDialog> dlg = new MailCommon::FilterActionMissingIdentityDialog(filterName);
40 if (dlg->exec()) {
41 mParameter = dlg->selectedIdentity();
42 needUpdate = true;
43 } else {
44 mParameter = -1;
45 }
46 delete dlg;
47 }
48 return needUpdate;
49}
50
51FilterAction::ReturnCode FilterActionSetIdentity::process(ItemContext &context, bool applyOnOutbound) const
52{
53 const KIdentityManagementCore::Identity &ident = KernelIf->identityManager()->identityForUoid(mParameter);
54
55 if (ident.isNull()) {
56 return ErrorButGoOn;
57 }
58
59 const auto msg = context.item().payload<KMime::Message::Ptr>();
60 uint currentId = 0;
61 if (auto hrd = msg->headerByType("X-KMail-Identity")) {
62 currentId = hrd->asUnicodeString().trimmed().toUInt();
63 }
64 if (currentId != mParameter) {
65 auto header = new KMime::Headers::Generic("X-KMail-Identity");
66 header->fromUnicodeString(QString::number(mParameter));
67 if (applyOnOutbound) {
68 msg->from()->fromUnicodeString(ident.fullEmailAddr());
69 if (!ident.bcc().isEmpty()) {
70 const auto mailboxes = KMime::Types::Mailbox::listFromUnicodeString(ident.bcc());
71 for (const KMime::Types::Mailbox &mailbox : mailboxes) {
72 msg->bcc()->addAddress(mailbox);
73 }
74 }
75 }
76 msg->setHeader(header);
77 msg->assemble();
78
79 context.setNeedsPayloadStore();
80 }
81
82 return GoOn;
83}
84
85SearchRule::RequiredPart FilterActionSetIdentity::requiredPart() const
86{
88}
89
90QWidget *FilterActionSetIdentity::createParamWidget(QWidget *parent) const
91{
92 auto comboBox = new KIdentityManagementWidgets::IdentityCombo(KernelIf->identityManager(), parent);
93 comboBox->setObjectName(QLatin1StringView("identitycombobox"));
94 comboBox->setCurrentIdentity(mParameter);
95
97 return comboBox;
98}
99
100void FilterActionSetIdentity::applyParamWidgetValue(QWidget *paramWidget)
101{
103 Q_ASSERT(comboBox);
104
105 mParameter = comboBox->currentIdentity();
106}
107
108void FilterActionSetIdentity::clearParamWidget(QWidget *paramWidget) const
109{
111 Q_ASSERT(comboBox);
112
113 comboBox->setCurrentIndex(0);
114}
115
116void FilterActionSetIdentity::setParamWidgetValue(QWidget *paramWidget) const
117{
119 Q_ASSERT(comboBox);
120
121 comboBox->setCurrentIdentity(mParameter);
122}
123
124#include "moc_filteractionsetidentity.cpp"
T payload() const
void setCurrentIdentity(const KIdentityManagementCore::Identity &identity)
KIdentityManagementCore::Identity::Id currentIdentity() const
static QList< Mailbox > listFromUnicodeString(QStringView s)
Abstract base class for filter actions with a free-form string as parameter.
void argsFromString(const QString &argsStr) override
Read extra arguments from given string.
Abstract base class for mail filter actions.
ReturnCode
Describes the possible return codes of filter processing:
@ ErrorButGoOn
A non-critical error occurred.
@ GoOn
Go on with applying filter actions.
void filterActionModified()
Called to notify that the current FilterAction has had some value modification.
A helper class for the filtering process.
Definition itemcontext.h:27
void setNeedsPayloadStore()
Marks that the item's payload has been changed and needs to be written back.
Akonadi::Item & item()
Returns the item of the context.
RequiredPart
Possible required parts.
Definition searchrule.h:68
@ CompleteMessage
Whole message.
Definition searchrule.h:71
QString i18n(const char *text, const TYPE &arg...)
The filter dialog.
void setCurrentIndex(int index)
void currentIndexChanged(int index)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QObject * parent() const const
T qobject_cast(QObject *object)
bool isEmpty() const const
QString number(double n, char format, int precision)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 22 2024 12:12:19 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.