Libksieve

vacationpagewidget.cpp
1 /*
2  Copyright (C) 2013-2020 Laurent Montel <[email protected]>
3 
4  This program is free software; you can redistribute it and/or modify it
5  under the terms of the GNU General Public License, version 2, as
6  published by the Free Software Foundation.
7 
8  This program is distributed in the hope that it will be useful, but
9  WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License along
14  with this program; if not, write to the Free Software Foundation, Inc.,
15  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16 */
17 
18 #include "vacationpagewidget.h"
19 #include "vacationeditwidget.h"
20 #include "vacationwarningwidget.h"
21 #include "vacationcreatescriptjob.h"
22 #include "vacationutils.h"
23 #include "multiimapvacationmanager.h"
24 #include <managescriptsjob/parseuserscriptjob.h>
25 #include "sieve-vacation.h"
26 
27 #include <kmanagesieve/sievejob.h>
28 
29 #include "libksieve_debug.h"
30 #include <KLocalizedString>
31 
32 #include <QStackedWidget>
33 #include <QVBoxLayout>
34 #include <QLabel>
35 
36 using namespace KSieveUi;
37 VacationPageWidget::VacationPageWidget(QWidget *parent)
38  : QWidget(parent)
39 {
40  QVBoxLayout *lay = new QVBoxLayout(this);
41  lay->setContentsMargins(0, 0, 0, 0);
42  mStackWidget = new QStackedWidget;
43  lay->addWidget(mStackWidget);
44 
45  //Main Page
46  QWidget *mainPage = new QWidget;
47  QVBoxLayout *vbox = new QVBoxLayout;
48  vbox->setContentsMargins(0, 0, 0, 0);
49  mainPage->setLayout(vbox);
50  mVacationWarningWidget = new VacationWarningWidget(this);
51  vbox->addWidget(mVacationWarningWidget);
52 
53  mVacationEditWidget = new VacationEditWidget(this);
54  vbox->addWidget(mVacationEditWidget);
55  mStackWidget->addWidget(mainPage);
56 
57  QWidget *w = new QWidget;
58  vbox = new QVBoxLayout;
59  QLabel *lab = new QLabel(i18n("Your server did not list \"vacation\" in "
60  "its list of supported Sieve extensions;"
61  "without it, KMail cannot install out-of-"
62  "office replies for you."
63  "Please contact your system administrator."));
64  QFont font = lab->font();
65  font.setBold(true);
66  lab->setFont(font);
67  vbox->addWidget(lab);
68  vbox->setAlignment(lab, Qt::AlignVCenter);
69 
70  lab->setWordWrap(true);
71  w->setLayout(vbox);
72  mStackWidget->addWidget(w);
73 
74  mStackWidget->setCurrentIndex(Script);
75 }
76 
77 VacationPageWidget::~VacationPageWidget()
78 {
79 }
80 
81 bool VacationPageWidget::wasChanged() const
82 {
83  return mVacationEditWidget->changed();
84 }
85 
86 void VacationPageWidget::setServerUrl(const QUrl &url)
87 {
88  mUrl = url;
89  mVacationEditWidget->setEnabled(false);
90 }
91 
92 void VacationPageWidget::setVacationManager(MultiImapVacationManager *vacationManager)
93 {
94  mVacationManager = vacationManager;
95  connect(mVacationManager, &MultiImapVacationManager::scriptAvailable, this, &VacationPageWidget::slotGetResult);
96  mVacationManager->checkVacation(mServerName, mUrl);
97 }
98 
99 void VacationPageWidget::setSieveImapAccountSettings(const SieveImapAccountSettings &account)
100 {
101  mVacationEditWidget->setSieveImapAccountSettings(account);
102 }
103 
104 void VacationPageWidget::setServerName(const QString &serverName)
105 {
106  mServerName = serverName;
107 }
108 
109 void VacationPageWidget::slotGetResult(const QString &serverName, const QStringList &sieveCapabilities, const QString &scriptName, const QString &script, bool active)
110 {
111  if (mServerName != serverName) {
112  return;
113  }
114  qCDebug(LIBKSIEVE_LOG) << serverName << sieveCapabilities
115  #if (QT_VERSION < QT_VERSION_CHECK(5, 15, 0))
116  << endl
117  #else
118  << Qt::endl
119  #endif
120  << scriptName << "(" << active << ")"
121  #if (QT_VERSION < QT_VERSION_CHECK(5, 15, 0))
122  << endl
123  #else
124  << Qt::endl
125  #endif
126  ;
127 
128  if (mUrl.scheme() == QLatin1String("sieve")
129  && !sieveCapabilities.contains(QLatin1String("vacation"))) {
130  mStackWidget->setCurrentIndex(ScriptNotSupported);
131  return;
132  }
133 
134  mUrl = mUrl.adjusted(QUrl::RemoveFilename);
135  mUrl.setPath(mUrl.path() + QLatin1Char('/') + scriptName);
136 
137  // Whether the server supports the "date" extension
138  mHasDateSupport = mUrl.scheme() == QLatin1String("sieve") && sieveCapabilities.contains(QLatin1String("date"));
139 
140  KSieveUi::VacationUtils::Vacation vacation = KSieveUi::VacationUtils::parseScript(script);
141 
142  if (!vacation.isValid() && !script.trimmed().isEmpty()) {
143  mVacationWarningWidget->setVisible(true);
144  }
145 
146  mWasActive = active;
147  mVacationEditWidget->setEnabled(true);
148  mVacationEditWidget->setActivateVacation(active && vacation.active);
149  mVacationEditWidget->setMessageText(vacation.messageText);
150  mVacationEditWidget->setSubject(vacation.subject);
151  mVacationEditWidget->setMailAction(vacation.mailAction, vacation.mailActionRecipient);
152  mVacationEditWidget->setNotificationInterval(vacation.notificationInterval);
153  mVacationEditWidget->setMailAliases(vacation.aliases);
154  mVacationEditWidget->setSendForSpam(vacation.sendForSpam);
155  mVacationEditWidget->setDomainName(vacation.reactOndomainName);
156  mVacationEditWidget->enableDomainAndSendForSpam(!VacationSettings::allowOutOfOfficeUploadButNoSettings());
157 
158  mVacationEditWidget->enableDates(mHasDateSupport);
159  if (mHasDateSupport) {
160  mVacationEditWidget->setStartDate(vacation.startDate);
161  mVacationEditWidget->setStartTime(vacation.startTime);
162  mVacationEditWidget->setEndDate(vacation.endDate);
163  mVacationEditWidget->setEndTime(vacation.endTime);
164  }
165  mVacationEditWidget->setChanged(false);
166 }
167 
168 KSieveUi::VacationCreateScriptJob *VacationPageWidget::writeScript(bool &errorFound)
169 {
170  if (mPageScript == Script) {
171  const bool active = mVacationEditWidget->activateVacation();
172  VacationUtils::Vacation vacation;
173  bool ok;
174  vacation.aliases = mVacationEditWidget->mailAliases(ok);
175  if (!ok) {
176  errorFound = true;
177  return nullptr;
178  }
179  vacation.valid = true;
180  vacation.active = active;
181  vacation.messageText = mVacationEditWidget->messageText();
182  vacation.subject = mVacationEditWidget->subject();
183  vacation.mailAction = mVacationEditWidget->mailAction();
184  //Check valid
185 
186  vacation.mailActionRecipient = mVacationEditWidget->mailActionRecipient(ok);
187  if (!ok) {
188  errorFound = true;
189  return nullptr;
190  }
191  vacation.notificationInterval = mVacationEditWidget->notificationInterval();
192  vacation.sendForSpam = mVacationEditWidget->sendForSpam();
193  vacation.reactOndomainName = mVacationEditWidget->domainName();
194  if (mHasDateSupport) {
195  vacation.startDate = mVacationEditWidget->startDate();
196  vacation.startTime = mVacationEditWidget->startTime();
197  vacation.endDate = mVacationEditWidget->endDate();
198  vacation.endTime = mVacationEditWidget->endTime();
199  } else {
200  vacation.startDate = QDate();
201  vacation.startTime = QTime();
202  vacation.endDate = QDate();
203  vacation.endTime = QTime();
204  }
205  const QString script = VacationUtils::composeScript(vacation);
206  KSieveUi::VacationCreateScriptJob *createJob = new KSieveUi::VacationCreateScriptJob;
207  createJob->setServerUrl(mUrl);
208  createJob->setServerName(mServerName);
209  createJob->setStatus(active, mWasActive);
210  createJob->setScript(script);
211  return createJob;
212  }
213  return nullptr;
214 }
215 
216 void VacationPageWidget::setDefault()
217 {
218  if (mVacationEditWidget->isEnabled()) {
219  mVacationEditWidget->setDefault();
220  }
221 }
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)
void addWidget(QWidget *widget, int stretch, Qt::Alignment alignment)
void setLayout(QLayout *layout)
bool isEmpty() const const
QString trimmed() const const
QString i18n(const char *text, const TYPE &arg...)
bool setAlignment(QWidget *w, Qt::Alignment alignment)
The SieveImapAccountSettings class.
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
void setWordWrap(bool on)
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.