Libksieve

sieveconditionservermetadata.cpp
1 /*
2  SPDX-FileCopyrightText: 2013-2021 Laurent Montel <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 #include "sieveconditionservermetadata.h"
7 #include "autocreatescripts/autocreatescriptutil_p.h"
8 #include "autocreatescripts/commonwidgets/selectmatchtypecombobox.h"
9 #include "editor/sieveeditorutil.h"
10 
11 #include <KLocalizedString>
12 #include <Libkdepim/LineEditCatchReturnKey>
13 #include <QLineEdit>
14 
15 #include "libksieve_debug.h"
16 #include <QHBoxLayout>
17 #include <QLabel>
18 #include <QWidget>
19 #include <QXmlStreamReader>
20 
21 using namespace KSieveUi;
22 SieveConditionServerMetaData::SieveConditionServerMetaData(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QObject *parent)
23  : SieveCondition(sieveGraphicalModeWidget, QStringLiteral("servermetadata"), i18n("Server Meta Data"), parent)
24 {
25 }
26 
27 QWidget *SieveConditionServerMetaData::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 selectType = new SelectMatchTypeComboBox(mSieveGraphicalModeWidget);
35  selectType->setObjectName(QStringLiteral("selecttype"));
36  connect(selectType, &SelectMatchTypeComboBox::valueChanged, this, &SieveConditionServerMetaData::valueChanged);
37  lay->addWidget(selectType);
38 
39  auto grid = new QGridLayout;
40  grid->setContentsMargins({});
41  lay->addLayout(grid);
42 
43  auto lab = new QLabel(i18n("Mailbox:"));
44  grid->addWidget(lab, 0, 0);
45 
46  auto mailbox = new QLineEdit;
47  new KPIM::LineEditCatchReturnKey(mailbox, w);
48  mailbox->setObjectName(QStringLiteral("mailbox"));
49  connect(mailbox, &QLineEdit::textChanged, this, &SieveConditionServerMetaData::valueChanged);
50  grid->addWidget(mailbox, 0, 1);
51 
52  lab = new QLabel(i18n("Annotations:"));
53  grid->addWidget(lab, 1, 0);
54 
55  auto annotation = new QLineEdit;
56  new KPIM::LineEditCatchReturnKey(annotation, w);
57  connect(annotation, &QLineEdit::textChanged, this, &SieveConditionServerMetaData::valueChanged);
58  annotation->setObjectName(QStringLiteral("annotation"));
59  grid->addWidget(annotation, 1, 1);
60 
61  lab = new QLabel(i18n("Value:"));
62  grid->addWidget(lab, 2, 0);
63 
64  AbstractRegexpEditorLineEdit *value = AutoCreateScriptUtil::createRegexpEditorLineEdit();
65  connect(value, &AbstractRegexpEditorLineEdit::textChanged, this, &SieveConditionServerMetaData::valueChanged);
66  connect(selectType, &SelectMatchTypeComboBox::switchToRegexp, value, &AbstractRegexpEditorLineEdit::switchToRegexpEditorLineEdit);
67  value->setObjectName(QStringLiteral("value"));
68  grid->addWidget(value, 2, 1);
69 
70  return w;
71 }
72 
73 QString SieveConditionServerMetaData::code(QWidget *w) const
74 {
75  const SelectMatchTypeComboBox *selectType = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("selecttype"));
76  bool isNegative = false;
77  const QString matchString = selectType->code(isNegative);
78 
79  QString result = (isNegative ? QStringLiteral("not ") : QString()) + QStringLiteral("servermetadata %1 ").arg(matchString);
80 
81  const QLineEdit *mailbox = w->findChild<QLineEdit *>(QStringLiteral("mailbox"));
82  const QString mailboxStr = mailbox->text();
83 
84  result += QStringLiteral("\"%1\" ").arg(mailboxStr);
85 
86  const QLineEdit *annotation = w->findChild<QLineEdit *>(QStringLiteral("annotation"));
87  const QString annotationStr = annotation->text();
88 
89  result += QStringLiteral("\"%1\" ").arg(annotationStr);
90 
91  const AbstractRegexpEditorLineEdit *value = w->findChild<AbstractRegexpEditorLineEdit *>(QStringLiteral("value"));
92  const QString valueStr = value->code();
93 
94  result += QStringLiteral("\"%1\"").arg(valueStr);
95  return result + AutoCreateScriptUtil::generateConditionComment(comment());
96 }
97 
98 QStringList SieveConditionServerMetaData::needRequires(QWidget *w) const
99 {
100  const SelectMatchTypeComboBox *selectType = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("selecttype"));
101  return QStringList() << QStringLiteral("servermetadata") << selectType->needRequires();
102 }
103 
104 bool SieveConditionServerMetaData::needCheckIfServerHasCapability() const
105 {
106  return true;
107 }
108 
109 QString SieveConditionServerMetaData::serverNeedsCapability() const
110 {
111  return QStringLiteral("servermetadata");
112 }
113 
114 QString SieveConditionServerMetaData::help() const
115 {
116  return i18n(
117  "This test retrieves the value of the server annotation \"annotation-name\". The retrieved value is compared to the \"key-list\". The test returns "
118  "true if the annotation exists and its value matches any of the keys.");
119 }
120 
121 void SieveConditionServerMetaData::setParamWidgetValue(QXmlStreamReader &element, QWidget *w, bool notCondition, QString &error)
122 {
123  int index = 0;
124  QString commentStr;
125  while (element.readNextStartElement()) {
126  const QStringRef tagName = element.name();
127  if (tagName == QLatin1String("str")) {
128  const QString tagValue = element.readElementText();
129  switch (index) {
130  case 0: {
131  auto mailbox = w->findChild<QLineEdit *>(QStringLiteral("mailbox"));
132  mailbox->setText(tagValue);
133  break;
134  }
135  case 1: {
136  auto annotation = w->findChild<QLineEdit *>(QStringLiteral("annotation"));
137  annotation->setText(tagValue);
138  break;
139  }
140  case 2: {
141  auto value = w->findChild<AbstractRegexpEditorLineEdit *>(QStringLiteral("value"));
142  value->setCode(tagValue);
143  break;
144  }
145  default:
146  tooManyArguments(tagName, index, 3, error);
147  qCDebug(LIBKSIEVE_LOG) << " SieveConditionServerMetaData::setParamWidgetValue too many argument " << index;
148  break;
149  }
150  ++index;
151  } else if (tagName == QLatin1String("tag")) {
152  auto selectType = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("selecttype"));
153  selectType->setCode(AutoCreateScriptUtil::tagValueWithCondition(element.readElementText(), notCondition), name(), error);
154  } else if (tagName == QLatin1String("crlf")) {
155  element.skipCurrentElement();
156  // nothing
157  } else if (tagName == QLatin1String("comment")) {
158  commentStr = AutoCreateScriptUtil::loadConditionComment(commentStr, element.readElementText());
159  } else {
160  unknownTag(tagName, error);
161  qCDebug(LIBKSIEVE_LOG) << " SieveConditionServerMetaData::setParamWidgetValue unknown tagName " << tagName;
162  }
163  }
164  if (!commentStr.isEmpty()) {
165  setComment(commentStr);
166  }
167 }
168 
169 QUrl SieveConditionServerMetaData::href() const
170 {
171  return SieveEditorUtil::helpUrl(SieveEditorUtil::strToVariableName(name()));
172 }
bool readNextStartElement()
void setContentsMargins(int left, int top, int right, int bottom)
QString name(const QVariant &location)
void textChanged(const QString &text)
void skipCurrentElement()
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 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 Sun Apr 11 2021 23:09:36 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.