Libksieve

renamescriptjob.cpp
1 /*
2  Copyright (C) 2016-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 "renamescriptjob.h"
19 #include <kmanagesieve/sievejob.h>
20 
21 #include <KLocalizedString>
22 using namespace KSieveUi;
23 
24 class KSieveUi::RenameScriptJobPrivate
25 {
26 public:
27  RenameScriptJobPrivate()
28  : mIsActive(false)
29  {
30  }
31 
32  QString mNewName;
33  QUrl mOldUrl;
34  QUrl mNewUrl;
35  bool mIsActive;
36 };
37 
38 RenameScriptJob::RenameScriptJob(QObject *parent)
39  : QObject(parent)
40  , d(new RenameScriptJobPrivate)
41 {
42 }
43 
44 RenameScriptJob::~RenameScriptJob()
45 {
46  delete d;
47 }
48 
49 void RenameScriptJob::setOldUrl(const QUrl &url)
50 {
51  d->mOldUrl = url;
52 }
53 
54 void RenameScriptJob::setIsActive(bool active)
55 {
56  d->mIsActive = active;
57 }
58 
59 void RenameScriptJob::setNewName(const QString &newName)
60 {
61  d->mNewName = newName;
62 }
63 
64 bool RenameScriptJob::canStart() const
65 {
66  return !d->mNewName.trimmed().isEmpty() && d->mOldUrl.isValid();
67 }
68 
69 void RenameScriptJob::start()
70 {
71  if (canStart()) {
73  connect(job, &KManageSieve::SieveJob::result, this, &RenameScriptJob::slotGetResult);
74  } else {
75  Q_EMIT finished(d->mOldUrl, d->mNewUrl, i18n("Impossible to start job"), false);
76  deleteLater();
77  }
78 }
79 
80 void RenameScriptJob::slotGetResult(KManageSieve::SieveJob *job, bool success, const QString &script, bool isActive)
81 {
82  Q_UNUSED(job);
83  Q_UNUSED(isActive);
84  if (!success) {
85  Q_EMIT finished(d->mOldUrl, d->mNewUrl, i18n("An error occurred during loading the sieve script."), false);
86  deleteLater();
87  return;
88  }
89  QUrl u = d->mOldUrl;
91  u.setPath(u.path() + d->mNewName);
92  d->mNewUrl = u;
93  KManageSieve::SieveJob *putJob = KManageSieve::SieveJob::put(d->mNewUrl, script, d->mIsActive, d->mIsActive);
94  connect(putJob, &KManageSieve::SieveJob::result, this, &RenameScriptJob::slotPutScript);
95 }
96 
97 void RenameScriptJob::slotPutScript(KManageSieve::SieveJob *job, bool success)
98 {
99  Q_UNUSED(job);
100  if (!success) {
101  Q_EMIT finished(d->mOldUrl, d->mNewUrl, i18n("An error occurred during saving the sieve script."), false);
102  deleteLater();
103  return;
104  }
105  KManageSieve::SieveJob *deleteJob = KManageSieve::SieveJob::del(d->mOldUrl);
106  connect(deleteJob, &KManageSieve::SieveJob::result, this, &RenameScriptJob::slotDeleteResult);
107 }
108 
109 void RenameScriptJob::slotDeleteResult(KManageSieve::SieveJob *job, bool success)
110 {
111  Q_UNUSED(job);
112  Q_EMIT finished(d->mOldUrl, d->mNewUrl, success ? QString() : i18n("An error occurred during deleting the sieve script."), success);
113  deleteLater();
114 }
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:288
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:337
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:310
A job to manage sieve scripts.
Definition: sievejob.h:41
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Tue Jul 14 2020 23:08:49 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.