Libksieve

sieveconditionbody.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 "sieveconditionbody.h"
20 #include "widgets/selectbodytypewidget.h"
21 #include "autocreatescripts/commonwidgets/selectmatchtypecombobox.h"
22 #include "autocreatescripts/autocreatescriptutil_p.h"
23 #include "editor/sieveeditorutil.h"
24 
25 #include <KLocalizedString>
26 
27 #include <QWidget>
28 #include <QHBoxLayout>
29 #include "libksieve_debug.h"
30 
31 using namespace KSieveUi;
32 SieveConditionBody::SieveConditionBody(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QObject *parent)
33  : SieveCondition(sieveGraphicalModeWidget, QStringLiteral("body"), i18n("Body"), parent)
34 {
35 }
36 
37 QWidget *SieveConditionBody::createParamWidget(QWidget *parent) const
38 {
39  QWidget *w = new QWidget(parent);
40  QHBoxLayout *lay = new QHBoxLayout;
41  lay->setContentsMargins(0, 0, 0, 0);
42  w->setLayout(lay);
43 
44  SelectBodyTypeWidget *bodyType = new SelectBodyTypeWidget;
45  bodyType->setObjectName(QStringLiteral("bodytype"));
46  connect(bodyType, &SelectBodyTypeWidget::valueChanged, this, &SieveConditionBody::valueChanged);
47  lay->addWidget(bodyType);
48 
49  SelectMatchTypeComboBox *matchType = new SelectMatchTypeComboBox(mSieveGraphicalModeWidget);
50  lay->addWidget(matchType);
51  matchType->setObjectName(QStringLiteral("matchtype"));
52  connect(matchType, &SelectMatchTypeComboBox::valueChanged, this, &SieveConditionBody::valueChanged);
53 
54  AbstractRegexpEditorLineEdit *edit = AutoCreateScriptUtil::createRegexpEditorLineEdit();
55  connect(edit, &AbstractRegexpEditorLineEdit::textChanged, this, &SieveConditionBody::valueChanged);
56  connect(matchType, &SelectMatchTypeComboBox::switchToRegexp, edit, &AbstractRegexpEditorLineEdit::switchToRegexpEditorLineEdit);
57  edit->setClearButtonEnabled(true);
58  lay->addWidget(edit);
59  edit->setObjectName(QStringLiteral("edit"));
60 
61  return w;
62 }
63 
64 QString SieveConditionBody::code(QWidget *w) const
65 {
66  const SelectBodyTypeWidget *bodyType = w->findChild<SelectBodyTypeWidget *>(QStringLiteral("bodytype"));
67  const QString bodyValue = bodyType->code();
68  const SelectMatchTypeComboBox *matchType = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("matchtype"));
69  bool isNegative = false;
70  const QString matchValue = matchType->code(isNegative);
71 
72  const AbstractRegexpEditorLineEdit *edit = w->findChild<AbstractRegexpEditorLineEdit *>(QStringLiteral("edit"));
73  const QString editValue = AutoCreateScriptUtil::createAddressList(edit->code().trimmed(), false);
74  return AutoCreateScriptUtil::negativeString(isNegative) + QStringLiteral("body %1 %2 %3").arg(bodyValue, matchValue, editValue)
75  + AutoCreateScriptUtil::generateConditionComment(comment());
76 }
77 
78 QStringList SieveConditionBody::needRequires(QWidget *w) const
79 {
80  const SelectMatchTypeComboBox *matchType = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("matchtype"));
81 
82  return QStringList() << QStringLiteral("body") << matchType->needRequires();
83 }
84 
85 bool SieveConditionBody::needCheckIfServerHasCapability() const
86 {
87  return true;
88 }
89 
90 QString SieveConditionBody::serverNeedsCapability() const
91 {
92  return QStringLiteral("body");
93 }
94 
95 QString SieveConditionBody::help() const
96 {
97  return i18n(
98  "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 itself, if present, is not considered to be part of the body.)");
99 }
100 
101 void SieveConditionBody::setParamWidgetValue(QXmlStreamReader &element, QWidget *w, bool notCondition, QString &error)
102 {
103  int index = 0;
104  int indexStr = 0;
105  QStringList tagValueList;
106  QStringList strValue;
107 
108  bool wasListElement = false;
109  QString commentStr;
110  while (element.readNextStartElement()) {
111  const QStringRef tagName = element.name();
112  if (tagName == QLatin1String("tag")) {
113  const QString tagValue = element.readElementText();
114  if (index == 0) {
115  tagValueList << AutoCreateScriptUtil::tagValue(tagValue);
116  } else if (index == 1) {
117  tagValueList << AutoCreateScriptUtil::tagValueWithCondition(tagValue, notCondition);
118  } else {
119  tooManyArguments(tagName, index, 2, error);
120  qCDebug(LIBKSIEVE_LOG) << " SieveConditionBody::setParamWidgetValue too many argument " << index;
121  }
122  ++index;
123  } else if (tagName == QLatin1String("str")) {
124  strValue << element.readElementText();
125  ++indexStr;
126  } else if (tagName == QLatin1String("crlf")) {
127  element.skipCurrentElement();
128  //nothing
129  } else if (tagName == QLatin1String("comment")) {
130  commentStr = AutoCreateScriptUtil::loadConditionComment(commentStr, element.readElementText());
131  } else if (tagName == QLatin1String("list")) {
132  strValue << AutoCreateScriptUtil::listValueToStr(element);
133  wasListElement = true;
134  ++indexStr;
135  } else {
136  unknownTag(tagName, error);
137  qCDebug(LIBKSIEVE_LOG) << " SieveConditionBody::setParamWidgetValue unknown tagName " << tagName;
138  }
139  }
140  if (!commentStr.isEmpty()) {
141  setComment(commentStr);
142  }
143  QString errorStr;
144  if (strValue.count() == 1) {
145  SelectBodyTypeWidget *bodyType = w->findChild<SelectBodyTypeWidget *>(QStringLiteral("bodytype"));
146  bodyType->setCode(tagValueList.at(0), QString(), name(), errorStr);
147  if (errorStr.isEmpty()) {
148  SelectMatchTypeComboBox *matchType = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("matchtype"));
149  matchType->setCode(tagValueList.at(1), name(), error);
150  } else {
151  SelectMatchTypeComboBox *matchType = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("matchtype"));
152  if (tagValueList.count() == 1) {
153  matchType->setCode(tagValueList.at(0), name(), error);
154  } else if (tagValueList.count() == 2) {
155  matchType->setCode(tagValueList.at(0), name(), error);
156  bodyType->setCode(tagValueList.at(1), QString(), name(), errorStr);
157  }
158  }
159  AbstractRegexpEditorLineEdit *edit = w->findChild<AbstractRegexpEditorLineEdit *>(QStringLiteral("edit"));
160  edit->setCode(wasListElement ? strValue.at(0) : AutoCreateScriptUtil::quoteStr(strValue.at(0)));
161  } else if (strValue.count() == 2) {
162  SelectBodyTypeWidget *bodyType = w->findChild<SelectBodyTypeWidget *>(QStringLiteral("bodytype"));
163  bodyType->setCode(tagValueList.at(0), indexStr == 2 ? strValue.at(0) : QString(), name(), errorStr);
164  SelectMatchTypeComboBox *matchType = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("matchtype"));
165  if (!errorStr.isEmpty()) {
166  matchType->setCode(tagValueList.at(0), name(), error);
167  bodyType->setCode(tagValueList.at(1), indexStr == 2 ? strValue.at(0) : QString(), name(), error);
168  } else {
169  matchType->setCode(tagValueList.at(1), name(), error);
170  }
171  AbstractRegexpEditorLineEdit *edit = w->findChild<AbstractRegexpEditorLineEdit *>(QStringLiteral("edit"));
172  edit->setCode(indexStr == 1 ? AutoCreateScriptUtil::quoteStr(strValue.at(0)) : AutoCreateScriptUtil::quoteStr(strValue.at(1)));
173  }
174 }
175 
176 QUrl SieveConditionBody::href() const
177 {
178  return SieveEditorUtil::helpUrl(SieveEditorUtil::strToVariableName(name()));
179 }
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()
void addWidget(QWidget *widget, int stretch, Qt::Alignment alignment)
int count(const T &value) const const
The AbstractRegexpEditorLineEdit class.
void setLayout(QLayout *layout)
QString readElementText(QXmlStreamReader::ReadElementTextBehaviour behaviour)
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-2020 The KDE developers.
Generated on Thu Jul 9 2020 23:07:44 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.