Libksieve

sieveactionconvert.cpp
1 /*
2  SPDX-FileCopyrightText: 2013-2021 Laurent Montel <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 #include "sieveactionconvert.h"
7 #include "autocreatescripts/autocreatescriptutil_p.h"
8 #include "autocreatescripts/commonwidgets/selectconvertparameterwidget.h"
9 #include "autocreatescripts/commonwidgets/selectmimetypecombobox.h"
10 
11 #include "libksieve_debug.h"
12 #include <KLocalizedString>
13 #include <QGridLayout>
14 #include <QLabel>
15 #include <QUrl>
16 #include <QXmlStreamReader>
17 
18 using namespace KSieveUi;
19 SieveActionConvert::SieveActionConvert(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QObject *parent)
20  : SieveAction(sieveGraphicalModeWidget, QStringLiteral("convert"), i18n("Convert"), parent)
21 {
22 }
23 
24 QWidget *SieveActionConvert::createParamWidget(QWidget *parent) const
25 {
26  auto w = new QWidget(parent);
27  auto lay = new QGridLayout;
28  lay->setContentsMargins({});
29  w->setLayout(lay);
30 
31  auto lab = new QLabel(i18n("From:"));
32  lay->addWidget(lab, 0, 0);
33 
34  auto fromMimeType = new SelectMimeTypeComboBox;
35  connect(fromMimeType, &SelectMimeTypeComboBox::valueChanged, this, &SieveActionConvert::valueChanged);
36  fromMimeType->setObjectName(QStringLiteral("from"));
37  lay->addWidget(fromMimeType, 0, 1);
38 
39  lab = new QLabel(i18n("To:"));
40  lay->addWidget(lab, 0, 2);
41 
42  auto toMimeType = new SelectMimeTypeComboBox;
43  connect(toMimeType, &SelectMimeTypeComboBox::valueChanged, this, &SieveActionConvert::valueChanged);
44  toMimeType->setObjectName(QStringLiteral("to"));
45  lay->addWidget(toMimeType, 0, 3);
46 
47  lab = new QLabel(i18n("Parameters:"));
48  lay->addWidget(lab, 1, 0);
49 
50  auto params = new SelectConvertParameterWidget;
51  connect(params, &SelectConvertParameterWidget::valueChanged, this, &SieveActionConvert::valueChanged);
52  params->setObjectName(QStringLiteral("params"));
53  lay->addWidget(params, 1, 1, 2, 3);
54 
55  return w;
56 }
57 
58 void SieveActionConvert::setParamWidgetValue(QXmlStreamReader &element, QWidget *w, QString &error)
59 {
60  int index = 0;
61  while (element.readNextStartElement()) {
62  const QStringRef tagName = element.name();
63  if (tagName == QLatin1String("str")) {
64  if (index == 0) {
65  auto fromMimeType = w->findChild<SelectMimeTypeComboBox *>(QStringLiteral("from"));
66  fromMimeType->setCode(element.readElementText(), name(), error);
67  } else if (index == 1) {
68  auto toMimeType = w->findChild<SelectMimeTypeComboBox *>(QStringLiteral("to"));
69  toMimeType->setCode(element.readElementText(), name(), error);
70  } else {
71  tooManyArguments(tagName, index, 2, error);
72  qCDebug(LIBKSIEVE_LOG) << " SieveActionConvert::setParamWidgetValue too many argument :" << index;
73  }
74  ++index;
75  } else if (tagName == QLatin1String("list")) {
76  auto params = w->findChild<SelectConvertParameterWidget *>(QStringLiteral("params"));
77  params->setCode(AutoCreateScriptUtil::listValue(element), error);
78  } else if (tagName == QLatin1String("crlf")) {
79  element.skipCurrentElement();
80  // nothing
81  } else if (tagName == QLatin1String("comment")) {
82  element.skipCurrentElement();
83  // implement in the future ?
84  } else {
85  unknownTag(tagName, error);
86  qCDebug(LIBKSIEVE_LOG) << "SieveActionConvert::setParamWidgetValue unknown tag " << tagName;
87  }
88  }
89 }
90 
91 QString SieveActionConvert::code(QWidget *w) const
92 {
93  QString result = QStringLiteral("convert ");
94  const SelectMimeTypeComboBox *fromMimeType = w->findChild<SelectMimeTypeComboBox *>(QStringLiteral("from"));
95  const QString fromMimeTypeStr = fromMimeType->code();
96  result += QStringLiteral("%1 ").arg(fromMimeTypeStr);
97 
98  const SelectMimeTypeComboBox *toMimeType = w->findChild<SelectMimeTypeComboBox *>(QStringLiteral("to"));
99  const QString toMimeTypeStr = toMimeType->code();
100  result += QStringLiteral("%1 ").arg(toMimeTypeStr);
101 
102  const SelectConvertParameterWidget *params = w->findChild<SelectConvertParameterWidget *>(QStringLiteral("params"));
103  const QString paramsStr = params->code();
104  if (!paramsStr.isEmpty()) {
105  result += paramsStr;
106  }
107  result += QLatin1Char(';');
108  return result;
109 }
110 
111 QStringList SieveActionConvert::needRequires(QWidget *) const
112 {
113  return QStringList() << QStringLiteral("convert");
114 }
115 
116 bool SieveActionConvert::needCheckIfServerHasCapability() const
117 {
118  return true;
119 }
120 
121 QString SieveActionConvert::serverNeedsCapability() const
122 {
123  return QStringLiteral("convert");
124 }
125 
126 QString SieveActionConvert::help() const
127 {
128  return i18n(
129  "The \"convert\" action specifies that all body parts with a media type equal to \"media-type\" be converted to the media type in \"media-type\" using "
130  "conversion parameters.");
131 }
132 
133 QUrl SieveActionConvert::href() const
134 {
135  return QUrl(QStringLiteral("https://tools.ietf.org/html/draft-ietf-sieve-convert-06"));
136 }
bool readNextStartElement()
void setContentsMargins(int left, int top, int right, int bottom)
QString name(const QVariant &location)
void skipCurrentElement()
QString readElementText(QXmlStreamReader::ReadElementTextBehaviour behaviour)
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)
QObject * parent() const const
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 Sun Apr 11 2021 23:09:36 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.