Libksieve

renamescriptjob.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 "renamescriptjob.h"
8#include "kmanagesieve/sievejob.h"
9
10#include <KLocalizedString>
11using namespace KSieveCore;
12
13class KSieveCore::RenameScriptJobPrivate
14{
15public:
16 QString mNewName;
17 QUrl mOldUrl;
18 QUrl mNewUrl;
19 bool mIsActive = false;
20};
21
22RenameScriptJob::RenameScriptJob(QObject *parent)
23 : QObject(parent)
24 , d(new RenameScriptJobPrivate)
25{
26}
27
28RenameScriptJob::~RenameScriptJob() = default;
29
30void RenameScriptJob::setOldUrl(const QUrl &url)
31{
32 d->mOldUrl = url;
33}
34
35void RenameScriptJob::setIsActive(bool active)
36{
37 d->mIsActive = active;
38}
39
40void RenameScriptJob::setNewName(const QString &newName)
41{
42 d->mNewName = newName;
43}
44
45bool RenameScriptJob::canStart() const
46{
47 return !d->mNewName.trimmed().isEmpty() && d->mOldUrl.isValid();
48}
49
50void RenameScriptJob::start()
51{
52 if (canStart()) {
54 connect(job, &KManageSieve::SieveJob::result, this, &RenameScriptJob::slotGetResult);
55 } else {
56 Q_EMIT finished(d->mOldUrl, d->mNewUrl, i18n("Impossible to start job"), false);
58 }
59}
60
61void RenameScriptJob::slotGetResult(KManageSieve::SieveJob *job, bool success, const QString &script, bool isActive)
62{
63 Q_UNUSED(job)
64 Q_UNUSED(isActive)
65 if (!success) {
66 Q_EMIT finished(d->mOldUrl, d->mNewUrl, i18n("An error occurred during loading the sieve script."), false);
68 return;
69 }
70 QUrl u = d->mOldUrl;
72 u.setPath(u.path() + d->mNewName);
73 d->mNewUrl = u;
74 KManageSieve::SieveJob *putJob = KManageSieve::SieveJob::put(d->mNewUrl, script, d->mIsActive, d->mIsActive);
75 connect(putJob, &KManageSieve::SieveJob::result, this, &RenameScriptJob::slotPutScript);
76}
77
78void RenameScriptJob::slotPutScript(KManageSieve::SieveJob *job, bool success)
79{
80 Q_UNUSED(job)
81 if (!success) {
82 Q_EMIT finished(d->mOldUrl, d->mNewUrl, i18n("An error occurred during saving the sieve script."), false);
84 return;
85 }
87 connect(deleteJob, &KManageSieve::SieveJob::result, this, &RenameScriptJob::slotDeleteResult);
88}
89
90void RenameScriptJob::slotDeleteResult(KManageSieve::SieveJob *job, bool success)
91{
92 Q_UNUSED(job)
93 Q_EMIT finished(d->mOldUrl, d->mNewUrl, success ? QString() : i18n("An error occurred during deleting the sieve script."), success);
95}
96
97#include "moc_renamescriptjob.cpp"
A job to manage sieve scripts.
Definition sievejob.h:31
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
static SieveJob * del(const QUrl &url)
Deletes the script with the given sieve url.
Definition sievejob.cpp:327
static SieveJob * get(const QUrl &source)
Gets a sieve script from an IMAP server.
Definition sievejob.cpp:300
QString i18n(const char *text, const TYPE &arg...)
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
void deleteLater()
RemoveFilename
QUrl adjusted(FormattingOptions options) 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 Tue Mar 26 2024 11:17:19 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.