Libksieve

sieveconditionmetadataexists.cpp
1 /*
2  SPDX-FileCopyrightText: 2013-2020 Laurent Montel <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 #include "sieveconditionmetadataexists.h"
7 #include "autocreatescripts/autocreatescriptutil_p.h"
8 #include "editor/sieveeditorutil.h"
9 #include "util/sieveimapaccountsettings.h"
10 #include <KLocalizedString>
11 #include <QLineEdit>
12 
13 #include <QLabel>
14 #include "libksieve_debug.h"
15 #include <QXmlStreamReader>
16 #include <QGridLayout>
17 #include <KSieveUi/AbstractMoveImapFolderWidget>
18 
19 using namespace KSieveUi;
20 SieveConditionMetaDataExists::SieveConditionMetaDataExists(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QObject *parent)
21  : SieveCondition(sieveGraphicalModeWidget, QStringLiteral("metadataexists"), i18n("Metadata exists"), parent)
22 {
23 }
24 
25 QWidget *SieveConditionMetaDataExists::createParamWidget(QWidget *parent) const
26 {
27  QWidget *w = new QWidget(parent);
28  auto *grid = new QGridLayout;
29  grid->setContentsMargins({});
30  w->setLayout(grid);
31 
32  QLabel *lab = new QLabel(i18n("Mailbox:"));
33  grid->addWidget(lab, 0, 0);
34 
35  KSieveUi::AbstractMoveImapFolderWidget *mailbox = AutoCreateScriptUtil::createImapFolderWidget();
36  mailbox->setSieveImapAccountSettings(sieveImapAccountSettings());
37  connect(mailbox, &KSieveUi::AbstractMoveImapFolderWidget::textChanged, this, &SieveConditionMetaDataExists::valueChanged);
38  mailbox->setObjectName(QStringLiteral("mailbox"));
39  grid->addWidget(mailbox, 0, 1);
40 
41  lab = new QLabel(i18n("Annotation:"));
42  grid->addWidget(lab, 1, 0);
43 
44  auto *value = new QLineEdit;
45  connect(value, &QLineEdit::textChanged, this, &SieveConditionMetaDataExists::valueChanged);
46  value->setObjectName(QStringLiteral("value"));
47  grid->addWidget(value, 1, 1);
48 
49  return w;
50 }
51 
52 QString SieveConditionMetaDataExists::code(QWidget *w) const
53 {
54  const KSieveUi::AbstractMoveImapFolderWidget *mailbox = w->findChild<KSieveUi::AbstractMoveImapFolderWidget *>(QStringLiteral("mailbox"));
55  const QString mailboxStr = mailbox->text();
56 
57  const QLineEdit *value = w->findChild<QLineEdit *>(QStringLiteral("value"));
58  const QString valueStr = value->text();
59  return QStringLiteral("metadataexists \"%1\" \"%2\"").arg(mailboxStr, valueStr) + AutoCreateScriptUtil::generateConditionComment(comment());
60 }
61 
62 QStringList SieveConditionMetaDataExists::needRequires(QWidget *) const
63 {
64  return QStringList() << QStringLiteral("mboxmetadata");
65 }
66 
67 bool SieveConditionMetaDataExists::needCheckIfServerHasCapability() const
68 {
69  return true;
70 }
71 
72 QString SieveConditionMetaDataExists::serverNeedsCapability() const
73 {
74  return QStringLiteral("mboxmetadata");
75 }
76 
77 QString SieveConditionMetaDataExists::help() const
78 {
79  return i18n("The \"metadataexists\" test is true if all of the annotations listed in the \"annotation-names\" argument exist for the specified mailbox.");
80 }
81 
82 void SieveConditionMetaDataExists::setParamWidgetValue(QXmlStreamReader &element, QWidget *w, bool /*notCondition*/, QString &error)
83 {
84  int index = 0;
85  QString commentStr;
86  while (element.readNextStartElement()) {
87  const QStringRef tagName = element.name();
88  if (tagName == QLatin1String("str")) {
89  const QString tagValue = element.readElementText();
90  if (index == 0) {
91  auto *mailbox = w->findChild<KSieveUi::AbstractMoveImapFolderWidget *>(QStringLiteral("mailbox"));
92  mailbox->setText(tagValue);
93  } else if (index == 1) {
94  auto *value = w->findChild<QLineEdit *>(QStringLiteral("value"));
95  value->setText(AutoCreateScriptUtil::quoteStr(tagValue));
96  } else {
97  tooManyArguments(tagName, index, 2, error);
98  qCDebug(LIBKSIEVE_LOG) << " SieveConditionServerMetaDataExists::setParamWidgetValue to many attribute " << index;
99  }
100  ++index;
101  } else if (tagName == QLatin1String("crlf")) {
102  element.skipCurrentElement();
103  //nothing
104  } else if (tagName == QLatin1String("comment")) {
105  commentStr = AutoCreateScriptUtil::loadConditionComment(commentStr, element.readElementText());
106  } else {
107  unknownTag(tagName, error);
108  qCDebug(LIBKSIEVE_LOG) << " SieveConditionServerMetaDataExists::setParamWidgetValue unknown tagName " << tagName;
109  }
110  }
111  if (!commentStr.isEmpty()) {
112  setComment(commentStr);
113  }
114 }
115 
116 QUrl SieveConditionMetaDataExists::href() const
117 {
118  return SieveEditorUtil::helpUrl(SieveEditorUtil::strToVariableName(name()));
119 }
bool readNextStartElement()
void setContentsMargins(int left, int top, int right, int bottom)
QString name(const QVariant &location)
void textChanged(const QString &text)
void skipCurrentElement()
void setLayout(QLayout *layout)
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...)
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-2020 The KDE developers.
Generated on Sat Nov 21 2020 23:09:36 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.