Libksieve

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

KDE's Doxygen guidelines are available online.