Libksieve

vacationcreatescriptjob.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 "vacationcreatescriptjob.h"
8#include "kmanagesieve/sievejob.h"
9#include "managescriptsjob/generateglobalscriptjob.h"
10#include "managescriptsjob/parseuserscriptjob.h"
11#include "vacationutils.h"
12
13#include "libksievecore_debug.h"
14#include <KLocalizedString>
15#include <KMessageBox>
16
17using namespace KSieveCore;
18
19VacationCreateScriptJob::VacationCreateScriptJob(QObject *parent)
20 : QObject(parent)
21{
22}
23
24VacationCreateScriptJob::~VacationCreateScriptJob()
25{
26 kill();
27}
28
29void 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
47void VacationCreateScriptJob::setStatus(bool activate, bool wasActive)
48{
49 mActivate = activate;
50 mScriptActive = wasActive;
51}
52
53void VacationCreateScriptJob::setServerName(const QString &servername)
54{
55 mServerName = servername;
56}
57
58const QString &VacationCreateScriptJob::serverName() const
59{
60 return mServerName;
61}
62
63void VacationCreateScriptJob::setKep14Support(bool kep14Support)
64{
65 mKep14Support = kep14Support;
66}
67
68void VacationCreateScriptJob::setServerUrl(const QUrl &url)
69{
70 mUrl = url;
71}
72
73void VacationCreateScriptJob::setScript(const QString &script)
74{
75 mScript = script;
76}
77
78void VacationCreateScriptJob::start()
79{
80 if (mUrl.isEmpty()) {
81 qCDebug(LIBKSIEVECORE_LOG) << " server url is empty";
83 return;
84 }
85
86 mUserJobRunning = false;
87
88 if (mKep14Support && mActivate && !mScriptActive) {
89 mUserJobRunning = true;
90 QUrl url = mUrl;
92 url.setPath(url.path() + QLatin1Char('/') + QLatin1StringView("USER"));
93 mParseUserJob = new KSieveCore::ParseUserScriptJob(url, this);
94 connect(mParseUserJob, &KSieveCore::ParseUserScriptJob::finished, this, &VacationCreateScriptJob::slotGotActiveScripts);
95 mParseUserJob->start();
96 } else {
97 createScript();
98 }
99}
100
101void VacationCreateScriptJob::createScript()
102{
103 mScriptJobRunning = true;
104 mSieveJob = KManageSieve::SieveJob::get(mUrl);
105 connect(mSieveJob, &KManageSieve::SieveJob::gotScript, this, &VacationCreateScriptJob::slotGetScript);
106}
107
108void 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 = KSieveCore::VacationUtils::mergeRequireLine(oldScript, mScript);
115 script = KSieveCore::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
125void 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
136void VacationCreateScriptJob::handleResult()
137{
138 if (mUserJobRunning || mScriptJobRunning) { // Not both jobs are done
139 return;
140 }
141
142 if (mSuccess) {
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(LIBKSIEVECORE_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
161void VacationCreateScriptJob::slotGotActiveScripts(KSieveCore::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 KSieveCore::GenerateGlobalScriptJob(mUrl, this);
175 mCreateJob->addUserActiveScripts(list);
176 connect(mCreateJob, &KSieveCore::GenerateGlobalScriptJob::success, this, [this]() {
177 this->slotGenerateDone();
178 });
179 connect(mCreateJob, &KSieveCore::GenerateGlobalScriptJob::error, this, &VacationCreateScriptJob::slotGenerateDone);
180 mCreateJob->start();
181}
182
183void VacationCreateScriptJob::slotGenerateDone(const QString &error)
184{
185 mCreateJob = nullptr;
186 mUserJobRunning = false;
187 if (!error.isEmpty()) {
188 qCWarning(LIBKSIEVECORE_LOG) << error;
189 mSuccess = false;
190 handleResult();
191 } else {
192 createScript();
193 }
194}
195
196#include "moc_vacationcreatescriptjob.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
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
static SieveJob * get(const QUrl &source)
Gets a sieve script from an IMAP server.
Definition sievejob.cpp:300
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...)
void information(QWidget *parent, const QString &text, const QString &title=QString(), const QString &dontShowAgainName=QString(), Options options=Notify)
void error(QWidget *parent, const QString &text, const QString &title, const KGuiItem &buttonOk, Options options=Notify)
KIOCORE_EXPORT QStringList list(const QString &fileClass)
void prepend(parameter_type value)
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
void deleteLater()
bool isEmpty() const const
QString trimmed() const const
bool contains(QLatin1StringView str, Qt::CaseSensitivity cs) const const
RemoveFilename
QUrl adjusted(FormattingOptions options) const const
QString fileName(ComponentFormattingOptions options) const const
bool isEmpty() 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 Sat Dec 21 2024 16:57:10 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.