Libksieve

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

KDE's Doxygen guidelines are available online.