Libksieve

sieveactiondeleteheader.cpp
1 /*
2  SPDX-FileCopyrightText: 2013-2021 Laurent Montel <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 #include "sieveactiondeleteheader.h"
7 #include "autocreatescripts/autocreatescriptutil_p.h"
8 #include "autocreatescripts/commonwidgets/selectmatchtypecombobox.h"
9 #include "editor/sieveeditorutil.h"
10 
11 #include <KLocalizedString>
12 #include <Libkdepim/LineEditCatchReturnKey>
13 #include <QLineEdit>
14 
15 #include "libksieve_debug.h"
16 #include <QGridLayout>
17 #include <QLabel>
18 #include <QWidget>
19 #include <QXmlStreamReader>
20 
21 using namespace KSieveUi;
22 
23 SieveActionDeleteHeader::SieveActionDeleteHeader(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QObject *parent)
24  : SieveActionAbstractEditHeader(sieveGraphicalModeWidget, QStringLiteral("deleteheader"), i18n("Delete header"), parent)
25 {
26 }
27 
28 QWidget *SieveActionDeleteHeader::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 matchType = new SelectMatchTypeComboBox(mSieveGraphicalModeWidget);
36  matchType->setObjectName(QStringLiteral("matchtype"));
37  connect(matchType, &SelectMatchTypeComboBox::valueChanged, this, &SieveActionDeleteHeader::valueChanged);
38  grid->addWidget(matchType, 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  headerEdit->setObjectName(QStringLiteral("headeredit"));
46  connect(headerEdit, &QLineEdit::textChanged, this, &SieveActionDeleteHeader::valueChanged);
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  valueEdit->setObjectName(QStringLiteral("valueedit"));
55  connect(valueEdit, &QLineEdit::textChanged, this, &SieveActionDeleteHeader::valueChanged);
56  grid->addWidget(valueEdit, 1, 2);
57  return w;
58 }
59 
60 void SieveActionDeleteHeader::parseValue(QXmlStreamReader &element, QWidget *w, QString &error, bool isNegative)
61 {
62  int index = 0;
63  while (element.readNextStartElement()) {
64  const QStringRef tagName = element.name();
65  if (tagName == QLatin1String("test")) {
66  QString nameValue;
67  if (element.attributes().hasAttribute(QLatin1String("name"))) {
68  nameValue = element.attributes().value(QLatin1String("name")).toString();
69  }
70  const bool isNegative = (nameValue == QLatin1String("not"));
71  parseValue(element, w, error, isNegative);
72  element.skipCurrentElement();
73  return;
74  } else if (tagName == QLatin1String("tag")) {
75  auto combo = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("matchtype"));
76  combo->setCode(AutoCreateScriptUtil::tagValueWithCondition(element.readElementText(), isNegative), name(), error);
77  } else if (tagName == QLatin1String("str")) {
78  if (index == 0) {
79  auto edit = w->findChild<QLineEdit *>(QStringLiteral("headeredit"));
80  edit->setText(element.readElementText());
81  } else if (index == 1) {
82  auto value = w->findChild<QLineEdit *>(QStringLiteral("valueedit"));
83  value->setText(element.readElementText());
84  } else {
85  tooManyArguments(tagName, index, 2, error);
86  qCDebug(LIBKSIEVE_LOG) << " SieveActionAddHeader::setParamWidgetValue too many argument :" << index;
87  }
88  ++index;
89  } else if (tagName == QLatin1String("crlf")) {
90  element.skipCurrentElement();
91  // nothing
92  } else if (tagName == QLatin1String("comment")) {
93  element.skipCurrentElement();
94  // implement in the future ?
95  } else {
96  unknownTag(tagName, error);
97  qCDebug(LIBKSIEVE_LOG) << "SieveActionAddHeader::setParamWidgetValue unknown tag " << tagName;
98  }
99  }
100 }
101 
102 void SieveActionDeleteHeader::setParamWidgetValue(QXmlStreamReader &element, QWidget *w, QString &error)
103 {
104  parseValue(element, w, error, false);
105 }
106 
107 QString SieveActionDeleteHeader::code(QWidget *w) const
108 {
109  const SelectMatchTypeComboBox *combo = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("matchtype"));
110  bool isNegative = false;
111  const QString matchTypeStr = combo->code(isNegative);
112 
113  const QLineEdit *edit = w->findChild<QLineEdit *>(QStringLiteral("headeredit"));
114  const QString headerStr = edit->text();
115 
116  const QLineEdit *value = w->findChild<QLineEdit *>(QStringLiteral("valueedit"));
117  const QString valueStr = value->text();
118 
119  return QStringLiteral("deleteheader %1 \"%2\" \"%3\";").arg((isNegative ? QLatin1String("not ") + matchTypeStr : matchTypeStr), headerStr, valueStr);
120 }
121 
122 QString SieveActionDeleteHeader::help() const
123 {
124  return i18n("By default, the deleteheader action deletes all occurrences of the named header field.");
125 }
126 
127 QUrl SieveActionDeleteHeader::href() const
128 {
129  return SieveEditorUtil::helpUrl(SieveEditorUtil::strToVariableName(name()));
130 }
131 
132 QStringList SieveActionDeleteHeader::needRequires(QWidget *w) const
133 {
134  const SelectMatchTypeComboBox *combo = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("matchtype"));
135  return SieveActionAbstractEditHeader::needRequires(w) + combo->needRequires();
136 }
bool readNextStartElement()
void setContentsMargins(int left, int top, int right, int bottom)
QString name(const QVariant &location)
void setText(const QString &)
QString toString() const const
void textChanged(const QString &text)
QStringRef value(const QString &namespaceUri, const QString &name) const const
void skipCurrentElement()
QString readElementText(QXmlStreamReader::ReadElementTextBehaviour behaviour)
bool hasAttribute(const QString &qualifiedName) const const
QString i18n(const char *text, const TYPE &arg...)
QXmlStreamAttributes attributes() 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.