Mailcommon

filteractionsettransport.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 "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
19using namespace MailCommon;
20
21FilterAction *FilterActionSetTransport::newAction()
22{
23 return new FilterActionSetTransport;
24}
25
26QWidget *FilterActionSetTransport::createParamWidget(QWidget *parent) const
27{
28 auto transportCombobox = new MailTransport::TransportComboBox(parent);
29 transportCombobox->setObjectName(QLatin1StringView("transportcombobox"));
30 setParamWidgetValue(transportCombobox);
32 return transportCombobox;
33}
34
35FilterActionSetTransport::FilterActionSetTransport(QObject *parent)
36 : FilterAction(QStringLiteral("set transport"), i18n("Set Transport To"), parent)
37 , mParameter(-1)
38{
39}
40
41bool 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
58FilterAction::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());
73 msg->setHeader(header);
74 msg->assemble();
75
76 context.setNeedsPayloadStore();
77
78 return GoOn;
79}
80
81SearchRule::RequiredPart FilterActionSetTransport::requiredPart() const
82{
84}
85
86void FilterActionSetTransport::applyParamWidgetValue(QWidget *paramWidget)
87{
89 Q_ASSERT(comboBox);
90
91 mParameter = comboBox->currentTransportId();
92 mTransportName.clear();
93}
94
95void 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
103void 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
111bool FilterActionSetTransport::isEmpty() const
112{
113 return mParameter == -1;
114}
115
116void FilterActionSetTransport::argsFromString(const QString &argsStr)
117{
118 bool ok;
119 const int val = argsStr.trimmed().toInt(&ok);
120 mParameter = ok ? val : -1;
121}
122
123QString FilterActionSetTransport::argsAsString() const
124{
125 return QString::number(mParameter);
126}
127
128QString 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() + QLatin1StringView(" \"") + (mTransportName.isEmpty() ? argsAsString().toHtmlEscaped() : mTransportName) + QLatin1StringView("\"");
138}
139
140QString FilterActionSetTransport::informationAboutNotValidAction() const
141{
142 return i18n("Mail transport not defined.");
143}
144
145#include "moc_filteractionsettransport.cpp"
T payload() const
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.
QString label() const
Returns i18n'd label, ie.
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:70
@ CompleteMessage
Whole message.
Definition searchrule.h:73
bool setCurrentTransport(int transportId)
Q_SCRIPTABLE QStringList transportNames() const
Transport * transportById(Transport::Id id, bool def=true) const
static TransportManager * self()
Q_SCRIPTABLE QList< int > transportIds() const
QString i18n(const char *text, const TYPE &arg...)
The filter dialog.
void setCurrentIndex(int index)
void currentIndexChanged(int index)
const_reference at(qsizetype i) const const
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
QObject * parent() const const
T qobject_cast(QObject *object)
void clear()
bool isEmpty() const const
QString number(double n, char format, int precision)
int toInt(bool *ok, int base) const const
QString trimmed() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:00:24 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.