Libksieve

sieveconditionihave.cpp
1 /*
2  SPDX-FileCopyrightText: 2013-2021 Laurent Montel <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 #include "sieveconditionihave.h"
7 #include "autocreatescripts/autocreatescriptutil_p.h"
8 #include "editor/sieveeditorutil.h"
9 
10 #include <KLocalizedString>
11 #include <Libkdepim/LineEditCatchReturnKey>
12 #include <QLineEdit>
13 
14 #include "libksieve_debug.h"
15 #include <QHBoxLayout>
16 #include <QWidget>
17 #include <QXmlStreamReader>
18 
19 using namespace KSieveUi;
20 SieveConditionIhave::SieveConditionIhave(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QObject *parent)
21  : SieveCondition(sieveGraphicalModeWidget, QStringLiteral("ihave"), i18n("IHave"), parent)
22 {
23 }
24 
25 QWidget *SieveConditionIhave::createParamWidget(QWidget *parent) const
26 {
27  auto w = new QWidget(parent);
28  auto lay = new QHBoxLayout;
29  lay->setContentsMargins({});
30  w->setLayout(lay);
31 
32  auto edit = new QLineEdit;
33  new KPIM::LineEditCatchReturnKey(edit, w);
34  connect(edit, &QLineEdit::textChanged, this, &SieveConditionIhave::valueChanged);
35  edit->setPlaceholderText(i18n("Use \",\" to separate capabilities"));
36  edit->setClearButtonEnabled(true);
37  lay->addWidget(edit);
38  edit->setObjectName(QStringLiteral("edit"));
39 
40  return w;
41 }
42 
43 QString SieveConditionIhave::code(QWidget *w) const
44 {
45  const QLineEdit *edit = w->findChild<QLineEdit *>(QStringLiteral("edit"));
46  const QString editValue = edit->text();
47  return QStringLiteral("ihave %1").arg(AutoCreateScriptUtil::createList(editValue, QLatin1Char(',')))
48  + AutoCreateScriptUtil::generateConditionComment(comment());
49 }
50 
51 QStringList SieveConditionIhave::needRequires(QWidget *) const
52 {
53  return QStringList() << QStringLiteral("ihave");
54 }
55 
56 bool SieveConditionIhave::needCheckIfServerHasCapability() const
57 {
58  return true;
59 }
60 
61 QString SieveConditionIhave::serverNeedsCapability() const
62 {
63  return QStringLiteral("ihave");
64 }
65 
66 QString SieveConditionIhave::help() const
67 {
68  return i18n("The \"ihave\" test provides a means for Sieve scripts to test for the existence of a given extension prior to actually using it.");
69 }
70 
71 void SieveConditionIhave::setParamWidgetValue(QXmlStreamReader &element, QWidget *w, bool, QString &error)
72 {
73  QString commentStr;
74  while (element.readNextStartElement()) {
75  const QStringRef tagName = element.name();
76  if (tagName == QLatin1String("str")) {
77  const QString tagValue = element.readElementText();
78  auto edit = w->findChild<QLineEdit *>(QStringLiteral("edit"));
79  edit->setText(tagValue);
80  } else if (tagName == QLatin1String("crlf")) {
81  element.skipCurrentElement();
82  // nothing
83  } else if (tagName == QLatin1String("comment")) {
84  commentStr = AutoCreateScriptUtil::loadConditionComment(commentStr, element.readElementText());
85  } else {
86  unknownTag(tagName, error);
87  qCDebug(LIBKSIEVE_LOG) << " SieveConditionIhave::setParamWidgetValue unknown tagName " << tagName;
88  }
89  }
90  if (!commentStr.isEmpty()) {
91  setComment(commentStr);
92  }
93 }
94 
95 QUrl SieveConditionIhave::href() const
96 {
97  return SieveEditorUtil::helpUrl(SieveEditorUtil::strToVariableName(name()));
98 }
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)
bool isEmpty() const const
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 Sun Apr 11 2021 23:09:36 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.