Libksieve

sieveconditionenvelope.cpp
1/*
2 SPDX-FileCopyrightText: 2013-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6#include "sieveconditionenvelope.h"
7#include "autocreatescripts/autocreatescriptutil_p.h"
8
9#include "autocreatescripts/commonwidgets/selectmatchtypecombobox.h"
10#include "editor/sieveeditorutil.h"
11#include "widgets/selectaddresspartcombobox.h"
12#include "widgets/selectheadertypecombobox.h"
13
14#include <KLocalizedString>
15
16#include "libksieveui_debug.h"
17#include <QHBoxLayout>
18#include <QLabel>
19#include <QXmlStreamReader>
20
21using namespace KSieveUi;
22
23SieveConditionEnvelope::SieveConditionEnvelope(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QObject *parent)
24 : SieveCondition(sieveGraphicalModeWidget, QStringLiteral("envelope"), i18n("Envelope"), parent)
25{
26}
27
28QWidget *SieveConditionEnvelope::createParamWidget(QWidget *parent) const
29{
30 auto w = new QWidget(parent);
31 auto lay = new QHBoxLayout;
32 lay->setContentsMargins({});
33 w->setLayout(lay);
34
35 auto selectAddressPart = new SelectAddressPartComboBox(mSieveGraphicalModeWidget);
36 connect(selectAddressPart, &SelectAddressPartComboBox::valueChanged, this, &SieveConditionEnvelope::valueChanged);
37 selectAddressPart->setObjectName(QLatin1StringView("addresspartcombobox"));
38 lay->addWidget(selectAddressPart);
39
40 auto grid = new QGridLayout;
41 grid->setContentsMargins({});
42 lay->addLayout(grid);
43
44 auto selectMatchCombobox = new SelectMatchTypeComboBox(mSieveGraphicalModeWidget);
45 selectMatchCombobox->setObjectName(QLatin1StringView("matchtypecombobox"));
46 connect(selectMatchCombobox, &SelectMatchTypeComboBox::valueChanged, this, &SieveConditionEnvelope::valueChanged);
47 grid->addWidget(selectMatchCombobox, 0, 0);
48
49 auto selectHeaderType = new SelectHeaderTypeComboBox(true);
50 selectHeaderType->setObjectName(QLatin1StringView("headertypecombobox"));
51 connect(selectHeaderType, &SelectHeaderTypeComboBox::valueChanged, this, &SieveConditionEnvelope::valueChanged);
52 grid->addWidget(selectHeaderType, 0, 1);
53
54 auto lab = new QLabel(i18nc("@label:textbox", "address:"));
55 grid->addWidget(lab, 1, 0);
56
57 AbstractRegexpEditorLineEdit *edit = AutoCreateScriptUtil::createRegexpEditorLineEdit();
58 edit->setObjectName(QLatin1StringView("editaddress"));
59 connect(edit, &AbstractRegexpEditorLineEdit::textChanged, this, &SieveConditionEnvelope::valueChanged);
60 connect(selectMatchCombobox, &SelectMatchTypeComboBox::switchToRegexp, edit, &AbstractRegexpEditorLineEdit::switchToRegexpEditorLineEdit);
61 edit->setClearButtonEnabled(true);
62 edit->setPlaceholderText(i18nc("@info:placeholder", "Use ; to separate emails"));
63 grid->addWidget(edit, 1, 1);
64
65 return w;
66}
67
68QString SieveConditionEnvelope::code(QWidget *w) const
69{
70 const SelectMatchTypeComboBox *selectMatchCombobox = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("matchtypecombobox"));
71 bool isNegative = false;
72 const QString matchTypeStr = selectMatchCombobox->code(isNegative);
73
74 const SelectAddressPartComboBox *selectAddressPart = w->findChild<SelectAddressPartComboBox *>(QStringLiteral("addresspartcombobox"));
75 const QString selectAddressPartStr = selectAddressPart->code();
76
77 const SelectHeaderTypeComboBox *selectHeaderType = w->findChild<SelectHeaderTypeComboBox *>(QStringLiteral("headertypecombobox"));
78 const QString selectHeaderTypeStr = selectHeaderType->code();
79
80 const AbstractRegexpEditorLineEdit *edit = w->findChild<AbstractRegexpEditorLineEdit *>(QStringLiteral("editaddress"));
81 const QString addressStr = AutoCreateScriptUtil::createAddressList(edit->code().trimmed(), false);
82 return AutoCreateScriptUtil::negativeString(isNegative)
83 + QStringLiteral("envelope %1 %2 %3 %4").arg(selectAddressPartStr, matchTypeStr, selectHeaderTypeStr, addressStr)
84 + AutoCreateScriptUtil::generateConditionComment(comment());
85}
86
87QStringList SieveConditionEnvelope::needRequires(QWidget *w) const
88{
89 const SelectAddressPartComboBox *selectAddressPart = w->findChild<SelectAddressPartComboBox *>(QStringLiteral("addresspartcombobox"));
90 const SelectMatchTypeComboBox *selectMatchCombobox = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("matchtypecombobox"));
91 return QStringList() << QStringLiteral("envelope") << selectAddressPart->extraRequire() << selectMatchCombobox->needRequires();
92}
93
94bool SieveConditionEnvelope::needCheckIfServerHasCapability() const
95{
96 return true;
97}
98
99QString SieveConditionEnvelope::serverNeedsCapability() const
100{
101 return QStringLiteral("envelope");
102}
103
104QString SieveConditionEnvelope::help() const
105{
106 return i18n("The \"envelope\" test is true if the specified part of the [SMTP] (or equivalent) envelope matches the specified key.");
107}
108
109void SieveConditionEnvelope::setParamWidgetValue(QXmlStreamReader &element, QWidget *w, bool notCondition, QString &error)
110{
111 int indexTag = 0;
112 int indexStr = 0;
113 QString commentStr;
114 while (element.readNextStartElement()) {
115 const QStringView tagName = element.name();
116
117 if (tagName == QLatin1StringView("tag")) {
118 const QString tagValue = element.readElementText();
119 if (indexTag == 0) {
120 QString err;
121 auto selectAddressPart = w->findChild<SelectAddressPartComboBox *>(QStringLiteral("addresspartcombobox"));
122 selectAddressPart->setCode(AutoCreateScriptUtil::tagValue(tagValue), name(), err);
123 // all: is default sometime we don't add it.
124 if (!err.isEmpty()) {
125 auto selectMatchCombobox = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("matchtypecombobox"));
126 selectMatchCombobox->setCode(AutoCreateScriptUtil::tagValueWithCondition(tagValue, notCondition), name(), error);
127 }
128 } else if (indexTag == 1) {
129 auto selectMatchCombobox = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("matchtypecombobox"));
130 selectMatchCombobox->setCode(AutoCreateScriptUtil::tagValueWithCondition(tagValue, notCondition), name(), error);
131 } else {
132 tooManyArguments(tagName, indexTag, 2, error);
133 qCDebug(LIBKSIEVEUI_LOG) << "SieveConditionEnvelope::setParamWidgetValue too many argument :" << indexTag;
134 }
135 ++indexTag;
136 } else if (tagName == QLatin1StringView("str")) {
137 if (indexStr == 0) {
138 auto selectHeaderType = w->findChild<SelectHeaderTypeComboBox *>(QStringLiteral("headertypecombobox"));
139 selectHeaderType->setCode(element.readElementText());
140 } else if (indexStr == 1) {
141 auto edit = w->findChild<AbstractRegexpEditorLineEdit *>(QStringLiteral("editaddress"));
142 edit->setCode(AutoCreateScriptUtil::quoteStr(element.readElementText()));
143 } else {
144 tooManyArguments(tagName, indexStr, 2, error);
145 qCDebug(LIBKSIEVEUI_LOG) << "SieveConditionEnvelope::setParamWidgetValue too many argument indexStr " << indexStr;
146 }
147 ++indexStr;
148 } else if (tagName == QLatin1StringView("list")) {
149 if (indexStr == 0) {
150 auto selectHeaderType = w->findChild<SelectHeaderTypeComboBox *>(QStringLiteral("headertypecombobox"));
151 selectHeaderType->setCode(AutoCreateScriptUtil::listValueToStr(element));
152 } else if (indexStr == 1) {
153 auto edit = w->findChild<AbstractRegexpEditorLineEdit *>(QStringLiteral("editaddress"));
154 edit->setCode(AutoCreateScriptUtil::listValueToStr(element));
155 }
156 ++indexStr;
157 } else if (tagName == QLatin1StringView("crlf")) {
158 element.skipCurrentElement();
159 // nothing
160 } else if (tagName == QLatin1StringView("comment")) {
161 commentStr = AutoCreateScriptUtil::loadConditionComment(commentStr, element.readElementText());
162 } else {
163 unknownTag(tagName, error);
164 qCDebug(LIBKSIEVEUI_LOG) << " SieveConditionEnvelope::setParamWidgetValue unknown tagName " << tagName;
165 }
166 }
167 if (!commentStr.isEmpty()) {
168 setComment(commentStr);
169 }
170}
171
172QUrl KSieveUi::SieveConditionEnvelope::href() const
173{
174 return SieveEditorUtil::helpUrl(SieveEditorUtil::strToVariableName(name()));
175}
176
177#include "moc_sieveconditionenvelope.cpp"
The AbstractRegexpEditorLineEdit class.
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
QString name(StandardAction id)
QByteArray tagValue(const Elem &elem, const char *keyName)
void setContentsMargins(const QMargins &margins)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
T findChild(const QString &name, Qt::FindChildOptions options) const const
QObject * parent() const const
void setObjectName(QAnyStringView name)
bool isEmpty() const const
QString trimmed() const const
QStringView name() const const
QString readElementText(ReadElementTextBehaviour behaviour)
bool readNextStartElement()
void skipCurrentElement()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:14:30 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.