Mailcommon

filteractionforward.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 "filteractionforward.h"
9 #include "mailcommon_debug.h"
10 
11 #include "filter/dialog/filteractionmissingtemplatedialog.h"
12 #include "kernel/mailkernel.h"
13 #include "util/mailutil.h"
14 
15 #include <Akonadi/EmailAddressRequester>
16 #include <MessageComposer/MessageFactoryNG>
17 #include <MessageComposer/MessageSender>
18 #include <MessageCore/StringUtil>
19 #include <TemplateParser/CustomTemplates>
20 #include <templateparser/customtemplates_kfg.h>
21 
22 #include <KComboBox>
23 #include <KLineEdit>
24 #include <KLocalizedString>
25 
26 #include <QHBoxLayout>
27 
28 using namespace MailCommon;
29 
30 FilterAction *FilterActionForward::newAction()
31 {
32  return new FilterActionForward;
33 }
34 
35 FilterActionForward::FilterActionForward(QObject *parent)
36  : FilterActionWithAddress(QStringLiteral("forward"), i18nc("Forward directly not with a command", "Forward To"), parent)
37 {
38 }
39 
40 FilterAction::ReturnCode FilterActionForward::process(ItemContext &context, bool) const
41 {
42  if (mParameter.isEmpty()) {
43  return ErrorButGoOn;
44  }
45 
46  const auto msg = context.item().payload<KMime::Message::Ptr>();
47  // avoid endless loops when this action is used in a filter
48  // which applies to sent messages
49  if (MessageCore::StringUtil::addressIsInAddressList(mParameter, QStringList(msg->to()->asUnicodeString()))) {
50  qCWarning(MAILCOMMON_LOG) << "Attempt to forward to recipient of original message, ignoring.";
51  return ErrorButGoOn;
52  }
53 #if 0 // PORT ME TO ASync
54  MessageComposer::MessageFactoryNG factory(msg, context.item().id());
55  factory.setIdentityManager(KernelIf->identityManager());
56  factory.setFolderIdentity(Util::folderIdentity(context.item()));
57  factory.setTemplate(mTemplate);
58 
59  KMime::Message::Ptr fwdMsg = factory.createForward();
60  fwdMsg->to()->fromUnicodeString(fwdMsg->to()->asUnicodeString() + QLatin1Char(',') + mParameter, "utf-8");
61  if (!KernelIf->msgSender()->send(fwdMsg, MessageComposer::MessageSender::SendDefault)) {
62  qCWarning(MAILCOMMON_LOG) << "FilterAction: could not forward message (sending failed)";
63  return ErrorButGoOn; // error: couldn't send
64  } else {
65  sendMDN(context.item(), KMime::MDN::Dispatched);
66  }
67 #endif
68  // (the msgSender takes ownership of the message, so don't delete it here)
69  return GoOn;
70 }
71 
72 SearchRule::RequiredPart FilterActionForward::requiredPart() const
73 {
75 }
76 
77 QWidget *FilterActionForward::createParamWidget(QWidget *parent) const
78 {
79  auto addressAndTemplate = new QWidget(parent);
80  auto layout = new QHBoxLayout(addressAndTemplate);
81  layout->setContentsMargins({});
82 
83  QWidget *addressEdit = FilterActionWithAddress::createParamWidget(addressAndTemplate);
84  addressEdit->setObjectName(QStringLiteral("addressEdit"));
85  layout->addWidget(addressEdit);
86 
87  auto addressRequester = qobject_cast<Akonadi::EmailAddressRequester *>(addressEdit);
88  Q_ASSERT(addressRequester);
89  KLineEdit *lineEdit = addressRequester->lineEdit();
90  lineEdit->setClearButtonEnabled(true);
91  lineEdit->setTrapReturnKey(true);
92  lineEdit->setToolTip(i18n("The addressee to whom the message will be forwarded."));
93  lineEdit->setWhatsThis(i18n("The filter will forward the message to the addressee entered here."));
94 
95  auto templateCombo = new KComboBox(addressAndTemplate);
96  templateCombo->setMinimumWidth(50);
97  templateCombo->setObjectName(QStringLiteral("templateCombo"));
98  layout->addWidget(templateCombo);
99 
100  templateCombo->addItem(i18n("Default Template"));
101 
102  const QStringList templateNames = SettingsIf->customTemplates();
103  for (const QString &templateName : templateNames) {
104  TemplateParser::CTemplates templat(templateName);
105  if (templat.type() == TemplateParser::CustomTemplates::TForward || templat.type() == TemplateParser::CustomTemplates::TUniversal) {
106  templateCombo->addItem(templateName);
107  }
108  }
109 
110  templateCombo->setEnabled(templateCombo->count() > 1);
111  templateCombo->setToolTip(i18n("The template used when forwarding"));
112  templateCombo->setWhatsThis(i18n("Set the forwarding template that will be used with this filter."));
113  connect(templateCombo, &KComboBox::currentIndexChanged, this, &FilterActionForward::filterActionModified);
114  connect(addressRequester, &Akonadi::EmailAddressRequester::textChanged, this, &FilterActionForward::filterActionModified);
115 
116  return addressAndTemplate;
117 }
118 
119 void FilterActionForward::applyParamWidgetValue(QWidget *paramWidget)
120 {
121  auto *addressEdit = paramWidget->findChild<QWidget *>(QStringLiteral("addressEdit"));
122  Q_ASSERT(addressEdit);
124 
125  const auto templateCombo = paramWidget->findChild<KComboBox *>(QStringLiteral("templateCombo"));
126  Q_ASSERT(templateCombo);
127 
128  if (templateCombo->currentIndex() == 0) {
129  // Default template, so don't use a custom one
130  mTemplate.clear();
131  } else {
132  mTemplate = templateCombo->currentText();
133  }
134 }
135 
136 void FilterActionForward::setParamWidgetValue(QWidget *paramWidget) const
137 {
138  auto *addressEdit = paramWidget->findChild<QWidget *>(QStringLiteral("addressEdit"));
139  Q_ASSERT(addressEdit);
141 
142  const auto templateCombo = paramWidget->findChild<KComboBox *>(QStringLiteral("templateCombo"));
143  Q_ASSERT(templateCombo);
144 
145  if (mTemplate.isEmpty()) {
146  templateCombo->setCurrentIndex(0);
147  } else {
148  int templateIndex = templateCombo->findText(mTemplate);
149  if (templateIndex != -1) {
150  templateCombo->setCurrentIndex(templateIndex);
151  } else {
152  mTemplate.clear();
153  }
154  }
155 }
156 
157 void FilterActionForward::clearParamWidget(QWidget *paramWidget) const
158 {
159  auto *addressEdit = paramWidget->findChild<QWidget *>(QStringLiteral("addressEdit"));
160  Q_ASSERT(addressEdit);
162 
163  const auto templateCombo = paramWidget->findChild<KComboBox *>(QStringLiteral("templateCombo"));
164  Q_ASSERT(templateCombo);
165 
166  templateCombo->setCurrentIndex(0);
167 }
168 
169 // We simply place a "@[email protected]" between the two parameters. The template is the last
170 // parameter in the string, for compatibility reasons.
171 namespace
172 {
173 inline const QString forwardFilterArgsSeperator()
174 {
175  return QStringLiteral("@[email protected]");
176 }
177 }
178 void FilterActionForward::argsFromString(const QString &argsStr)
179 {
180  const int seperatorPos = argsStr.indexOf(forwardFilterArgsSeperator());
181 
182  if (seperatorPos == -1) {
183  // Old config, assume that the whole string is the addressee
185  } else {
186  const QString addressee = argsStr.left(seperatorPos);
187  mTemplate = argsStr.mid(seperatorPos + forwardFilterArgsSeperator().length());
189  }
190 }
191 
192 bool FilterActionForward::argsFromStringInteractive(const QString &argsStr, const QString &filterName)
193 {
194  bool needUpdate = false;
195  argsFromString(argsStr);
196  if (!mTemplate.isEmpty()) {
197  const QStringList templateNames = SettingsIf->customTemplates();
198  QStringList currentTemplateList;
199  currentTemplateList << i18n("Default Template");
200  for (const QString &templateName : templateNames) {
201  TemplateParser::CTemplates templat(templateName);
202  if (templat.type() == TemplateParser::CustomTemplates::TForward || templat.type() == TemplateParser::CustomTemplates::TUniversal) {
203  if (templateName == mTemplate) {
204  return false;
205  }
206  currentTemplateList << templateName;
207  }
208  }
209  QPointer<MailCommon::FilterActionMissingTemplateDialog> dlg = new MailCommon::FilterActionMissingTemplateDialog(currentTemplateList, filterName);
210  if (dlg->exec()) {
211  mTemplate = dlg->selectedTemplate();
212  needUpdate = true;
213  }
214  delete dlg;
215  }
216  return needUpdate;
217 }
218 
219 QString FilterActionForward::argsAsString() const
220 {
221  return FilterActionWithAddress::argsAsString() + forwardFilterArgsSeperator() + mTemplate;
222 }
223 
224 QString FilterActionForward::displayString() const
225 {
226  if (mTemplate.isEmpty()) {
227  return i18n("Forward to %1 with default template", mParameter);
228  } else {
229  return i18n("Forward to %1 with template %2", mParameter, mTemplate);
230  }
231 }
232 
233 QString FilterActionForward::informationAboutNotValidAction() const
234 {
235  return i18n("Email address was not defined.");
236 }
Abstract base class for filter actions with a mail address as parameter.
void setWhatsThis(const QString &)
void applyParamWidgetValue(QWidget *paramWidget) override
The filter action shall set it's parameter from the widget's contents.
MAILCOMMON_EXPORT uint folderIdentity(const Akonadi::Item &item)
Returns the identity of the folder that contains the given Akonadi::Item.
Definition: mailutil.cpp:149
void setTrapReturnKey(bool trap)
void setClearButtonEnabled(bool enable)
void argsFromString(const QString &argsStr) override
Read extra arguments from given string.
void setParamWidgetValue(QWidget *paramWidget) const override
The filter action shall set it's widget's contents from it's 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
int indexOf(QChar ch, int from, Qt::CaseSensitivity cs) const const
bool addressIsInAddressList(const QString &address, const QStringList &addresses)
QString argsAsString() const override
Return extra arguments as string.
T findChild(const QString &name, Qt::FindChildOptions options) const const
A helper class for the filtering process.
Definition: itemcontext.h:26
ReturnCode
Describes the possible return codes of filter processing:
Definition: filteraction.h:45
void clearParamWidget(QWidget *paramWidget) const override
The filter action shall clear it's parameter widget's contents.
Id id() const
void setToolTip(const QString &)
QString left(int n) const const
@ CompleteMessage
Whole message.
Definition: searchrule.h:71
void setObjectName(const QString &name)
void currentIndexChanged(int index)
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString mid(int position, int n) const const
Akonadi::Item & item()
Returns the item of the context.
Definition: itemcontext.cpp:18
QWidget * createParamWidget(QWidget *parent) const override
Creates a widget for setting the filter action parameter.
The filter dialog.
T payload() const
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Sat Oct 1 2022 04:00:52 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.