| 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 | |
| 22 | namespace KNSCore |
| 23 | { |
| 24 | void handleData(XmlLoader *q, const QByteArray &data) |
| 25 | { |
| 26 | qCDebug(KNEWSTUFFCORE) << "--Xml Loader-START--" ; |
| 27 | qCDebug(KNEWSTUFFCORE) << QString::fromUtf8(ba: 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 | |
| 37 | XmlLoader::XmlLoader(QObject *parent) |
| 38 | : QObject(parent) |
| 39 | { |
| 40 | } |
| 41 | |
| 42 | void 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(interval: 0, receiver: this, slot: [this, url]() { |
| 49 | m_jobdata.clear(); |
| 50 | static const QStringList remoteSchemeOptions{QLatin1String{"http" }, QLatin1String{"https" }, QLatin1String{"ftp" }}; |
| 51 | if (remoteSchemeOptions.contains(str: url.scheme())) { |
| 52 | HTTPJob *job = HTTPJob::get(source: url, loadType: Reload, flags: JobFlag::HideProgressInfo, parent: this); |
| 53 | connect(sender: job, signal: &KJob::result, context: this, slot: &XmlLoader::slotJobResult); |
| 54 | connect(sender: job, signal: &HTTPJob::data, context: this, slot: &XmlLoader::slotJobData); |
| 55 | connect(sender: job, signal: &HTTPJob::httpError, context: this, slot: &XmlLoader::signalHttpError); |
| 56 | Q_EMIT jobStarted(job); |
| 57 | } else if (url.isLocalFile()) { |
| 58 | QFile localProvider(url.toLocalFile()); |
| 59 | if (localProvider.open(flags: QFile::ReadOnly)) { |
| 60 | m_jobdata = localProvider.readAll(); |
| 61 | handleData(q: this, data: 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 | |
| 73 | void XmlLoader::slotJobData(KJob *, const QByteArray &data) |
| 74 | { |
| 75 | m_jobdata.append(a: data); |
| 76 | } |
| 77 | |
| 78 | void XmlLoader::slotJobResult(KJob *job) |
| 79 | { |
| 80 | deleteLater(); |
| 81 | if (job->error()) { |
| 82 | Q_EMIT signalFailed(); |
| 83 | } else { |
| 84 | handleData(q: this, data: m_jobdata); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | QDomElement addElement(QDomDocument &doc, QDomElement &parent, const QString &tag, const QString &value) |
| 89 | { |
| 90 | QDomElement n = doc.createElement(tagName: tag); |
| 91 | n.appendChild(newChild: doc.createTextNode(data: value)); |
| 92 | parent.appendChild(newChild: n); |
| 93 | |
| 94 | return n; |
| 95 | } |
| 96 | } // end KNS namespace |
| 97 | |
| 98 | #include "moc_xmlloader_p.cpp" |
| 99 | |