Libksieve

sieveconditionheader.cpp
1 /*
2  Copyright (C) 2013-2020 Laurent Montel <[email protected]>
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Library General Public
6  License as published by the Free Software Foundation; either
7  version 2 of the License, or (at your option) any later version.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Library General Public License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to
16  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  Boston, MA 02110-1301, USA.
18 */
19 #include "sieveconditionheader.h"
20 #include "autocreatescripts/commonwidgets/selectmatchtypecombobox.h"
21 #include "autocreatescripts/autocreatescriptutil_p.h"
22 #include "widgets/selectheadertypecombobox.h"
23 #include "editor/sieveeditorutil.h"
24 
25 #include <KLocalizedString>
26 
27 #include <QHBoxLayout>
28 #include <QLabel>
29 #include <QXmlStreamReader>
30 #include "libksieve_debug.h"
31 
32 using namespace KSieveUi;
33 
34 SieveConditionHeader::SieveConditionHeader(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QObject *parent)
35  : SieveCondition(sieveGraphicalModeWidget, QStringLiteral("header"), i18n("Header"), parent)
36 {
37 }
38 
39 QWidget *SieveConditionHeader::createParamWidget(QWidget *parent) const
40 {
41  QWidget *w = new QWidget(parent);
42  QHBoxLayout *lay = new QHBoxLayout;
43  lay->setContentsMargins(0, 0, 0, 0);
44  w->setLayout(lay);
45 
46  SelectMatchTypeComboBox *matchTypeCombo = new SelectMatchTypeComboBox(mSieveGraphicalModeWidget);
47  matchTypeCombo->setObjectName(QStringLiteral("matchtypecombobox"));
48  connect(matchTypeCombo, &SelectMatchTypeComboBox::valueChanged, this, &SieveConditionHeader::valueChanged);
49  lay->addWidget(matchTypeCombo);
50 
51  QGridLayout *grid = new QGridLayout;
52  lay->addLayout(grid);
53 
54  SelectHeaderTypeComboBox *headerType = new SelectHeaderTypeComboBox;
55  headerType->setObjectName(QStringLiteral("headertype"));
56  connect(headerType, &SelectHeaderTypeComboBox::valueChanged, this, &SieveConditionHeader::valueChanged);
57  grid->addWidget(headerType, 0, 0, 1, 2);
58 
59  QLabel *lab = new QLabel(i18n("With value:"));
60  grid->addWidget(lab, 1, 0);
61 
62  AbstractRegexpEditorLineEdit *value = AutoCreateScriptUtil::createRegexpEditorLineEdit();
63  connect(value, &AbstractRegexpEditorLineEdit::textChanged, this, &SieveConditionHeader::valueChanged);
64  connect(matchTypeCombo, &SelectMatchTypeComboBox::switchToRegexp, value, &AbstractRegexpEditorLineEdit::switchToRegexpEditorLineEdit);
65  value->setObjectName(QStringLiteral("value"));
66  grid->addWidget(value, 1, 1);
67  return w;
68 }
69 
70 QString SieveConditionHeader::code(QWidget *w) const
71 {
72  const SelectMatchTypeComboBox *matchTypeCombo = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("matchtypecombobox"));
73  bool isNegative = false;
74  const QString matchString = matchTypeCombo->code(isNegative);
75 
76  const SelectHeaderTypeComboBox *headerType = w->findChild<SelectHeaderTypeComboBox *>(QStringLiteral("headertype"));
77  const QString headerStr = headerType->code();
78 
79  const AbstractRegexpEditorLineEdit *edit = w->findChild<AbstractRegexpEditorLineEdit *>(QStringLiteral("value"));
80  QString valueStr = edit->code();
81 
82  valueStr = AutoCreateScriptUtil::fixListValue(valueStr);
83  return AutoCreateScriptUtil::negativeString(isNegative) + QStringLiteral("header %1 %2 %3").arg(matchString, headerStr, valueStr)
84  + AutoCreateScriptUtil::generateConditionComment(comment());
85 }
86 
87 QString SieveConditionHeader::help() const
88 {
89  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.");
90 }
91 
92 void SieveConditionHeader::setParamWidgetValue(QXmlStreamReader &element, QWidget *w, bool notCondition, QString &error)
93 {
94  int index = 0;
95  QString commentStr;
96  while (element.readNextStartElement()) {
97  const QStringRef tagName = element.name();
98 
99  if (tagName == QLatin1String("tag")) {
100  const QString tagValue = element.readElementText();
101  if (tagValue == QLatin1String("comparator")) {
102  qCWarning(LIBKSIEVE_LOG) << " comparator support not implemented yet!";
103  } else {
104  SelectMatchTypeComboBox *selectMatchCombobox = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("matchtypecombobox"));
105  selectMatchCombobox->setCode(AutoCreateScriptUtil::tagValueWithCondition(tagValue, notCondition), name(), error);
106  }
107  } else if (tagName == QLatin1String("str")) {
108  if (index == 0) {
109  SelectHeaderTypeComboBox *headerType = w->findChild<SelectHeaderTypeComboBox *>(QStringLiteral("headertype"));
110  headerType->setCode(element.readElementText());
111  } else if (index == 1) {
112  AbstractRegexpEditorLineEdit *value = w->findChild<AbstractRegexpEditorLineEdit *>(QStringLiteral("value"));
113  QString st = AutoCreateScriptUtil::quoteStr(element.readElementText(), true);
114  value->setCode(st);
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("list")) {
121  //Header list
122  if (index == 0) {
123  SelectHeaderTypeComboBox *headerType = w->findChild<SelectHeaderTypeComboBox *>(QStringLiteral("headertype"));
124  headerType->setCode(AutoCreateScriptUtil::listValueToStr(element));
125  } else if (index == 1) {
126  AbstractRegexpEditorLineEdit *value = w->findChild<AbstractRegexpEditorLineEdit *>(QStringLiteral("value"));
127  value->setCode(AutoCreateScriptUtil::listValueToStr(element));
128  } else {
129  tooManyArguments(tagName, index, 2, error);
130  qCDebug(LIBKSIEVE_LOG) << " SieveConditionHeader::setParamWidgetValue too many argument " << index;
131  }
132  ++index;
133  } else if (tagName == QLatin1String("crlf")) {
134  element.skipCurrentElement();
135  //nothing
136  } else if (tagName == QLatin1String("comment")) {
137  commentStr = AutoCreateScriptUtil::loadConditionComment(commentStr, element.readElementText());
138  } else {
139  unknownTag(tagName, error);
140  qCDebug(LIBKSIEVE_LOG) << " SieveConditionHeader::setParamWidgetValue unknown tagName " << tagName;
141  }
142  }
143  if (!commentStr.isEmpty()) {
144  setComment(commentStr);
145  }
146 }
147 
148 QStringList KSieveUi::SieveConditionHeader::needRequires(QWidget *w) const
149 {
150  const SelectMatchTypeComboBox *matchTypeCombo = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("matchtypecombobox"));
151  return matchTypeCombo->needRequires();
152 }
bool readNextStartElement()
void setContentsMargins(int left, int top, int right, int bottom)
QString name(const QVariant &location)
void addWidget(QWidget *widget, int row, int column, Qt::Alignment alignment)
void skipCurrentElement()
void addWidget(QWidget *widget, int stretch, Qt::Alignment alignment)
The AbstractRegexpEditorLineEdit class.
void setLayout(QLayout *layout)
QString readElementText(QXmlStreamReader::ReadElementTextBehaviour behaviour)
void setObjectName(const QString &name)
bool isEmpty() const const
QString i18n(const char *text, const TYPE &arg...)
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
void addLayout(QLayout *layout, int stretch)
T findChild(const QString &name, Qt::FindChildOptions options) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Aug 10 2020 23:12:59 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.