File indexing completed on 2024-03-24 03:59:14
0001 /* 0002 knewstuff3/xmlloader.cpp. 0003 SPDX-FileCopyrightText: 2002 Cornelius Schumacher <schumacher@kde.org> 0004 SPDX-FileCopyrightText: 2003-2007 Josef Spillner <spillner@kde.org> 0005 SPDX-FileCopyrightText: 2009 Jeremy Whiting <jpwhiting@kde.org> 0006 SPDX-FileCopyrightText: 2010 Frederik Gladhorn <gladhorn@kde.org> 0007 0008 SPDX-License-Identifier: LGPL-2.1-or-later 0009 */ 0010 0011 #include "xmlloader_p.h" 0012 0013 #include "jobs/httpjob.h" 0014 #include "knewstuffcore_debug.h" 0015 0016 #include <KConfig> 0017 0018 #include <QByteArray> 0019 #include <QFile> 0020 #include <QTimer> 0021 0022 namespace KNSCore 0023 { 0024 void handleData(XmlLoader *q, const QByteArray &data) 0025 { 0026 qCDebug(KNEWSTUFFCORE) << "--Xml Loader-START--"; 0027 qCDebug(KNEWSTUFFCORE) << QString::fromUtf8(data); 0028 qCDebug(KNEWSTUFFCORE) << "--Xml Loader-END--"; 0029 QDomDocument doc; 0030 if (doc.setContent(data)) { 0031 Q_EMIT q->signalLoaded(doc); 0032 } else { 0033 Q_EMIT q->signalFailed(); 0034 } 0035 } 0036 0037 XmlLoader::XmlLoader(QObject *parent) 0038 : QObject(parent) 0039 { 0040 } 0041 0042 void XmlLoader::load(const QUrl &url) 0043 { 0044 qCDebug(KNEWSTUFFCORE) << "XmlLoader::load(): url: " << url; 0045 // The load call is expected to be asynchronous (to allow for people to connect to signals 0046 // after it is called), and so we need to postpone its implementation until the listeners 0047 // are actually listening 0048 QTimer::singleShot(0, this, [this, url]() { 0049 m_jobdata.clear(); 0050 static const QStringList remoteSchemeOptions{QLatin1String{"http"}, QLatin1String{"https"}, QLatin1String{"ftp"}}; 0051 if (remoteSchemeOptions.contains(url.scheme())) { 0052 HTTPJob *job = HTTPJob::get(url, Reload, JobFlag::HideProgressInfo); 0053 connect(job, &KJob::result, this, &XmlLoader::slotJobResult); 0054 connect(job, &HTTPJob::data, this, &XmlLoader::slotJobData); 0055 connect(job, &HTTPJob::httpError, this, &XmlLoader::signalHttpError); 0056 Q_EMIT jobStarted(job); 0057 } else if (url.isLocalFile()) { 0058 QFile localProvider(url.toLocalFile()); 0059 if (localProvider.open(QFile::ReadOnly)) { 0060 m_jobdata = localProvider.readAll(); 0061 handleData(this, m_jobdata); 0062 } else { 0063 Q_EMIT signalFailed(); 0064 } 0065 } else { 0066 // This is not supported 0067 qCDebug(KNEWSTUFFCORE) << "Attempted to load data from unsupported URL:" << url; 0068 Q_EMIT signalFailed(); 0069 } 0070 }); 0071 } 0072 0073 void XmlLoader::slotJobData(KJob *, const QByteArray &data) 0074 { 0075 m_jobdata.append(data); 0076 } 0077 0078 void XmlLoader::slotJobResult(KJob *job) 0079 { 0080 deleteLater(); 0081 if (job->error()) { 0082 Q_EMIT signalFailed(); 0083 } else { 0084 handleData(this, m_jobdata); 0085 } 0086 } 0087 0088 QDomElement addElement(QDomDocument &doc, QDomElement &parent, const QString &tag, const QString &value) 0089 { 0090 QDomElement n = doc.createElement(tag); 0091 n.appendChild(doc.createTextNode(value)); 0092 parent.appendChild(n); 0093 0094 return n; 0095 } 0096 } // end KNS namespace 0097 0098 #include "moc_xmlloader_p.cpp"