Libksieve

vacationeditwidget.cpp
1 /*
2  SPDX-FileCopyrightText: 2013-2021 Laurent Montel <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "vacationeditwidget.h"
8 #include "vacationmailactionwidget.h"
9 #include "vacationmaillineedit.h"
10 
11 #include <KDateComboBox>
12 #include <KLocalizedString>
13 #include <KTimeComboBox>
14 #include <QLineEdit>
15 #include <QRegularExpressionValidator>
16 #include <QSpinBox>
17 
18 #include "libksieve_debug.h"
19 #include <KPIMTextEdit/PlainTextEditor>
20 #include <KPIMTextEdit/PlainTextEditorWidget>
21 #include <Libkdepim/LineEditCatchReturnKey>
22 
23 #include <kmime/kmime_header_parsing.h>
24 
25 #include <PimCommon/SpellCheckLineEdit>
26 #include <QCheckBox>
27 #include <QGridLayout>
28 #include <QLabel>
29 
30 using KMime::HeaderParsing::parseAddressList;
31 using KMime::Types::AddressList;
33 using KMime::Types::MailboxList;
34 
35 using namespace KSieveUi;
36 VacationEditWidget::VacationEditWidget(QWidget *parent)
37  : QWidget(parent)
38 {
39  int row = -1;
40 
41  auto glay = new QGridLayout(this);
42  glay->setContentsMargins({});
43  glay->setColumnStretch(1, 1);
44 
45  // explanation label:
46  ++row;
47  auto configureVacationLabel = new QLabel(i18n("Configure vacation "
48  "notifications to be sent:"),
49  this);
50  configureVacationLabel->setObjectName(QStringLiteral("configureVacationLabel"));
51  glay->addWidget(configureVacationLabel, row, 0, 1, 2);
52 
53  // Activate checkbox:
54  ++row;
55  mActiveCheck = new QCheckBox(i18n("&Activate vacation notifications"), this);
56  mActiveCheck->setObjectName(QStringLiteral("mActiveCheck"));
57  glay->addWidget(mActiveCheck, row, 0, 1, 2);
58  connect(mActiveCheck, &QCheckBox::clicked, this, &VacationEditWidget::setWasChanged);
59 
60  // Message text edit:
61  ++row;
62  glay->setRowStretch(row, 1);
63  mTextEdit = new KPIMTextEdit::PlainTextEditorWidget(this);
64  mTextEdit->setObjectName(QStringLiteral("mTextEdit"));
65  glay->addWidget(mTextEdit, row, 0, 1, 2);
66  connect(mTextEdit->editor(), &KPIMTextEdit::PlainTextEditor::textChanged, this, &VacationEditWidget::setWasChanged);
67 
68  // Subject
69  ++row;
70  mSubject = new PimCommon::SpellCheckLineEdit(this, QString());
71  mSubject->setObjectName(QStringLiteral("mSubject"));
72  connect(mSubject, &PimCommon::SpellCheckLineEdit::textChanged, this, &VacationEditWidget::setWasChanged);
73  // mSubject->setClearButtonEnabled(true);
74  auto subjectOfVacationLabel = new QLabel(i18n("&Subject of the vacation mail:"), this);
75  subjectOfVacationLabel->setObjectName(QStringLiteral("subjectOfVacationLabel"));
76  subjectOfVacationLabel->setBuddy(mSubject);
77  glay->addWidget(subjectOfVacationLabel, row, 0);
78  glay->addWidget(mSubject, row, 1);
79  ++row;
80 
81  auto timeLayout = new QHBoxLayout;
82  // Start date
83  mStartDate = new KDateComboBox(this);
84  mStartDate->setObjectName(QStringLiteral("mStartDate"));
86  connect(mStartDate, &KDateComboBox::dateChanged, this, &VacationEditWidget::setWasChanged);
87  connect(mStartDate, &KDateComboBox::dateEdited, this, &VacationEditWidget::setWasChanged);
88 
89  mStartTime = new KTimeComboBox(this);
90  mStartTime->setObjectName(QStringLiteral("mStartTime"));
92  mStartTime->setEnabled(false); // Disable by default - we need an extension to support this
93  connect(mStartTime, &KTimeComboBox::timeChanged, this, &VacationEditWidget::setWasChanged);
94  connect(mStartTime, &KTimeComboBox::timeEdited, this, &VacationEditWidget::setWasChanged);
95 
96  mStartTimeActive = new QCheckBox(this);
97  mStartTimeActive->setObjectName(QStringLiteral("mStartTimeActive"));
98  connect(mStartTimeActive, &QCheckBox::toggled, mStartTime, &KTimeComboBox::setEnabled);
99 
100  timeLayout->addWidget(mStartDate);
101  timeLayout->addWidget(mStartTimeActive);
102  timeLayout->addWidget(mStartTime);
103 
104  mStartDateLabel = new QLabel(i18n("Start date:"), this);
105  mStartDateLabel->setObjectName(QStringLiteral("mStartDateLabel"));
106  mStartDateLabel->setBuddy(mStartDate);
107  glay->addWidget(mStartDateLabel, row, 0);
108  glay->addLayout(timeLayout, row, 1);
109 
110  ++row;
111  // End date
112  timeLayout = new QHBoxLayout;
113 
114  mEndDate = new KDateComboBox(this);
115  mEndDate->setObjectName(QStringLiteral("mEndDate"));
117  connect(mEndDate, &KDateComboBox::dateChanged, this, &VacationEditWidget::setWasChanged);
118  connect(mEndDate, &KDateComboBox::dateEdited, this, &VacationEditWidget::setWasChanged);
119 
120  mEndTime = new KTimeComboBox(this);
121  mEndTime->setObjectName(QStringLiteral("mEndTime"));
123  mEndTime->setEnabled(false); // Disable by default - we need an extension to support this
124  connect(mEndTime, &KTimeComboBox::timeChanged, this, &VacationEditWidget::setWasChanged);
125  connect(mEndTime, &KTimeComboBox::timeEdited, this, &VacationEditWidget::setWasChanged);
126 
127  mEndTimeActive = new QCheckBox(this);
128  mEndTimeActive->setObjectName(QStringLiteral("mEndTimeActive"));
129  connect(mEndTimeActive, &QCheckBox::toggled, mEndTime, &KTimeComboBox::setEnabled);
130  connect(mEndTimeActive, &QCheckBox::toggled, this, &VacationEditWidget::setWasChanged);
131 
132  mEndDateLabel = new QLabel(i18n("End date:"), this);
133  mEndDateLabel->setObjectName(QStringLiteral("mEndDateLabel"));
134  mEndDateLabel->setBuddy(mEndDate);
135  glay->addWidget(mEndDateLabel, row, 0);
136 
137  timeLayout->addWidget(mEndDate);
138  timeLayout->addWidget(mEndTimeActive);
139  timeLayout->addWidget(mEndTime);
140  glay->addLayout(timeLayout, row, 1);
141 
142  // Hide the date edits by default - they must be enabled by caller when the
143  // server supports this feature
144  enableDates(false);
145 
146  // "Resent only after" spinbox and label:
147  ++row;
148  int defDayInterval = 7; // default day interval
149  mIntervalSpin = new QSpinBox(this);
150  mIntervalSpin->setMaximum(356);
151  mIntervalSpin->setMinimum(1);
152  mIntervalSpin->setSingleStep(1);
153  mIntervalSpin->setValue(defDayInterval);
154  mIntervalSpin->setObjectName(QStringLiteral("mIntervalSpin"));
155  mIntervalSpin->setSuffix(i18np(" day", " days", defDayInterval));
156  connect(mIntervalSpin, QOverload<int>::of(&QSpinBox::valueChanged), this, &VacationEditWidget::slotIntervalSpinChanged);
157  auto resendNotificationLabel = new QLabel(i18n("&Resend notification only after:"), this);
158  resendNotificationLabel->setObjectName(QStringLiteral("resendNotificationLabel"));
159  resendNotificationLabel->setBuddy(mIntervalSpin);
160  glay->addWidget(resendNotificationLabel, row, 0);
161  glay->addWidget(mIntervalSpin, row, 1);
162 
163  // "Send responses for these addresses" lineedit and label:
164  ++row;
165  mMailAliasesEdit = new KSieveUi::VacationMailLineEdit(this);
166  mMailAliasesEdit->setObjectName(QStringLiteral("mMailAliasesEdit"));
167  mMailAliasesEdit->setClearButtonEnabled(true);
168  connect(mMailAliasesEdit, &KSieveUi::VacationMailLineEdit::textChanged, this, &VacationEditWidget::setWasChanged);
169  auto sendResponseLabel = new QLabel(i18n("&Send responses for these addresses:"), this);
170  sendResponseLabel->setObjectName(QStringLiteral("sendResponseLabel"));
171  sendResponseLabel->setBuddy(mMailAliasesEdit);
172  glay->addWidget(sendResponseLabel, row, 0);
173  glay->addWidget(mMailAliasesEdit, row, 1);
174 
175  // Action for incoming mails
176  mMailAction = new QComboBox(this);
177  for (int i = 0; i < 4; ++i) {
178  mMailAction->addItem(VacationUtils::mailAction(static_cast<VacationUtils::MailAction>(i)));
179  }
180  mMailAction->setObjectName(QStringLiteral("mMailAction"));
181  connect(mMailAction, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &VacationEditWidget::mailActionChanged);
182 
183  // Add imap select folder plugin here.
184  mMailActionRecipient = new VacationMailActionWidget(this);
185  mMailActionRecipient->setObjectName(QStringLiteral("mMailActionRecipient"));
186  connect(mMailActionRecipient, &VacationMailActionWidget::wasChanged, this, &VacationEditWidget::setWasChanged);
187  // Add setChanged here too
188 
189  auto hLayout = new QHBoxLayout;
190 
191  hLayout->addWidget(mMailAction);
192  hLayout->addWidget(mMailActionRecipient);
193 
194  ++row;
195  auto actionIncomingMailsLabel = new QLabel(i18n("&Action for incoming mails:"), this);
196  actionIncomingMailsLabel->setBuddy(mMailAction);
197  actionIncomingMailsLabel->setObjectName(QStringLiteral("actionIncomingMailsLabel"));
198  glay->addWidget(actionIncomingMailsLabel, row, 0);
199  glay->addLayout(hLayout, row, 1);
200 
201  // "Send responses also to SPAM mail" checkbox:
202  ++row;
203  mSpamCheck = new QCheckBox(i18n("Do not send vacation replies to spam messages"), this);
204  mSpamCheck->setObjectName(QStringLiteral("mSpamCheck"));
205  mSpamCheck->setChecked(true);
206  glay->addWidget(mSpamCheck, row, 0, 1, 2);
207  connect(mSpamCheck, &QCheckBox::clicked, this, &VacationEditWidget::setWasChanged);
208 
209  // domain checkbox and linedit:
210  ++row;
211  mDomainCheck = new QCheckBox(i18n("Only react to mail coming from domain"), this);
212  mDomainCheck->setObjectName(QStringLiteral("mDomainCheck"));
213  mDomainCheck->setChecked(false);
214  connect(mDomainCheck, &QCheckBox::clicked, this, &VacationEditWidget::setWasChanged);
215 
216  mDomainEdit = new QLineEdit(this);
217  new KPIM::LineEditCatchReturnKey(mDomainEdit, this);
218  mDomainEdit->setObjectName(QStringLiteral("mDomainEdit"));
219  mDomainEdit->setClearButtonEnabled(true);
220  mDomainEdit->setEnabled(false);
221  mDomainEdit->setValidator(new QRegularExpressionValidator(QRegularExpression(QStringLiteral("[a-zA-Z0-9+-]+(?:\\.[a-zA-Z0-9+-]+)*")), mDomainEdit));
222  glay->addWidget(mDomainCheck, row, 0);
223  glay->addWidget(mDomainEdit, row, 1);
224  connect(mDomainCheck, &QCheckBox::toggled, mDomainEdit, &QLineEdit::setEnabled);
225  connect(mDomainEdit, &QLineEdit::textChanged, this, &VacationEditWidget::setWasChanged);
226 }
227 
228 VacationEditWidget::~VacationEditWidget()
229 {
230 }
231 
232 bool VacationEditWidget::activateVacation() const
233 {
234  return mActiveCheck->isChecked();
235 }
236 
237 void VacationEditWidget::setActivateVacation(bool activate)
238 {
239  mActiveCheck->setChecked(activate);
240 }
241 
242 QString VacationEditWidget::messageText() const
243 {
244  return mTextEdit->toPlainText().trimmed();
245 }
246 
247 void VacationEditWidget::setMessageText(const QString &text)
248 {
249  mTextEdit->setPlainText(text);
250  const int height = (mTextEdit->fontMetrics().lineSpacing() + 1) * 11;
251  mTextEdit->setMinimumHeight(height);
252 }
253 
254 int VacationEditWidget::notificationInterval() const
255 {
256  return mIntervalSpin->value();
257 }
258 
259 void VacationEditWidget::setNotificationInterval(int days)
260 {
261  mIntervalSpin->setValue(days);
262 }
263 
264 AddrSpecList VacationEditWidget::mailAliases(bool &ok) const
265 {
266  QByteArray text = mMailAliasesEdit->text().toLatin1(); // ### IMAA: !ok
267  AddressList al;
268  const char *s = text.cbegin();
269  AddrSpecList asl;
270  if (parseAddressList(s, text.cend(), al)) {
271  AddressList::const_iterator end(al.constEnd());
272  for (AddressList::const_iterator it = al.constBegin(); it != end; ++it) {
273  const MailboxList &mbl = (*it).mailboxList;
274  MailboxList::const_iterator endJt = mbl.constEnd();
275  for (MailboxList::const_iterator jt = mbl.constBegin(); jt != endJt; ++jt) {
276  asl.push_back((*jt).addrSpec());
277  }
278  }
279  ok = true;
280  } else {
281  qCWarning(LIBKSIEVE_LOG) << "Impossible to parse email!" << text;
282  ok = false;
283  }
284  mMailAliasesEdit->setInvalidEmail(!ok);
285  return asl;
286 }
287 
288 void VacationEditWidget::setMailAliases(const AddrSpecList &aliases)
289 {
290  QStringList sl;
291  AddrSpecList::const_iterator end(aliases.constEnd());
292  sl.reserve(aliases.count());
293  for (AddrSpecList::const_iterator it = aliases.constBegin(); it != end; ++it) {
294  sl.push_back((*it).asString());
295  }
296  mMailAliasesEdit->setText(sl.join(QLatin1String(", ")));
297 }
298 
299 void VacationEditWidget::setMailAliases(const QString &aliases)
300 {
301  mMailAliasesEdit->setText(aliases);
302 }
303 
304 void VacationEditWidget::slotIntervalSpinChanged(int value)
305 {
306  mIntervalSpin->setSuffix(i18np(" day", " days", value));
307  setWasChanged();
308 }
309 
310 QString VacationEditWidget::domainName() const
311 {
312  return mDomainCheck->isChecked() ? mDomainEdit->text() : QString();
313 }
314 
315 void VacationEditWidget::setDomainName(const QString &domain)
316 {
317  if (!domain.isEmpty()) {
318  mDomainEdit->setText(domain);
319  mDomainCheck->setChecked(true);
320  }
321 }
322 
323 bool VacationEditWidget::domainCheck() const
324 {
325  return mDomainCheck->isChecked();
326 }
327 
328 void VacationEditWidget::setDomainCheck(bool check)
329 {
330  mDomainCheck->setChecked(check);
331 }
332 
333 bool VacationEditWidget::sendForSpam() const
334 {
335  return !mSpamCheck->isChecked();
336 }
337 
338 void VacationEditWidget::setSendForSpam(bool enable)
339 {
340  mSpamCheck->setChecked(!enable);
341 }
342 
343 QDate VacationEditWidget::endDate() const
344 {
345  if (mEndDate->isEnabled()) {
346  return mEndDate->date();
347  } else {
348  return {};
349  }
350 }
351 
352 void VacationEditWidget::setEndDate(QDate endDate)
353 {
354  mEndDate->setDate(endDate);
355 }
356 
357 QTime VacationEditWidget::endTime() const
358 {
359  if (mEndTime->isEnabled()) {
360  return mEndTime->time();
361  } else {
362  return {};
363  }
364 }
365 
366 void VacationEditWidget::setEndTime(QTime endTime)
367 {
368  mEndTimeActive->setChecked(endTime.isValid());
369  mEndTime->setEnabled(endTime.isValid());
370  mEndTime->setTime(endTime);
371 }
372 
373 QDate VacationEditWidget::startDate() const
374 {
375  if (mStartDate->isEnabled()) {
376  return mStartDate->date();
377  } else {
378  return {};
379  }
380 }
381 
382 void VacationEditWidget::setStartDate(QDate startDate)
383 {
384  mStartDate->setDate(startDate);
385 }
386 
387 QTime VacationEditWidget::startTime() const
388 {
389  if (mStartTime->isEnabled()) {
390  return mStartTime->time();
391  } else {
392  return {};
393  }
394 }
395 
396 void VacationEditWidget::setStartTime(QTime startTime)
397 {
398  mStartTimeActive->setChecked(startTime.isValid());
399  mStartTime->setEnabled(startTime.isValid());
400  mStartTime->setTime(startTime);
401 }
402 
403 void VacationEditWidget::setSubject(const QString &subject)
404 {
405  mSubject->setText(subject);
406 }
407 
408 QString VacationEditWidget::subject() const
409 {
410  if (mSubject->isEnabled()) {
411  return mSubject->toPlainText();
412  } else {
413  return QString();
414  }
415 }
416 
417 void VacationEditWidget::enableDates(bool enable)
418 {
419  mStartDate->setVisible(enable);
420  mStartDateLabel->setVisible(enable);
421  mEndDate->setVisible(enable);
422  mEndDateLabel->setVisible(enable);
423  mStartTime->setVisible(enable);
424  mStartTimeActive->setVisible(enable);
425  mEndTime->setVisible(enable);
426  mEndTimeActive->setVisible(enable);
427 }
428 
429 void VacationEditWidget::mailActionChanged(int action)
430 {
431  mMailActionRecipient->mailActionChanged(static_cast<KSieveUi::VacationUtils::MailAction>(action));
432  setWasChanged();
433 }
434 
435 void VacationEditWidget::setWasChanged()
436 {
437  mChanged = true;
438 }
439 
440 bool VacationEditWidget::changed() const
441 {
442  return mChanged;
443 }
444 
445 void VacationEditWidget::setChanged(bool changed)
446 {
447  mChanged = changed;
448 }
449 
450 void VacationEditWidget::setMailAction(VacationUtils::MailAction action, const QString &recipient)
451 {
452  mMailAction->setCurrentIndex(action);
453  mMailActionRecipient->setMailAction(action, recipient);
454 }
455 
456 void VacationEditWidget::setSieveImapAccountSettings(const SieveImapAccountSettings &account)
457 {
458  mMailActionRecipient->setSieveImapAccountSettings(account);
459 }
460 
461 VacationUtils::MailAction VacationEditWidget::mailAction() const
462 {
463  return static_cast<VacationUtils::MailAction>(mMailAction->currentIndex());
464 }
465 
466 QString VacationEditWidget::mailActionRecipient(bool &valid) const
467 {
468  return mMailActionRecipient->mailActionRecipient(valid);
469 }
470 
471 void VacationEditWidget::enableDomainAndSendForSpam(bool enable)
472 {
473  mDomainCheck->setEnabled(enable);
474  mDomainEdit->setEnabled(enable && mDomainCheck->isChecked());
475  mSpamCheck->setEnabled(enable);
476 }
477 
478 void VacationEditWidget::setDefault()
479 {
480  setActivateVacation(true);
481  setMessageText(VacationUtils::defaultMessageText());
482  setSubject(VacationUtils::defaultSubject());
483  setNotificationInterval(VacationUtils::defaultNotificationInterval());
484  setMailAliases(VacationUtils::defaultMailAliases());
485  setSendForSpam(VacationUtils::defaultSendForSpam());
486  setDomainName(VacationUtils::defaultDomainName());
487  setMailAction(VacationUtils::defaultMailAction(), QString());
488  mStartTimeActive->setChecked(false);
489  mEndTimeActive->setChecked(false);
490  mStartTime->setTime(QTime());
491  mEndTime->setTime(QTime());
492  mStartDate->setDate(QDate());
493  mEndDate->setDate(QDate());
494  setDomainCheck(false);
495  mDomainEdit->clear();
496 }
QByteArray::const_iterator cbegin() const const
void textChanged()
QByteArray::const_iterator cend() const const
void push_back(const T &value)
void dateEdited(const QDate &date)
void reserve(int alloc)
bool isValid() const const
void textChanged(const QString &text)
QString i18np(const char *singular, const char *plural, const TYPE &arg...)
QString join(const QString &separator) const const
void valueChanged(int i)
void setEnabled(bool)
void addWidget(QWidget *widget, int stretch, Qt::Alignment alignment)
bool isEmpty() const const
QString trimmed() const const
void clicked(bool checked)
void dateChanged(const QDate &date)
void textChanged()
void timeChanged(const QTime &time)
QString i18n(const char *text, const TYPE &arg...)
bool setDate(int year, int month, int day)
const QList< QKeySequence > & end()
QByteArray toLatin1() const const
void toggled(bool checked)
The SieveImapAccountSettings class.
void timeEdited(const QTime &time)
void currentIndexChanged(int index)
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sun Apr 11 2021 23:09:37 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.