Libksieve

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