Libksieve

sieveconditionenvelope.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 "sieveconditionenvelope.h"
20 #include "autocreatescripts/autocreatescriptutil_p.h"
21 
22 #include "widgets/selectaddresspartcombobox.h"
23 #include "autocreatescripts/commonwidgets/selectmatchtypecombobox.h"
24 #include "widgets/selectheadertypecombobox.h"
25 #include "editor/sieveeditorutil.h"
26 
27 #include <KLocalizedString>
28 
29 #include <QHBoxLayout>
30 #include <QLabel>
31 #include "libksieve_debug.h"
32 #include <QXmlStreamReader>
33 
34 using namespace KSieveUi;
35 
36 SieveConditionEnvelope::SieveConditionEnvelope(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QObject *parent)
37  : SieveCondition(sieveGraphicalModeWidget, QStringLiteral("envelope"), i18n("Envelope"), parent)
38 {
39 }
40 
41 QWidget *SieveConditionEnvelope::createParamWidget(QWidget *parent) const
42 {
43  QWidget *w = new QWidget(parent);
44  QHBoxLayout *lay = new QHBoxLayout;
45  lay->setContentsMargins(0, 0, 0, 0);
46  w->setLayout(lay);
47 
48  SelectAddressPartComboBox *selectAddressPart = new SelectAddressPartComboBox(mSieveGraphicalModeWidget);
49  connect(selectAddressPart, &SelectAddressPartComboBox::valueChanged, this, &SieveConditionEnvelope::valueChanged);
50  selectAddressPart->setObjectName(QStringLiteral("addresspartcombobox"));
51  lay->addWidget(selectAddressPart);
52 
53  QGridLayout *grid = new QGridLayout;
54  grid->setContentsMargins(0, 0, 0, 0);
55  lay->addLayout(grid);
56 
57  SelectMatchTypeComboBox *selectMatchCombobox = new SelectMatchTypeComboBox(mSieveGraphicalModeWidget);
58  selectMatchCombobox->setObjectName(QStringLiteral("matchtypecombobox"));
59  connect(selectMatchCombobox, &SelectMatchTypeComboBox::valueChanged, this, &SieveConditionEnvelope::valueChanged);
60  grid->addWidget(selectMatchCombobox, 0, 0);
61 
62  SelectHeaderTypeComboBox *selectHeaderType = new SelectHeaderTypeComboBox(true);
63  selectHeaderType->setObjectName(QStringLiteral("headertypecombobox"));
64  connect(selectHeaderType, &SelectHeaderTypeComboBox::valueChanged, this, &SieveConditionEnvelope::valueChanged);
65  grid->addWidget(selectHeaderType, 0, 1);
66 
67  QLabel *lab = new QLabel(i18n("address:"));
68  grid->addWidget(lab, 1, 0);
69 
70  AbstractRegexpEditorLineEdit *edit = AutoCreateScriptUtil::createRegexpEditorLineEdit();
71  edit->setObjectName(QStringLiteral("editaddress"));
72  connect(edit, &AbstractRegexpEditorLineEdit::textChanged, this, &SieveConditionEnvelope::valueChanged);
73  connect(selectMatchCombobox, &SelectMatchTypeComboBox::switchToRegexp, edit, &AbstractRegexpEditorLineEdit::switchToRegexpEditorLineEdit);
74  edit->setClearButtonEnabled(true);
75  edit->setPlaceholderText(i18n("Use ; to separate emails"));
76  grid->addWidget(edit, 1, 1);
77 
78  return w;
79 }
80 
81 QString SieveConditionEnvelope::code(QWidget *w) const
82 {
83  const SelectMatchTypeComboBox *selectMatchCombobox = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("matchtypecombobox"));
84  bool isNegative = false;
85  const QString matchTypeStr = selectMatchCombobox->code(isNegative);
86 
87  const SelectAddressPartComboBox *selectAddressPart = w->findChild<SelectAddressPartComboBox *>(QStringLiteral("addresspartcombobox"));
88  const QString selectAddressPartStr = selectAddressPart->code();
89 
90  const SelectHeaderTypeComboBox *selectHeaderType = w->findChild<SelectHeaderTypeComboBox *>(QStringLiteral("headertypecombobox"));
91  const QString selectHeaderTypeStr = selectHeaderType->code();
92 
93  const AbstractRegexpEditorLineEdit *edit = w->findChild<AbstractRegexpEditorLineEdit *>(QStringLiteral("editaddress"));
94  const QString addressStr = AutoCreateScriptUtil::createAddressList(edit->code().trimmed(), false);
95  return AutoCreateScriptUtil::negativeString(isNegative) + QStringLiteral("envelope %1 %2 %3 %4").arg(selectAddressPartStr, matchTypeStr, selectHeaderTypeStr, addressStr)
96  + AutoCreateScriptUtil::generateConditionComment(comment());
97 }
98 
99 QStringList SieveConditionEnvelope::needRequires(QWidget *w) const
100 {
101  const SelectAddressPartComboBox *selectAddressPart = w->findChild<SelectAddressPartComboBox *>(QStringLiteral("addresspartcombobox"));
102  const SelectMatchTypeComboBox *selectMatchCombobox = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("matchtypecombobox"));
103  return QStringList() << QStringLiteral("envelope") << selectAddressPart->extraRequire() << selectMatchCombobox->needRequires();
104 }
105 
106 bool SieveConditionEnvelope::needCheckIfServerHasCapability() const
107 {
108  return true;
109 }
110 
111 QString SieveConditionEnvelope::serverNeedsCapability() const
112 {
113  return QStringLiteral("envelope");
114 }
115 
116 QString SieveConditionEnvelope::help() const
117 {
118  return i18n("The \"envelope\" test is true if the specified part of the [SMTP] (or equivalent) envelope matches the specified key.");
119 }
120 
121 void SieveConditionEnvelope::setParamWidgetValue(QXmlStreamReader &element, QWidget *w, bool notCondition, QString &error)
122 {
123  int indexTag = 0;
124  int indexStr = 0;
125  QString commentStr;
126  while (element.readNextStartElement()) {
127  const QStringRef tagName = element.name();
128 
129  if (tagName == QLatin1String("tag")) {
130  const QString tagValue = element.readElementText();
131  if (indexTag == 0) {
132  QString err;
133  SelectAddressPartComboBox *selectAddressPart = w->findChild<SelectAddressPartComboBox *>(QStringLiteral("addresspartcombobox"));
134  selectAddressPart->setCode(AutoCreateScriptUtil::tagValue(tagValue), name(), err);
135  //all: is default sometime we don't add it.
136  if (!err.isEmpty()) {
137  SelectMatchTypeComboBox *selectMatchCombobox = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("matchtypecombobox"));
138  selectMatchCombobox->setCode(AutoCreateScriptUtil::tagValueWithCondition(tagValue, notCondition), name(), error);
139  }
140  } else if (indexTag == 1) {
141  SelectMatchTypeComboBox *selectMatchCombobox = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("matchtypecombobox"));
142  selectMatchCombobox->setCode(AutoCreateScriptUtil::tagValueWithCondition(tagValue, notCondition), name(), error);
143  } else {
144  tooManyArguments(tagName, indexTag, 2, error);
145  qCDebug(LIBKSIEVE_LOG) << "SieveConditionEnvelope::setParamWidgetValue too many argument :" << indexTag;
146  }
147  ++indexTag;
148  } else if (tagName == QLatin1String("str")) {
149  if (indexStr == 0) {
150  SelectHeaderTypeComboBox *selectHeaderType = w->findChild<SelectHeaderTypeComboBox *>(QStringLiteral("headertypecombobox"));
151  selectHeaderType->setCode(element.readElementText());
152  } else if (indexStr == 1) {
153  AbstractRegexpEditorLineEdit *edit = w->findChild<AbstractRegexpEditorLineEdit *>(QStringLiteral("editaddress"));
154  edit->setCode(AutoCreateScriptUtil::quoteStr(element.readElementText()));
155  } else {
156  tooManyArguments(tagName, indexStr, 2, error);
157  qCDebug(LIBKSIEVE_LOG) << "SieveConditionEnvelope::setParamWidgetValue too many argument indexStr " << indexStr;
158  }
159  ++indexStr;
160  } else if (tagName == QLatin1String("list")) {
161  if (indexStr == 0) {
162  SelectHeaderTypeComboBox *selectHeaderType = w->findChild<SelectHeaderTypeComboBox *>(QStringLiteral("headertypecombobox"));
163  selectHeaderType->setCode(AutoCreateScriptUtil::listValueToStr(element));
164  } else if (indexStr == 1) {
165  AbstractRegexpEditorLineEdit *edit = w->findChild<AbstractRegexpEditorLineEdit *>(QStringLiteral("editaddress"));
166  edit->setCode(AutoCreateScriptUtil::listValueToStr(element));
167  }
168  ++indexStr;
169  } else if (tagName == QLatin1String("crlf")) {
170  element.skipCurrentElement();
171  //nothing
172  } else if (tagName == QLatin1String("comment")) {
173  commentStr = AutoCreateScriptUtil::loadConditionComment(commentStr, element.readElementText());
174  } else {
175  unknownTag(tagName, error);
176  qCDebug(LIBKSIEVE_LOG) << " SieveConditionEnvelope::setParamWidgetValue unknown tagName " << tagName;
177  }
178  }
179  if (!commentStr.isEmpty()) {
180  setComment(commentStr);
181  }
182 }
183 
184 QUrl KSieveUi::SieveConditionEnvelope::href() const
185 {
186  return SieveEditorUtil::helpUrl(SieveEditorUtil::strToVariableName(name()));
187 }
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 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
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 Fri Aug 7 2020 23:06:34 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.