Libksieve

sieveconditionbody.cpp
1 /*
2  SPDX-FileCopyrightText: 2013-2021 Laurent Montel <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 #include "sieveconditionbody.h"
7 #include "autocreatescripts/autocreatescriptutil_p.h"
8 #include "autocreatescripts/commonwidgets/selectmatchtypecombobox.h"
9 #include "editor/sieveeditorutil.h"
10 #include "widgets/selectbodytypewidget.h"
11 
12 #include <KLocalizedString>
13 
14 #include "libksieve_debug.h"
15 #include <QHBoxLayout>
16 #include <QWidget>
17 
18 using namespace KSieveUi;
19 SieveConditionBody::SieveConditionBody(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QObject *parent)
20  : SieveCondition(sieveGraphicalModeWidget, QStringLiteral("body"), i18n("Body"), parent)
21 {
22 }
23 
24 QWidget *SieveConditionBody::createParamWidget(QWidget *parent) const
25 {
26  auto w = new QWidget(parent);
27  auto lay = new QHBoxLayout;
28  lay->setContentsMargins({});
29  w->setLayout(lay);
30 
31  auto bodyType = new SelectBodyTypeWidget;
32  bodyType->setObjectName(QStringLiteral("bodytype"));
33  connect(bodyType, &SelectBodyTypeWidget::valueChanged, this, &SieveConditionBody::valueChanged);
34  lay->addWidget(bodyType);
35 
36  auto matchType = new SelectMatchTypeComboBox(mSieveGraphicalModeWidget);
37  lay->addWidget(matchType);
38  matchType->setObjectName(QStringLiteral("matchtype"));
39  connect(matchType, &SelectMatchTypeComboBox::valueChanged, this, &SieveConditionBody::valueChanged);
40 
41  AbstractRegexpEditorLineEdit *edit = AutoCreateScriptUtil::createRegexpEditorLineEdit();
42  connect(edit, &AbstractRegexpEditorLineEdit::textChanged, this, &SieveConditionBody::valueChanged);
43  connect(matchType, &SelectMatchTypeComboBox::switchToRegexp, edit, &AbstractRegexpEditorLineEdit::switchToRegexpEditorLineEdit);
44  edit->setClearButtonEnabled(true);
45  lay->addWidget(edit);
46  edit->setObjectName(QStringLiteral("edit"));
47 
48  return w;
49 }
50 
51 QString SieveConditionBody::code(QWidget *w) const
52 {
53  const SelectBodyTypeWidget *bodyType = w->findChild<SelectBodyTypeWidget *>(QStringLiteral("bodytype"));
54  const QString bodyValue = bodyType->code();
55  const SelectMatchTypeComboBox *matchType = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("matchtype"));
56  bool isNegative = false;
57  const QString matchValue = matchType->code(isNegative);
58 
59  const AbstractRegexpEditorLineEdit *edit = w->findChild<AbstractRegexpEditorLineEdit *>(QStringLiteral("edit"));
60  const QString editValue = AutoCreateScriptUtil::createAddressList(edit->code().trimmed(), false);
61  return AutoCreateScriptUtil::negativeString(isNegative) + QStringLiteral("body %1 %2 %3").arg(bodyValue, matchValue, editValue)
62  + AutoCreateScriptUtil::generateConditionComment(comment());
63 }
64 
65 QStringList SieveConditionBody::needRequires(QWidget *w) const
66 {
67  const SelectMatchTypeComboBox *matchType = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("matchtype"));
68 
69  return QStringList() << QStringLiteral("body") << matchType->needRequires();
70 }
71 
72 bool SieveConditionBody::needCheckIfServerHasCapability() const
73 {
74  return true;
75 }
76 
77 QString SieveConditionBody::serverNeedsCapability() const
78 {
79  return QStringLiteral("body");
80 }
81 
82 QString SieveConditionBody::help() const
83 {
84  return i18n(
85  "The body test matches content in the body of an email message, that is, anything following the first empty line after the header. (The empty line "
86  "itself, if present, is not considered to be part of the body.)");
87 }
88 
89 void SieveConditionBody::setParamWidgetValue(QXmlStreamReader &element, QWidget *w, bool notCondition, QString &error)
90 {
91  int index = 0;
92  int indexStr = 0;
93  QStringList tagValueList;
94  QStringList strValue;
95 
96  bool wasListElement = false;
97  QString commentStr;
98  while (element.readNextStartElement()) {
99  const QStringRef tagName = element.name();
100  if (tagName == QLatin1String("tag")) {
101  const QString tagValue = element.readElementText();
102  if (index == 0) {
103  tagValueList << AutoCreateScriptUtil::tagValue(tagValue);
104  } else if (index == 1) {
105  tagValueList << AutoCreateScriptUtil::tagValueWithCondition(tagValue, notCondition);
106  } else {
107  tooManyArguments(tagName, index, 2, error);
108  qCDebug(LIBKSIEVE_LOG) << " SieveConditionBody::setParamWidgetValue too many argument " << index;
109  }
110  ++index;
111  } else if (tagName == QLatin1String("str")) {
112  strValue << element.readElementText();
113  ++indexStr;
114  } else if (tagName == QLatin1String("crlf")) {
115  element.skipCurrentElement();
116  // nothing
117  } else if (tagName == QLatin1String("comment")) {
118  commentStr = AutoCreateScriptUtil::loadConditionComment(commentStr, element.readElementText());
119  } else if (tagName == QLatin1String("list")) {
120  strValue << AutoCreateScriptUtil::listValueToStr(element);
121  wasListElement = true;
122  ++indexStr;
123  } else {
124  unknownTag(tagName, error);
125  qCDebug(LIBKSIEVE_LOG) << " SieveConditionBody::setParamWidgetValue unknown tagName " << tagName;
126  }
127  }
128  if (!commentStr.isEmpty()) {
129  setComment(commentStr);
130  }
131  QString errorStr;
132  if (strValue.count() == 1) {
133  auto bodyType = w->findChild<SelectBodyTypeWidget *>(QStringLiteral("bodytype"));
134  bodyType->setCode(tagValueList.at(0), QString(), name(), errorStr);
135  if (errorStr.isEmpty()) {
136  auto matchType = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("matchtype"));
137  matchType->setCode(tagValueList.at(1), name(), error);
138  } else {
139  auto matchType = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("matchtype"));
140  if (tagValueList.count() == 1) {
141  matchType->setCode(tagValueList.at(0), name(), error);
142  } else if (tagValueList.count() == 2) {
143  matchType->setCode(tagValueList.at(0), name(), error);
144  bodyType->setCode(tagValueList.at(1), QString(), name(), errorStr);
145  }
146  }
147  auto edit = w->findChild<AbstractRegexpEditorLineEdit *>(QStringLiteral("edit"));
148  edit->setCode(wasListElement ? strValue.at(0) : AutoCreateScriptUtil::quoteStr(strValue.at(0)));
149  } else if (strValue.count() == 2) {
150  auto bodyType = w->findChild<SelectBodyTypeWidget *>(QStringLiteral("bodytype"));
151  bodyType->setCode(tagValueList.at(0), indexStr == 2 ? strValue.at(0) : QString(), name(), errorStr);
152  auto matchType = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("matchtype"));
153  if (!errorStr.isEmpty()) {
154  matchType->setCode(tagValueList.at(0), name(), error);
155  bodyType->setCode(tagValueList.at(1), indexStr == 2 ? strValue.at(0) : QString(), name(), error);
156  } else {
157  matchType->setCode(tagValueList.at(1), name(), error);
158  }
159  auto edit = w->findChild<AbstractRegexpEditorLineEdit *>(QStringLiteral("edit"));
160  edit->setCode(indexStr == 1 ? AutoCreateScriptUtil::quoteStr(strValue.at(0)) : AutoCreateScriptUtil::quoteStr(strValue.at(1)));
161  }
162 }
163 
164 QUrl SieveConditionBody::href() const
165 {
166  return SieveEditorUtil::helpUrl(SieveEditorUtil::strToVariableName(name()));
167 }
bool readNextStartElement()
void setContentsMargins(int left, int top, int right, int bottom)
QString name(const QVariant &location)
const T & at(int i) const const
void skipCurrentElement()
int count(const T &value) const const
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 Sat Apr 17 2021 23:09:34 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.