Libksieve

sieveconditionmetadata.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 "sieveconditionmetadata.h"
20 #include "autocreatescripts/autocreatescriptutil_p.h"
21 #include "autocreatescripts/commonwidgets/selectmatchtypecombobox.h"
22 #include "editor/sieveeditorutil.h"
23 #include "util/sieveimapaccountsettings.h"
24 #include <KLocalizedString>
25 #include <QLineEdit>
26 
27 #include <QWidget>
28 #include <QLabel>
29 #include <QHBoxLayout>
30 #include "libksieve_debug.h"
31 #include <QXmlStreamReader>
32 #include <KSieveUi/AbstractMoveImapFolderWidget>
33 
34 using namespace KSieveUi;
35 SieveConditionMetaData::SieveConditionMetaData(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QObject *parent)
36  : SieveCondition(sieveGraphicalModeWidget, QStringLiteral("metadata"), i18n("Meta Data"), parent)
37 {
38 }
39 
40 QWidget *SieveConditionMetaData::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  SelectMatchTypeComboBox *selectType = new SelectMatchTypeComboBox(mSieveGraphicalModeWidget);
48  selectType->setObjectName(QStringLiteral("selecttype"));
49  connect(selectType, &SelectMatchTypeComboBox::valueChanged, this, &SieveConditionMetaData::valueChanged);
50  lay->addWidget(selectType);
51 
52  QGridLayout *grid = new QGridLayout;
53  grid->setContentsMargins(0, 0, 0, 0);
54  lay->addLayout(grid);
55 
56  QLabel *lab = new QLabel(i18n("Mailbox:"));
57  grid->addWidget(lab, 0, 0);
58 
59  KSieveUi::AbstractMoveImapFolderWidget *mailbox = AutoCreateScriptUtil::createImapFolderWidget();
60  mailbox->setSieveImapAccountSettings(sieveImapAccountSettings());
61 
62  connect(mailbox, &KSieveUi::AbstractMoveImapFolderWidget::textChanged, this, &SieveConditionMetaData::valueChanged);
63  mailbox->setObjectName(QStringLiteral("mailbox"));
64  grid->addWidget(mailbox, 0, 1);
65 
66  lab = new QLabel(i18n("Annotations:"));
67  grid->addWidget(lab, 1, 0);
68 
69  QLineEdit *annotation = new QLineEdit;
70  connect(annotation, &QLineEdit::textChanged, this, &SieveConditionMetaData::valueChanged);
71  annotation->setObjectName(QStringLiteral("annotation"));
72  grid->addWidget(annotation, 1, 1);
73 
74  lab = new QLabel(i18n("Value:"));
75  grid->addWidget(lab, 2, 0);
76 
77  AbstractRegexpEditorLineEdit *value = AutoCreateScriptUtil::createRegexpEditorLineEdit();
78  connect(value, &AbstractRegexpEditorLineEdit::textChanged, this, &SieveConditionMetaData::valueChanged);
79  connect(selectType, &SelectMatchTypeComboBox::switchToRegexp, value, &AbstractRegexpEditorLineEdit::switchToRegexpEditorLineEdit);
80  value->setObjectName(QStringLiteral("value"));
81  grid->addWidget(value, 2, 1);
82 
83  return w;
84 }
85 
86 QString SieveConditionMetaData::code(QWidget *w) const
87 {
88  const SelectMatchTypeComboBox *selectType = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("selecttype"));
89  bool isNegative = false;
90  const QString matchString = selectType->code(isNegative);
91 
92  QString result = AutoCreateScriptUtil::negativeString(isNegative) + QStringLiteral("metadata %1 ").arg(matchString);
93 
94  const KSieveUi::AbstractMoveImapFolderWidget *mailbox = w->findChild<KSieveUi::AbstractMoveImapFolderWidget *>(QStringLiteral("mailbox"));
95  const QString mailboxStr = mailbox->text();
96 
97  result += QStringLiteral("\"%1\" ").arg(mailboxStr);
98 
99  const QLineEdit *annotation = w->findChild<QLineEdit *>(QStringLiteral("annotation"));
100  const QString annotationStr = annotation->text();
101 
102  result += QStringLiteral("\"%1\" ").arg(annotationStr);
103 
104  const AbstractRegexpEditorLineEdit *value = w->findChild<AbstractRegexpEditorLineEdit *>(QStringLiteral("value"));
105  const QString valueStr = value->code();
106 
107  result += QStringLiteral("\"%1\"").arg(valueStr);
108  return result + AutoCreateScriptUtil::generateConditionComment(comment());
109 }
110 
111 QStringList SieveConditionMetaData::needRequires(QWidget *w) const
112 {
113  const SelectMatchTypeComboBox *selectType = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("selecttype"));
114  return QStringList() << QStringLiteral("mboxmetadata") << selectType->needRequires();
115 }
116 
117 bool SieveConditionMetaData::needCheckIfServerHasCapability() const
118 {
119  return true;
120 }
121 
122 QString SieveConditionMetaData::serverNeedsCapability() const
123 {
124  return QStringLiteral("mboxmetadata");
125 }
126 
127 QString SieveConditionMetaData::help() const
128 {
129  return i18n(
130  "This test retrieves the value of the mailbox annotation \"annotation-name\" for the mailbox \"mailbox\". The retrieved value is compared to the \"key-list\". The test returns true if the annotation exists and its value matches any of the keys.");
131 }
132 
133 void SieveConditionMetaData::setParamWidgetValue(QXmlStreamReader &element, QWidget *w, bool notCondition, QString &error)
134 {
135  int index = 0;
136  QString commentStr;
137  while (element.readNextStartElement()) {
138  const QStringRef tagName = element.name();
139  if (tagName == QLatin1String("str")) {
140  const QString tagValue = element.readElementText();
141  switch (index) {
142  case 0:
143  {
145  mailbox->setText(tagValue);
146  break;
147  }
148  case 1:
149  {
150  QLineEdit *annotation = w->findChild<QLineEdit *>(QStringLiteral("annotation"));
151  annotation->setText(AutoCreateScriptUtil::quoteStr(tagValue));
152  break;
153  }
154  case 2:
155  {
156  AbstractRegexpEditorLineEdit *value = w->findChild<AbstractRegexpEditorLineEdit *>(QStringLiteral("value"));
157  value->setCode(AutoCreateScriptUtil::quoteStr(tagValue));
158  break;
159  }
160  default:
161  tooManyArguments(tagName, index, 3, error);
162  qCDebug(LIBKSIEVE_LOG) << " SieveConditionMetaData::setParamWidgetValue too many argument " << index;
163  break;
164  }
165  ++index;
166  } else if (tagName == QLatin1String("tag")) {
167  SelectMatchTypeComboBox *selectType = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("selecttype"));
168  selectType->setCode(AutoCreateScriptUtil::tagValueWithCondition(element.readElementText(), notCondition), name(), error);
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) << " SieveConditionMetaData::setParamWidgetValue unknown tagName " << tagName;
177  }
178  }
179  if (!commentStr.isEmpty()) {
180  setComment(commentStr);
181  }
182 }
183 
184 QUrl SieveConditionMetaData::href() const
185 {
186  return SieveEditorUtil::helpUrl(SieveEditorUtil::strToVariableName(name()));
187 }
bool readNextStartElement()
void setContentsMargins(int left, int top, int right, int bottom)
void addWidget(QWidget *widget, int row, int column, Qt::Alignment alignment)
void textChanged(const QString &text)
QString name(StandardShortcut id)
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
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
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 Sun May 24 2020 22:46:41 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.