Libksieve

sieveactionvacation.cpp
1 /*
2  SPDX-FileCopyrightText: 2013-2021 Laurent Montel <[email protected]>
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 <KLocalizedString>
14 #include <Libkdepim/LineEditCatchReturnKey>
15 #include <QLineEdit>
16 
17 #include "libksieve_debug.h"
18 #include <KSieveUi/AbstractSelectEmailLineEdit>
19 #include <QGridLayout>
20 #include <QLabel>
21 #include <QSpinBox>
22 #include <QXmlStreamReader>
23 
24 using namespace KSieveUi;
25 
26 SieveActionVacation::SieveActionVacation(SieveEditorGraphicalModeWidget *sieveGraphicalModeWidget, QObject *parent)
27  : SieveAction(sieveGraphicalModeWidget, QStringLiteral("vacation"), i18n("Vacation"), parent)
28 {
29  mHasVacationSecondsSupport = sieveCapabilities().contains(QLatin1String("vacation-seconds"));
30 }
31 
32 QWidget *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(QStringLiteral("vacationcombobox"));
43  connect(vacation, &SelectVacationComboBox::valueChanged, this, &SieveActionVacation::valueChanged);
44  grid->addWidget(vacation, 0, 0);
45  } else {
46  lab = new QLabel(i18n("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(QStringLiteral("day"));
54  connect(day, QOverload<int>::of(&QSpinBox::valueChanged), this, &SieveActionVacation::valueChanged);
55  grid->addWidget(day, 0, 1);
56 
57  lab = new QLabel(i18n("Message subject:"));
58  grid->addWidget(lab, 1, 0);
59 
60  auto subject = new QLineEdit;
61  new KPIM::LineEditCatchReturnKey(subject, w);
62  subject->setObjectName(QStringLiteral("subject"));
63  connect(subject, &QLineEdit::textChanged, this, &SieveActionVacation::valueChanged);
64  grid->addWidget(subject, 1, 1);
65 
66  lab = new QLabel(i18n("Additional email:"));
67  grid->addWidget(lab, 2, 0);
68 
69  AbstractSelectEmailLineEdit *addresses = AutoCreateScriptUtil::createSelectEmailsWidget();
70  addresses->setObjectName(QStringLiteral("addresses"));
71  addresses->setMultiSelection(true);
72  connect(addresses, &AbstractSelectEmailLineEdit::valueChanged, this, &SieveActionVacation::valueChanged);
73  grid->addWidget(addresses, 2, 1);
74 
75  lab = new QLabel(i18n("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(QStringLiteral("text"));
81  grid->addWidget(text, 3, 1);
82 
83  return w;
84 }
85 
86 void SieveActionVacation::setParamWidgetValue(QXmlStreamReader &element, QWidget *w, QString &error)
87 {
88  while (element.readNextStartElement()) {
89  const QStringRef tagName = element.name();
90  if (tagName == QLatin1String("tag")) {
91  const QString tagValue = element.readElementText();
92  if (tagValue == QLatin1String("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 == QLatin1String("days")) {
100  // Nothing wait num tag for it.
101  } else if (tagValue == QLatin1String("addresses")) {
102  auto addresses = w->findChild<AbstractSelectEmailLineEdit *>(QStringLiteral("addresses"));
103  if (element.readNextStartElement()) {
104  const QStringRef textElementTagName = element.name();
105  if (textElementTagName == QLatin1String("str")) {
106  addresses->setText(element.readElementText());
107  } else if (textElementTagName == QLatin1String("list")) {
108  addresses->setText(AutoCreateScriptUtil::listValueToStr(element));
109  }
110  }
111  } else if (tagValue == QLatin1String("subject")) {
112  auto subject = w->findChild<QLineEdit *>(QStringLiteral("subject"));
113  subject->setText(AutoCreateScriptUtil::strValue(element));
114  } else {
115  unknownTagValue(tagValue, error);
116  qCDebug(LIBKSIEVE_LOG) << "SieveActionVacation::setParamWidgetValue unknown tagValue :" << tagValue;
117  }
118  } else if (tagName == QLatin1String("num")) {
119  auto day = w->findChild<QSpinBox *>(QStringLiteral("day"));
120  day->setValue(element.readElementText().toInt());
121  } else if (tagName == QLatin1String("str")) {
122  auto text = w->findChild<MultiLineEdit *>(QStringLiteral("text"));
123  text->setPlainText(element.readElementText());
124  } else if (tagName == QLatin1String("crlf")) {
125  element.skipCurrentElement();
126  // nothing
127  } else if (tagName == QLatin1String("comment")) {
128  element.skipCurrentElement();
129  // implement in the future ?
130  } else {
131  unknownTag(tagName, error);
132  qCDebug(LIBKSIEVE_LOG) << " SieveActionVacation::setParamWidgetValue unknown tagName " << tagName;
133  }
134  }
135 }
136 
137 QString 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 
173 QString SieveActionVacation::serverNeedsCapability() const
174 {
175  return QStringLiteral("vacation");
176 }
177 
178 bool SieveActionVacation::needCheckIfServerHasCapability() const
179 {
180  return true;
181 }
182 
183 QStringList 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 
193 QString 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 
207 QUrl SieveActionVacation::href() const
208 {
209  return SieveEditorUtil::helpUrl(SieveEditorUtil::strToVariableName(name()));
210 }
void setMinimum(int min)
bool readNextStartElement()
void setContentsMargins(int left, int top, int right, int bottom)
QString name(const QVariant &location)
void setText(const QString &)
void textChanged(const QString &text)
The AbstractSelectEmailLineEdit class.
void valueChanged(int i)
void skipCurrentElement()
QString number(int n, int base)
QString readElementText(QXmlStreamReader::ReadElementTextBehaviour behaviour)
QByteArray tagValue(const Elem &elem, TagKey key)
int toInt(bool *ok, int base) const const
void setObjectName(const QString &name)
QString i18n(const char *text, const TYPE &arg...)
void setValue(int val)
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)
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-2021 The KDE developers.
Generated on Sun Apr 11 2021 23:09:36 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.