Libksieve

vacationcheckjob.cpp
1 /*
2  SPDX-FileCopyrightText: 2013-2021 Laurent Montel <[email protected]>
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 "util/util_p.h"
12 
13 #include <kmanagesieve/sievejob.h>
14 
15 #include <KLocalizedString>
16 #include <QDate>
17 
18 #include "libksieve_debug.h"
19 
20 using namespace KSieveUi;
21 VacationCheckJob::VacationCheckJob(const QUrl &url, const QString &serverName, QObject *parent)
22  : QObject(parent)
23  , mServerName(serverName)
24  , mUrl(url)
25 {
26 }
27 
28 VacationCheckJob::~VacationCheckJob()
29 {
30  kill();
31 }
32 
33 void 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 
46 void VacationCheckJob::setKep14Support(bool kep14Support)
47 {
48  mKep14Support = kep14Support;
49 }
50 
51 void VacationCheckJob::start()
52 {
53  if (mKep14Support) {
54  QUrl url = mUrl;
55  url = url.adjusted(QUrl::RemoveFilename);
56  url.setPath(url.path() + QLatin1Char('/') + QLatin1String("USER"));
57  mParseJob = new ParseUserScriptJob(url, this);
58  connect(mParseJob, &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 
66 void 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(LIBKSIEVE_LOG) << "vacation script found :)";
86  }
87  } else if (isLastScript()) {
88  mNoScriptFound = true;
89  Q_EMIT vacationScriptActive(this, QString(), false);
90  qCDebug(LIBKSIEVE_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 
119 void VacationCheckJob::slotGotActiveScripts(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 
134 void 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 
151 void VacationCheckJob::emitError(const QString &errorMessage)
152 {
153  qCWarning(LIBKSIEVE_LOG) << errorMessage;
154  Q_EMIT error(errorMessage);
155 }
156 
157 void VacationCheckJob::searchVacationScript()
158 {
159  QStringList scriptList = mActiveScripts;
160 
161  // Reorder script list
162  for (const QString &script : qAsConst(mAvailableScripts)) {
163  if (!scriptList.contains(script)) {
164  scriptList.append(script);
165  }
166  }
167 
168  mAvailableScripts = scriptList;
169  mScriptPos = 0;
170  getNextScript();
171 }
172 
173 void VacationCheckJob::getNextScript()
174 {
175  if (isLastScript()) {
176  // TODO: no script found
177  mNoScriptFound = true;
178  Q_EMIT vacationScriptActive(this, QString(), false);
179  qCDebug(LIBKSIEVE_LOG) << "no vacation script found :(";
180  return;
181  }
182  QUrl url = mUrl;
183  url = url.adjusted(QUrl::RemoveFilename);
184  url.setPath(url.path() + QLatin1Char('/') + mAvailableScripts[mScriptPos]);
185  mScriptPos += 1;
186  if (Util::isKep14ProtectedName(url.fileName())) {
187  getNextScript();
188  }
189  mSieveJob = KManageSieve::SieveJob::get(url);
190  connect(mSieveJob, &KManageSieve::SieveJob::gotScript, this, &VacationCheckJob::slotGetResult);
191 }
192 
193 bool VacationCheckJob::isLastScript() const
194 {
195  return mScriptPos >= mAvailableScripts.count();
196 }
197 
198 bool VacationCheckJob::noScriptFound() const
199 {
200  return mNoScriptFound;
201 }
202 
203 QString VacationCheckJob::serverName() const
204 {
205  return mServerName;
206 }
207 
208 QString VacationCheckJob::script() const
209 {
210  return mScript;
211 }
212 
213 QStringList VacationCheckJob::sieveCapabilities() const
214 {
215  return mSieveCapabilities;
216 }
RemoveFilename
bool contains(const QString &str, Qt::CaseSensitivity cs) const const
void setPath(const QString &path, QUrl::ParsingMode mode)
void append(const T &value)
bool isEmpty() const const
void error(QWidget *parent, const QString &text, const QString &caption=QString(), Options options=Notify)
QString path(QUrl::ComponentFormattingOptions options) const const
void gotScript(KManageSieve::SieveJob *job, bool success, const QString &script, bool active)
This signal is emitted when a get job has finished.
bool contains(QChar ch, Qt::CaseSensitivity cs) const const
QString errorString() const
A human-readable error message.
Definition: sievejob.cpp:268
void gotList(KManageSieve::SieveJob *job, bool success, const QStringList &scriptList, const QString &activeScript)
This signal is emitted when a list job has finished.
QString i18n(const char *text, const TYPE &arg...)
QUrl adjusted(QUrl::FormattingOptions options) const const
QDate currentDate()
static SieveJob * get(const QUrl &source)
Gets a sieve script from an IMAP server.
Definition: sievejob.cpp:300
static SieveJob * list(const QUrl &url)
Lists all available scripts at the given sieve url.
Definition: sievejob.cpp:314
A job to manage sieve scripts.
Definition: sievejob.h:37
QString fileName(QUrl::ComponentFormattingOptions options) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Fri Apr 16 2021 23:09:34 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.