Libksieve

sieveactionvacation.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 "sieveactionvacation.h"
7#include "autocreatescripts/autocreatescriptutil_p.h"
8#include "autocreatescripts/sieveeditorgraphicalmodewidget.h"
9#include "editor/sieveeditorutil.h"
10#include "widgets/multilineedit.h"
11#include "widgets/selectvacationcombobox.h"
12
13#include <KLineEditEventHandler>
14#include <KLocalizedString>
15#include <QLineEdit>
16
17#include "libksieveui_debug.h"
18#include <KSieveUi/AbstractSelectEmailLineEdit>
19#include <QGridLayout>
20#include <QLabel>
21#include <QSpinBox>
22#include <QXmlStreamReader>
23
24using namespace KSieveUi;
25
26SieveActionVacation::SieveActionVacation(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QObject *parent)
27 : SieveAction(sieveGraphicalModeWidget, QStringLiteral("vacation"), i18n("Vacation"), parent)
28{
29 mHasVacationSecondsSupport = sieveCapabilities().contains(QLatin1StringView("vacation-seconds"));
30}
31
32QWidget *SieveActionVacation::createParamWidget(QWidget *parent) const
33{
34 auto w = new QWidget(parent);
35 auto grid = new QGridLayout;
36 grid->setContentsMargins({});
37 w->setLayout(grid);
38
39 QLabel *lab = nullptr;
40 if (mHasVacationSecondsSupport) {
41 auto vacation = new SelectVacationComboBox;
42 vacation->setObjectName(QLatin1StringView("vacationcombobox"));
43 connect(vacation, &SelectVacationComboBox::valueChanged, this, &SieveActionVacation::valueChanged);
44 grid->addWidget(vacation, 0, 0);
45 } else {
46 lab = new QLabel(i18nc("@label:textbox", "day:"));
47 grid->addWidget(lab, 0, 0);
48 }
49
50 auto day = new QSpinBox;
51 day->setMinimum(1);
52 day->setMaximum(999);
53 day->setObjectName(QLatin1StringView("day"));
54 connect(day, &QSpinBox::valueChanged, this, &SieveActionVacation::valueChanged);
55 grid->addWidget(day, 0, 1);
56
57 lab = new QLabel(i18nc("@label:textbox", "Message subject:"));
58 grid->addWidget(lab, 1, 0);
59
60 auto subject = new QLineEdit;
62 subject->setObjectName(QLatin1StringView("subject"));
63 connect(subject, &QLineEdit::textChanged, this, &SieveActionVacation::valueChanged);
64 grid->addWidget(subject, 1, 1);
65
66 lab = new QLabel(i18nc("@label:textbox", "Additional email:"));
67 grid->addWidget(lab, 2, 0);
68
69 AbstractSelectEmailLineEdit *addresses = AutoCreateScriptUtil::createSelectEmailsWidget();
70 addresses->setObjectName(QLatin1StringView("addresses"));
71 addresses->setMultiSelection(true);
72 connect(addresses, &AbstractSelectEmailLineEdit::valueChanged, this, &SieveActionVacation::valueChanged);
73 grid->addWidget(addresses, 2, 1);
74
75 lab = new QLabel(i18nc("@label:textbox", "Vacation reason:"));
76 grid->addWidget(lab, 3, 0);
77
78 auto text = new MultiLineEdit;
79 connect(text, &MultiLineEdit::valueChanged, this, &SieveActionVacation::valueChanged);
80 text->setObjectName(QLatin1StringView("text"));
81 grid->addWidget(text, 3, 1);
82
83 return w;
84}
85
86void SieveActionVacation::setParamWidgetValue(QXmlStreamReader &element, QWidget *w, QString &error)
87{
88 while (element.readNextStartElement()) {
89 const QStringView tagName = element.name();
90 if (tagName == QLatin1StringView("tag")) {
91 const QString tagValue = element.readElementText();
92 if (tagValue == QLatin1StringView("seconds")) {
93 if (mHasVacationSecondsSupport) {
94 auto vacationcombobox = w->findChild<SelectVacationComboBox *>(QStringLiteral("vacationcombobox"));
95 vacationcombobox->setCode(AutoCreateScriptUtil::tagValue(tagValue), name(), error);
96 } else {
97 serverDoesNotSupportFeatures(QStringLiteral("seconds"), error);
98 }
99 } else if (tagValue == QLatin1StringView("days")) {
100 // Nothing wait num tag for it.
101 } else if (tagValue == QLatin1StringView("addresses")) {
102 auto addresses = w->findChild<AbstractSelectEmailLineEdit *>(QStringLiteral("addresses"));
103 if (element.readNextStartElement()) {
104 const QStringView textElementTagName = element.name();
105 if (textElementTagName == QLatin1StringView("str")) {
106 addresses->setText(element.readElementText());
107 } else if (textElementTagName == QLatin1StringView("list")) {
108 addresses->setText(AutoCreateScriptUtil::listValueToStr(element));
109 }
110 }
111 } else if (tagValue == QLatin1StringView("subject")) {
112 auto subject = w->findChild<QLineEdit *>(QStringLiteral("subject"));
113 subject->setText(AutoCreateScriptUtil::strValue(element));
114 } else {
115 unknownTagValue(tagValue, error);
116 qCDebug(LIBKSIEVEUI_LOG) << "SieveActionVacation::setParamWidgetValue unknown tagValue :" << tagValue;
117 }
118 } else if (tagName == QLatin1StringView("num")) {
119 auto day = w->findChild<QSpinBox *>(QStringLiteral("day"));
120 day->setValue(element.readElementText().toInt());
121 } else if (tagName == QLatin1StringView("str")) {
122 auto text = w->findChild<MultiLineEdit *>(QStringLiteral("text"));
123 text->setPlainText(element.readElementText());
124 } else if (tagName == QLatin1StringView("crlf")) {
125 element.skipCurrentElement();
126 // nothing
127 } else if (tagName == QLatin1StringView("comment")) {
128 element.skipCurrentElement();
129 // implement in the future ?
130 } else {
131 unknownTag(tagName, error);
132 qCDebug(LIBKSIEVEUI_LOG) << " SieveActionVacation::setParamWidgetValue unknown tagName " << tagName;
133 }
134 }
135}
136
137QString SieveActionVacation::code(QWidget *w) const
138{
139 QString vacationTypeStr = QStringLiteral(":days");
140 if (mHasVacationSecondsSupport) {
141 const SelectVacationComboBox *vacationcombobox = w->findChild<SelectVacationComboBox *>(QStringLiteral("vacationcombobox"));
142 vacationTypeStr = vacationcombobox->code();
143 }
144 const QSpinBox *day = w->findChild<QSpinBox *>(QStringLiteral("day"));
145 const QString dayStr = QString::number(day->value());
146
147 const MultiLineEdit *text = w->findChild<MultiLineEdit *>(QStringLiteral("text"));
148 const QString textStr = text->toPlainText();
149
150 const QLineEdit *subject = w->findChild<QLineEdit *>(QStringLiteral("subject"));
151 const QString subjectStr = subject->text();
152
153 const AbstractSelectEmailLineEdit *addresses = w->findChild<AbstractSelectEmailLineEdit *>(QStringLiteral("addresses"));
154 const QString addressesStr = addresses->text();
155 QString result = QStringLiteral("vacation");
156 if (!dayStr.isEmpty()) {
157 result += QStringLiteral(" %1 %2").arg(vacationTypeStr, dayStr);
158 }
159 if (!subjectStr.isEmpty()) {
160 result += QStringLiteral(" :subject \"%1\"").arg(subjectStr);
161 }
162 if (!addressesStr.isEmpty()) {
163 result += QStringLiteral(" :addresses %1").arg(AutoCreateScriptUtil::createAddressList(addressesStr, false));
164 }
165 if (!textStr.isEmpty()) {
166 result += QStringLiteral(" text:%1").arg(AutoCreateScriptUtil::createMultiLine(textStr));
167 } else {
168 result += QLatin1Char(';'); // Be sure to have ";"
169 }
170 return result;
171}
172
173QString SieveActionVacation::serverNeedsCapability() const
174{
175 return QStringLiteral("vacation");
176}
177
178bool SieveActionVacation::needCheckIfServerHasCapability() const
179{
180 return true;
181}
182
183QStringList SieveActionVacation::needRequires(QWidget *) const
184{
185 QStringList lst;
186 if (mHasVacationSecondsSupport) {
187 lst << QStringLiteral("vacation-seconds");
188 }
189 lst << QStringLiteral("vacation");
190 return lst;
191}
192
193QString SieveActionVacation::help() const
194{
195 QString helpStr = i18n(
196 "The \"vacation\" action implements a vacation autoresponder similar to the vacation command available under many versions of Unix. Its purpose is to "
197 "provide correspondents with notification that the user is away for an extended period of time and that they should not expect quick responses.");
198 if (mHasVacationSecondsSupport) {
199 helpStr = QLatin1Char('\n')
200 + i18n("Through the \":days\" parameter, it limits the number of auto-replies to the same sender to one per [n] days, for a specified number of "
201 "days. But there are cases when one needs more granularity, if one would like to generate \"vacation\" replies more frequently.");
202 helpStr += QLatin1Char('\n') + i18n("This extension defines a \":seconds\" parameter to provide more granularity for such situations.");
203 }
204 return helpStr;
205}
206
207QUrl SieveActionVacation::href() const
208{
209 return SieveEditorUtil::helpUrl(SieveEditorUtil::strToVariableName(name()));
210}
211
212#include "moc_sieveactionvacation.cpp"
The AbstractSelectEmailLineEdit class.
QString i18nc(const char *context, const char *text, const TYPE &arg...)
QString i18n(const char *text, const TYPE &arg...)
void catchReturnKey(QObject *lineEdit)
QByteArray tagValue(const Elem &elem, const char *keyName)
void setContentsMargins(const QMargins &margins)
void setText(const QString &)
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
void setObjectName(QAnyStringView name)
QString toPlainText() const const
void setMinimum(int min)
void setValue(int val)
void valueChanged(int i)
QString arg(Args &&... args) const const
QString number(double n, char format, int precision)
int toInt(bool *ok, int base) 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.