Libksieve

sieveactionaddheader.cpp
1 /*
2  SPDX-FileCopyrightText: 2013-2021 Laurent Montel <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 #include "sieveactionaddheader.h"
7 #include "autocreatescripts/autocreatescriptutil_p.h"
8 #include "widgets/selectaddheaderpositioncombobox.h"
9 #include <Libkdepim/LineEditCatchReturnKey>
10 
11 #include <KLocalizedString>
12 #include <QLineEdit>
13 
14 #include "libksieve_debug.h"
15 #include <QGridLayout>
16 #include <QLabel>
17 #include <QUrl>
18 #include <QWidget>
19 #include <QXmlStreamReader>
20 
21 using namespace KSieveUi;
22 
23 SieveActionAddHeader::SieveActionAddHeader(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QObject *parent)
24  : SieveActionAbstractEditHeader(sieveGraphicalModeWidget, QStringLiteral("addheader"), i18n("Add header"), parent)
25 {
26 }
27 
28 QWidget *SieveActionAddHeader::createParamWidget(QWidget *parent) const
29 {
30  auto w = new QWidget(parent);
31  auto grid = new QGridLayout;
32  grid->setContentsMargins({});
33  w->setLayout(grid);
34 
35  auto combo = new SelectAddHeaderPositionCombobox;
36  combo->setObjectName(QStringLiteral("selectposition"));
37  connect(combo, &SelectAddHeaderPositionCombobox::valueChanged, this, &SieveActionAddHeader::valueChanged);
38  grid->addWidget(combo, 0, 0);
39 
40  auto lab = new QLabel(i18n("header:"));
41  grid->addWidget(lab, 0, 1);
42 
43  auto headerEdit = new QLineEdit;
44  new KPIM::LineEditCatchReturnKey(headerEdit, w);
45  connect(headerEdit, &QLineEdit::textChanged, this, &SieveActionAddHeader::valueChanged);
46  headerEdit->setObjectName(QStringLiteral("headeredit"));
47  grid->addWidget(headerEdit, 0, 2);
48 
49  lab = new QLabel(i18n("value:"));
50  grid->addWidget(lab, 1, 1);
51 
52  auto valueEdit = new QLineEdit;
53  new KPIM::LineEditCatchReturnKey(valueEdit, w);
54  connect(valueEdit, &QLineEdit::textChanged, this, &SieveActionAddHeader::valueChanged);
55  valueEdit->setObjectName(QStringLiteral("valueedit"));
56  grid->addWidget(valueEdit, 1, 2);
57 
58  return w;
59 }
60 
61 void SieveActionAddHeader::setParamWidgetValue(QXmlStreamReader &element, QWidget *w, QString &error)
62 {
63  int index = 0;
64  while (element.readNextStartElement()) {
65  const QStringRef tagName = element.name();
66  if (tagName == QLatin1String("tag")) {
67  auto combo = w->findChild<SelectAddHeaderPositionCombobox *>(QStringLiteral("selectposition"));
68  combo->setCode(AutoCreateScriptUtil::tagValue(element.readElementText()), name(), error);
69  } else if (tagName == QLatin1String("str")) {
70  if (index == 0) {
71  auto edit = w->findChild<QLineEdit *>(QStringLiteral("headeredit"));
72  edit->setText(element.readElementText());
73  } else if (index == 1) {
74  auto value = w->findChild<QLineEdit *>(QStringLiteral("valueedit"));
75  value->setText(AutoCreateScriptUtil::quoteStr(element.readElementText()));
76  } else {
77  tooManyArguments(tagName, index, 2, error);
78  qCDebug(LIBKSIEVE_LOG) << " SieveActionAddHeader::setParamWidgetValue too many argument :" << index;
79  }
80  ++index;
81  } else if (tagName == QLatin1String("crlf")) {
82  element.skipCurrentElement();
83  // nothing
84  } else if (tagName == QLatin1String("comment")) {
85  element.skipCurrentElement();
86  // implement in the future ?
87  } else {
88  unknownTag(tagName, error);
89  qCDebug(LIBKSIEVE_LOG) << "SieveActionAddHeader::setParamWidgetValue unknown tag " << tagName;
90  }
91  }
92 }
93 
94 QString SieveActionAddHeader::code(QWidget *w) const
95 {
96  const SelectAddHeaderPositionCombobox *combo = w->findChild<SelectAddHeaderPositionCombobox *>(QStringLiteral("selectposition"));
97  const QString position = combo->code();
98 
99  const QLineEdit *edit = w->findChild<QLineEdit *>(QStringLiteral("headeredit"));
100  const QString headerStr = edit->text();
101 
102  const QLineEdit *value = w->findChild<QLineEdit *>(QStringLiteral("valueedit"));
103  const QString valueStr = value->text();
104 
105  if (position.isEmpty()) {
106  return QStringLiteral("addheader \"%1\" \"%2\";").arg(headerStr, valueStr);
107  } else {
108  return QStringLiteral("addheader %1 \"%2\" \"%3\";").arg(position, headerStr, valueStr);
109  }
110 }
111 
112 QString SieveActionAddHeader::help() const
113 {
114  return i18n("The addheader action adds a header field to the existing message header.");
115 }
116 
117 QUrl SieveActionAddHeader::href() const
118 {
119  return QUrl(QStringLiteral("https://tools.ietf.org/html/rfc5293"));
120 }
bool readNextStartElement()
void setContentsMargins(int left, int top, int right, int bottom)
QString name(const QVariant &location)
void setText(const QString &)
void textChanged(const QString &text)
void skipCurrentElement()
QString readElementText(QXmlStreamReader::ReadElementTextBehaviour behaviour)
QString i18n(const char *text, const TYPE &arg...)
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.