Mailcommon

filteractionsettransport.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 "filteractionsettransport.h"
9 
10 #include "filter/dialog/filteractionmissingtransportdialog.h"
11 
12 #include <KLocalizedString>
13 #include <MailTransport/Transport>
14 #include <MailTransport/TransportComboBox>
15 #include <MailTransport/TransportManager>
16 
17 #include <QPointer>
18 
19 using namespace MailCommon;
20 
21 FilterAction *FilterActionSetTransport::newAction()
22 {
23  return new FilterActionSetTransport;
24 }
25 
26 QWidget *FilterActionSetTransport::createParamWidget(QWidget *parent) const
27 {
28  auto transportCombobox = new MailTransport::TransportComboBox(parent);
29  transportCombobox->setObjectName(QStringLiteral("transportcombobox"));
30  setParamWidgetValue(transportCombobox);
31  connect(transportCombobox, &MailTransport::TransportComboBox::currentIndexChanged, this, &FilterActionSetTransport::filterActionModified);
32  return transportCombobox;
33 }
34 
35 FilterActionSetTransport::FilterActionSetTransport(QObject *parent)
36  : FilterAction(QStringLiteral("set transport"), i18n("Set Transport To"), parent)
37  , mParameter(-1)
38 {
39 }
40 
41 bool FilterActionSetTransport::argsFromStringInteractive(const QString &argsStr, const QString &filterName)
42 {
43  bool needUpdate = false;
44  argsFromString(argsStr);
45  if (!MailTransport::TransportManager::self()->transportById(mParameter, false)) {
46  QPointer<MailCommon::FilterActionMissingTransportDialog> dlg = new MailCommon::FilterActionMissingTransportDialog(filterName);
47  if (dlg->exec()) {
48  mParameter = dlg->selectedTransport();
49  needUpdate = true;
50  } else {
51  mParameter = -1;
52  }
53  delete dlg;
54  }
55  return needUpdate;
56 }
57 
58 FilterAction::ReturnCode FilterActionSetTransport::process(ItemContext &context, bool) const
59 {
60  if (isEmpty()) {
61  return ErrorButGoOn;
62  }
63 
65  // Error if we don't have transport here.
66  if (!transport) {
67  return ErrorButGoOn;
68  }
69 
70  const auto msg = context.item().payload<KMime::Message::Ptr>();
71  auto header = new KMime::Headers::Generic("X-KMail-Transport");
72  header->fromUnicodeString(argsAsString(), "utf-8");
73  msg->setHeader(header);
74  msg->assemble();
75 
76  context.setNeedsPayloadStore();
77 
78  return GoOn;
79 }
80 
81 SearchRule::RequiredPart FilterActionSetTransport::requiredPart() const
82 {
84 }
85 
86 void FilterActionSetTransport::applyParamWidgetValue(QWidget *paramWidget)
87 {
88  const MailTransport::TransportComboBox *comboBox = qobject_cast<MailTransport::TransportComboBox *>(paramWidget);
89  Q_ASSERT(comboBox);
90 
91  mParameter = comboBox->currentTransportId();
92  mTransportName.clear();
93 }
94 
95 void FilterActionSetTransport::clearParamWidget(QWidget *paramWidget) const
96 {
97  auto comboBox = qobject_cast<MailTransport::TransportComboBox *>(paramWidget);
98  Q_ASSERT(comboBox);
99 
100  comboBox->setCurrentIndex(0);
101 }
102 
103 void FilterActionSetTransport::setParamWidgetValue(QWidget *paramWidget) const
104 {
105  auto comboBox = qobject_cast<MailTransport::TransportComboBox *>(paramWidget);
106  Q_ASSERT(comboBox);
107 
108  comboBox->setCurrentTransport(mParameter);
109 }
110 
111 bool FilterActionSetTransport::isEmpty() const
112 {
113  return mParameter == -1;
114 }
115 
116 void FilterActionSetTransport::argsFromString(const QString &argsStr)
117 {
118  bool ok;
119  const int val = argsStr.trimmed().toInt(&ok);
120  mParameter = ok ? val : -1;
121 }
122 
123 QString FilterActionSetTransport::argsAsString() const
124 {
125  return QString::number(mParameter);
126 }
127 
128 QString FilterActionSetTransport::displayString() const
129 {
130  if (mTransportName.isEmpty()) {
132  const int index = MailTransport::TransportManager::self()->transportIds().indexOf(mParameter);
133  if (index != -1) {
134  mTransportName = listNames.at(index);
135  }
136  }
137  return label() + QLatin1String(" \"") + (mTransportName.isEmpty() ? argsAsString().toHtmlEscaped() : mTransportName) + QLatin1String("\"");
138 }
139 
140 QString FilterActionSetTransport::informationAboutNotValidAction() const
141 {
142  return i18n("Mail transport not defined.");
143 }
Transport * transportById(int id, bool def=true) const
QString number(int n, int base)
bool setCurrentTransport(int transportId)
QString trimmed() const const
void setCurrentIndex(int index)
Q_SCRIPTABLE QVector< int > transportIds() const
Q_SCRIPTABLE QStringList transportNames() const
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
const T & at(int i) const const
static TransportManager * self()
int toInt(bool *ok, int base) 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
QString label(StandardShortcut id)
ReturnCode
Describes the possible return codes of filter processing:
Definition: filteraction.h:45
@ CompleteMessage
Whole message.
Definition: searchrule.h:71
void currentIndexChanged(int index)
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 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.