Libksieve

sieveforeverypartwidget.cpp
1/*
2 SPDX-FileCopyrightText: 2013-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "sieveforeverypartwidget.h"
8#include "autocreatescripts/autocreatescriptutil_p.h"
9#include "autocreatescriptutil_p.h"
10#include "commonwidgets/sievehelpbutton.h"
11#include "editor/sieveeditorutil.h"
12#include "sievescriptblockwidget.h"
13
14#include "widgets/lineeditvalidator.h"
15#include <KLineEditEventHandler>
16#include <KLocalizedString>
17#include <QLineEdit>
18
19#include "libksieveui_debug.h"
20#include <QCheckBox>
21#include <QHBoxLayout>
22#include <QLabel>
23#include <QWhatsThis>
24#include <QXmlStreamReader>
25
26using namespace KSieveUi;
27
28SieveForEveryPartWidget::SieveForEveryPartWidget(QWidget *parent)
29 : SieveWidgetPageAbstract(parent)
30 , mForLoop(new QCheckBox(i18nc("@option:check", "Add ForEveryPart loop"), this))
31 , mName(new LineEditValidator(this))
32 , mHelpButton(new SieveHelpButton(this))
33{
34 auto mainLayout = new QVBoxLayout(this);
35 auto lay = new QHBoxLayout;
36 mainLayout->addLayout(lay);
37 mainLayout->addStretch(1);
38
39 lay->addWidget(mHelpButton);
40 connect(mHelpButton, &SieveHelpButton::clicked, this, &SieveForEveryPartWidget::slotHelp);
41
42 connect(mForLoop, &QCheckBox::toggled, this, &SieveForEveryPartWidget::valueChanged);
43 lay->addWidget(mForLoop);
44
45 auto lab = new QLabel(i18nc("@label:textbox", "Name (optional):"), this);
46 lay->addWidget(lab);
47
49 connect(mName, &QLineEdit::textChanged, this, &SieveForEveryPartWidget::valueChanged);
50 mName->setEnabled(false);
51 lay->addWidget(mName);
52
54 setPageType(KSieveUi::SieveScriptBlockWidget::ForEveryPart);
55}
56
57SieveForEveryPartWidget::~SieveForEveryPartWidget() = default;
58
59void SieveForEveryPartWidget::slotHelp()
60{
61 const QString help = i18n(
62 "\"foreverypart\", which is an iterator that walks though every MIME part of a message, including nested parts, depth first, and applies the commands "
63 "in the specified block to each of them.");
64 const QUrl href = KSieveUi::SieveEditorUtil::helpUrl(KSieveUi::SieveEditorUtil::ForEveryPart);
65 const QString fullWhatsThis = AutoCreateScriptUtil::createFullWhatsThis(help, href.toString());
66 QWhatsThis::showText(QCursor::pos(), fullWhatsThis, mHelpButton);
67}
68
69void SieveForEveryPartWidget::generatedScript(QString &script, QStringList &required, bool inForEveryPartLoop)
70{
71 Q_UNUSED(inForEveryPartLoop)
72 if (mForLoop->isChecked()) {
73 required << QStringLiteral("foreverypart");
74 const QString loopName = mName->text();
75 if (loopName.isEmpty()) {
76 script += QStringLiteral("foreverypart {");
77 } else {
78 script += QStringLiteral("foreverypart :name \"%1\" {").arg(loopName);
79 }
80 }
81}
82
83void SieveForEveryPartWidget::loadScript(QXmlStreamReader &element, QString &error)
84{
85 if (element.readNextStartElement()) {
86 const QStringView tagName = element.name();
87 if (tagName == QLatin1StringView("tag")) {
88 const QString tagValue = element.readElementText();
89 if (tagValue == QLatin1StringView("name")) {
90 mName->setText(AutoCreateScriptUtil::strValue(element));
91 } else {
92 error += i18n("Unknown tagValue \"%1\" during loading loop \"for\"", tagValue) + QLatin1Char('\n');
93 qCDebug(LIBKSIEVEUI_LOG) << " SieveForEveryPartWidget::loadScript unknown tagValue " << tagValue;
94 }
95 mForLoop->setChecked(true);
96 mName->setEnabled(true);
97 } else if (tagName == QLatin1StringView("block")) {
98 // Nothing
99 // It's when name is empty
100 } else if (tagName == QLatin1StringView("crlf")) {
101 // Nothing
102 element.skipCurrentElement();
103 } else {
104 error += i18n("Unknown tag \"%1\" during loading loop \"for\"", tagName.toString()) + QLatin1Char('\n');
105 qCDebug(LIBKSIEVEUI_LOG) << " SieveForEveryPartWidget::loadScript unknown tagName " << tagName;
106 }
107 } else {
108 qCDebug(LIBKSIEVEUI_LOG) << " SieveForEveryPartWidget::loadScript problem during loading";
109 }
110}
111
112#include "moc_sieveforeverypartwidget.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 error(QWidget *parent, const QString &text, const QString &title, const KGuiItem &buttonOk, Options options=Notify)
KGuiItem help()
QByteArray tagValue(const Elem &elem, const char *keyName)
bool isChecked() const const
void clicked(bool checked)
void toggled(bool checked)
void addLayout(QLayout *layout, int stretch)
QPoint pos()
void textChanged(const QString &text)
QString arg(Args &&... args) const const
bool isEmpty() const const
QString toString() const const
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
QString toString(FormattingOptions options) const const
void showText(const QPoint &pos, const QString &text, QWidget *w)
void setEnabled(bool)
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.