File indexing completed on 2024-12-08 10:16:06
0001 /* 0002 SPDX-FileCopyrightText: 2020 Volker Krause <vkrause@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #include "wikidataquerymanager.h" 0008 #include "wikidataquery.h" 0009 0010 #include <QNetworkAccessManager> 0011 #include <QNetworkDiskCache> 0012 #include <QNetworkReply> 0013 #include <QStandardPaths> 0014 0015 WikidataQueryManager::WikidataQueryManager(QObject *parent) 0016 : QObject(parent) 0017 { 0018 m_nam = new QNetworkAccessManager(this); 0019 m_nam->setRedirectPolicy(QNetworkRequest::NoLessSafeRedirectPolicy); 0020 m_nam->setStrictTransportSecurityEnabled(true); 0021 m_nam->enableStrictTransportSecurityStore(true, QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation) + QLatin1String("/org.kde.osm/hsts/")); 0022 0023 auto diskCache = new QNetworkDiskCache; 0024 diskCache->setCacheDirectory(QStandardPaths::writableLocation(QStandardPaths::GenericCacheLocation) + QLatin1String("/org.kde.wikidata/http-cache/")); 0025 diskCache->setMaximumCacheSize(1'000'000'000); // 1GB 0026 m_nam->setCache(diskCache); 0027 } 0028 0029 WikidataQueryManager::~WikidataQueryManager() = default; 0030 0031 void WikidataQueryManager::execute(WikidataQuery *query) 0032 { 0033 executeNextSubQuery(query); 0034 } 0035 0036 void WikidataQueryManager::executeNextSubQuery(WikidataQuery *query) 0037 { 0038 auto req = query->nextRequest(); 0039 // see https://www.mediawiki.org/wiki/API:Etiquette 0040 req.setHeader(QNetworkRequest::UserAgentHeader, QStringLiteral("KPublicTransport/KnowledgeDBGenerator (kde-pim@kde.org)")); 0041 const auto reply = m_nam->get(req); 0042 connect(reply, &QNetworkReply::finished, this, [query, reply, this]() { subQueryFinished(query, reply); }); 0043 } 0044 0045 void WikidataQueryManager::subQueryFinished(WikidataQuery *query, QNetworkReply *reply) 0046 { 0047 reply->deleteLater(); 0048 0049 if (reply->error() != QNetworkReply::NoError) { 0050 qWarning() << reply->errorString(); 0051 query->m_error = WikidataQuery::NetworkError; 0052 Q_EMIT query->finished(); 0053 return; 0054 } 0055 0056 if (query->processReply(reply)) { 0057 return; 0058 } 0059 executeNextSubQuery(query); 0060 }