Libksieve

sieveactionextracttext.cpp
1/*
2 SPDX-FileCopyrightText: 2013-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6#include "sieveactionextracttext.h"
7#include "editor/sieveeditorutil.h"
8
9#include <KLineEditEventHandler>
10#include <KLocalizedString>
11#include <QLineEdit>
12
13#include "libksieveui_debug.h"
14#include <QGridLayout>
15#include <QLabel>
16#include <QSpinBox>
17#include <QXmlStreamReader>
18
19using namespace KSieveUi;
20SieveActionExtractText::SieveActionExtractText(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QObject *parent)
21 : SieveAction(sieveGraphicalModeWidget, QStringLiteral("extracttext"), i18n("Extract Text"), parent)
22{
23}
24
25QWidget *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(i18nc("@label:textbox", "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(QLatin1StringView("numberOfCharacters"));
39 grid->addWidget(nbCharacters, 0, 1);
40 connect(nbCharacters, &QSpinBox::valueChanged, this, &SieveActionExtractText::valueChanged);
41
42 lab = new QLabel(i18nc("@label:textbox", "Stored in variable name:"));
43 grid->addWidget(lab, 1, 0);
44
45 auto variableName = new QLineEdit;
47 connect(variableName, &QLineEdit::textChanged, this, &SieveActionExtractText::valueChanged);
48 variableName->setObjectName(QLatin1StringView("variablename"));
49 grid->addWidget(variableName, 1, 1);
50
51 return w;
52}
53
54void SieveActionExtractText::setParamWidgetValue(QXmlStreamReader &element, QWidget *w, QString &error)
55{
56 while (element.readNextStartElement()) {
57 const QStringView tagName = element.name();
58
59 if (tagName == QLatin1StringView("tag")) {
60 element.skipCurrentElement();
61 // TODO ?
62 } else if (tagName == QLatin1StringView("num")) {
63 auto numberOfCharacters = w->findChild<QSpinBox *>(QStringLiteral("numberOfCharacters"));
64 numberOfCharacters->setValue(element.readElementText().toInt());
65 } else if (tagName == QLatin1StringView("str")) {
66 auto variableName = w->findChild<QLineEdit *>(QStringLiteral("variablename"));
67 variableName->setText(element.readElementText());
68 } else if (tagName == QLatin1StringView("crlf")) {
69 element.skipCurrentElement();
70 // nothing
71 } else if (tagName == QLatin1StringView("comment")) {
72 element.skipCurrentElement();
73 // implement in the future ?
74 } else {
75 unknownTag(tagName, error);
76 qCDebug(LIBKSIEVEUI_LOG) << " SieveActionExtractText::setParamWidgetValue unknown tagName " << tagName;
77 }
78 }
79}
80
81QString 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
93QStringList SieveActionExtractText::needRequires(QWidget * /*parent*/) const
94{
95 return QStringList() << QStringLiteral("extracttext");
96}
97
98bool SieveActionExtractText::needCheckIfServerHasCapability() const
99{
100 return true;
101}
102
103QString SieveActionExtractText::serverNeedsCapability() const
104{
105 return QStringLiteral("extracttext");
106}
107
108QString 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
113QUrl SieveActionExtractText::href() const
114{
115 return SieveEditorUtil::helpUrl(SieveEditorUtil::strToVariableName(name()));
116}
117
118#include "moc_sieveactionextracttext.cpp"
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
void catchReturnKey(QObject *lineEdit)
void setContentsMargins(const QMargins &margins)
void setText(const QString &)
void textChanged(const QString &text)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
T findChild(const QString &name, Qt::FindChildOptions options) const const
QObject * parent() const const
void setMinimum(int min)
void setValue(int val)
void valueChanged(int i)
QString arg(Args &&... args) const const
QString number(double n, char format, int precision)
int toInt(bool *ok, int base) const const
QStringView name() const const
QString readElementText(ReadElementTextBehaviour behaviour)
bool readNextStartElement()
void skipCurrentElement()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 29 2024 11:56:12 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.