Libksieve

checkscriptjob.cpp
1 /*
2  SPDX-FileCopyrightText: 2016-2021 Laurent Montel <[email protected]>
3 
4  SPDX-License-Identifier: GPL-2.0-only
5 */
6 
7 #include "checkscriptjob.h"
8 #include "libksieve_debug.h"
9 #include <KLocalizedString>
10 
11 #include <sievejob.h>
12 
13 using namespace KSieveUi;
14 
15 class KSieveUi::CheckScriptJobPrivate
16 {
17 public:
18  QUrl mUrl;
19  QString mOriginalScript;
20  QString mCurrentScript;
21  bool mIsActive = false;
22 };
23 
24 CheckScriptJob::CheckScriptJob(QObject *parent)
25  : QObject(parent)
26  , d(new CheckScriptJobPrivate)
27 {
28 }
29 
30 CheckScriptJob::~CheckScriptJob() = default;
31 
32 void CheckScriptJob::setOriginalScript(const QString &script)
33 {
34  d->mOriginalScript = script;
35 }
36 
37 void CheckScriptJob::setCurrentScript(const QString &script)
38 {
39  d->mCurrentScript = script;
40 }
41 
42 void CheckScriptJob::setUrl(const QUrl &url)
43 {
44  d->mUrl = url;
45 }
46 
47 void CheckScriptJob::setIsActive(bool active)
48 {
49  d->mIsActive = active;
50 }
51 
52 void 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 {
58  deleteLater();
59  }
60 }
61 
62 void 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 
75 void CheckScriptJob::slotRestoreFile(KManageSieve::SieveJob *job, bool success)
76 {
77  Q_UNUSED(job)
78  if (!success) {
79  qCWarning(LIBKSIEVE_LOG) << "Script can't be restored" << d->mUrl;
80  }
81  deleteLater();
82 }
83 
84 bool CheckScriptJob::canStart() const
85 {
86  return d->mUrl.isValid() && !d->mCurrentScript.isEmpty();
87 }
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.
bool isEmpty() const const
void deleteLater()
QString errorString() const
A human-readable error message.
Definition: sievejob.cpp:268
QString i18n(const char *text, const TYPE &arg...)
QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
A job to manage sieve scripts.
Definition: sievejob.h:37
Q_EMITQ_EMIT
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Fri Apr 16 2021 23:09:33 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.