Libksieve

parseuserscriptjob.cpp
1/*
2 SPDX-FileCopyrightText: 2013-2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: GPL-2.0-only
5*/
6
7#include "parseuserscriptjob.h"
8#include "kmanagesieve/sievejob.h"
9#include "ksievecore/scriptsparsing/parsingutil.h"
10#include <KLocalizedString>
11#include <QXmlStreamReader>
12
13using namespace KSieveCore;
14ParseUserScriptJob::ParseUserScriptJob(const QUrl &url, QObject *parent)
15 : QObject(parent)
16 , mCurrentUrl(url)
17{
18}
19
20ParseUserScriptJob::~ParseUserScriptJob()
21{
22 kill();
23 delete mStreamReader;
24}
25
26void ParseUserScriptJob::kill()
27{
28 if (mSieveJob) {
29 mSieveJob->kill();
30 }
31 mSieveJob = nullptr;
32}
33
34QUrl ParseUserScriptJob::scriptUrl() const
35{
36 return mCurrentUrl;
37}
38
39void ParseUserScriptJob::start()
40{
41 if (mCurrentUrl.isEmpty()) {
42 emitError(i18n("Path is not specified."));
43 return;
44 }
45 if (mSieveJob) {
46 mSieveJob->kill();
47 }
48 mActiveScripts = QStringList();
49 mError = QString();
50 mSieveJob = KManageSieve::SieveJob::get(mCurrentUrl);
51 connect(mSieveJob, &KManageSieve::SieveJob::result, this, &ParseUserScriptJob::slotGetResult);
52}
53
54void ParseUserScriptJob::setAutoDelete(bool deleteMe)
55{
56 mAutoDelete = deleteMe;
57}
58
59void ParseUserScriptJob::slotGetResult(KManageSieve::SieveJob *job, bool success, const QString &script, bool)
60{
61 mSieveJob = nullptr;
62 if (!success) {
63 emitError(
64 i18n("Retrieving the script failed.\n"
65 "The server responded:\n%1",
66 job->errorString()));
67 return;
68 }
69 if (script.isEmpty()) {
70 emitError(i18n("Script is empty. (%1)", mCurrentUrl.fileName()));
71 return;
72 }
73 bool result;
74 const QStringList lst = parsescript(script, result);
75 if (result) {
76 emitSuccess(lst);
77 } else {
78 emitError(i18n("Script parsing error."));
79 }
80}
81
82void ParseUserScriptJob::emitError(const QString &msgError)
83{
84 mError = msgError;
85 Q_EMIT finished(this);
86 if (mAutoDelete) {
88 }
89}
90
91void ParseUserScriptJob::emitSuccess(const QStringList &activeScriptList)
92{
93 mActiveScripts = activeScriptList;
94 Q_EMIT finished(this);
95 if (mAutoDelete) {
97 }
98}
99
100QStringList ParseUserScriptJob::parsescript(const QString &script, bool &result)
101{
102 QStringList lst;
103 const QString doc = ParsingUtil::parseScript(script, result);
104 if (result) {
105 lst = extractActiveScript(doc);
106 }
107 return lst;
108}
109
110QStringList ParseUserScriptJob::activeScriptList() const
111{
112 return mActiveScripts;
113}
114
115QString ParseUserScriptJob::error() const
116{
117 return mError;
118}
119
120QStringList ParseUserScriptJob::extractActiveScript(const QString &doc)
121{
122 mStreamReader = new QXmlStreamReader(doc);
123 QStringList lstScript;
124 if (mStreamReader->readNextStartElement()) {
125 while (mStreamReader->readNextStartElement()) {
126 const QStringView tagname = mStreamReader->name();
127 if (tagname == QLatin1StringView("action")) {
128 if (mStreamReader->attributes().hasAttribute(QLatin1StringView("name"))) {
129 const QString actionName = mStreamReader->attributes().value(QLatin1StringView("name")).toString();
130 if (actionName == QLatin1StringView("include")) {
131 // Load includes
132 const QString str = loadInclude();
133 if (!str.isEmpty()) {
134 if (!lstScript.contains(str)) {
135 lstScript.append(str);
136 }
137 }
138 } else {
139 mStreamReader->skipCurrentElement();
140 }
141 }
142 } else {
143 mStreamReader->skipCurrentElement();
144 }
145 }
146 }
147 return lstScript;
148}
149
150QString ParseUserScriptJob::loadInclude()
151{
152 QString scriptName;
153 while (mStreamReader->readNextStartElement()) {
154 if (mStreamReader->name() == QLatin1StringView("str")) {
155 scriptName = mStreamReader->readElementText();
156 } else {
157 mStreamReader->skipCurrentElement();
158 }
159 }
160 return scriptName;
161}
162
163#include "moc_parseuserscriptjob.cpp"
A job to manage sieve scripts.
Definition sievejob.h:31
void kill(KJob::KillVerbosity verbosity=KJob::Quietly)
Kills the sieve job.
Definition sievejob.cpp:246
QString errorString() const
A human-readable error message.
Definition sievejob.cpp:268
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 * get(const QUrl &source)
Gets a sieve script from an IMAP server.
Definition sievejob.cpp:300
QString i18n(const char *text, const TYPE &arg...)
void append(QList< T > &&value)
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
void deleteLater()
bool isEmpty() const const
bool contains(QLatin1StringView str, Qt::CaseSensitivity cs) const const
QString toString() const const
QString fileName(ComponentFormattingOptions options) const const
bool isEmpty() const const
bool hasAttribute(QAnyStringView namespaceUri, QAnyStringView name) const const
QStringView value(QAnyStringView namespaceUri, QAnyStringView name) const const
QXmlStreamAttributes attributes() const const
QStringView name() const const
QString readElementText(ReadElementTextBehaviour behaviour)
bool readNextStartElement()
void skipCurrentElement()
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:14:30 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.