Libksieve

sieveactionextracttext.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 "sieveactionextracttext.h"
20 #include "editor/sieveeditorutil.h"
21 
22 #include <KLocalizedString>
23 #include <QLineEdit>
24 
25 #include <QLabel>
26 #include <QSpinBox>
27 #include <QXmlStreamReader>
28 #include "libksieve_debug.h"
29 #include <QGridLayout>
30 
31 using namespace KSieveUi;
32 SieveActionExtractText::SieveActionExtractText(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QObject *parent)
33  : SieveAction(sieveGraphicalModeWidget, QStringLiteral("extracttext"), i18n("Extract Text"), parent)
34 {
35 }
36 
37 QWidget *SieveActionExtractText::createParamWidget(QWidget *parent) const
38 {
39  QWidget *w = new QWidget(parent);
40  QGridLayout *grid = new QGridLayout;
41  grid->setContentsMargins(0, 0, 0, 0);
42  w->setLayout(grid);
43 
44  QLabel *lab = new QLabel(i18n("Number of characters:"));
45  grid->addWidget(lab, 0, 0);
46 
47  QSpinBox *nbCharacters = new QSpinBox;
48  nbCharacters->setMinimum(1);
49  nbCharacters->setMaximum(99999);
50  nbCharacters->setObjectName(QStringLiteral("numberOfCharacters"));
51  grid->addWidget(nbCharacters, 0, 1);
52  connect(nbCharacters, QOverload<int>::of(&QSpinBox::valueChanged), this, &SieveActionExtractText::valueChanged);
53 
54  lab = new QLabel(i18n("Stored in variable name:"));
55  grid->addWidget(lab, 1, 0);
56 
57  QLineEdit *variableName = new QLineEdit;
58  connect(variableName, &QLineEdit::textChanged, this, &SieveActionExtractText::valueChanged);
59  variableName->setObjectName(QStringLiteral("variablename"));
60  grid->addWidget(variableName, 1, 1);
61 
62  return w;
63 }
64 
65 void SieveActionExtractText::setParamWidgetValue(QXmlStreamReader &element, QWidget *w, QString &error)
66 {
67  while (element.readNextStartElement()) {
68  const QStringRef tagName = element.name();
69 
70  if (tagName == QLatin1String("tag")) {
71  element.skipCurrentElement();
72  //TODO ?
73  } else if (tagName == QLatin1String("num")) {
74  QSpinBox *numberOfCharacters = w->findChild<QSpinBox *>(QStringLiteral("numberOfCharacters"));
75  numberOfCharacters->setValue(element.readElementText().toInt());
76  } else if (tagName == QLatin1String("str")) {
77  QLineEdit *variableName = w->findChild<QLineEdit *>(QStringLiteral("variablename"));
78  variableName->setText(element.readElementText());
79  } else if (tagName == QLatin1String("crlf")) {
80  element.skipCurrentElement();
81  //nothing
82  } else if (tagName == QLatin1String("comment")) {
83  element.skipCurrentElement();
84  //implement in the future ?
85  } else {
86  unknownTag(tagName, error);
87  qCDebug(LIBKSIEVE_LOG) << " SieveActionExtractText::setParamWidgetValue unknown tagName " << tagName;
88  }
89  }
90 }
91 
92 QString SieveActionExtractText::code(QWidget *w) const
93 {
94  const QSpinBox *numberOfCharacters = w->findChild<QSpinBox *>(QStringLiteral("numberOfCharacters"));
95  const QString numberOfCharactersStr = QString::number(numberOfCharacters->value());
96 
97  const QLineEdit *variableName = w->findChild<QLineEdit *>(QStringLiteral("variablename"));
98  const QString variableNameStr = variableName->text();
99 
100  const QString result = QStringLiteral("extracttext :first %1 \"%2\";").arg(numberOfCharactersStr, variableNameStr);
101  return result;
102 }
103 
104 QStringList SieveActionExtractText::needRequires(QWidget * /*parent*/) const
105 {
106  return QStringList() << QStringLiteral("extracttext");
107 }
108 
109 bool SieveActionExtractText::needCheckIfServerHasCapability() const
110 {
111  return true;
112 }
113 
114 QString SieveActionExtractText::serverNeedsCapability() const
115 {
116  return QStringLiteral("extracttext");
117 }
118 
119 QString SieveActionExtractText::help() const
120 {
121  return i18n("The \"extracttext\" action may be used within the context of a \"foreverypart\" loop and is used to store text into a variable");
122 }
123 
124 QUrl SieveActionExtractText::href() const
125 {
126  return SieveEditorUtil::helpUrl(SieveEditorUtil::strToVariableName(name()));
127 }
void setMinimum(int min)
bool readNextStartElement()
void setContentsMargins(int left, int top, int right, int bottom)
QString name(const QVariant &location)
void addWidget(QWidget *widget, int row, int column, Qt::Alignment alignment)
void setText(const QString &)
void textChanged(const QString &text)
void valueChanged(int i)
void skipCurrentElement()
QString number(int n, int base)
void setLayout(QLayout *layout)
QString readElementText(QXmlStreamReader::ReadElementTextBehaviour behaviour)
int toInt(bool *ok, int base) const const
void setObjectName(const QString &name)
void setMaximum(int max)
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-2020 The KDE developers.
Generated on Thu Jul 9 2020 23:07:44 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.