Libksieve

sieveconditionenvelope.cpp
1 /*
2  SPDX-FileCopyrightText: 2013-2021 Laurent Montel <[email protected]>
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 "libksieve_debug.h"
17 #include <QHBoxLayout>
18 #include <QLabel>
19 #include <QXmlStreamReader>
20 
21 using namespace KSieveUi;
22 
23 SieveConditionEnvelope::SieveConditionEnvelope(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QObject *parent)
24  : SieveCondition(sieveGraphicalModeWidget, QStringLiteral("envelope"), i18n("Envelope"), parent)
25 {
26 }
27 
28 QWidget *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(QStringLiteral("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(QStringLiteral("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(QStringLiteral("headertypecombobox"));
51  connect(selectHeaderType, &SelectHeaderTypeComboBox::valueChanged, this, &SieveConditionEnvelope::valueChanged);
52  grid->addWidget(selectHeaderType, 0, 1);
53 
54  auto lab = new QLabel(i18n("address:"));
55  grid->addWidget(lab, 1, 0);
56 
57  AbstractRegexpEditorLineEdit *edit = AutoCreateScriptUtil::createRegexpEditorLineEdit();
58  edit->setObjectName(QStringLiteral("editaddress"));
59  connect(edit, &AbstractRegexpEditorLineEdit::textChanged, this, &SieveConditionEnvelope::valueChanged);
60  connect(selectMatchCombobox, &SelectMatchTypeComboBox::switchToRegexp, edit, &AbstractRegexpEditorLineEdit::switchToRegexpEditorLineEdit);
61  edit->setClearButtonEnabled(true);
62  edit->setPlaceholderText(i18n("Use ; to separate emails"));
63  grid->addWidget(edit, 1, 1);
64 
65  return w;
66 }
67 
68 QString 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 
87 QStringList 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 
94 bool SieveConditionEnvelope::needCheckIfServerHasCapability() const
95 {
96  return true;
97 }
98 
99 QString SieveConditionEnvelope::serverNeedsCapability() const
100 {
101  return QStringLiteral("envelope");
102 }
103 
104 QString 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 
109 void 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 QStringRef tagName = element.name();
116 
117  if (tagName == QLatin1String("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(LIBKSIEVE_LOG) << "SieveConditionEnvelope::setParamWidgetValue too many argument :" << indexTag;
134  }
135  ++indexTag;
136  } else if (tagName == QLatin1String("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(LIBKSIEVE_LOG) << "SieveConditionEnvelope::setParamWidgetValue too many argument indexStr " << indexStr;
146  }
147  ++indexStr;
148  } else if (tagName == QLatin1String("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 == QLatin1String("crlf")) {
158  element.skipCurrentElement();
159  // nothing
160  } else if (tagName == QLatin1String("comment")) {
161  commentStr = AutoCreateScriptUtil::loadConditionComment(commentStr, element.readElementText());
162  } else {
163  unknownTag(tagName, error);
164  qCDebug(LIBKSIEVE_LOG) << " SieveConditionEnvelope::setParamWidgetValue unknown tagName " << tagName;
165  }
166  }
167  if (!commentStr.isEmpty()) {
168  setComment(commentStr);
169  }
170 }
171 
172 QUrl KSieveUi::SieveConditionEnvelope::href() const
173 {
174  return SieveEditorUtil::helpUrl(SieveEditorUtil::strToVariableName(name()));
175 }
bool readNextStartElement()
void setContentsMargins(int left, int top, int right, int bottom)
QString name(const QVariant &location)
void skipCurrentElement()
The AbstractRegexpEditorLineEdit class.
QString readElementText(QXmlStreamReader::ReadElementTextBehaviour behaviour)
QByteArray tagValue(const Elem &elem, TagKey key)
void setObjectName(const QString &name)
bool isEmpty() const const
QString trimmed() const const
QString i18n(const char *text, const TYPE &arg...)
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QObject * parent() const const
QStringRef name() const const
T findChild(const QString &name, Qt::FindChildOptions options) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Fri Apr 16 2021 23:09:34 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.