Libksieve

sieveactionkeep.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 #include "sieveactionkeep.h"
20 #include "editor/sieveeditorutil.h"
21 #include "widgets/selectflagswidget.h"
22 #include "autocreatescripts/autocreatescriptutil_p.h"
23 #include "autocreatescripts/sieveeditorgraphicalmodewidget.h"
24 
25 #include <KLocalizedString>
26 #include <QLabel>
27 #include <QHBoxLayout>
28 #include <QXmlStreamReader>
29 #include "libksieve_debug.h"
30 
31 using namespace KSieveUi;
32 SieveActionKeep::SieveActionKeep(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QObject *parent)
33  : SieveAction(sieveGraphicalModeWidget, QStringLiteral("keep"), i18n("Keep"), parent)
34 {
35  mHasImapFlag4Support = sieveCapabilities().contains(QLatin1String("imap4flags"));
36  mHasFlagSupport = sieveCapabilities().contains(QLatin1String("imapflags")) || mHasImapFlag4Support;
37 }
38 
39 QString SieveActionKeep::code(QWidget *w) const
40 {
41  if (mHasFlagSupport) {
42  const SelectFlagsWidget *flagsWidget = w->findChild<SelectFlagsWidget *>(QStringLiteral("flagswidget"));
43  const QString flagCode = flagsWidget->code();
44  if (flagCode.isEmpty()) {
45  return QStringLiteral("keep;");
46  } else {
47  return QStringLiteral("keep :flags") + QLatin1Char(' ') + flagCode;
48  }
49  } else {
50  return QStringLiteral("keep;");
51  }
52 }
53 
54 QString SieveActionKeep::help() const
55 {
56  return i18n(
57  "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 the message into the user's main mailbox.");
58 }
59 
60 QWidget *SieveActionKeep::createParamWidget(QWidget *parent) const
61 {
62  if (mHasFlagSupport) {
63  QWidget *w = new QWidget(parent);
64  QHBoxLayout *lay = new QHBoxLayout;
65  lay->setContentsMargins(0, 0, 0, 0);
66  w->setLayout(lay);
67  QLabel *addFlags = new QLabel(i18n("Add flags:"));
68  lay->addWidget(addFlags);
69 
70  SelectFlagsWidget *flagsWidget = new SelectFlagsWidget;
71  connect(flagsWidget, &SelectFlagsWidget::valueChanged, this, &SieveActionKeep::valueChanged);
72  flagsWidget->setObjectName(QStringLiteral("flagswidget"));
73  lay->addWidget(flagsWidget);
74  return w;
75  } else {
76  return nullptr;
77  }
78 }
79 
80 void SieveActionKeep::setParamWidgetValue(QXmlStreamReader &element, QWidget *w, QString &error)
81 {
82  if (mHasFlagSupport) {
83  while (element.readNextStartElement()) {
84  const QStringRef tagName = element.name();
85  if (tagName == QLatin1String("list")) {
86  SelectFlagsWidget *flagsWidget = w->findChild<SelectFlagsWidget *>(QStringLiteral("flagswidget"));
87  flagsWidget->setFlags(AutoCreateScriptUtil::listValue(element));
88  } else if (tagName == QLatin1String("str")) {
89  SelectFlagsWidget *flagsWidget = w->findChild<SelectFlagsWidget *>(QStringLiteral("flagswidget"));
90  flagsWidget->setFlags(QStringList() << element.readElementText());
91  } else if (tagName == QLatin1String("tag") && element.readElementText() == QLatin1String("flags")) {
92  //nothing :)
93  //Don't skip here.
94  } else if (tagName == QLatin1String("crlf")) {
95  element.skipCurrentElement();
96  //nothing
97  } else if (tagName == QLatin1String("comment")) {
98  element.skipCurrentElement();
99  //implement in the future ?
100  } else {
101  unknownTag(tagName, error);
102  qCDebug(LIBKSIEVE_LOG) << " SieveActionAbstractFlags::setParamWidgetValue unknown tag :" << tagName;
103  }
104  }
105  } else {
106  qCDebug(LIBKSIEVE_LOG) << " Server doesn't support imapflags";
107  }
108 }
109 
110 QStringList SieveActionKeep::needRequires(QWidget *) const
111 {
112  QStringList requiresLst;
113  if (mHasImapFlag4Support) {
114  requiresLst << QStringLiteral("imap4flags");
115  } else if (mHasFlagSupport) {
116  requiresLst << QStringLiteral("imapflags");
117  }
118  return requiresLst;
119 }
120 
121 QUrl SieveActionKeep::href() const
122 {
123  return SieveEditorUtil::helpUrl(SieveEditorUtil::strToVariableName(name()));
124 }
bool readNextStartElement()
void setContentsMargins(int left, int top, int right, int bottom)
QString name(StandardShortcut id)
void skipCurrentElement()
void addWidget(QWidget *widget, int stretch, Qt::Alignment alignment)
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-2020 The KDE developers.
Generated on Sun May 31 2020 22:46:48 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.