summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMontel Laurent <montel@kde.org>2016-11-02 12:13:36 (GMT)
committerMontel Laurent <montel@kde.org>2016-11-02 12:13:36 (GMT)
commit0557b7fd35111d725f82cdf8a4498c763ced1551 (patch)
treeaf9e8a1b0e3bd6288a98bb691cce4f049dc06f1d
parent3bf3931583fd6daf9610bec498ddfdde362da9bf (diff)
Extract check script method
-rw-r--r--src/ksieveui/CMakeLists.txt14
-rw-r--r--src/ksieveui/managescriptsjob/checkscriptjob.cpp107
-rw-r--r--src/ksieveui/managescriptsjob/checkscriptjob.h57
3 files changed, 174 insertions, 4 deletions
diff --git a/src/ksieveui/CMakeLists.txt b/src/ksieveui/CMakeLists.txt
index 493061b..037a2fa 100644
--- a/src/ksieveui/CMakeLists.txt
+++ b/src/ksieveui/CMakeLists.txt
@@ -173,6 +173,14 @@ set(ksieveui_sievescriptdebugger_LIB_SRCS
sievescriptdebugger/sievescriptdebuggerresulteditor.cpp
)
+set(ksieveui_managescriptjob_LIB_SRCS
+ managescriptsjob/checkkolabkep14supportjob.cpp
+ managescriptsjob/generateglobalscriptjob.cpp
+ managescriptsjob/parseuserscriptjob.cpp
+ managescriptsjob/renamescriptjob.cpp
+ managescriptsjob/checkscriptjob.cpp
+ )
+
set(ksieveui_LIB_SRCS
managesievescriptsdialog.cpp
widgets/custommanagesievewidget.cpp
@@ -181,10 +189,6 @@ set(ksieveui_LIB_SRCS
widgets/managesievewidget.cpp
debug/sievedebugdialog.cpp
util/util.cpp
- managescriptsjob/checkkolabkep14supportjob.cpp
- managescriptsjob/generateglobalscriptjob.cpp
- managescriptsjob/parseuserscriptjob.cpp
- managescriptsjob/renamescriptjob.cpp
templates/sievetemplatewidget.cpp
templates/sievedefaulttemplate.cpp
templates/sievetemplateeditdialog.cpp
@@ -193,6 +197,7 @@ set(ksieveui_LIB_SRCS
${ksieveui_editor_LIB_SRCS}
${ksieveui_autocreatescripts_LIB_SRCS}
${ksieveui_sievescriptdebugger_LIB_SRCS}
+ ${ksieveui_managescriptjob_LIB_SRCS}
)
ecm_qt_declare_logging_category(ksieveui_LIB_SRCS HEADER libksieve_debug.h IDENTIFIER LIBKSIEVE_LOG CATEGORY_NAME org.kde.pim.libksieve)
@@ -254,6 +259,7 @@ ecm_generate_headers(ksieveui_Camelcaseutil_HEADERS
ecm_generate_headers(ksieveui_Camelmanagescriptsjob_HEADERS
HEADER_NAMES
RenameScriptJob
+ CheckScriptJob
REQUIRED_HEADERS ksieveui_managescriptsjob_HEADERS
PREFIX KSieveUi
RELATIVE managescriptsjob
diff --git a/src/ksieveui/managescriptsjob/checkscriptjob.cpp b/src/ksieveui/managescriptsjob/checkscriptjob.cpp
new file mode 100644
index 0000000..a79f14d
--- /dev/null
+++ b/src/ksieveui/managescriptsjob/checkscriptjob.cpp
@@ -0,0 +1,107 @@
+/*
+ Copyright (c) 2016 Montel Laurent <montel@kde.org>
+
+ This program is free software; you can redistribute it and/or modify it
+ under the terms of the GNU General Public License, version 2, as
+ published by the Free Software Foundation.
+
+ This program is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License along
+ with this program; if not, write to the Free Software Foundation, Inc.,
+ 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+*/
+
+#include "checkscriptjob.h"
+#include "libksieve_debug.h"
+#include <KLocalizedString>
+
+#include <sievejob.h>
+
+using namespace KSieveUi;
+
+class KSieveUi::CheckScriptJobPrivate
+{
+public:
+ CheckScriptJobPrivate()
+ : mIsActive(false)
+ {
+
+ }
+ bool mIsActive;
+ QString mOriginalScript;
+ QString mCurrentScript;
+ QUrl mUrl;
+};
+
+CheckScriptJob::CheckScriptJob(QObject *parent)
+ : QObject(parent),
+ d(new CheckScriptJobPrivate)
+{
+
+}
+
+CheckScriptJob::~CheckScriptJob()
+{
+ delete d;
+}
+
+void CheckScriptJob::setOriginalScript(const QString &script)
+{
+ d->mOriginalScript = script;
+}
+
+void CheckScriptJob::setCurrentScript(const QString &script)
+{
+ d->mCurrentScript = script;
+}
+
+void CheckScriptJob::setUrl(const QUrl &url)
+{
+ d->mUrl = url;
+}
+
+void CheckScriptJob::setIsActive(bool active)
+{
+ d->mIsActive = active;
+}
+
+void CheckScriptJob::start()
+{
+ if (canStart()) {
+ KManageSieve::SieveJob *job = KManageSieve::SieveJob::put(d->mUrl, d->mCurrentScript, d->mIsActive, d->mIsActive);
+ connect(job, &KManageSieve::SieveJob::result, this, &CheckScriptJob::slotPutCheckSyntaxResult);
+ } else {
+ deleteLater();
+ }
+}
+
+void CheckScriptJob::slotPutCheckSyntaxResult(KManageSieve::SieveJob *job, bool success)
+{
+ if (success) {
+ Q_EMIT finished(i18n("No errors found."), true);
+ } else {
+ const QString errorMsg = job->errorString();
+ Q_EMIT finished(errorMsg.isEmpty() ? i18n("An unknown error was encountered.") : errorMsg, false);
+ }
+ //Put original script after check otherwise we will put a script even if we don't click on ok
+ KManageSieve::SieveJob *restoreJob = KManageSieve::SieveJob::put(d->mUrl, d->mOriginalScript, d->mIsActive, d->mIsActive);
+ connect(restoreJob, &KManageSieve::SieveJob::result, this, &CheckScriptJob::slotRestoreFile);
+}
+
+void CheckScriptJob::slotRestoreFile(KManageSieve::SieveJob *job, bool success)
+{
+ Q_UNUSED(job);
+ if (!success) {
+ qCWarning(LIBKSIEVE_LOG) << "Script can't be restored" << d->mUrl;
+ }
+ deleteLater();
+}
+
+bool CheckScriptJob::canStart() const
+{
+ return d->mUrl.isValid() && !d->mCurrentScript.isEmpty();
+}
diff --git a/src/ksieveui/managescriptsjob/checkscriptjob.h b/src/ksieveui/managescriptsjob/checkscriptjob.h
new file mode 100644
index 0000000..8b1b3d7
--- /dev/null
+++ b/src/ksieveui/managescriptsjob/checkscriptjob.h
@@ -0,0 +1,57 @@
+/*
+ Copyright (c) 2016 Montel Laurent <montel@kde.org>
+
+ This program is free software; you can redistribute it and/or modify it
+ under the terms of the GNU General Public License, version 2, as
+ published by the Free Software Foundation.
+
+ This program is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License along
+ with this program; if not, write to the Free Software Foundation, Inc.,
+ 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+*/
+
+#ifndef CHECKSCRIPTJOB_H
+#define CHECKSCRIPTJOB_H
+
+#include <QObject>
+#include <QUrl>
+#include "ksieveui_export.h"
+namespace KManageSieve {
+class SieveJob;
+}
+namespace KSieveUi
+{
+class CheckScriptJobPrivate;
+class KSIEVEUI_EXPORT CheckScriptJob : public QObject
+{
+ Q_OBJECT
+public:
+ explicit CheckScriptJob(QObject *parent = Q_NULLPTR);
+ ~CheckScriptJob();
+
+ void setOriginalScript(const QString &script);
+ void setCurrentScript(const QString &script);
+ void setUrl(const QUrl &url);
+ void setIsActive(bool active);
+
+ bool canStart() const;
+ void start();
+
+
+Q_SIGNALS:
+ void finished(const QString &msg, bool success);
+
+private Q_SLOTS:
+ void slotPutCheckSyntaxResult(KManageSieve::SieveJob *job, bool success);
+ void slotRestoreFile(KManageSieve::SieveJob *job, bool success);
+
+private:
+ CheckScriptJobPrivate *const d;
+};
+}
+#endif // CHECKSCRIPTJOB_H