Libksieve

sieveconditionconvert.cpp
1 /*
2  SPDX-FileCopyrightText: 2013-2021 Laurent Montel <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 #include "sieveconditionconvert.h"
7 #include "autocreatescripts/autocreatescriptutil_p.h"
8 #include "autocreatescripts/commonwidgets/selectconvertparameterwidget.h"
9 #include "autocreatescripts/commonwidgets/selectmimetypecombobox.h"
10 
11 #include <KLocalizedString>
12 
13 #include "libksieve_debug.h"
14 #include <QGridLayout>
15 #include <QLabel>
16 #include <QXmlStreamReader>
17 
18 using namespace KSieveUi;
19 SieveConditionConvert::SieveConditionConvert(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QObject *parent)
20  : SieveCondition(sieveGraphicalModeWidget, QStringLiteral("convert"), i18n("Convert"), parent)
21 {
22 }
23 
24 QWidget *SieveConditionConvert::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  fromMimeType->setObjectName(QStringLiteral("from"));
36  connect(fromMimeType, &SelectMimeTypeComboBox::valueChanged, this, &SieveConditionConvert::valueChanged);
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, &SieveConditionConvert::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, &SieveConditionConvert::valueChanged);
52  params->setObjectName(QStringLiteral("params"));
53  lay->addWidget(params, 1, 1, 2, 3);
54  return w;
55 }
56 
57 QString SieveConditionConvert::code(QWidget *w) const
58 {
59  QString result = QStringLiteral("convert ");
60  const SelectMimeTypeComboBox *fromMimeType = w->findChild<SelectMimeTypeComboBox *>(QStringLiteral("from"));
61  const QString fromMimeTypeStr = fromMimeType->code();
62  result += QStringLiteral("%1 ").arg(fromMimeTypeStr);
63 
64  const SelectMimeTypeComboBox *toMimeType = w->findChild<SelectMimeTypeComboBox *>(QStringLiteral("to"));
65  const QString toMimeTypeStr = toMimeType->code();
66  result += QStringLiteral("%1 ").arg(toMimeTypeStr);
67 
68  const SelectConvertParameterWidget *params = w->findChild<SelectConvertParameterWidget *>(QStringLiteral("params"));
69  const QString paramsStr = params->code();
70  if (!paramsStr.isEmpty()) {
71  result += paramsStr;
72  }
73  return result + AutoCreateScriptUtil::generateConditionComment(comment());
74 }
75 
76 QStringList SieveConditionConvert::needRequires(QWidget *) const
77 {
78  return QStringList() << QStringLiteral("convert");
79 }
80 
81 bool SieveConditionConvert::needCheckIfServerHasCapability() const
82 {
83  return true;
84 }
85 
86 QString SieveConditionConvert::serverNeedsCapability() const
87 {
88  return QStringLiteral("convert");
89 }
90 
91 QString SieveConditionConvert::help() const
92 {
93  return i18n(
94  "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 "
95  "conversion parameters.");
96 }
97 
98 void SieveConditionConvert::setParamWidgetValue(QXmlStreamReader &element, QWidget *w, bool /*notCondition*/, QString &error)
99 {
100  int index = 0;
101  QString commentStr;
102  while (element.readNextStartElement()) {
103  const QStringRef tagName = element.name();
104  if (tagName == QLatin1String("str")) {
105  if (index == 0) {
106  auto fromMimeType = w->findChild<SelectMimeTypeComboBox *>(QStringLiteral("from"));
107  fromMimeType->setCode(element.readElementText(), name(), error);
108  } else if (index == 1) {
109  auto toMimeType = w->findChild<SelectMimeTypeComboBox *>(QStringLiteral("to"));
110  toMimeType->setCode(element.readElementText(), name(), error);
111  } else {
112  tooManyArguments(tagName, index, 2, error);
113  qCDebug(LIBKSIEVE_LOG) << " SieveActionConvert::setParamWidgetValue too many argument :" << index;
114  }
115  ++index;
116  } else if (tagName == QLatin1String("list")) {
117  auto params = w->findChild<SelectConvertParameterWidget *>(QStringLiteral("params"));
118  params->setCode(AutoCreateScriptUtil::listValue(element), error);
119  } else if (tagName == QLatin1String("crlf")) {
120  element.skipCurrentElement();
121  // nothing
122  } else if (tagName == QLatin1String("comment")) {
123  commentStr = AutoCreateScriptUtil::loadConditionComment(commentStr, element.readElementText());
124  } else {
125  unknownTag(tagName, error);
126  qCDebug(LIBKSIEVE_LOG) << "SieveActionConvert::setParamWidgetValue unknown tag " << tagName;
127  }
128  }
129  if (!commentStr.isEmpty()) {
130  setComment(commentStr);
131  }
132 }
bool readNextStartElement()
void setContentsMargins(int left, int top, int right, int bottom)
QString name(const QVariant &location)
void skipCurrentElement()
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-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.