Libksieve

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

KDE's Doxygen guidelines are available online.