Libksieve

sieveactionconvert.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 "sieveactionconvert.h"
20 #include "autocreatescripts/autocreatescriptutil_p.h"
21 #include "autocreatescripts/commonwidgets/selectconvertparameterwidget.h"
22 #include "autocreatescripts/commonwidgets/selectmimetypecombobox.h"
23 
24 #include <KLocalizedString>
25 
26 #include <QLabel>
27 #include "libksieve_debug.h"
28 #include <QXmlStreamReader>
29 #include <QGridLayout>
30 
31 using namespace KSieveUi;
32 SieveActionConvert::SieveActionConvert(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QObject *parent)
33  : SieveAction(sieveGraphicalModeWidget, QStringLiteral("convert"), i18n("Convert"), parent)
34 {
35 }
36 
37 QWidget *SieveActionConvert::createParamWidget(QWidget *parent) const
38 {
39  QWidget *w = new QWidget(parent);
40  QGridLayout *lay = new QGridLayout;
41  lay->setContentsMargins(0, 0, 0, 0);
42  w->setLayout(lay);
43 
44  QLabel *lab = new QLabel(i18n("From:"));
45  lay->addWidget(lab, 0, 0);
46 
47  SelectMimeTypeComboBox *fromMimeType = new SelectMimeTypeComboBox;
48  connect(fromMimeType, &SelectMimeTypeComboBox::valueChanged, this, &SieveActionConvert::valueChanged);
49  fromMimeType->setObjectName(QStringLiteral("from"));
50  lay->addWidget(fromMimeType, 0, 1);
51 
52  lab = new QLabel(i18n("To:"));
53  lay->addWidget(lab, 0, 2);
54 
55  SelectMimeTypeComboBox *toMimeType = new SelectMimeTypeComboBox;
56  connect(toMimeType, &SelectMimeTypeComboBox::valueChanged, this, &SieveActionConvert::valueChanged);
57  toMimeType->setObjectName(QStringLiteral("to"));
58  lay->addWidget(toMimeType, 0, 3);
59 
60  lab = new QLabel(i18n("Parameters:"));
61  lay->addWidget(lab, 1, 0);
62 
63  SelectConvertParameterWidget *params = new SelectConvertParameterWidget;
64  connect(params, &SelectConvertParameterWidget::valueChanged, this, &SieveActionConvert::valueChanged);
65  params->setObjectName(QStringLiteral("params"));
66  lay->addWidget(params, 1, 1, 2, 3);
67 
68  return w;
69 }
70 
71 void SieveActionConvert::setParamWidgetValue(QXmlStreamReader &element, QWidget *w, QString &error)
72 {
73  int index = 0;
74  while (element.readNextStartElement()) {
75  const QStringRef tagName = element.name();
76  if (tagName == QLatin1String("str")) {
77  if (index == 0) {
78  SelectMimeTypeComboBox *fromMimeType = w->findChild<SelectMimeTypeComboBox *>(QStringLiteral("from"));
79  fromMimeType->setCode(element.readElementText(), name(), error);
80  } else if (index == 1) {
81  SelectMimeTypeComboBox *toMimeType = w->findChild<SelectMimeTypeComboBox *>(QStringLiteral("to"));
82  toMimeType->setCode(element.readElementText(), name(), error);
83  } else {
84  tooManyArguments(tagName, index, 2, error);
85  qCDebug(LIBKSIEVE_LOG) << " SieveActionConvert::setParamWidgetValue too many argument :" << index;
86  }
87  ++index;
88  } else if (tagName == QLatin1String("list")) {
89  SelectConvertParameterWidget *params = w->findChild<SelectConvertParameterWidget *>(QStringLiteral("params"));
90  params->setCode(AutoCreateScriptUtil::listValue(element), error);
91  } else if (tagName == QLatin1String("crlf")) {
92  element.skipCurrentElement();
93  //nothing
94  } else if (tagName == QLatin1String("comment")) {
95  element.skipCurrentElement();
96  //implement in the future ?
97  } else {
98  unknownTag(tagName, error);
99  qCDebug(LIBKSIEVE_LOG) << "SieveActionConvert::setParamWidgetValue unknown tag " << tagName;
100  }
101  }
102 }
103 
104 QString SieveActionConvert::code(QWidget *w) const
105 {
106  QString result = QStringLiteral("convert ");
107  const SelectMimeTypeComboBox *fromMimeType = w->findChild<SelectMimeTypeComboBox *>(QStringLiteral("from"));
108  const QString fromMimeTypeStr = fromMimeType->code();
109  result += QStringLiteral("%1 ").arg(fromMimeTypeStr);
110 
111  const SelectMimeTypeComboBox *toMimeType = w->findChild<SelectMimeTypeComboBox *>(QStringLiteral("to"));
112  const QString toMimeTypeStr = toMimeType->code();
113  result += QStringLiteral("%1 ").arg(toMimeTypeStr);
114 
115  const SelectConvertParameterWidget *params = w->findChild<SelectConvertParameterWidget *>(QStringLiteral("params"));
116  const QString paramsStr = params->code();
117  if (!paramsStr.isEmpty()) {
118  result += paramsStr;
119  }
120  result += QLatin1Char(';');
121  return result;
122 }
123 
124 QStringList SieveActionConvert::needRequires(QWidget *) const
125 {
126  return QStringList() << QStringLiteral("convert");
127 }
128 
129 bool SieveActionConvert::needCheckIfServerHasCapability() const
130 {
131  return true;
132 }
133 
134 QString SieveActionConvert::serverNeedsCapability() const
135 {
136  return QStringLiteral("convert");
137 }
138 
139 QString SieveActionConvert::help() const
140 {
141  return i18n("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 conversion parameters.");
142 }
143 
144 QUrl SieveActionConvert::href() const
145 {
146  return QUrl(QStringLiteral("https://tools.ietf.org/html/draft-ietf-sieve-convert-06"));
147 }
bool readNextStartElement()
void setContentsMargins(int left, int top, int right, int bottom)
QString name(const QVariant &location)
void addWidget(QWidget *widget, int row, int column, Qt::Alignment alignment)
void skipCurrentElement()
void setLayout(QLayout *layout)
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-2020 The KDE developers.
Generated on Mon Aug 10 2020 23:12:59 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.