Libksieve

renamescriptjob.cpp
1 /*
2  SPDX-FileCopyrightText: 2016-2021 Laurent Montel <[email protected]>
3 
4  SPDX-License-Identifier: GPL-2.0-only
5 */
6 
7 #include "renamescriptjob.h"
8 #include <kmanagesieve/sievejob.h>
9 
10 #include <KLocalizedString>
11 using namespace KSieveUi;
12 
13 class KSieveUi::RenameScriptJobPrivate
14 {
15 public:
16  QString mNewName;
17  QUrl mOldUrl;
18  QUrl mNewUrl;
19  bool mIsActive = false;
20 };
21 
22 RenameScriptJob::RenameScriptJob(QObject *parent)
23  : QObject(parent)
24  , d(new RenameScriptJobPrivate)
25 {
26 }
27 
28 RenameScriptJob::~RenameScriptJob()
29 {
30 }
31 
32 void RenameScriptJob::setOldUrl(const QUrl &url)
33 {
34  d->mOldUrl = url;
35 }
36 
37 void RenameScriptJob::setIsActive(bool active)
38 {
39  d->mIsActive = active;
40 }
41 
42 void RenameScriptJob::setNewName(const QString &newName)
43 {
44  d->mNewName = newName;
45 }
46 
47 bool RenameScriptJob::canStart() const
48 {
49  return !d->mNewName.trimmed().isEmpty() && d->mOldUrl.isValid();
50 }
51 
52 void RenameScriptJob::start()
53 {
54  if (canStart()) {
56  connect(job, &KManageSieve::SieveJob::result, this, &RenameScriptJob::slotGetResult);
57  } else {
58  Q_EMIT finished(d->mOldUrl, d->mNewUrl, i18n("Impossible to start job"), false);
59  deleteLater();
60  }
61 }
62 
63 void RenameScriptJob::slotGetResult(KManageSieve::SieveJob *job, bool success, const QString &script, bool isActive)
64 {
65  Q_UNUSED(job)
66  Q_UNUSED(isActive)
67  if (!success) {
68  Q_EMIT finished(d->mOldUrl, d->mNewUrl, i18n("An error occurred during loading the sieve script."), false);
69  deleteLater();
70  return;
71  }
72  QUrl u = d->mOldUrl;
74  u.setPath(u.path() + d->mNewName);
75  d->mNewUrl = u;
76  KManageSieve::SieveJob *putJob = KManageSieve::SieveJob::put(d->mNewUrl, script, d->mIsActive, d->mIsActive);
77  connect(putJob, &KManageSieve::SieveJob::result, this, &RenameScriptJob::slotPutScript);
78 }
79 
80 void RenameScriptJob::slotPutScript(KManageSieve::SieveJob *job, bool success)
81 {
82  Q_UNUSED(job)
83  if (!success) {
84  Q_EMIT finished(d->mOldUrl, d->mNewUrl, i18n("An error occurred during saving the sieve script."), false);
85  deleteLater();
86  return;
87  }
88  KManageSieve::SieveJob *deleteJob = KManageSieve::SieveJob::del(d->mOldUrl);
89  connect(deleteJob, &KManageSieve::SieveJob::result, this, &RenameScriptJob::slotDeleteResult);
90 }
91 
92 void RenameScriptJob::slotDeleteResult(KManageSieve::SieveJob *job, bool success)
93 {
94  Q_UNUSED(job)
95  Q_EMIT finished(d->mOldUrl, d->mNewUrl, success ? QString() : i18n("An error occurred during deleting the sieve script."), success);
96  deleteLater();
97 }
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
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 * del(const QUrl &url)
Deletes the script with the given sieve url.
Definition: sievejob.cpp:327
void setPath(const QString &path, QUrl::ParsingMode mode)
bool isEmpty() const const
QString trimmed() const const
QString path(QUrl::ComponentFormattingOptions options) const const
QString i18n(const char *text, const TYPE &arg...)
QUrl adjusted(QUrl::FormattingOptions options) const const
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
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sun Apr 11 2021 23:09:36 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.