Libksieve

sieveforeverypartwidget.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 
20 #include "sieveforeverypartwidget.h"
21 #include "sievescriptblockwidget.h"
22 #include "autocreatescriptutil_p.h"
23 #include "commonwidgets/sievehelpbutton.h"
24 #include "autocreatescripts/autocreatescriptutil_p.h"
25 #include "editor/sieveeditorutil.h"
26 
27 #include <KLocalizedString>
28 #include <QLineEdit>
29 #include "widgets/lineeditvalidator.h"
30 
31 #include <QHBoxLayout>
32 #include <QLabel>
33 #include <QCheckBox>
34 #include <QWhatsThis>
35 #include <QXmlStreamReader>
36 #include "libksieve_debug.h"
37 
38 using namespace KSieveUi;
39 
40 SieveForEveryPartWidget::SieveForEveryPartWidget(QWidget *parent)
41  : SieveWidgetPageAbstract(parent)
42 {
43  QVBoxLayout *mainLayout = new QVBoxLayout(this);
44  QHBoxLayout *lay = new QHBoxLayout;
45  mainLayout->addLayout(lay);
46  mainLayout->addStretch(1);
47 
48  mHelpButton = new SieveHelpButton;
49  lay->addWidget(mHelpButton);
50  connect(mHelpButton, &SieveHelpButton::clicked, this, &SieveForEveryPartWidget::slotHelp);
51 
52  mForLoop = new QCheckBox(i18n("Add ForEveryPart loop"));
53  connect(mForLoop, &QCheckBox::toggled, this, &SieveForEveryPartWidget::valueChanged);
54  lay->addWidget(mForLoop);
55 
56  QLabel *lab = new QLabel(i18n("Name (optional):"));
57  lay->addWidget(lab);
58 
59  mName = new LineEditValidator;
60  connect(mName, &QLineEdit::textChanged, this, &SieveForEveryPartWidget::valueChanged);
61  mName->setEnabled(false);
62  lay->addWidget(mName);
63 
65  setPageType(KSieveUi::SieveScriptBlockWidget::ForEveryPart);
66 }
67 
68 SieveForEveryPartWidget::~SieveForEveryPartWidget()
69 {
70 }
71 
72 void SieveForEveryPartWidget::slotHelp()
73 {
74  const QString help = i18n(
75  "\"foreverypart\", which is an iterator that walks though every MIME part of a message, including nested parts, depth first, and applies the commands in the specified block to each of them.");
76  const QUrl href = KSieveUi::SieveEditorUtil::helpUrl(KSieveUi::SieveEditorUtil::ForEveryPart);
77  const QString fullWhatsThis = AutoCreateScriptUtil::createFullWhatsThis(help, href.toString());
78  QWhatsThis::showText(QCursor::pos(), fullWhatsThis, mHelpButton);
79 }
80 
81 void SieveForEveryPartWidget::generatedScript(QString &script, QStringList &required, bool inForEveryPartLoop)
82 {
83  Q_UNUSED(inForEveryPartLoop);
84  if (mForLoop->isChecked()) {
85  required << QStringLiteral("foreverypart");
86  const QString loopName = mName->text();
87  if (loopName.isEmpty()) {
88  script += QStringLiteral("foreverypart {");
89  } else {
90  script += QStringLiteral("foreverypart :name \"%1\" {").arg(loopName);
91  }
92  }
93 }
94 
95 void SieveForEveryPartWidget::loadScript(QXmlStreamReader &element, QString &error)
96 {
97  if (element.readNextStartElement()) {
98  const QStringRef tagName = element.name();
99  if (tagName == QLatin1String("tag")) {
100  const QString tagValue = element.readElementText();
101  if (tagValue == QLatin1String("name")) {
102  mName->setText(AutoCreateScriptUtil::strValue(element));
103  } else {
104  error += i18n("Unknown tagValue \"%1\" during loading loop \"for\"", tagValue) + QLatin1Char('\n');
105  qCDebug(LIBKSIEVE_LOG) << " SieveForEveryPartWidget::loadScript unknown tagValue " << tagValue;
106  }
107  mForLoop->setChecked(true);
108  mName->setEnabled(true);
109  } else if (tagName == QLatin1String("block")) {
110  //Nothing
111  //It's when name is empty
112  } else if (tagName == QLatin1String("crlf")) {
113  //Nothing
114  element.skipCurrentElement();
115  } else {
116  error += i18n("Unknown tag \"%1\" during loading loop \"for\"", tagName.toString()) + QLatin1Char('\n');
117  qCDebug(LIBKSIEVE_LOG) << " SieveForEveryPartWidget::loadScript unknown tagName " << tagName;
118  }
119  } else {
120  qCDebug(LIBKSIEVE_LOG) << " SieveForEveryPartWidget::loadScript problem during loading";
121  }
122 }
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)
void addWidget(QWidget *widget, int stretch, Qt::Alignment alignment)
const QList< QKeySequence > & help()
QString readElementText(QXmlStreamReader::ReadElementTextBehaviour behaviour)
bool isEmpty() const const
void clicked(bool checked)
QString i18n(const char *text, const TYPE &arg...)
QPoint pos()
void addStretch(int stretch)
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-2020 The KDE developers.
Generated on Mon Jul 13 2020 23:08:35 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.