Libksieve

vacationpagewidget.cpp
1/*
2 SPDX-FileCopyrightText: 2013-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: GPL-2.0-only
5*/
6
7#include "vacationpagewidget.h"
8#include "managescriptsjob/parseuserscriptjob.h"
9#include "vacationeditwidget.h"
10#include "vacationwarningwidget.h"
11#include <KSieveCore/MultiImapVacationManager>
12#include <KSieveCore/VacationCreateScriptJob>
13#include <KSieveCore/VacationUtils>
14#include <ksievecore/sieve-vacation.h>
15
16#include "kmanagesieve/sievejob.h"
17
18#include "libksieveui_debug.h"
19#include <KLocalizedString>
20
21#include <QLabel>
22#include <QStackedWidget>
23#include <QVBoxLayout>
24
25using namespace KSieveUi;
26VacationPageWidget::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 = new QLabel(i18nc("@label:textbox",
49 "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
67VacationPageWidget::~VacationPageWidget() = default;
68
69bool VacationPageWidget::wasChanged() const
70{
71 return mVacationEditWidget->changed();
72}
73
74void VacationPageWidget::setServerUrl(const QUrl &url)
75{
76 mUrl = url;
77 mVacationEditWidget->setEnabled(false);
78}
79
80void VacationPageWidget::setVacationManager(KSieveCore::MultiImapVacationManager *vacationManager)
81{
82 mVacationManager = vacationManager;
83 connect(mVacationManager, &KSieveCore::MultiImapVacationManager::scriptAvailable, this, &VacationPageWidget::slotGetResult);
84 mVacationManager->checkVacation(mServerName, mUrl);
85}
86
87void VacationPageWidget::setSieveImapAccountSettings(const KSieveCore::SieveImapAccountSettings &account)
88{
89 mVacationEditWidget->setSieveImapAccountSettings(account);
90}
91
92void VacationPageWidget::setServerName(const QString &serverName)
93{
94 mServerName = serverName;
95}
96
97void VacationPageWidget::slotGetResult(const QString &serverName,
98 const QStringList &sieveCapabilities,
99 const QString &scriptName,
100 const QString &script,
101 bool active)
102{
103 if (mServerName != serverName) {
104 return;
105 }
106 qCDebug(LIBKSIEVEUI_LOG) << serverName << sieveCapabilities << Qt::endl << scriptName << "(" << active << ")" << Qt::endl;
107
108 if (mUrl.scheme() == QLatin1StringView("sieve") && !sieveCapabilities.contains(QLatin1StringView("vacation"))) {
109 mStackWidget->setCurrentIndex(ScriptNotSupported);
110 return;
111 }
112
113 mUrl = mUrl.adjusted(QUrl::RemoveFilename);
114 mUrl.setPath(mUrl.path() + QLatin1Char('/') + scriptName);
115
116 // Whether the server supports the "date" extension
117 mHasDateSupport = mUrl.scheme() == QLatin1StringView("sieve") && sieveCapabilities.contains(QLatin1StringView("date"));
118
119 KSieveCore::VacationUtils::Vacation vacation = KSieveCore::VacationUtils::parseScript(script);
120
121 if (!vacation.isValid() && !script.trimmed().isEmpty()) {
122 mVacationWarningWidget->setVisible(true);
123 }
124
125 mWasActive = active;
126 mVacationEditWidget->setEnabled(true);
127 mVacationEditWidget->setActivateVacation(active && vacation.active);
128 mVacationEditWidget->setMessageText(vacation.messageText);
129 mVacationEditWidget->setSubject(vacation.subject);
130 mVacationEditWidget->setMailAction(vacation.mailAction, vacation.mailActionRecipient);
131 mVacationEditWidget->setNotificationInterval(vacation.notificationInterval);
132 mVacationEditWidget->setMailAliases(vacation.aliases);
133 mVacationEditWidget->setSendForSpam(vacation.sendForSpam);
134 mVacationEditWidget->setDomainName(vacation.reactOndomainName);
135 mVacationEditWidget->enableDomainAndSendForSpam(!KSieveCore::VacationSettings::allowOutOfOfficeUploadButNoSettings());
136
137 mVacationEditWidget->enableDates(mHasDateSupport);
138 if (mHasDateSupport) {
139 mVacationEditWidget->setStartDate(vacation.startDate);
140 mVacationEditWidget->setStartTime(vacation.startTime);
141 mVacationEditWidget->setEndDate(vacation.endDate);
142 mVacationEditWidget->setEndTime(vacation.endTime);
143 }
144 mVacationEditWidget->setChanged(false);
145}
146
147KSieveCore::VacationCreateScriptJob *VacationPageWidget::writeScript(bool &errorFound)
148{
149 if (mPageScript == Script) {
150 const bool active = mVacationEditWidget->activateVacation();
151 KSieveCore::VacationUtils::Vacation vacation;
152 bool ok;
153 vacation.aliases = mVacationEditWidget->mailAliases(ok);
154 if (!ok) {
155 errorFound = true;
156 return nullptr;
157 }
158 vacation.valid = true;
159 vacation.active = active;
160 vacation.messageText = mVacationEditWidget->messageText();
161 vacation.subject = mVacationEditWidget->subject();
162 vacation.mailAction = mVacationEditWidget->mailAction();
163 // Check valid
164
165 vacation.mailActionRecipient = mVacationEditWidget->mailActionRecipient(ok);
166 if (!ok) {
167 errorFound = true;
168 return nullptr;
169 }
170 vacation.notificationInterval = mVacationEditWidget->notificationInterval();
171 vacation.sendForSpam = mVacationEditWidget->sendForSpam();
172 vacation.reactOndomainName = mVacationEditWidget->domainName();
173 if (mHasDateSupport) {
174 vacation.startDate = mVacationEditWidget->startDate();
175 vacation.startTime = mVacationEditWidget->startTime();
176 vacation.endDate = mVacationEditWidget->endDate();
177 vacation.endTime = mVacationEditWidget->endTime();
178 } else {
179 vacation.startDate = QDate();
180 vacation.startTime = QTime();
181 vacation.endDate = QDate();
182 vacation.endTime = QTime();
183 }
184 const QString script = KSieveCore::VacationUtils::composeScript(vacation);
185 auto createJob = new KSieveCore::VacationCreateScriptJob;
186 createJob->setServerUrl(mUrl);
187 createJob->setServerName(mServerName);
188 createJob->setStatus(active, mWasActive);
189 createJob->setScript(script);
190 return createJob;
191 }
192 return nullptr;
193}
194
195void VacationPageWidget::setDefault()
196{
197 if (mVacationEditWidget->isEnabled()) {
198 mVacationEditWidget->setDefault();
199 }
200}
201
202#include "moc_vacationpagewidget.cpp"
The MultiImapVacationManager class.
The SieveImapAccountSettings class.
QString i18nc(const char *context, const char *text, const TYPE &arg...)
void setBold(bool enable)
void setContentsMargins(const QMargins &margins)
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
int addWidget(QWidget *widget)
void setCurrentIndex(int index)
bool isEmpty() const const
QString trimmed() const const
bool contains(QLatin1StringView str, Qt::CaseSensitivity cs) const const
AlignVCenter
QTextStream & endl(QTextStream &stream)
RemoveFilename
QUrl adjusted(FormattingOptions options) const const
QString path(ComponentFormattingOptions options) const const
QString scheme() const const
void setPath(const QString &path, ParsingMode mode)
void setEnabled(bool)
virtual void setVisible(bool visible)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:14:30 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.