Libksieve

sieveactionextracttext.cpp
1 /*
2  SPDX-FileCopyrightText: 2013-2021 Laurent Montel <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 #include "sieveactionextracttext.h"
7 #include "editor/sieveeditorutil.h"
8 
9 #include <KLocalizedString>
10 #include <Libkdepim/LineEditCatchReturnKey>
11 #include <QLineEdit>
12 
13 #include "libksieve_debug.h"
14 #include <QGridLayout>
15 #include <QLabel>
16 #include <QSpinBox>
17 #include <QXmlStreamReader>
18 
19 using namespace KSieveUi;
20 SieveActionExtractText::SieveActionExtractText(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QObject *parent)
21  : SieveAction(sieveGraphicalModeWidget, QStringLiteral("extracttext"), i18n("Extract Text"), parent)
22 {
23 }
24 
25 QWidget *SieveActionExtractText::createParamWidget(QWidget *parent) const
26 {
27  auto w = new QWidget(parent);
28  auto grid = new QGridLayout;
29  grid->setContentsMargins({});
30  w->setLayout(grid);
31 
32  auto lab = new QLabel(i18n("Number of characters:"));
33  grid->addWidget(lab, 0, 0);
34 
35  auto nbCharacters = new QSpinBox;
36  nbCharacters->setMinimum(1);
37  nbCharacters->setMaximum(99999);
38  nbCharacters->setObjectName(QStringLiteral("numberOfCharacters"));
39  grid->addWidget(nbCharacters, 0, 1);
40  connect(nbCharacters, QOverload<int>::of(&QSpinBox::valueChanged), this, &SieveActionExtractText::valueChanged);
41 
42  lab = new QLabel(i18n("Stored in variable name:"));
43  grid->addWidget(lab, 1, 0);
44 
45  auto variableName = new QLineEdit;
46  new KPIM::LineEditCatchReturnKey(variableName, w);
47  connect(variableName, &QLineEdit::textChanged, this, &SieveActionExtractText::valueChanged);
48  variableName->setObjectName(QStringLiteral("variablename"));
49  grid->addWidget(variableName, 1, 1);
50 
51  return w;
52 }
53 
54 void SieveActionExtractText::setParamWidgetValue(QXmlStreamReader &element, QWidget *w, QString &error)
55 {
56  while (element.readNextStartElement()) {
57  const QStringRef tagName = element.name();
58 
59  if (tagName == QLatin1String("tag")) {
60  element.skipCurrentElement();
61  // TODO ?
62  } else if (tagName == QLatin1String("num")) {
63  auto numberOfCharacters = w->findChild<QSpinBox *>(QStringLiteral("numberOfCharacters"));
64  numberOfCharacters->setValue(element.readElementText().toInt());
65  } else if (tagName == QLatin1String("str")) {
66  auto variableName = w->findChild<QLineEdit *>(QStringLiteral("variablename"));
67  variableName->setText(element.readElementText());
68  } else if (tagName == QLatin1String("crlf")) {
69  element.skipCurrentElement();
70  // nothing
71  } else if (tagName == QLatin1String("comment")) {
72  element.skipCurrentElement();
73  // implement in the future ?
74  } else {
75  unknownTag(tagName, error);
76  qCDebug(LIBKSIEVE_LOG) << " SieveActionExtractText::setParamWidgetValue unknown tagName " << tagName;
77  }
78  }
79 }
80 
81 QString SieveActionExtractText::code(QWidget *w) const
82 {
83  const QSpinBox *numberOfCharacters = w->findChild<QSpinBox *>(QStringLiteral("numberOfCharacters"));
84  const QString numberOfCharactersStr = QString::number(numberOfCharacters->value());
85 
86  const QLineEdit *variableName = w->findChild<QLineEdit *>(QStringLiteral("variablename"));
87  const QString variableNameStr = variableName->text();
88 
89  const QString result = QStringLiteral("extracttext :first %1 \"%2\";").arg(numberOfCharactersStr, variableNameStr);
90  return result;
91 }
92 
93 QStringList SieveActionExtractText::needRequires(QWidget * /*parent*/) const
94 {
95  return QStringList() << QStringLiteral("extracttext");
96 }
97 
98 bool SieveActionExtractText::needCheckIfServerHasCapability() const
99 {
100  return true;
101 }
102 
103 QString SieveActionExtractText::serverNeedsCapability() const
104 {
105  return QStringLiteral("extracttext");
106 }
107 
108 QString SieveActionExtractText::help() const
109 {
110  return i18n("The \"extracttext\" action may be used within the context of a \"foreverypart\" loop and is used to store text into a variable");
111 }
112 
113 QUrl SieveActionExtractText::href() const
114 {
115  return SieveEditorUtil::helpUrl(SieveEditorUtil::strToVariableName(name()));
116 }
void setMinimum(int min)
bool readNextStartElement()
void setContentsMargins(int left, int top, int right, int bottom)
QString name(const QVariant &location)
void setText(const QString &)
void textChanged(const QString &text)
void valueChanged(int i)
void skipCurrentElement()
QString number(int n, int base)
QString readElementText(QXmlStreamReader::ReadElementTextBehaviour behaviour)
int toInt(bool *ok, int base) const const
QString i18n(const char *text, const TYPE &arg...)
void setValue(int val)
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
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 Fri Apr 16 2021 23:09:34 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.