Libksieve

sieveconditiondate.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 "sieveconditiondate.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 #include <QLineEdit>
27 
28 #include <QHBoxLayout>
29 #include <QLabel>
30 #include "libksieve_debug.h"
31 
32 using namespace KSieveUi;
33 
34 SieveConditionDate::SieveConditionDate(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QObject *parent)
35  : SieveCondition(sieveGraphicalModeWidget, QStringLiteral("date"), i18n("Date"), parent)
36 {
37 }
38 
39 QWidget *SieveConditionDate::createParamWidget(QWidget *parent) const
40 {
41  QWidget *w = new QWidget(parent);
42  QHBoxLayout *lay = new QHBoxLayout;
43  lay->setContentsMargins(0, 0, 0, 0);
44  w->setLayout(lay);
45 
46  SelectMatchTypeComboBox *matchTypeCombo = new SelectMatchTypeComboBox(mSieveGraphicalModeWidget);
47  matchTypeCombo->setObjectName(QStringLiteral("matchtype"));
48  connect(matchTypeCombo, &SelectMatchTypeComboBox::valueChanged, this, &SieveConditionDate::valueChanged);
49  lay->addWidget(matchTypeCombo);
50 
51  QGridLayout *grid = new QGridLayout;
52  grid->setContentsMargins(0, 0, 0, 0);
53  lay->addLayout(grid);
54  QLabel *lab = new QLabel(i18n("header:"));
55  grid->addWidget(lab, 0, 0);
56 
57  QLineEdit *header = new QLineEdit;
58  connect(header, &QLineEdit::textChanged, this, &SieveConditionDate::valueChanged);
59  header->setObjectName(QStringLiteral("header"));
60  grid->addWidget(header, 0, 1);
61 
62  SelectDateWidget *dateWidget = new SelectDateWidget;
63  connect(dateWidget, &SelectDateWidget::valueChanged, this, &SieveConditionDate::valueChanged);
64  dateWidget->setObjectName(QStringLiteral("datewidget"));
65  grid->addWidget(dateWidget, 1, 0, 1, 2);
66 
67  return w;
68 }
69 
70 QString SieveConditionDate::code(QWidget *w) const
71 {
72  const SelectMatchTypeComboBox *selectMatchCombobox = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("matchtype"));
73  bool isNegative = false;
74  const QString matchTypeStr = selectMatchCombobox->code(isNegative);
75 
76  const QLineEdit *header = w->findChild<QLineEdit *>(QStringLiteral("header"));
77  const QString headerStr = header->text();
78 
79  const SelectDateWidget *dateWidget = w->findChild<SelectDateWidget *>(QStringLiteral("datewidget"));
80  const QString dateWidgetStr = dateWidget->code();
81 
82  return AutoCreateScriptUtil::negativeString(isNegative) + QStringLiteral("date %1 \"%2\" %3").arg(matchTypeStr, headerStr, dateWidgetStr)
83  + AutoCreateScriptUtil::generateConditionComment(comment());
84 }
85 
86 bool SieveConditionDate::needCheckIfServerHasCapability() const
87 {
88  return true;
89 }
90 
91 QString SieveConditionDate::serverNeedsCapability() const
92 {
93  return QStringLiteral("date");
94 }
95 
96 QStringList SieveConditionDate::needRequires(QWidget *w) const
97 {
98  const SelectMatchTypeComboBox *selectMatchCombobox = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("matchtype"));
99  return QStringList() << QStringLiteral("date") << selectMatchCombobox->needRequires();
100 }
101 
102 QString SieveConditionDate::help() const
103 {
104  return i18n("The date test matches date/time information derived from headers containing date-time values.");
105 }
106 
107 void SieveConditionDate::setParamWidgetValue(QXmlStreamReader &element, QWidget *w, bool notCondition, QString &error)
108 {
109  int index = 0;
110  QString type;
111  QString value;
112  QString headerStr;
113  QString commentStr;
114  while (element.readNextStartElement()) {
115  const QStringRef tagName = element.name();
116  if (tagName == QLatin1String("str")) {
117  if (index == 0) {
118  headerStr = element.readElementText();
119  } else if (index == 1) {
120  type = element.readElementText();
121  } else if (index == 2) {
122  value = element.readElementText();
123  } else {
124  tooManyArguments(tagName, index, 3, error);
125  qCDebug(LIBKSIEVE_LOG) << " SieveConditionDate::setParamWidgetValue too many argument :" << index;
126  }
127  ++index;
128  } else if (tagName == QLatin1String("tag")) {
129  SelectMatchTypeComboBox *selectMatchCombobox = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("matchtype"));
130  selectMatchCombobox->setCode(AutoCreateScriptUtil::tagValueWithCondition(element.readElementText(), notCondition), name(), error);
131  } else if (tagName == QLatin1String("crlf")) {
132  element.skipCurrentElement();
133  //nothing
134  } else if (tagName == QLatin1String("comment")) {
135  commentStr = AutoCreateScriptUtil::loadConditionComment(commentStr, element.readElementText());
136  } else {
137  unknownTag(tagName, error);
138  qCDebug(LIBKSIEVE_LOG) << "SieveConditionDate::setParamWidgetValue unknown tag " << tagName;
139  }
140  }
141  if (!commentStr.isEmpty()) {
142  setComment(commentStr);
143  }
144 
145  SelectDateWidget *dateWidget = w->findChild<SelectDateWidget *>(QStringLiteral("datewidget"));
146  dateWidget->setCode(type, value);
147  QLineEdit *header = w->findChild<QLineEdit *>(QStringLiteral("header"));
148  header->setText(headerStr);
149 }
150 
151 QUrl SieveConditionDate::href() const
152 {
153  return SieveEditorUtil::helpUrl(SieveEditorUtil::strToVariableName(name()));
154 }
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 textChanged(const QString &text)
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)
void setObjectName(const QString &name)
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
void addLayout(QLayout *layout, int stretch)
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 Thu Jul 9 2020 23:07:44 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.