Libksieve

sieveactionfileinto.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 "sieveactionfileinto.h"
20 #include "autocreatescripts/autocreatescriptutil_p.h"
21 #include "editor/sieveeditorutil.h"
22 #include "widgets/moveimapfolderwidget.h"
23 #include "autocreatescripts/sieveeditorgraphicalmodewidget.h"
24 #include <KLocalizedString>
25 
26 #include <QCheckBox>
27 #include <QHBoxLayout>
28 #include "libksieve_debug.h"
29 #include <QXmlStreamReader>
30 //Add support for adding flags
31 using namespace KSieveUi;
32 SieveActionFileInto::SieveActionFileInto(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QObject *parent)
33  : SieveAction(sieveGraphicalModeWidget, QStringLiteral("fileinto"), i18n("File Into"), parent)
34 {
35  mHasCopySupport = sieveCapabilities().contains(QLatin1String("copy"));
36  mHasMailBoxSupport = sieveCapabilities().contains(QLatin1String("mailbox"));
37 }
38 
39 QString SieveActionFileInto::code(QWidget *w) const
40 {
41  QString result = QStringLiteral("fileinto ");
42  const KSieveUi::AbstractMoveImapFolderWidget *edit = w->findChild<KSieveUi::AbstractMoveImapFolderWidget *>(QStringLiteral("fileintolineedit"));
43  const QString text = edit->text();
44  if (mHasCopySupport) {
45  const QCheckBox *copy = w->findChild<QCheckBox *>(QStringLiteral("copy"));
46  if (copy->isChecked()) {
47  result += QLatin1String(":copy ");
48  }
49  }
50  if (mHasMailBoxSupport) {
51  const QCheckBox *create = w->findChild<QCheckBox *>(QStringLiteral("create"));
52  if (create->isChecked()) {
53  result += QLatin1String(":create ");
54  }
55  }
56  return result + QStringLiteral("\"%1\";").arg(text);
57 }
58 
59 void SieveActionFileInto::setParamWidgetValue(QXmlStreamReader &element, QWidget *w, QString &error)
60 {
61  while (element.readNextStartElement()) {
62  const QStringRef tagName = element.name();
63  if (tagName == QLatin1String("tag")) {
64  const QString tagValue = element.readElementText();
65  if (tagValue == QLatin1String("copy")) {
66  if (mHasCopySupport) {
67  QCheckBox *copy = w->findChild<QCheckBox *>(QStringLiteral("copy"));
68  copy->setChecked(true);
69  } else {
70  error += i18n("Action \"fileinto\" has \"copy\" argument but current server does not support it") + QLatin1Char('\n');
71  qCDebug(LIBKSIEVE_LOG) << "SieveActionFileInto::setParamWidgetValue has not copy support ";
72  }
73  } else if (tagValue == QLatin1String("create")) {
74  if (mHasMailBoxSupport) {
75  QCheckBox *create = w->findChild<QCheckBox *>(QStringLiteral("create"));
76  create->setChecked(true);
77  } else {
78  serverDoesNotSupportFeatures(QStringLiteral("create"), error);
79  qCDebug(LIBKSIEVE_LOG) << "SieveActionFileInto::setParamWidgetValue server has not create support ";
80  }
81  } else {
82  serverDoesNotSupportFeatures(tagValue, error);
83  qCDebug(LIBKSIEVE_LOG) << "SieveActionFileInto::setParamWidgetValue server has not flags support ";
84  }
85  } else if (tagName == QLatin1String("str")) {
86  const QString tagValue = element.readElementText();
87  KSieveUi::AbstractMoveImapFolderWidget *edit = w->findChild<KSieveUi::AbstractMoveImapFolderWidget *>(QStringLiteral("fileintolineedit"));
88  edit->setText(AutoCreateScriptUtil::protectSlash(tagValue));
89  } else if (tagName == QLatin1String("crlf")) {
90  element.skipCurrentElement();
91  //nothing
92  } else if (tagName == QLatin1String("comment")) {
93  element.skipCurrentElement();
94  //implement in the future ?
95  } else {
96  unknownTag(tagName, error);
97  qCDebug(LIBKSIEVE_LOG) << " SieveActionFileInto::setParamWidgetValue unknown tagName " << tagName;
98  }
99  }
100 }
101 
102 QWidget *SieveActionFileInto::createParamWidget(QWidget *parent) const
103 {
104  QWidget *w = new QWidget(parent);
105  QHBoxLayout *lay = new QHBoxLayout(w);
106  lay->setContentsMargins(0, 0, 0, 0);
107 
108  if (mHasCopySupport) {
109  QCheckBox *copy = new QCheckBox(i18n("Keep a copy"));
110  copy->setObjectName(QStringLiteral("copy"));
111  lay->addWidget(copy);
112  connect(copy, &QCheckBox::clicked, this, &SieveActionFileInto::valueChanged);
113  }
114  if (mHasMailBoxSupport) {
115  QCheckBox *create = new QCheckBox(i18n("Create folder"));
116  create->setObjectName(QStringLiteral("create"));
117  connect(create, &QCheckBox::clicked, this, &SieveActionFileInto::valueChanged);
118  lay->addWidget(create);
119  }
120 
121  KSieveUi::AbstractMoveImapFolderWidget *edit = AutoCreateScriptUtil::createImapFolderWidget();
122  edit->setSieveImapAccountSettings(sieveImapAccountSettings());
123  connect(edit, &KSieveUi::AbstractMoveImapFolderWidget::textChanged, this, &SieveActionFileInto::valueChanged);
124  lay->addWidget(edit);
125  edit->setObjectName(QStringLiteral("fileintolineedit"));
126  return w;
127 }
128 
129 QStringList SieveActionFileInto::needRequires(QWidget *parent) const
130 {
131  QStringList lst;
132  lst << QStringLiteral("fileinto");
133  if (mHasCopySupport) {
134  const QCheckBox *copy = parent->findChild<QCheckBox *>(QStringLiteral("copy"));
135  if (copy->isChecked()) {
136  lst << QStringLiteral("copy");
137  }
138  }
139  if (mHasMailBoxSupport) {
140  const QCheckBox *create = parent->findChild<QCheckBox *>(QStringLiteral("create"));
141  if (create->isChecked()) {
142  lst << QStringLiteral("mailbox");
143  }
144  }
145  return lst;
146 }
147 
148 bool SieveActionFileInto::needCheckIfServerHasCapability() const
149 {
150  return true;
151 }
152 
153 QString SieveActionFileInto::serverNeedsCapability() const
154 {
155  return QStringLiteral("fileinto");
156 }
157 
158 QString SieveActionFileInto::help() const
159 {
160  QString helpStr = i18n("The \"fileinto\" action delivers the message into the specified mailbox.");
161  if (mHasMailBoxSupport) {
162  helpStr += QLatin1Char('\n') + i18n(
163  "If the optional \":create\" argument is specified, it instructs the Sieve interpreter to create the specified mailbox, if needed, before attempting to deliver the message into the specified mailbox.");
164  }
165  if (mHasCopySupport) {
166  helpStr += QLatin1Char('\n') + i18n(
167  "If the optional \":copy\" keyword is specified, the tagged command does not cancel the implicit \"keep\". Instead, it merely files or redirects a copy in addition to whatever else is happening to the message.");
168  }
169  return helpStr;
170 }
171 
172 QUrl KSieveUi::SieveActionFileInto::href() const
173 {
174  return SieveEditorUtil::helpUrl(SieveEditorUtil::strToVariableName(name()));
175 }
bool readNextStartElement()
void setContentsMargins(int left, int top, int right, int bottom)
QString name(const QVariant &location)
void skipCurrentElement()
KIOCORE_EXPORT CopyJob * copy(const QUrl &src, const QUrl &dest, JobFlags flags=DefaultFlags)
void addWidget(QWidget *widget, int stretch, Qt::Alignment alignment)
QString readElementText(QXmlStreamReader::ReadElementTextBehaviour behaviour)
void setObjectName(const QString &name)
void clicked(bool checked)
The AbstractMoveImapFolderWidget class.
KDEGAMES_EXPORT QAction * create(StandardGameAction id, const QObject *recvr, const char *slot, QObject *parent)
bool isChecked() const const
QString i18n(const char *text, const TYPE &arg...)
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
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 Fri Jun 5 2020 23:09:20 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.