Libksieve

checkscriptjob.cpp
1/*
2 SPDX-FileCopyrightText: 2016-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: GPL-2.0-only
5*/
6
7#include "checkscriptjob.h"
8#include "libksievecore_debug.h"
9#include <KLocalizedString>
10
11#include "sievejob.h"
12
13using namespace KSieveCore;
14
15class KSieveCore::CheckScriptJobPrivate
16{
17public:
18 QUrl mUrl;
19 QString mOriginalScript;
20 QString mCurrentScript;
21 bool mIsActive = false;
22};
23
24CheckScriptJob::CheckScriptJob(QObject *parent)
25 : QObject(parent)
26 , d(new CheckScriptJobPrivate)
27{
28}
29
30CheckScriptJob::~CheckScriptJob() = default;
31
32void CheckScriptJob::setOriginalScript(const QString &script)
33{
34 d->mOriginalScript = script;
35}
36
37void CheckScriptJob::setCurrentScript(const QString &script)
38{
39 d->mCurrentScript = script;
40}
41
42void CheckScriptJob::setUrl(const QUrl &url)
43{
44 d->mUrl = url;
45}
46
47void CheckScriptJob::setIsActive(bool active)
48{
49 d->mIsActive = active;
50}
51
52void CheckScriptJob::start()
53{
54 if (canStart()) {
55 KManageSieve::SieveJob *job = KManageSieve::SieveJob::put(d->mUrl, d->mCurrentScript, d->mIsActive, d->mIsActive);
56 connect(job, &KManageSieve::SieveJob::result, this, &CheckScriptJob::slotPutCheckSyntaxResult);
57 } else {
59 }
60}
61
62void CheckScriptJob::slotPutCheckSyntaxResult(KManageSieve::SieveJob *job, bool success)
63{
64 if (success) {
65 Q_EMIT finished(i18n("No errors found."), true);
66 } else {
67 const QString errorMsg = job->errorString();
68 Q_EMIT finished(errorMsg.isEmpty() ? i18n("An unknown error was encountered.") : errorMsg, false);
69 }
70 // Put original script after check otherwise we will put a script even if we don't click on ok
71 KManageSieve::SieveJob *restoreJob = KManageSieve::SieveJob::put(d->mUrl, d->mOriginalScript, d->mIsActive, d->mIsActive);
72 connect(restoreJob, &KManageSieve::SieveJob::result, this, &CheckScriptJob::slotRestoreFile);
73}
74
75void CheckScriptJob::slotRestoreFile(KManageSieve::SieveJob *job, bool success)
76{
77 Q_UNUSED(job)
78 if (!success) {
79 qCWarning(LIBKSIEVECORE_LOG) << "Script can't be restored" << d->mUrl;
80 }
82}
83
84bool CheckScriptJob::canStart() const
85{
86 return d->mUrl.isValid() && !d->mCurrentScript.isEmpty();
87}
88
89#include "moc_checkscriptjob.cpp"
A job to manage sieve scripts.
Definition sievejob.h:31
QString errorString() const
A human-readable error message.
Definition sievejob.cpp:268
void result(KManageSieve::SieveJob *job, bool success, const QString &script, bool active)
This signal is emitted for all kind of jobs when they have finished.
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
QString i18n(const char *text, const TYPE &arg...)
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
void deleteLater()
bool isEmpty() const const
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.