Libksieve

sieveconditiondate.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 "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 <KLineEditEventHandler>
13#include <KLocalizedString>
14#include <QLineEdit>
15
16#include "libksieveui_debug.h"
17#include <QHBoxLayout>
18#include <QLabel>
19
20using namespace KSieveUi;
21
22SieveConditionDate::SieveConditionDate(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QObject *parent)
23 : SieveCondition(sieveGraphicalModeWidget, QStringLiteral("date"), i18n("Date"), parent)
24{
25}
26
27QWidget *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(QLatin1StringView("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(i18nc("@label:textbox", "header:"));
43 grid->addWidget(lab, 0, 0);
44
45 auto header = new QLineEdit;
47 connect(header, &QLineEdit::textChanged, this, &SieveConditionDate::valueChanged);
48 header->setObjectName(QLatin1StringView("header"));
49 grid->addWidget(header, 0, 1);
50
51 auto dateWidget = new SelectDateWidget;
52 connect(dateWidget, &SelectDateWidget::valueChanged, this, &SieveConditionDate::valueChanged);
53 dateWidget->setObjectName(QLatin1StringView("datewidget"));
54 grid->addWidget(dateWidget, 1, 0, 1, 2);
55
56 return w;
57}
58
59QString 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
75bool SieveConditionDate::needCheckIfServerHasCapability() const
76{
77 return true;
78}
79
80QString SieveConditionDate::serverNeedsCapability() const
81{
82 return QStringLiteral("date");
83}
84
85QStringList SieveConditionDate::needRequires(QWidget *w) const
86{
87 const SelectMatchTypeComboBox *selectMatchCombobox = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("matchtype"));
88 return QStringList() << QStringLiteral("date") << selectMatchCombobox->needRequires();
89}
90
91QString SieveConditionDate::help() const
92{
93 return i18n("The date test matches date/time information derived from headers containing date-time values.");
94}
95
96void SieveConditionDate::setParamWidgetValue(QXmlStreamReader &element, QWidget *w, bool notCondition, QString &error)
97{
98 int index = 0;
100 QString value;
101 QString headerStr;
102 QString commentStr;
103 while (element.readNextStartElement()) {
104 const QStringView tagName = element.name();
105 if (tagName == QLatin1StringView("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(LIBKSIEVEUI_LOG) << " SieveConditionDate::setParamWidgetValue too many argument :" << index;
115 }
116 ++index;
117 } else if (tagName == QLatin1StringView("tag")) {
118 auto selectMatchCombobox = w->findChild<SelectMatchTypeComboBox *>(QStringLiteral("matchtype"));
119 selectMatchCombobox->setCode(AutoCreateScriptUtil::tagValueWithCondition(element.readElementText(), notCondition), name(), error);
120 } else if (tagName == QLatin1StringView("crlf")) {
121 element.skipCurrentElement();
122 // nothing
123 } else if (tagName == QLatin1StringView("comment")) {
124 commentStr = AutoCreateScriptUtil::loadConditionComment(commentStr, element.readElementText());
125 } else {
126 unknownTag(tagName, error);
127 qCDebug(LIBKSIEVEUI_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
140QUrl SieveConditionDate::href() const
141{
142 return SieveEditorUtil::helpUrl(SieveEditorUtil::strToVariableName(name()));
143}
144
145#include "moc_sieveconditiondate.cpp"
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
Type type(const QSqlDatabase &db)
void catchReturnKey(QObject *lineEdit)
void setContentsMargins(const QMargins &margins)
void textChanged(const QString &text)
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 Mon Nov 18 2024 12:14:30 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.