Libksieve

sieveconditioncurrentdate.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 "sieveconditioncurrentdate.h"
20 #include "autocreatescripts/commonwidgets/selectmatchtypecombobox.h"
21 #include "autocreatescripts/autocreatescriptutil_p.h"
22 #include "widgets/selectdatewidget.h"
23 #include "editor/sieveeditorutil.h"
24 
25 #include <KLocalizedString>
26 
27 #include <QHBoxLayout>
28 #include <QXmlStreamReader>
29 #include "libksieve_debug.h"
30 
31 using namespace KSieveUi;
32 
33 SieveConditionCurrentDate::SieveConditionCurrentDate(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QObject *parent)
34  : SieveCondition(sieveGraphicalModeWidget, QStringLiteral("currentdate"), i18n("Currentdate"), parent)
35 {
36 }
37 
38 QWidget *SieveConditionCurrentDate::createParamWidget(QWidget *parent) const
39 {
40  QWidget *w = new QWidget(parent);
41  QHBoxLayout *lay = new QHBoxLayout;
42  lay->setContentsMargins(0, 0, 0, 0);
43  w->setLayout(lay);
44 
45  SelectMatchTypeComboBox *matchTypeCombo = new SelectMatchTypeComboBox(mSieveGraphicalModeWidget);
46  matchTypeCombo->setObjectName(QStringLiteral("matchtype"));
47  lay->addWidget(matchTypeCombo);
48  connect(matchTypeCombo, &SelectMatchTypeComboBox::valueChanged, this, &SieveConditionCurrentDate::valueChanged);
49 
50  SelectDateWidget *dateWidget = new SelectDateWidget;
51  connect(dateWidget, &SelectDateWidget::valueChanged, this, &SieveConditionCurrentDate::valueChanged);
52  dateWidget->setObjectName(QStringLiteral("datewidget"));
53  lay->addWidget(dateWidget);
54 
55  return w;
56 }
57 
58 QString SieveConditionCurrentDate::code(QWidget *w) const
59 {
60  const SelectMatchTypeComboBox *selectMatchCombobox = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("matchtype"));
61  bool isNegative = false;
62  const QString matchTypeStr = selectMatchCombobox->code(isNegative);
63 
64  const SelectDateWidget *dateWidget = w->findChild<SelectDateWidget *>(QStringLiteral("datewidget"));
65  const QString dateWidgetStr = dateWidget->code();
66 
67  return AutoCreateScriptUtil::negativeString(isNegative) + QStringLiteral("currentdate %1 %2").arg(matchTypeStr, dateWidgetStr)
68  + AutoCreateScriptUtil::generateConditionComment(comment());
69 }
70 
71 bool SieveConditionCurrentDate::needCheckIfServerHasCapability() const
72 {
73  return true;
74 }
75 
76 QString SieveConditionCurrentDate::serverNeedsCapability() const
77 {
78  return QStringLiteral("date");
79 }
80 
81 QStringList SieveConditionCurrentDate::needRequires(QWidget *w) const
82 {
83  const SelectMatchTypeComboBox *selectMatchCombobox = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("matchtype"));
84 
85  return QStringList() << QStringLiteral("date") << selectMatchCombobox->needRequires();
86 }
87 
88 QString SieveConditionCurrentDate::help() const
89 {
90  return i18n("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 header.");
91 }
92 
93 void SieveConditionCurrentDate::setParamWidgetValue(QXmlStreamReader &element, QWidget *w, bool notCondition, QString &error)
94 {
95  int index = 0;
96  QString type;
97  QString value;
98  QString commentStr;
99  while (element.readNextStartElement()) {
100  const QStringRef tagName = element.name();
101  if (tagName == QLatin1String("str")) {
102  if (index == 0) {
103  type = element.readElementText();
104  } else if (index == 1) {
105  value = element.readElementText();
106  } else {
107  tooManyArguments(tagName, index, 2, error);
108  qCDebug(LIBKSIEVE_LOG) << " SieveConditionCurrentDate::setParamWidgetValue too many argument :" << index;
109  }
110  ++index;
111  } else if (tagName == QLatin1String("tag")) {
112  SelectMatchTypeComboBox *selectMatchCombobox = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("matchtype"));
113  selectMatchCombobox->setCode(AutoCreateScriptUtil::tagValueWithCondition(element.readElementText(), notCondition), name(), error);
114  } else if (tagName == QLatin1String("crlf")) {
115  element.skipCurrentElement();
116  //nothing
117  } else if (tagName == QLatin1String("comment")) {
118  commentStr = AutoCreateScriptUtil::loadConditionComment(commentStr, element.readElementText());
119  } else {
120  unknownTag(tagName, error);
121  qCDebug(LIBKSIEVE_LOG) << "SieveConditionCurrentDate::setParamWidgetValue unknown tag " << tagName;
122  }
123  }
124  if (!commentStr.isEmpty()) {
125  setComment(commentStr);
126  }
127 
128  SelectDateWidget *dateWidget = w->findChild<SelectDateWidget *>(QStringLiteral("datewidget"));
129  dateWidget->setCode(type, value);
130 }
131 
132 QUrl SieveConditionCurrentDate::href() const
133 {
134  return SieveEditorUtil::helpUrl(SieveEditorUtil::strToVariableName(name()));
135 }
bool readNextStartElement()
void setContentsMargins(int left, int top, int right, int bottom)
QString name(const QVariant &location)
void skipCurrentElement()
void addWidget(QWidget *widget, int stretch, Qt::Alignment alignment)
void setLayout(QLayout *layout)
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-2020 The KDE developers.
Generated on Mon Aug 10 2020 23:12:59 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.