Libksieve

sieveconditionheader.cpp
1 /*
2  SPDX-FileCopyrightText: 2013-2021 Laurent Montel <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 #include "sieveconditionheader.h"
7 #include "autocreatescripts/autocreatescriptutil_p.h"
8 #include "autocreatescripts/commonwidgets/selectmatchtypecombobox.h"
9 #include "editor/sieveeditorutil.h"
10 #include "widgets/selectheadertypecombobox.h"
11 
12 #include <KLocalizedString>
13 
14 #include "libksieve_debug.h"
15 #include <QHBoxLayout>
16 #include <QLabel>
17 #include <QXmlStreamReader>
18 
19 using namespace KSieveUi;
20 
21 SieveConditionHeader::SieveConditionHeader(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QObject *parent)
22  : SieveCondition(sieveGraphicalModeWidget, QStringLiteral("header"), i18n("Header"), parent)
23 {
24 }
25 
26 QWidget *SieveConditionHeader::createParamWidget(QWidget *parent) const
27 {
28  auto w = new QWidget(parent);
29  auto lay = new QHBoxLayout;
30  lay->setContentsMargins({});
31  w->setLayout(lay);
32 
33  auto matchTypeCombo = new SelectMatchTypeComboBox(mSieveGraphicalModeWidget);
34  matchTypeCombo->setObjectName(QStringLiteral("matchtypecombobox"));
35  connect(matchTypeCombo, &SelectMatchTypeComboBox::valueChanged, this, &SieveConditionHeader::valueChanged);
36  lay->addWidget(matchTypeCombo);
37 
38  auto grid = new QGridLayout;
39  lay->addLayout(grid);
40 
41  auto headerType = new SelectHeaderTypeComboBox;
42  headerType->setObjectName(QStringLiteral("headertype"));
43  connect(headerType, &SelectHeaderTypeComboBox::valueChanged, this, &SieveConditionHeader::valueChanged);
44  grid->addWidget(headerType, 0, 0, 1, 2);
45 
46  auto lab = new QLabel(i18n("With value:"));
47  grid->addWidget(lab, 1, 0);
48 
49  AbstractRegexpEditorLineEdit *value = AutoCreateScriptUtil::createRegexpEditorLineEdit();
50  connect(value, &AbstractRegexpEditorLineEdit::textChanged, this, &SieveConditionHeader::valueChanged);
51  connect(matchTypeCombo, &SelectMatchTypeComboBox::switchToRegexp, value, &AbstractRegexpEditorLineEdit::switchToRegexpEditorLineEdit);
52  value->setObjectName(QStringLiteral("value"));
53  grid->addWidget(value, 1, 1);
54  return w;
55 }
56 
57 QString SieveConditionHeader::code(QWidget *w) const
58 {
59  const SelectMatchTypeComboBox *matchTypeCombo = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("matchtypecombobox"));
60  bool isNegative = false;
61  const QString matchString = matchTypeCombo->code(isNegative);
62 
63  const SelectHeaderTypeComboBox *headerType = w->findChild<SelectHeaderTypeComboBox *>(QStringLiteral("headertype"));
64  const QString headerStr = headerType->code();
65 
66  const AbstractRegexpEditorLineEdit *edit = w->findChild<AbstractRegexpEditorLineEdit *>(QStringLiteral("value"));
67  QString valueStr = edit->code();
68 
69  valueStr = AutoCreateScriptUtil::fixListValue(valueStr);
70  return AutoCreateScriptUtil::negativeString(isNegative) + QStringLiteral("header %1 %2 %3").arg(matchString, headerStr, valueStr)
71  + AutoCreateScriptUtil::generateConditionComment(comment());
72 }
73 
74 QString SieveConditionHeader::help() const
75 {
76  return i18n("The \"header\" test evaluates to true if the value of any of the named headers, ignoring leading and trailing whitespace, matches any key.");
77 }
78 
79 void SieveConditionHeader::setParamWidgetValue(QXmlStreamReader &element, QWidget *w, bool notCondition, QString &error)
80 {
81  int index = 0;
82  QString commentStr;
83  while (element.readNextStartElement()) {
84  const QStringRef tagName = element.name();
85 
86  if (tagName == QLatin1String("tag")) {
87  const QString tagValue = element.readElementText();
88  if (tagValue == QLatin1String("comparator")) {
89  qCWarning(LIBKSIEVE_LOG) << " comparator support not implemented yet!";
90  } else {
91  auto selectMatchCombobox = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("matchtypecombobox"));
92  selectMatchCombobox->setCode(AutoCreateScriptUtil::tagValueWithCondition(tagValue, notCondition), name(), error);
93  }
94  } else if (tagName == QLatin1String("str")) {
95  if (index == 0) {
96  auto headerType = w->findChild<SelectHeaderTypeComboBox *>(QStringLiteral("headertype"));
97  headerType->setCode(element.readElementText());
98  } else if (index == 1) {
99  auto value = w->findChild<AbstractRegexpEditorLineEdit *>(QStringLiteral("value"));
100  QString st = AutoCreateScriptUtil::quoteStr(element.readElementText(), true);
101  value->setCode(st);
102  } else {
103  tooManyArguments(tagName, index, 2, error);
104  qCDebug(LIBKSIEVE_LOG) << " SieveConditionHeader::setParamWidgetValue too many argument " << index;
105  }
106  ++index;
107  } else if (tagName == QLatin1String("list")) {
108  // Header list
109  if (index == 0) {
110  auto headerType = w->findChild<SelectHeaderTypeComboBox *>(QStringLiteral("headertype"));
111  headerType->setCode(AutoCreateScriptUtil::listValueToStr(element));
112  } else if (index == 1) {
113  auto value = w->findChild<AbstractRegexpEditorLineEdit *>(QStringLiteral("value"));
114  value->setCode(AutoCreateScriptUtil::listValueToStr(element));
115  } else {
116  tooManyArguments(tagName, index, 2, error);
117  qCDebug(LIBKSIEVE_LOG) << " SieveConditionHeader::setParamWidgetValue too many argument " << index;
118  }
119  ++index;
120  } else if (tagName == QLatin1String("crlf")) {
121  element.skipCurrentElement();
122  // nothing
123  } else if (tagName == QLatin1String("comment")) {
124  commentStr = AutoCreateScriptUtil::loadConditionComment(commentStr, element.readElementText());
125  } else {
126  unknownTag(tagName, error);
127  qCDebug(LIBKSIEVE_LOG) << " SieveConditionHeader::setParamWidgetValue unknown tagName " << tagName;
128  }
129  }
130  if (!commentStr.isEmpty()) {
131  setComment(commentStr);
132  }
133 }
134 
135 QStringList KSieveUi::SieveConditionHeader::needRequires(QWidget *w) const
136 {
137  const SelectMatchTypeComboBox *matchTypeCombo = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("matchtypecombobox"));
138  return matchTypeCombo->needRequires();
139 }
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 i18n(const char *text, const TYPE &arg...)
void addLayout(QLayout *layout, int row, int column, Qt::Alignment alignment)
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.