Libksieve

sieveconditiondate.cpp
1 /*
2  SPDX-FileCopyrightText: 2013-2021 Laurent Montel <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 #include "sieveconditiondate.h"
7 #include "autocreatescripts/autocreatescriptutil_p.h"
8 #include "autocreatescripts/commonwidgets/selectmatchtypecombobox.h"
9 #include "editor/sieveeditorutil.h"
10 #include "widgets/selectdatewidget.h"
11 
12 #include <KLocalizedString>
13 #include <Libkdepim/LineEditCatchReturnKey>
14 #include <QLineEdit>
15 
16 #include "libksieve_debug.h"
17 #include <QHBoxLayout>
18 #include <QLabel>
19 
20 using namespace KSieveUi;
21 
22 SieveConditionDate::SieveConditionDate(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QObject *parent)
23  : SieveCondition(sieveGraphicalModeWidget, QStringLiteral("date"), i18n("Date"), parent)
24 {
25 }
26 
27 QWidget *SieveConditionDate::createParamWidget(QWidget *parent) const
28 {
29  auto w = new QWidget(parent);
30  auto lay = new QHBoxLayout;
31  lay->setContentsMargins({});
32  w->setLayout(lay);
33 
34  auto matchTypeCombo = new SelectMatchTypeComboBox(mSieveGraphicalModeWidget);
35  matchTypeCombo->setObjectName(QStringLiteral("matchtype"));
36  connect(matchTypeCombo, &SelectMatchTypeComboBox::valueChanged, this, &SieveConditionDate::valueChanged);
37  lay->addWidget(matchTypeCombo);
38 
39  auto grid = new QGridLayout;
40  grid->setContentsMargins({});
41  lay->addLayout(grid);
42  auto lab = new QLabel(i18n("header:"));
43  grid->addWidget(lab, 0, 0);
44 
45  auto header = new QLineEdit;
46  new KPIM::LineEditCatchReturnKey(header, w);
47  connect(header, &QLineEdit::textChanged, this, &SieveConditionDate::valueChanged);
48  header->setObjectName(QStringLiteral("header"));
49  grid->addWidget(header, 0, 1);
50 
51  auto dateWidget = new SelectDateWidget;
52  connect(dateWidget, &SelectDateWidget::valueChanged, this, &SieveConditionDate::valueChanged);
53  dateWidget->setObjectName(QStringLiteral("datewidget"));
54  grid->addWidget(dateWidget, 1, 0, 1, 2);
55 
56  return w;
57 }
58 
59 QString SieveConditionDate::code(QWidget *w) const
60 {
61  const SelectMatchTypeComboBox *selectMatchCombobox = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("matchtype"));
62  bool isNegative = false;
63  const QString matchTypeStr = selectMatchCombobox->code(isNegative);
64 
65  const QLineEdit *header = w->findChild<QLineEdit *>(QStringLiteral("header"));
66  const QString headerStr = header->text();
67 
68  const SelectDateWidget *dateWidget = w->findChild<SelectDateWidget *>(QStringLiteral("datewidget"));
69  const QString dateWidgetStr = dateWidget->code();
70 
71  return AutoCreateScriptUtil::negativeString(isNegative) + QStringLiteral("date %1 \"%2\" %3").arg(matchTypeStr, headerStr, dateWidgetStr)
72  + AutoCreateScriptUtil::generateConditionComment(comment());
73 }
74 
75 bool SieveConditionDate::needCheckIfServerHasCapability() const
76 {
77  return true;
78 }
79 
80 QString SieveConditionDate::serverNeedsCapability() const
81 {
82  return QStringLiteral("date");
83 }
84 
85 QStringList SieveConditionDate::needRequires(QWidget *w) const
86 {
87  const SelectMatchTypeComboBox *selectMatchCombobox = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("matchtype"));
88  return QStringList() << QStringLiteral("date") << selectMatchCombobox->needRequires();
89 }
90 
91 QString SieveConditionDate::help() const
92 {
93  return i18n("The date test matches date/time information derived from headers containing date-time values.");
94 }
95 
96 void SieveConditionDate::setParamWidgetValue(QXmlStreamReader &element, QWidget *w, bool notCondition, QString &error)
97 {
98  int index = 0;
99  QString type;
100  QString value;
101  QString headerStr;
102  QString commentStr;
103  while (element.readNextStartElement()) {
104  const QStringRef tagName = element.name();
105  if (tagName == QLatin1String("str")) {
106  if (index == 0) {
107  headerStr = element.readElementText();
108  } else if (index == 1) {
109  type = element.readElementText();
110  } else if (index == 2) {
111  value = element.readElementText();
112  } else {
113  tooManyArguments(tagName, index, 3, error);
114  qCDebug(LIBKSIEVE_LOG) << " SieveConditionDate::setParamWidgetValue too many argument :" << index;
115  }
116  ++index;
117  } else if (tagName == QLatin1String("tag")) {
118  auto selectMatchCombobox = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("matchtype"));
119  selectMatchCombobox->setCode(AutoCreateScriptUtil::tagValueWithCondition(element.readElementText(), notCondition), name(), error);
120  } else if (tagName == QLatin1String("crlf")) {
121  element.skipCurrentElement();
122  // nothing
123  } else if (tagName == QLatin1String("comment")) {
124  commentStr = AutoCreateScriptUtil::loadConditionComment(commentStr, element.readElementText());
125  } else {
126  unknownTag(tagName, error);
127  qCDebug(LIBKSIEVE_LOG) << "SieveConditionDate::setParamWidgetValue unknown tag " << tagName;
128  }
129  }
130  if (!commentStr.isEmpty()) {
131  setComment(commentStr);
132  }
133 
134  auto dateWidget = w->findChild<SelectDateWidget *>(QStringLiteral("datewidget"));
135  dateWidget->setCode(type, value);
136  auto header = w->findChild<QLineEdit *>(QStringLiteral("header"));
137  header->setText(headerStr);
138 }
139 
140 QUrl SieveConditionDate::href() const
141 {
142  return SieveEditorUtil::helpUrl(SieveEditorUtil::strToVariableName(name()));
143 }
bool readNextStartElement()
void setContentsMargins(int left, int top, int right, int bottom)
QString name(const QVariant &location)
void textChanged(const QString &text)
void skipCurrentElement()
QString readElementText(QXmlStreamReader::ReadElementTextBehaviour behaviour)
Type type(const QSqlDatabase &db)
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.