Libksieve

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