Libksieve

sieveconditioncurrentdate.cpp
1/*
2 SPDX-FileCopyrightText: 2013-2024 Laurent Montel <montel@kde.org>
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 "libksieveui_debug.h"
15#include <QHBoxLayout>
16#include <QXmlStreamReader>
17
18using namespace KSieveUi;
19
20SieveConditionCurrentDate::SieveConditionCurrentDate(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QObject *parent)
21 : SieveCondition(sieveGraphicalModeWidget, QStringLiteral("currentdate"), i18n("Currentdate"), parent)
22{
23}
24
25QWidget *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(QLatin1StringView("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(QLatin1StringView("datewidget"));
40 lay->addWidget(dateWidget);
41
42 return w;
43}
44
45QString 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
58bool SieveConditionCurrentDate::needCheckIfServerHasCapability() const
59{
60 return true;
61}
62
63QString SieveConditionCurrentDate::serverNeedsCapability() const
64{
65 return QStringLiteral("date");
66}
67
68QStringList 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
75QString 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
82void SieveConditionCurrentDate::setParamWidgetValue(QXmlStreamReader &element, QWidget *w, bool notCondition, QString &error)
83{
84 int index = 0;
86 QString value;
87 QString commentStr;
88 while (element.readNextStartElement()) {
89 const QStringView tagName = element.name();
90 if (tagName == QLatin1StringView("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(LIBKSIEVEUI_LOG) << " SieveConditionCurrentDate::setParamWidgetValue too many argument :" << index;
98 }
99 ++index;
100 } else if (tagName == QLatin1StringView("tag")) {
101 auto selectMatchCombobox = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("matchtype"));
102 selectMatchCombobox->setCode(AutoCreateScriptUtil::tagValueWithCondition(element.readElementText(), notCondition), name(), error);
103 } else if (tagName == QLatin1StringView("crlf")) {
104 element.skipCurrentElement();
105 // nothing
106 } else if (tagName == QLatin1StringView("comment")) {
107 commentStr = AutoCreateScriptUtil::loadConditionComment(commentStr, element.readElementText());
108 } else {
109 unknownTag(tagName, error);
110 qCDebug(LIBKSIEVEUI_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
121QUrl SieveConditionCurrentDate::href() const
122{
123 return SieveEditorUtil::helpUrl(SieveEditorUtil::strToVariableName(name()));
124}
125
126#include "moc_sieveconditioncurrentdate.cpp"
QString i18n(const char *text, const TYPE &arg...)
VehicleSection::Type type(QStringView coachNumber, QStringView coachClassification)
void setContentsMargins(const QMargins &margins)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
T findChild(const QString &name, Qt::FindChildOptions options) const const
QObject * parent() const const
QString arg(Args &&... args) const const
bool isEmpty() const const
QStringView name() const const
QString readElementText(ReadElementTextBehaviour behaviour)
bool readNextStartElement()
void skipCurrentElement()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 22 2024 12:09:40 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.