KNewStuff

xmlloader.cpp
1/*
2 knewstuff3/xmlloader.cpp.
3 SPDX-FileCopyrightText: 2002 Cornelius Schumacher <schumacher@kde.org>
4 SPDX-FileCopyrightText: 2003-2007 Josef Spillner <spillner@kde.org>
5 SPDX-FileCopyrightText: 2009 Jeremy Whiting <jpwhiting@kde.org>
6 SPDX-FileCopyrightText: 2010 Frederik Gladhorn <gladhorn@kde.org>
7
8 SPDX-License-Identifier: LGPL-2.1-or-later
9*/
10
11#include "xmlloader_p.h"
12
13#include "jobs/httpjob.h"
14#include "knewstuffcore_debug.h"
15
16#include <KConfig>
17
18#include <QByteArray>
19#include <QFile>
20#include <QTimer>
21
22namespace KNSCore
23{
24void handleData(XmlLoader *q, const QByteArray &data)
25{
26 qCDebug(KNEWSTUFFCORE) << "--Xml Loader-START--";
27 qCDebug(KNEWSTUFFCORE) << QString::fromUtf8(data);
28 qCDebug(KNEWSTUFFCORE) << "--Xml Loader-END--";
29 QDomDocument doc;
30 if (doc.setContent(data)) {
31 Q_EMIT q->signalLoaded(doc);
32 } else {
33 Q_EMIT q->signalFailed();
34 }
35}
36
38 : QObject(parent)
39{
40}
41
42void XmlLoader::load(const QUrl &url)
43{
44 qCDebug(KNEWSTUFFCORE) << "XmlLoader::load(): url: " << url;
45 // The load call is expected to be asynchronous (to allow for people to connect to signals
46 // after it is called), and so we need to postpone its implementation until the listeners
47 // are actually listening
48 QTimer::singleShot(0, this, [this, url]() {
50 static const QStringList remoteSchemeOptions{QLatin1String{"http"}, QLatin1String{"https"}, QLatin1String{"ftp"}};
51 if (remoteSchemeOptions.contains(url.scheme())) {
52 HTTPJob *job = HTTPJob::get(url, Reload, JobFlag::HideProgressInfo);
53 connect(job, &KJob::result, this, &XmlLoader::slotJobResult);
54 connect(job, &HTTPJob::data, this, &XmlLoader::slotJobData);
55 connect(job, &HTTPJob::httpError, this, &XmlLoader::signalHttpError);
56 Q_EMIT jobStarted(job);
57 } else if (url.isLocalFile()) {
58 QFile localProvider(url.toLocalFile());
59 if (localProvider.open(QFile::ReadOnly)) {
60 m_jobdata = localProvider.readAll();
61 handleData(this, m_jobdata);
62 } else {
63 Q_EMIT signalFailed();
64 }
65 } else {
66 // This is not supported
67 qCDebug(KNEWSTUFFCORE) << "Attempted to load data from unsupported URL:" << url;
68 Q_EMIT signalFailed();
69 }
70 });
71}
72
73void XmlLoader::slotJobData(KJob *, const QByteArray &data)
74{
75 m_jobdata.append(data);
76}
77
79{
80 deleteLater();
81 if (job->error()) {
82 Q_EMIT signalFailed();
83 } else {
84 handleData(this, m_jobdata);
85 }
86}
87
88QDomElement addElement(QDomDocument &doc, QDomElement &parent, const QString &tag, const QString &value)
89{
90 QDomElement n = doc.createElement(tag);
91 n.appendChild(doc.createTextNode(value));
92 parent.appendChild(n);
93
94 return n;
95}
96} // end KNS namespace
97
98#include "moc_xmlloader_p.cpp"
int error() const
QDomElement createElement(const QString &tagName)
QDomText createTextNode(const QString &value)
ParseResult setContent(QAnyStringView text, ParseOptions options)
QDomNode appendChild(const QDomNode &newChild)
T * data() const const
QString fromUtf8(QByteArrayView str)
bool isLocalFile() const const
QString scheme() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri May 24 2024 11:54:38 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.