Libksieve

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

KDE's Doxygen guidelines are available online.