Libksieve

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

KDE's Doxygen guidelines are available online.