Libksieve

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

KDE's Doxygen guidelines are available online.