Libksieve

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

KDE's Doxygen guidelines are available online.