Libksieve

sieveconditioncurrentdate.cpp
1 /*
2  SPDX-FileCopyrightText: 2013-2021 Laurent Montel <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 #include "sieveconditioncurrentdate.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 
14 #include "libksieve_debug.h"
15 #include <QHBoxLayout>
16 #include <QXmlStreamReader>
17 
18 using namespace KSieveUi;
19 
20 SieveConditionCurrentDate::SieveConditionCurrentDate(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QObject *parent)
21  : SieveCondition(sieveGraphicalModeWidget, QStringLiteral("currentdate"), i18n("Currentdate"), parent)
22 {
23 }
24 
25 QWidget *SieveConditionCurrentDate::createParamWidget(QWidget *parent) const
26 {
27  auto w = new QWidget(parent);
28  auto lay = new QHBoxLayout;
29  lay->setContentsMargins({});
30  w->setLayout(lay);
31 
32  auto matchTypeCombo = new SelectMatchTypeComboBox(mSieveGraphicalModeWidget);
33  matchTypeCombo->setObjectName(QStringLiteral("matchtype"));
34  lay->addWidget(matchTypeCombo);
35  connect(matchTypeCombo, &SelectMatchTypeComboBox::valueChanged, this, &SieveConditionCurrentDate::valueChanged);
36 
37  auto dateWidget = new SelectDateWidget;
38  connect(dateWidget, &SelectDateWidget::valueChanged, this, &SieveConditionCurrentDate::valueChanged);
39  dateWidget->setObjectName(QStringLiteral("datewidget"));
40  lay->addWidget(dateWidget);
41 
42  return w;
43 }
44 
45 QString SieveConditionCurrentDate::code(QWidget *w) const
46 {
47  const SelectMatchTypeComboBox *selectMatchCombobox = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("matchtype"));
48  bool isNegative = false;
49  const QString matchTypeStr = selectMatchCombobox->code(isNegative);
50 
51  const SelectDateWidget *dateWidget = w->findChild<SelectDateWidget *>(QStringLiteral("datewidget"));
52  const QString dateWidgetStr = dateWidget->code();
53 
54  return AutoCreateScriptUtil::negativeString(isNegative) + QStringLiteral("currentdate %1 %2").arg(matchTypeStr, dateWidgetStr)
55  + AutoCreateScriptUtil::generateConditionComment(comment());
56 }
57 
58 bool SieveConditionCurrentDate::needCheckIfServerHasCapability() const
59 {
60  return true;
61 }
62 
63 QString SieveConditionCurrentDate::serverNeedsCapability() const
64 {
65  return QStringLiteral("date");
66 }
67 
68 QStringList SieveConditionCurrentDate::needRequires(QWidget *w) const
69 {
70  const SelectMatchTypeComboBox *selectMatchCombobox = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("matchtype"));
71 
72  return QStringList() << QStringLiteral("date") << selectMatchCombobox->needRequires();
73 }
74 
75 QString SieveConditionCurrentDate::help() const
76 {
77  return i18n(
78  "The currentdate test is similar to the date test, except that it operates on the current date/time rather than a value extracted from the message "
79  "header.");
80 }
81 
82 void SieveConditionCurrentDate::setParamWidgetValue(QXmlStreamReader &element, QWidget *w, bool notCondition, QString &error)
83 {
84  int index = 0;
85  QString type;
86  QString value;
87  QString commentStr;
88  while (element.readNextStartElement()) {
89  const QStringRef tagName = element.name();
90  if (tagName == QLatin1String("str")) {
91  if (index == 0) {
92  type = element.readElementText();
93  } else if (index == 1) {
94  value = element.readElementText();
95  } else {
96  tooManyArguments(tagName, index, 2, error);
97  qCDebug(LIBKSIEVE_LOG) << " SieveConditionCurrentDate::setParamWidgetValue too many argument :" << index;
98  }
99  ++index;
100  } else if (tagName == QLatin1String("tag")) {
101  auto selectMatchCombobox = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("matchtype"));
102  selectMatchCombobox->setCode(AutoCreateScriptUtil::tagValueWithCondition(element.readElementText(), notCondition), name(), error);
103  } else if (tagName == QLatin1String("crlf")) {
104  element.skipCurrentElement();
105  // nothing
106  } else if (tagName == QLatin1String("comment")) {
107  commentStr = AutoCreateScriptUtil::loadConditionComment(commentStr, element.readElementText());
108  } else {
109  unknownTag(tagName, error);
110  qCDebug(LIBKSIEVE_LOG) << "SieveConditionCurrentDate::setParamWidgetValue unknown tag " << tagName;
111  }
112  }
113  if (!commentStr.isEmpty()) {
114  setComment(commentStr);
115  }
116 
117  auto dateWidget = w->findChild<SelectDateWidget *>(QStringLiteral("datewidget"));
118  dateWidget->setCode(type, value);
119 }
120 
121 QUrl SieveConditionCurrentDate::href() const
122 {
123  return SieveEditorUtil::helpUrl(SieveEditorUtil::strToVariableName(name()));
124 }
bool readNextStartElement()
void setContentsMargins(int left, int top, int right, int bottom)
QString name(const QVariant &location)
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 Sat Apr 10 2021 23:09:45 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.