Libksieve

sieveactionaddheader.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 "sieveactionaddheader.h"
20 #include "autocreatescripts/autocreatescriptutil_p.h"
21 #include "widgets/selectaddheaderpositioncombobox.h"
22 
23 #include <KLocalizedString>
24 #include <QLineEdit>
25 
26 #include <QWidget>
27 #include <QLabel>
28 #include <QXmlStreamReader>
29 #include "libksieve_debug.h"
30 #include <QGridLayout>
31 
32 using namespace KSieveUi;
33 
34 SieveActionAddHeader::SieveActionAddHeader(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QObject *parent)
35  : SieveActionAbstractEditHeader(sieveGraphicalModeWidget, QStringLiteral("addheader"), i18n("Add header"), parent)
36 {
37 }
38 
39 QWidget *SieveActionAddHeader::createParamWidget(QWidget *parent) const
40 {
41  QWidget *w = new QWidget(parent);
42  QGridLayout *grid = new QGridLayout;
43  grid->setContentsMargins(0, 0, 0, 0);
44  w->setLayout(grid);
45 
46  SelectAddHeaderPositionCombobox *combo = new SelectAddHeaderPositionCombobox;
47  combo->setObjectName(QStringLiteral("selectposition"));
48  connect(combo, &SelectAddHeaderPositionCombobox::valueChanged, this, &SieveActionAddHeader::valueChanged);
49  grid->addWidget(combo, 0, 0);
50 
51  QLabel *lab = new QLabel(i18n("header:"));
52  grid->addWidget(lab, 0, 1);
53 
54  QLineEdit *headerEdit = new QLineEdit;
55  connect(headerEdit, &QLineEdit::textChanged, this, &SieveActionAddHeader::valueChanged);
56  headerEdit->setObjectName(QStringLiteral("headeredit"));
57  grid->addWidget(headerEdit, 0, 2);
58 
59  lab = new QLabel(i18n("value:"));
60  grid->addWidget(lab, 1, 1);
61 
62  QLineEdit *valueEdit = new QLineEdit;
63  connect(valueEdit, &QLineEdit::textChanged, this, &SieveActionAddHeader::valueChanged);
64  valueEdit->setObjectName(QStringLiteral("valueedit"));
65  grid->addWidget(valueEdit, 1, 2);
66 
67  return w;
68 }
69 
70 void SieveActionAddHeader::setParamWidgetValue(QXmlStreamReader &element, QWidget *w, QString &error)
71 {
72  int index = 0;
73  while (element.readNextStartElement()) {
74  const QStringRef tagName = element.name();
75  if (tagName == QLatin1String("tag")) {
76  SelectAddHeaderPositionCombobox *combo = w->findChild<SelectAddHeaderPositionCombobox *>(QStringLiteral("selectposition"));
77  combo->setCode(AutoCreateScriptUtil::tagValue(element.readElementText()), name(), error);
78  } else if (tagName == QLatin1String("str")) {
79  if (index == 0) {
80  QLineEdit *edit = w->findChild<QLineEdit *>(QStringLiteral("headeredit"));
81  edit->setText(element.readElementText());
82  } else if (index == 1) {
83  QLineEdit *value = w->findChild<QLineEdit *>(QStringLiteral("valueedit"));
84  value->setText(AutoCreateScriptUtil::quoteStr(element.readElementText()));
85  } else {
86  tooManyArguments(tagName, index, 2, error);
87  qCDebug(LIBKSIEVE_LOG) << " SieveActionAddHeader::setParamWidgetValue too many argument :" << index;
88  }
89  ++index;
90  } else if (tagName == QLatin1String("crlf")) {
91  element.skipCurrentElement();
92  //nothing
93  } else if (tagName == QLatin1String("comment")) {
94  element.skipCurrentElement();
95  //implement in the future ?
96  } else {
97  unknownTag(tagName, error);
98  qCDebug(LIBKSIEVE_LOG) << "SieveActionAddHeader::setParamWidgetValue unknown tag " << tagName;
99  }
100  }
101 }
102 
103 QString SieveActionAddHeader::code(QWidget *w) const
104 {
105  const SelectAddHeaderPositionCombobox *combo = w->findChild<SelectAddHeaderPositionCombobox *>(QStringLiteral("selectposition"));
106  const QString position = combo->code();
107 
108  const QLineEdit *edit = w->findChild<QLineEdit *>(QStringLiteral("headeredit"));
109  const QString headerStr = edit->text();
110 
111  const QLineEdit *value = w->findChild<QLineEdit *>(QStringLiteral("valueedit"));
112  const QString valueStr = value->text();
113 
114  if (position.isEmpty()) {
115  return QStringLiteral("addheader \"%1\" \"%2\";").arg(headerStr, valueStr);
116  } else {
117  return QStringLiteral("addheader %1 \"%2\" \"%3\";").arg(position, headerStr, valueStr);
118  }
119 }
120 
121 QString SieveActionAddHeader::help() const
122 {
123  return i18n("The addheader action adds a header field to the existing message header.");
124 }
125 
126 QUrl SieveActionAddHeader::href() const
127 {
128  return QUrl(QStringLiteral("https://tools.ietf.org/html/rfc5293"));
129 }
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 setText(const QString &)
void textChanged(const QString &text)
void skipCurrentElement()
void setLayout(QLayout *layout)
QString readElementText(QXmlStreamReader::ReadElementTextBehaviour behaviour)
void setObjectName(const QString &name)
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-2020 The KDE developers.
Generated on Fri Jun 5 2020 23:09:20 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.