Libksieve

parseuserscriptjob.cpp
1 /*
2  Copyright (C) 2013-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 "parseuserscriptjob.h"
19 #include "ksieveui/scriptsparsing/parsingutil.h"
20 #include <kmanagesieve/sievejob.h>
21 #include <QXmlStreamReader>
22 #include <KLocalizedString>
23 
24 using namespace KSieveUi;
25 ParseUserScriptJob::ParseUserScriptJob(const QUrl &url, QObject *parent)
26  : QObject(parent)
27  , mCurrentUrl(url)
28 {
29 }
30 
31 ParseUserScriptJob::~ParseUserScriptJob()
32 {
33  kill();
34  delete mStreamReader;
35 }
36 
37 void ParseUserScriptJob::kill()
38 {
39  if (mSieveJob) {
40  mSieveJob->kill();
41  }
42  mSieveJob = nullptr;
43 }
44 
45 QUrl ParseUserScriptJob::scriptUrl() const
46 {
47  return mCurrentUrl;
48 }
49 
50 void ParseUserScriptJob::start()
51 {
52  if (mCurrentUrl.isEmpty()) {
53  emitError(i18n("Path is not specified."));
54  return;
55  }
56  if (mSieveJob) {
57  mSieveJob->kill();
58  }
59  mActiveScripts = QStringList();
60  mError = QString();
61  mSieveJob = KManageSieve::SieveJob::get(mCurrentUrl);
62  connect(mSieveJob, &KManageSieve::SieveJob::result, this, &ParseUserScriptJob::slotGetResult);
63 }
64 
65 void ParseUserScriptJob::setAutoDelete(bool deleteMe)
66 {
67  mAutoDelete = deleteMe;
68 }
69 
70 void ParseUserScriptJob::slotGetResult(KManageSieve::SieveJob *job, bool success, const QString &script, bool)
71 {
72  mSieveJob = nullptr;
73  if (!success) {
74  emitError(i18n("Retrieving the script failed.\n"
75  "The server responded:\n%1", job->errorString()));
76  return;
77  }
78  if (script.isEmpty()) {
79  emitError(i18n("Script is empty. (%1)", mCurrentUrl.fileName()));
80  return;
81  }
82  bool result;
83  const QStringList lst = parsescript(script, result);
84  if (result) {
85  emitSuccess(lst);
86  } else {
87  emitError(i18n("Script parsing error."));
88  }
89 }
90 
91 void ParseUserScriptJob::emitError(const QString &msgError)
92 {
93  mError = msgError;
94  Q_EMIT finished(this);
95  if (mAutoDelete) {
96  deleteLater();
97  }
98 }
99 
100 void ParseUserScriptJob::emitSuccess(const QStringList &activeScriptList)
101 {
102  mActiveScripts = activeScriptList;
103  Q_EMIT finished(this);
104  if (mAutoDelete) {
105  deleteLater();
106  }
107 }
108 
109 QStringList ParseUserScriptJob::parsescript(const QString &script, bool &result)
110 {
111  QStringList lst;
112  const QString doc = ParsingUtil::parseScript(script, result);
113  if (result) {
114  lst = extractActiveScript(doc);
115  }
116  return lst;
117 }
118 
119 QStringList ParseUserScriptJob::activeScriptList() const
120 {
121  return mActiveScripts;
122 }
123 
124 QString ParseUserScriptJob::error() const
125 {
126  return mError;
127 }
128 
129 QStringList ParseUserScriptJob::extractActiveScript(const QString &doc)
130 {
131  mStreamReader = new QXmlStreamReader(doc);
132  QStringList lstScript;
133  if (mStreamReader->readNextStartElement()) {
134  while (mStreamReader->readNextStartElement()) {
135  const QStringRef tagname = mStreamReader->name();
136  if (tagname == QLatin1String("action")) {
137  if (mStreamReader->attributes().hasAttribute(QLatin1String("name"))) {
138  const QString actionName = mStreamReader->attributes().value(QLatin1String("name")).toString();
139  if (actionName == QLatin1String("include")) {
140  //Load includes
141  const QString str = loadInclude();
142  if (!str.isEmpty()) {
143  if (!lstScript.contains(str)) {
144  lstScript.append(str);
145  }
146  }
147  } else {
148  mStreamReader->skipCurrentElement();
149  }
150  }
151  } else {
152  mStreamReader->skipCurrentElement();
153  }
154  }
155  }
156  return lstScript;
157 }
158 
159 QString ParseUserScriptJob::loadInclude()
160 {
161  QString scriptName;
162  while (mStreamReader->readNextStartElement()) {
163  if (mStreamReader->name() == QLatin1String("str")) {
164  scriptName = mStreamReader->readElementText();
165  } else {
166  mStreamReader->skipCurrentElement();
167  }
168  }
169  return scriptName;
170 }
bool contains(const QString &str, Qt::CaseSensitivity cs) const const
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.
void append(const T &value)
bool isEmpty() const const
QString errorString() const
A human-readable error message.
Definition: sievejob.cpp:278
QString i18n(const char *text, const TYPE &arg...)
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 Mon Jul 13 2020 23:08:34 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.