Libksieve

vacationcheckjob.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 "vacationcheckjob.h"
8#include "managescriptsjob/parseuserscriptjob.h"
9#include "vacationutils.h"
10
11#include <KSieveCore/Util>
12
13#include "kmanagesieve/sievejob.h"
14
15#include <KLocalizedString>
16#include <QDate>
17
18#include "libksievecore_debug.h"
19
20using namespace KSieveCore;
21VacationCheckJob::VacationCheckJob(const QUrl &url, const QString &serverName, QObject *parent)
22 : QObject(parent)
23 , mServerName(serverName)
24 , mUrl(url)
25{
26}
27
28VacationCheckJob::~VacationCheckJob()
29{
30 kill();
31}
32
33void VacationCheckJob::kill()
34{
35 if (mSieveJob) {
36 mSieveJob->kill();
37 }
38 mSieveJob = nullptr;
39
40 if (mParseJob) {
41 mParseJob->kill();
42 }
43 mParseJob = nullptr;
44}
45
46void VacationCheckJob::setKep14Support(bool kep14Support)
47{
48 mKep14Support = kep14Support;
49}
50
51void VacationCheckJob::start()
52{
53 if (mKep14Support) {
54 QUrl url = mUrl;
56 url.setPath(url.path() + QLatin1Char('/') + QLatin1StringView("USER"));
57 mParseJob = new KSieveCore::ParseUserScriptJob(url, this);
58 connect(mParseJob, &KSieveCore::ParseUserScriptJob::finished, this, &VacationCheckJob::slotGotActiveScripts);
59 mParseJob->start();
60 } else {
61 mSieveJob = KManageSieve::SieveJob::get(mUrl);
62 connect(mSieveJob, &KManageSieve::SieveJob::gotScript, this, &VacationCheckJob::slotGetResult);
63 }
64}
65
66void VacationCheckJob::slotGetResult(KManageSieve::SieveJob *job, bool success, const QString &script, bool active)
67{
68 Q_ASSERT(job == mSieveJob);
69 mScript = script;
70 mSieveCapabilities = mSieveJob->sieveCapabilities();
71 mSieveJob = nullptr;
72
73 if (mKep14Support) {
74 VacationUtils::Vacation vacation = VacationUtils::parseScript(script);
75 if (vacation.isValid()) {
76 if (mAvailableScripts.isEmpty()) {
77 Q_EMIT vacationScriptActive(this, QString(), false);
78 } else {
79 const QString &scriptName = mAvailableScripts[mScriptPos - 1];
80 bool hasVacationActive = mActiveScripts.contains(scriptName) && vacation.active;
81 if (hasVacationActive && vacation.startDate.isValid() && vacation.endDate.isValid()) {
82 hasVacationActive = (vacation.startDate <= QDate::currentDate() && vacation.endDate >= QDate::currentDate());
83 }
84 Q_EMIT vacationScriptActive(this, scriptName, hasVacationActive);
85 qCDebug(LIBKSIEVECORE_LOG) << "vacation script found :)";
86 }
87 } else if (isLastScript()) {
88 mNoScriptFound = true;
89 Q_EMIT vacationScriptActive(this, QString(), false);
90 qCDebug(LIBKSIEVECORE_LOG) << "no vacation script found :(";
91 } else {
92 getNextScript();
93 }
94 } else {
95 if (!success || !active) {
96 if (!success) {
97 mNoScriptFound = true;
98 }
99 Q_EMIT vacationScriptActive(this, mUrl.fileName(), false);
100 return;
101 }
102
103 bool hasVacationActive = active;
104 VacationUtils::Vacation vacation = VacationUtils::parseScript(script);
105 if (vacation.isValid()) {
106 hasVacationActive = vacation.active;
107 if (hasVacationActive && vacation.startDate.isValid() && vacation.endDate.isValid()) {
108 hasVacationActive = (vacation.startDate <= QDate::currentDate() && vacation.endDate >= QDate::currentDate());
109 }
110 }
111
112 if (hasVacationActive) {
113 mActiveScripts << mUrl.fileName();
114 }
115 Q_EMIT vacationScriptActive(this, mUrl.fileName(), hasVacationActive);
116 }
117}
118
119void VacationCheckJob::slotGotActiveScripts(KSieveCore::ParseUserScriptJob *job)
120{
121 Q_ASSERT(job == mParseJob);
122 mParseJob = nullptr;
123 const QString jobError = job->error();
124 if (!jobError.isEmpty()) {
125 emitError(i18n("ParseUserScriptJob failed: %1", jobError));
126 return;
127 }
128 mActiveScripts = job->activeScriptList();
129
130 mSieveJob = KManageSieve::SieveJob::list(job->scriptUrl());
131 connect(mSieveJob, &KManageSieve::SieveJob::gotList, this, &VacationCheckJob::slotGotList);
132}
133
134void VacationCheckJob::slotGotList(KManageSieve::SieveJob *job, bool success, const QStringList &availableScripts, const QString &activeScript)
135{
136 Q_UNUSED(activeScript)
137 Q_ASSERT(job == mSieveJob);
138 mSieveJob = nullptr;
139 if (!success) {
140 emitError(
141 i18n("Failed to get the list of Sieve scripts.\n"
142 "The server responded:\n%1",
143 job->errorString()));
144 return;
145 }
146
147 mAvailableScripts = availableScripts;
148 searchVacationScript();
149}
150
151void VacationCheckJob::emitError(const QString &errorMessage)
152{
153 qCWarning(LIBKSIEVECORE_LOG) << errorMessage;
154 Q_EMIT error(errorMessage);
155}
156
157void VacationCheckJob::searchVacationScript()
158{
159 QStringList scriptList = mActiveScripts;
160
161 // Reorder script list
162 for (const QString &script : std::as_const(mAvailableScripts)) {
163 if (!scriptList.contains(script)) {
164 scriptList.append(script);
165 }
166 }
167
168 mAvailableScripts = scriptList;
169 mScriptPos = 0;
170 getNextScript();
171}
172
173void VacationCheckJob::getNextScript()
174{
175 if (isLastScript()) {
176 // TODO: no script found
177 mNoScriptFound = true;
178 Q_EMIT vacationScriptActive(this, QString(), false);
179 qCDebug(LIBKSIEVECORE_LOG) << "no vacation script found :(";
180 return;
181 }
182 QUrl url = mUrl;
184 url.setPath(url.path() + QLatin1Char('/') + mAvailableScripts[mScriptPos]);
185 mScriptPos += 1;
187 getNextScript();
188 }
189 mSieveJob = KManageSieve::SieveJob::get(url);
190 connect(mSieveJob, &KManageSieve::SieveJob::gotScript, this, &VacationCheckJob::slotGetResult);
191}
192
193bool VacationCheckJob::isLastScript() const
194{
195 return mScriptPos >= mAvailableScripts.count();
196}
197
198bool VacationCheckJob::noScriptFound() const
199{
200 return mNoScriptFound;
201}
202
203QString VacationCheckJob::serverName() const
204{
205 return mServerName;
206}
207
208QString VacationCheckJob::script() const
209{
210 return mScript;
211}
212
213QStringList VacationCheckJob::sieveCapabilities() const
214{
215 return mSieveCapabilities;
216}
217
218#include "moc_vacationcheckjob.cpp"
A job to manage sieve scripts.
Definition sievejob.h:31
void kill(KJob::KillVerbosity verbosity=KJob::Quietly)
Kills the sieve job.
Definition sievejob.cpp:246
QString errorString() const
A human-readable error message.
Definition sievejob.cpp:268
QStringList sieveCapabilities() const
Returns the sieve capabilities of the IMAP server.
Definition sievejob.cpp:254
static SieveJob * list(const QUrl &url)
Lists all available scripts at the given sieve url.
Definition sievejob.cpp:314
static SieveJob * get(const QUrl &source)
Gets a sieve script from an IMAP server.
Definition sievejob.cpp:300
void gotList(KManageSieve::SieveJob *job, bool success, const QStringList &scriptList, const QString &activeScript)
This signal is emitted when a list job has finished.
void gotScript(KManageSieve::SieveJob *job, bool success, const QString &script, bool active)
This signal is emitted when a get job has finished.
QString i18n(const char *text, const TYPE &arg...)
KCALUTILS_EXPORT QString errorMessage(const KCalendarCore::Exception &exception)
KSIEVECORE_EXPORT bool isKep14ProtectedName(const QString &scriptName)
Is the given scriptName a protected KEP:14 name, that a normal user should not touch directly.
Definition util.cpp:94
QDate currentDate()
bool isValid(int year, int month, int day)
void append(QList< T > &&value)
qsizetype count() const const
bool isEmpty() const const
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
bool isEmpty() const const
bool contains(QLatin1StringView str, Qt::CaseSensitivity cs) const const
RemoveFilename
QUrl adjusted(FormattingOptions options) const const
QString fileName(ComponentFormattingOptions options) const const
QString path(ComponentFormattingOptions options) const const
void setPath(const QString &path, ParsingMode mode)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 22 2024 12:09:39 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.