Libksieve

vacationcreatescriptjob.cpp
1 /*
2  SPDX-FileCopyrightText: 2013-2021 Laurent Montel <[email protected]>
3 
4  SPDX-License-Identifier: GPL-2.0-only
5 */
6 
7 #include "vacationcreatescriptjob.h"
8 #include "vacationutils.h"
9 #include <kmanagesieve/sievejob.h>
10 #include <managescriptsjob/generateglobalscriptjob.h>
11 #include <managescriptsjob/parseuserscriptjob.h>
12 
13 #include "libksieve_debug.h"
14 #include <KLocalizedString>
15 #include <KMessageBox>
16 
17 using namespace KSieveUi;
18 
19 VacationCreateScriptJob::VacationCreateScriptJob(QObject *parent)
20  : QObject(parent)
21 {
22 }
23 
24 VacationCreateScriptJob::~VacationCreateScriptJob()
25 {
26  kill();
27 }
28 
29 void VacationCreateScriptJob::kill()
30 {
31  if (mSieveJob) {
32  mSieveJob->kill();
33  }
34  mSieveJob = nullptr;
35 
36  if (mParseUserJob) {
37  mParseUserJob->kill();
38  }
39  mParseUserJob = nullptr;
40 
41  if (mCreateJob) {
42  mCreateJob->kill();
43  }
44  mParseUserJob = nullptr;
45 }
46 
47 void VacationCreateScriptJob::setStatus(bool activate, bool wasActive)
48 {
49  mActivate = activate;
50  mScriptActive = wasActive;
51 }
52 
53 void VacationCreateScriptJob::setServerName(const QString &servername)
54 {
55  mServerName = servername;
56 }
57 
58 const QString &VacationCreateScriptJob::serverName() const
59 {
60  return mServerName;
61 }
62 
63 void VacationCreateScriptJob::setKep14Support(bool kep14Support)
64 {
65  mKep14Support = kep14Support;
66 }
67 
68 void VacationCreateScriptJob::setServerUrl(const QUrl &url)
69 {
70  mUrl = url;
71 }
72 
73 void VacationCreateScriptJob::setScript(const QString &script)
74 {
75  mScript = script;
76 }
77 
78 void VacationCreateScriptJob::start()
79 {
80  if (mUrl.isEmpty()) {
81  qCDebug(LIBKSIEVE_LOG) << " server url is empty";
82  deleteLater();
83  return;
84  }
85 
86  mUserJobRunning = false;
87 
88  if (mKep14Support && mActivate && !mScriptActive) {
89  mUserJobRunning = true;
90  QUrl url = mUrl;
91  url = url.adjusted(QUrl::RemoveFilename);
92  url.setPath(url.path() + QLatin1Char('/') + QLatin1String("USER"));
93  mParseUserJob = new ParseUserScriptJob(url, this);
94  connect(mParseUserJob, &ParseUserScriptJob::finished, this, &VacationCreateScriptJob::slotGotActiveScripts);
95  mParseUserJob->start();
96  } else {
97  createScript();
98  }
99 }
100 
101 void VacationCreateScriptJob::createScript()
102 {
103  mScriptJobRunning = true;
104  mSieveJob = KManageSieve::SieveJob::get(mUrl);
105  connect(mSieveJob, &KManageSieve::SieveJob::gotScript, this, &VacationCreateScriptJob::slotGetScript);
106 }
107 
108 void VacationCreateScriptJob::slotGetScript(KManageSieve::SieveJob *job, bool success, const QString &oldScript, bool active)
109 {
110  Q_UNUSED(active)
111  Q_ASSERT(job == mSieveJob);
112  mSieveJob = nullptr;
113  if (success || !oldScript.trimmed().isEmpty()) {
114  QString script = VacationUtils::mergeRequireLine(oldScript, mScript);
115  script = VacationUtils::updateVacationBlock(oldScript, script);
116  }
117  if (mKep14Support) {
118  mSieveJob = KManageSieve::SieveJob::put(mUrl, mScript, false, false);
119  } else {
120  mSieveJob = KManageSieve::SieveJob::put(mUrl, mScript, mActivate, false); // Never deactivate
121  }
122  connect(mSieveJob, &KManageSieve::SieveJob::gotScript, this, &VacationCreateScriptJob::slotPutResult);
123 }
124 
125 void VacationCreateScriptJob::slotPutResult(KManageSieve::SieveJob *job, bool success)
126 {
127  Q_ASSERT(job == mSieveJob);
128  mSieveJob = nullptr;
129  mScriptJobRunning = false;
130  if (!success) {
131  mSuccess = false;
132  }
133  handleResult();
134 }
135 
136 void VacationCreateScriptJob::handleResult()
137 {
138  if (mUserJobRunning || mScriptJobRunning) { // Not both jobs are done
139  return;
140  }
141 
142  if (mSuccess) {
143  KMessageBox::information(nullptr,
144  mActivate ? i18n("Sieve script installed successfully on the server \'%1\'.\n"
145  "Out of Office reply is now active.",
146  mServerName)
147  : i18n("Sieve script installed successfully on the server \'%1\'.\n"
148  "Out of Office reply has been deactivated.",
149  mServerName));
150  } else {
151  KMessageBox::information(nullptr, i18n("Impossible to install script on server \'%1\'", mServerName));
152  }
153 
154  qCDebug(LIBKSIEVE_LOG) << "( ???," << mSuccess << ", ? )";
155  mSieveJob = nullptr; // job deletes itself after returning from this slot!
156  Q_EMIT result(mSuccess);
157  Q_EMIT scriptActive(mActivate, mServerName);
158  deleteLater();
159 }
160 
161 void VacationCreateScriptJob::slotGotActiveScripts(ParseUserScriptJob *job)
162 {
163  Q_ASSERT(job == mParseUserJob);
164  mParseUserJob = nullptr;
165  if (!job->error().isEmpty()) {
166  slotGenerateDone(job->error());
167  return;
168  }
169 
170  QStringList list = job->activeScriptList();
171  if (!list.contains(mUrl.fileName())) {
172  list.prepend(mUrl.fileName());
173  }
174  mCreateJob = new GenerateGlobalScriptJob(mUrl, this);
175  mCreateJob->addUserActiveScripts(list);
176  connect(mCreateJob, &GenerateGlobalScriptJob::success, this, [=]() {
177  this->slotGenerateDone();
178  });
179  connect(mCreateJob, &GenerateGlobalScriptJob::error, this, &VacationCreateScriptJob::slotGenerateDone);
180  mCreateJob->start();
181 }
182 
183 void VacationCreateScriptJob::slotGenerateDone(const QString &error)
184 {
185  mCreateJob = nullptr;
186  mUserJobRunning = false;
187  if (!error.isEmpty()) {
188  qCWarning(LIBKSIEVE_LOG) << error;
189  mSuccess = false;
190  handleResult();
191  } else {
192  createScript();
193  }
194 }
RemoveFilename
static SieveJob * put(const QUrl &destination, const QString &script, bool makeActive, bool wasActive)
Stores a sieve script on an IMAP server.
Definition: sievejob.cpp:278
bool contains(const QString &str, Qt::CaseSensitivity cs) const const
void setPath(const QString &path, QUrl::ParsingMode mode)
bool isEmpty() const const
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.
QString i18n(const char *text, const TYPE &arg...)
QUrl adjusted(QUrl::FormattingOptions options) const const
void prepend(const T &value)
void information(QWidget *parent, const QString &text, const QString &caption=QString(), const QString &dontShowAgainName=QString(), Options options=Notify)
static SieveJob * get(const QUrl &source)
Gets a sieve script from an IMAP server.
Definition: sievejob.cpp:300
A job to manage sieve scripts.
Definition: sievejob.h:37
KIOFILEWIDGETS_EXPORT QStringList list(const QString &fileClass)
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sun Apr 11 2021 23:09:37 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.