Libksieve

sieveactionkeep.cpp
1 /*
2  SPDX-FileCopyrightText: 2013-2021 Laurent Montel <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 #include "sieveactionkeep.h"
7 #include "autocreatescripts/autocreatescriptutil_p.h"
8 #include "autocreatescripts/sieveeditorgraphicalmodewidget.h"
9 #include "editor/sieveeditorutil.h"
10 #include "widgets/selectflagswidget.h"
11 
12 #include "libksieve_debug.h"
13 #include <KLocalizedString>
14 #include <QHBoxLayout>
15 #include <QLabel>
16 #include <QXmlStreamReader>
17 
18 using namespace KSieveUi;
19 SieveActionKeep::SieveActionKeep(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QObject *parent)
20  : SieveAction(sieveGraphicalModeWidget, QStringLiteral("keep"), i18n("Keep"), parent)
21 {
22  mHasImapFlag4Support = sieveCapabilities().contains(QLatin1String("imap4flags"));
23  mHasFlagSupport = sieveCapabilities().contains(QLatin1String("imapflags")) || mHasImapFlag4Support;
24 }
25 
26 QString SieveActionKeep::code(QWidget *w) const
27 {
28  if (mHasFlagSupport) {
29  const SelectFlagsWidget *flagsWidget = w->findChild<SelectFlagsWidget *>(QStringLiteral("flagswidget"));
30  const QString flagCode = flagsWidget->code();
31  if (flagCode.isEmpty()) {
32  return QStringLiteral("keep;");
33  } else {
34  return QStringLiteral("keep :flags") + QLatin1Char(' ') + flagCode;
35  }
36  } else {
37  return QStringLiteral("keep;");
38  }
39 }
40 
41 QString SieveActionKeep::help() const
42 {
43  return i18n(
44  "The \"keep\" action is whatever action is taken in lieu of all other actions, if no filtering happens at all; generally, this simply means to file "
45  "the message into the user's main mailbox.");
46 }
47 
48 QWidget *SieveActionKeep::createParamWidget(QWidget *parent) const
49 {
50  if (mHasFlagSupport) {
51  auto w = new QWidget(parent);
52  auto lay = new QHBoxLayout;
53  lay->setContentsMargins({});
54  w->setLayout(lay);
55  auto addFlags = new QLabel(i18n("Add flags:"));
56  lay->addWidget(addFlags);
57 
58  auto flagsWidget = new SelectFlagsWidget;
59  connect(flagsWidget, &SelectFlagsWidget::valueChanged, this, &SieveActionKeep::valueChanged);
60  flagsWidget->setObjectName(QStringLiteral("flagswidget"));
61  lay->addWidget(flagsWidget);
62  return w;
63  } else {
64  return nullptr;
65  }
66 }
67 
68 void SieveActionKeep::setParamWidgetValue(QXmlStreamReader &element, QWidget *w, QString &error)
69 {
70  if (mHasFlagSupport) {
71  while (element.readNextStartElement()) {
72  const QStringRef tagName = element.name();
73  if (tagName == QLatin1String("list")) {
74  auto flagsWidget = w->findChild<SelectFlagsWidget *>(QStringLiteral("flagswidget"));
75  flagsWidget->setFlags(AutoCreateScriptUtil::listValue(element));
76  } else if (tagName == QLatin1String("str")) {
77  auto flagsWidget = w->findChild<SelectFlagsWidget *>(QStringLiteral("flagswidget"));
78  flagsWidget->setFlags(QStringList() << element.readElementText());
79  } else if (tagName == QLatin1String("tag") && element.readElementText() == QLatin1String("flags")) {
80  // nothing :)
81  // Don't skip here.
82  } else if (tagName == QLatin1String("crlf")) {
83  element.skipCurrentElement();
84  // nothing
85  } else if (tagName == QLatin1String("comment")) {
86  element.skipCurrentElement();
87  // implement in the future ?
88  } else {
89  unknownTag(tagName, error);
90  qCDebug(LIBKSIEVE_LOG) << " SieveActionAbstractFlags::setParamWidgetValue unknown tag :" << tagName;
91  }
92  }
93  } else {
94  qCDebug(LIBKSIEVE_LOG) << " Server doesn't support imapflags";
95  }
96 }
97 
98 QStringList SieveActionKeep::needRequires(QWidget *) const
99 {
100  QStringList requiresLst;
101  if (mHasImapFlag4Support) {
102  requiresLst << QStringLiteral("imap4flags");
103  } else if (mHasFlagSupport) {
104  requiresLst << QStringLiteral("imapflags");
105  }
106  return requiresLst;
107 }
108 
109 QUrl SieveActionKeep::href() const
110 {
111  return SieveEditorUtil::helpUrl(SieveEditorUtil::strToVariableName(name()));
112 }
bool readNextStartElement()
void setContentsMargins(int left, int top, int right, int bottom)
QString name(const QVariant &location)
void skipCurrentElement()
void setLayout(QLayout *layout)
QString readElementText(QXmlStreamReader::ReadElementTextBehaviour behaviour)
QString i18n(const char *text, const TYPE &arg...)
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
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-2021 The KDE developers.
Generated on Fri Apr 16 2021 23:09:34 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.