Mailcommon

filteractionsetidentity.cpp
1 /*
2  * SPDX-FileCopyrightText: 1996-1998 Stefan Taferner <[email protected]>
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 <KIdentityManagement/Identity>
15 #include <KIdentityManagement/IdentityCombo>
16 #include <KIdentityManagement/IdentityManager>
17 
18 #include <KLocalizedString>
19 #include <QPointer>
20 
21 using namespace MailCommon;
22 
23 FilterAction *FilterActionSetIdentity::newAction()
24 {
25  return new FilterActionSetIdentity;
26 }
27 
28 FilterActionSetIdentity::FilterActionSetIdentity(QObject *parent)
29  : FilterActionWithUOID(QStringLiteral("set identity"), i18n("Set Identity To"), parent)
30 {
31  mParameter = KernelIf->identityManager()->defaultIdentity().uoid();
32 }
33 
34 bool 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 
51 FilterAction::ReturnCode FilterActionSetIdentity::process(ItemContext &context, bool applyOnOutbound) const
52 {
53  const KIdentityManagement::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), "utf-8");
67  if (applyOnOutbound) {
68  msg->from()->fromUnicodeString(ident.fullEmailAddr(), "utf-8");
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 
85 SearchRule::RequiredPart FilterActionSetIdentity::requiredPart() const
86 {
88 }
89 
90 QWidget *FilterActionSetIdentity::createParamWidget(QWidget *parent) const
91 {
92  auto comboBox = new KIdentityManagement::IdentityCombo(KernelIf->identityManager(), parent);
93  comboBox->setObjectName(QStringLiteral("identitycombobox"));
94  comboBox->setCurrentIdentity(mParameter);
95 
96  connect(comboBox, qOverload<int>(&KIdentityManagement::IdentityCombo::currentIndexChanged), this, &FilterActionSetIdentity::filterActionModified);
97  return comboBox;
98 }
99 
100 void FilterActionSetIdentity::applyParamWidgetValue(QWidget *paramWidget)
101 {
102  const KIdentityManagement::IdentityCombo *comboBox = qobject_cast<KIdentityManagement::IdentityCombo *>(paramWidget);
103  Q_ASSERT(comboBox);
104 
105  mParameter = comboBox->currentIdentity();
106 }
107 
108 void FilterActionSetIdentity::clearParamWidget(QWidget *paramWidget) const
109 {
110  auto comboBox = qobject_cast<KIdentityManagement::IdentityCombo *>(paramWidget);
111  Q_ASSERT(comboBox);
112 
113  comboBox->setCurrentIndex(0);
114 }
115 
116 void FilterActionSetIdentity::setParamWidgetValue(QWidget *paramWidget) const
117 {
118  auto comboBox = qobject_cast<KIdentityManagement::IdentityCombo *>(paramWidget);
119  Q_ASSERT(comboBox);
120 
121  comboBox->setCurrentIdentity(mParameter);
122 }
QString number(int n, int base)
void setCurrentIndex(int index)
Abstract base class for filter actions with a free-form string as parameter.
Abstract base class for mail filter actions.
Definition: filteraction.h:38
QString i18n(const char *text, const TYPE &arg...)
RequiredPart
Possible required parts.
Definition: searchrule.h:68
bool isEmpty() const const
void setNeedsPayloadStore()
Marks that the item's payload has been changed and needs to be written back.
Definition: itemcontext.cpp:33
A helper class for the filtering process.
Definition: itemcontext.h:26
ReturnCode
Describes the possible return codes of filter processing:
Definition: filteraction.h:45
@ CompleteMessage
Whole message.
Definition: searchrule.h:71
void currentIndexChanged(int index)
static QVector< Mailbox > listFromUnicodeString(const QString &s)
Akonadi::Item & item()
Returns the item of the context.
Definition: itemcontext.cpp:18
The filter dialog.
T payload() const
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Sat Sep 24 2022 03:58:15 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.