Libksieve

vacationpagewidget.cpp
1 /*
2  SPDX-FileCopyrightText: 2013-2021 Laurent Montel <[email protected]>
3 
4  SPDX-License-Identifier: GPL-2.0-only
5 */
6 
7 #include "vacationpagewidget.h"
8 #include "multiimapvacationmanager.h"
9 #include "sieve-vacation.h"
10 #include "vacationcreatescriptjob.h"
11 #include "vacationeditwidget.h"
12 #include "vacationutils.h"
13 #include "vacationwarningwidget.h"
14 #include <managescriptsjob/parseuserscriptjob.h>
15 
16 #include <kmanagesieve/sievejob.h>
17 
18 #include "libksieve_debug.h"
19 #include <KLocalizedString>
20 
21 #include <QLabel>
22 #include <QStackedWidget>
23 #include <QVBoxLayout>
24 
25 using namespace KSieveUi;
26 VacationPageWidget::VacationPageWidget(QWidget *parent)
27  : QWidget(parent)
28 {
29  auto lay = new QVBoxLayout(this);
30  lay->setContentsMargins({});
31  mStackWidget = new QStackedWidget;
32  lay->addWidget(mStackWidget);
33 
34  // Main Page
35  auto mainPage = new QWidget;
36  auto vbox = new QVBoxLayout;
37  vbox->setContentsMargins({});
38  mainPage->setLayout(vbox);
39  mVacationWarningWidget = new VacationWarningWidget(this);
40  vbox->addWidget(mVacationWarningWidget);
41 
42  mVacationEditWidget = new VacationEditWidget(this);
43  vbox->addWidget(mVacationEditWidget);
44  mStackWidget->addWidget(mainPage);
45 
46  auto w = new QWidget;
47  vbox = new QVBoxLayout;
48  auto lab =
49  new QLabel(i18n("Your server did not list \"vacation\" in "
50  "its list of supported Sieve extensions;"
51  "without it, KMail cannot install out-of-"
52  "office replies for you."
53  "Please contact your system administrator."));
54  QFont font = lab->font();
55  font.setBold(true);
56  lab->setFont(font);
57  vbox->addWidget(lab);
58  vbox->setAlignment(lab, Qt::AlignVCenter);
59 
60  lab->setWordWrap(true);
61  w->setLayout(vbox);
62  mStackWidget->addWidget(w);
63 
64  mStackWidget->setCurrentIndex(Script);
65 }
66 
67 VacationPageWidget::~VacationPageWidget()
68 {
69 }
70 
71 bool VacationPageWidget::wasChanged() const
72 {
73  return mVacationEditWidget->changed();
74 }
75 
76 void VacationPageWidget::setServerUrl(const QUrl &url)
77 {
78  mUrl = url;
79  mVacationEditWidget->setEnabled(false);
80 }
81 
82 void VacationPageWidget::setVacationManager(MultiImapVacationManager *vacationManager)
83 {
84  mVacationManager = vacationManager;
85  connect(mVacationManager, &MultiImapVacationManager::scriptAvailable, this, &VacationPageWidget::slotGetResult);
86  mVacationManager->checkVacation(mServerName, mUrl);
87 }
88 
89 void VacationPageWidget::setSieveImapAccountSettings(const SieveImapAccountSettings &account)
90 {
91  mVacationEditWidget->setSieveImapAccountSettings(account);
92 }
93 
94 void VacationPageWidget::setServerName(const QString &serverName)
95 {
96  mServerName = serverName;
97 }
98 
99 void VacationPageWidget::slotGetResult(const QString &serverName,
100  const QStringList &sieveCapabilities,
101  const QString &scriptName,
102  const QString &script,
103  bool active)
104 {
105  if (mServerName != serverName) {
106  return;
107  }
108  qCDebug(LIBKSIEVE_LOG) << serverName << sieveCapabilities << Qt::endl << scriptName << "(" << active << ")" << Qt::endl;
109 
110  if (mUrl.scheme() == QLatin1String("sieve") && !sieveCapabilities.contains(QLatin1String("vacation"))) {
111  mStackWidget->setCurrentIndex(ScriptNotSupported);
112  return;
113  }
114 
115  mUrl = mUrl.adjusted(QUrl::RemoveFilename);
116  mUrl.setPath(mUrl.path() + QLatin1Char('/') + scriptName);
117 
118  // Whether the server supports the "date" extension
119  mHasDateSupport = mUrl.scheme() == QLatin1String("sieve") && sieveCapabilities.contains(QLatin1String("date"));
120 
121  KSieveUi::VacationUtils::Vacation vacation = KSieveUi::VacationUtils::parseScript(script);
122 
123  if (!vacation.isValid() && !script.trimmed().isEmpty()) {
124  mVacationWarningWidget->setVisible(true);
125  }
126 
127  mWasActive = active;
128  mVacationEditWidget->setEnabled(true);
129  mVacationEditWidget->setActivateVacation(active && vacation.active);
130  mVacationEditWidget->setMessageText(vacation.messageText);
131  mVacationEditWidget->setSubject(vacation.subject);
132  mVacationEditWidget->setMailAction(vacation.mailAction, vacation.mailActionRecipient);
133  mVacationEditWidget->setNotificationInterval(vacation.notificationInterval);
134  mVacationEditWidget->setMailAliases(vacation.aliases);
135  mVacationEditWidget->setSendForSpam(vacation.sendForSpam);
136  mVacationEditWidget->setDomainName(vacation.reactOndomainName);
137  mVacationEditWidget->enableDomainAndSendForSpam(!VacationSettings::allowOutOfOfficeUploadButNoSettings());
138 
139  mVacationEditWidget->enableDates(mHasDateSupport);
140  if (mHasDateSupport) {
141  mVacationEditWidget->setStartDate(vacation.startDate);
142  mVacationEditWidget->setStartTime(vacation.startTime);
143  mVacationEditWidget->setEndDate(vacation.endDate);
144  mVacationEditWidget->setEndTime(vacation.endTime);
145  }
146  mVacationEditWidget->setChanged(false);
147 }
148 
149 KSieveUi::VacationCreateScriptJob *VacationPageWidget::writeScript(bool &errorFound)
150 {
151  if (mPageScript == Script) {
152  const bool active = mVacationEditWidget->activateVacation();
153  VacationUtils::Vacation vacation;
154  bool ok;
155  vacation.aliases = mVacationEditWidget->mailAliases(ok);
156  if (!ok) {
157  errorFound = true;
158  return nullptr;
159  }
160  vacation.valid = true;
161  vacation.active = active;
162  vacation.messageText = mVacationEditWidget->messageText();
163  vacation.subject = mVacationEditWidget->subject();
164  vacation.mailAction = mVacationEditWidget->mailAction();
165  // Check valid
166 
167  vacation.mailActionRecipient = mVacationEditWidget->mailActionRecipient(ok);
168  if (!ok) {
169  errorFound = true;
170  return nullptr;
171  }
172  vacation.notificationInterval = mVacationEditWidget->notificationInterval();
173  vacation.sendForSpam = mVacationEditWidget->sendForSpam();
174  vacation.reactOndomainName = mVacationEditWidget->domainName();
175  if (mHasDateSupport) {
176  vacation.startDate = mVacationEditWidget->startDate();
177  vacation.startTime = mVacationEditWidget->startTime();
178  vacation.endDate = mVacationEditWidget->endDate();
179  vacation.endTime = mVacationEditWidget->endTime();
180  } else {
181  vacation.startDate = QDate();
182  vacation.startTime = QTime();
183  vacation.endDate = QDate();
184  vacation.endTime = QTime();
185  }
186  const QString script = VacationUtils::composeScript(vacation);
187  auto createJob = new KSieveUi::VacationCreateScriptJob;
188  createJob->setServerUrl(mUrl);
189  createJob->setServerName(mServerName);
190  createJob->setStatus(active, mWasActive);
191  createJob->setScript(script);
192  return createJob;
193  }
194  return nullptr;
195 }
196 
197 void VacationPageWidget::setDefault()
198 {
199  if (mVacationEditWidget->isEnabled()) {
200  mVacationEditWidget->setDefault();
201  }
202 }
RemoveFilename
void setContentsMargins(int left, int top, int right, int bottom)
bool contains(const QString &str, Qt::CaseSensitivity cs) const const
QTextStream & endl(QTextStream &stream)
AlignVCenter
The MultiImapVacationManager class.
void setBold(bool enable)
bool isEmpty() const const
QString trimmed() const const
QString i18n(const char *text, const TYPE &arg...)
The SieveImapAccountSettings class.
int addWidget(QWidget *widget)
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sat Apr 17 2021 23:09:34 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.