Libksieve

sieveforeverypartwidget.cpp
1 /*
2  SPDX-FileCopyrightText: 2013-2021 Laurent Montel <[email protected]>
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 <KLocalizedString>
16 #include <Libkdepim/LineEditCatchReturnKey>
17 #include <QLineEdit>
18 
19 #include "libksieve_debug.h"
20 #include <QCheckBox>
21 #include <QHBoxLayout>
22 #include <QLabel>
23 #include <QWhatsThis>
24 #include <QXmlStreamReader>
25 
26 using namespace KSieveUi;
27 
28 SieveForEveryPartWidget::SieveForEveryPartWidget(QWidget *parent)
29  : SieveWidgetPageAbstract(parent)
30 {
31  auto mainLayout = new QVBoxLayout(this);
32  auto lay = new QHBoxLayout;
33  mainLayout->addLayout(lay);
34  mainLayout->addStretch(1);
35 
36  mHelpButton = new SieveHelpButton(this);
37  lay->addWidget(mHelpButton);
38  connect(mHelpButton, &SieveHelpButton::clicked, this, &SieveForEveryPartWidget::slotHelp);
39 
40  mForLoop = new QCheckBox(i18n("Add ForEveryPart loop"), this);
41  connect(mForLoop, &QCheckBox::toggled, this, &SieveForEveryPartWidget::valueChanged);
42  lay->addWidget(mForLoop);
43 
44  auto lab = new QLabel(i18n("Name (optional):"), this);
45  lay->addWidget(lab);
46 
47  mName = new LineEditValidator(this);
48  new KPIM::LineEditCatchReturnKey(mName, this);
49  connect(mName, &QLineEdit::textChanged, this, &SieveForEveryPartWidget::valueChanged);
50  mName->setEnabled(false);
51  lay->addWidget(mName);
52 
54  setPageType(KSieveUi::SieveScriptBlockWidget::ForEveryPart);
55 }
56 
57 SieveForEveryPartWidget::~SieveForEveryPartWidget()
58 {
59 }
60 
61 void SieveForEveryPartWidget::slotHelp()
62 {
63  const QString help = i18n(
64  "\"foreverypart\", which is an iterator that walks though every MIME part of a message, including nested parts, depth first, and applies the commands "
65  "in the specified block to each of them.");
66  const QUrl href = KSieveUi::SieveEditorUtil::helpUrl(KSieveUi::SieveEditorUtil::ForEveryPart);
67  const QString fullWhatsThis = AutoCreateScriptUtil::createFullWhatsThis(help, href.toString());
68  QWhatsThis::showText(QCursor::pos(), fullWhatsThis, mHelpButton);
69 }
70 
71 void SieveForEveryPartWidget::generatedScript(QString &script, QStringList &required, bool inForEveryPartLoop)
72 {
73  Q_UNUSED(inForEveryPartLoop)
74  if (mForLoop->isChecked()) {
75  required << QStringLiteral("foreverypart");
76  const QString loopName = mName->text();
77  if (loopName.isEmpty()) {
78  script += QStringLiteral("foreverypart {");
79  } else {
80  script += QStringLiteral("foreverypart :name \"%1\" {").arg(loopName);
81  }
82  }
83 }
84 
85 void SieveForEveryPartWidget::loadScript(QXmlStreamReader &element, QString &error)
86 {
87  if (element.readNextStartElement()) {
88  const QStringRef tagName = element.name();
89  if (tagName == QLatin1String("tag")) {
90  const QString tagValue = element.readElementText();
91  if (tagValue == QLatin1String("name")) {
92  mName->setText(AutoCreateScriptUtil::strValue(element));
93  } else {
94  error += i18n("Unknown tagValue \"%1\" during loading loop \"for\"", tagValue) + QLatin1Char('\n');
95  qCDebug(LIBKSIEVE_LOG) << " SieveForEveryPartWidget::loadScript unknown tagValue " << tagValue;
96  }
97  mForLoop->setChecked(true);
98  mName->setEnabled(true);
99  } else if (tagName == QLatin1String("block")) {
100  // Nothing
101  // It's when name is empty
102  } else if (tagName == QLatin1String("crlf")) {
103  // Nothing
104  element.skipCurrentElement();
105  } else {
106  error += i18n("Unknown tag \"%1\" during loading loop \"for\"", tagName.toString()) + QLatin1Char('\n');
107  qCDebug(LIBKSIEVE_LOG) << " SieveForEveryPartWidget::loadScript unknown tagName " << tagName;
108  }
109  } else {
110  qCDebug(LIBKSIEVE_LOG) << " SieveForEveryPartWidget::loadScript problem during loading";
111  }
112 }
void showText(const QPoint &pos, const QString &text, QWidget *w)
bool readNextStartElement()
QString toString() const const
void textChanged(const QString &text)
QString toString(QUrl::FormattingOptions options) const const
void skipCurrentElement()
void setEnabled(bool)
const QList< QKeySequence > & help()
QString readElementText(QXmlStreamReader::ReadElementTextBehaviour behaviour)
QByteArray tagValue(const Elem &elem, TagKey key)
bool isEmpty() const const
void clicked(bool checked)
QString i18n(const char *text, const TYPE &arg...)
QPoint pos()
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
void toggled(bool checked)
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QStringRef name() const const
void addLayout(QLayout *layout, int stretch)
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.