Libksieve

sieveconditionconvert.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 "sieveconditionconvert.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 <QXmlStreamReader>
28 #include "libksieve_debug.h"
29 #include <QGridLayout>
30 
31 using namespace KSieveUi;
32 SieveConditionConvert::SieveConditionConvert(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QObject *parent)
33  : SieveCondition(sieveGraphicalModeWidget, QStringLiteral("convert"), i18n("Convert"), parent)
34 {
35 }
36 
37 QWidget *SieveConditionConvert::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  fromMimeType->setObjectName(QStringLiteral("from"));
49  connect(fromMimeType, &SelectMimeTypeComboBox::valueChanged, this, &SieveConditionConvert::valueChanged);
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, &SieveConditionConvert::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, &SieveConditionConvert::valueChanged);
65  params->setObjectName(QStringLiteral("params"));
66  lay->addWidget(params, 1, 1, 2, 3);
67  return w;
68 }
69 
70 QString SieveConditionConvert::code(QWidget *w) const
71 {
72  QString result = QStringLiteral("convert ");
73  const SelectMimeTypeComboBox *fromMimeType = w->findChild<SelectMimeTypeComboBox *>(QStringLiteral("from"));
74  const QString fromMimeTypeStr = fromMimeType->code();
75  result += QStringLiteral("%1 ").arg(fromMimeTypeStr);
76 
77  const SelectMimeTypeComboBox *toMimeType = w->findChild<SelectMimeTypeComboBox *>(QStringLiteral("to"));
78  const QString toMimeTypeStr = toMimeType->code();
79  result += QStringLiteral("%1 ").arg(toMimeTypeStr);
80 
81  const SelectConvertParameterWidget *params = w->findChild<SelectConvertParameterWidget *>(QStringLiteral("params"));
82  const QString paramsStr = params->code();
83  if (!paramsStr.isEmpty()) {
84  result += paramsStr;
85  }
86  return result + AutoCreateScriptUtil::generateConditionComment(comment());
87 }
88 
89 QStringList SieveConditionConvert::needRequires(QWidget *) const
90 {
91  return QStringList() << QStringLiteral("convert");
92 }
93 
94 bool SieveConditionConvert::needCheckIfServerHasCapability() const
95 {
96  return true;
97 }
98 
99 QString SieveConditionConvert::serverNeedsCapability() const
100 {
101  return QStringLiteral("convert");
102 }
103 
104 QString SieveConditionConvert::help() const
105 {
106  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.");
107 }
108 
109 void SieveConditionConvert::setParamWidgetValue(QXmlStreamReader &element, QWidget *w, bool /*notCondition*/, QString &error)
110 {
111  int index = 0;
112  QString commentStr;
113  while (element.readNextStartElement()) {
114  const QStringRef tagName = element.name();
115  if (tagName == QLatin1String("str")) {
116  if (index == 0) {
117  SelectMimeTypeComboBox *fromMimeType = w->findChild<SelectMimeTypeComboBox *>(QStringLiteral("from"));
118  fromMimeType->setCode(element.readElementText(), name(), error);
119  } else if (index == 1) {
120  SelectMimeTypeComboBox *toMimeType = w->findChild<SelectMimeTypeComboBox *>(QStringLiteral("to"));
121  toMimeType->setCode(element.readElementText(), name(), error);
122  } else {
123  tooManyArguments(tagName, index, 2, error);
124  qCDebug(LIBKSIEVE_LOG) << " SieveActionConvert::setParamWidgetValue too many argument :" << index;
125  }
126  ++index;
127  } else if (tagName == QLatin1String("list")) {
128  SelectConvertParameterWidget *params = w->findChild<SelectConvertParameterWidget *>(QStringLiteral("params"));
129  params->setCode(AutoCreateScriptUtil::listValue(element), error);
130  } else if (tagName == QLatin1String("crlf")) {
131  element.skipCurrentElement();
132  //nothing
133  } else if (tagName == QLatin1String("comment")) {
134  commentStr = AutoCreateScriptUtil::loadConditionComment(commentStr, element.readElementText());
135  } else {
136  unknownTag(tagName, error);
137  qCDebug(LIBKSIEVE_LOG) << "SieveActionConvert::setParamWidgetValue unknown tag " << tagName;
138  }
139  }
140  if (!commentStr.isEmpty()) {
141  setComment(commentStr);
142  }
143 }
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)
bool isEmpty() 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)
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 Jul 13 2020 23:08:35 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.