KOSMIndoorMap

wikidataquerymanager.cpp
1/*
2 SPDX-FileCopyrightText: 2020 Volker Krause <vkrause@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "wikidataquerymanager.h"
8#include "wikidataquery.h"
9#include "logging.h"
10
11#include <QCoreApplication>
12#include <QNetworkAccessManager>
13#include <QNetworkDiskCache>
14#include <QNetworkReply>
15#include <QStandardPaths>
16
17using namespace Qt::Literals::StringLiterals;
18using namespace Wikidata;
19
20QueryManager::QueryManager(QObject *parent)
21 : QObject(parent)
22{
23}
24
25QueryManager::~QueryManager() = default;
26
28{
29 m_email = email;
30}
31
33{
34 executeNextSubQuery(query);
35}
36
37void QueryManager::executeNextSubQuery(Query *query)
38{
39 if (m_email.isEmpty()) {
40 qFatal("User-Agent email address not set!");
41 }
42
43 auto req = query->nextRequest();
44 // see https://www.mediawiki.org/wiki/API:Etiquette
46 const auto reply = nam()->get(req);
47 reply->setParent(query);
48 connect(reply, &QNetworkReply::finished, this, [query, reply, this]() { subQueryFinished(query, reply); });
49}
50
51void QueryManager::subQueryFinished(Query *query, QNetworkReply *reply)
52{
53 reply->deleteLater();
54
55 if (reply->error() != QNetworkReply::NoError) {
56 qCWarning(Log) << reply->errorString();
57 query->m_error = Query::NetworkError;
58 Q_EMIT query->finished();
59 return;
60 }
61
62 if (query->processReply(reply)) {
63 return;
64 }
65 executeNextSubQuery(query);
66}
67
68QNetworkAccessManager* QueryManager::nam()
69{
70 if (!m_nam) {
71 m_nam = new QNetworkAccessManager(this);
75
76 auto diskCache = new QNetworkDiskCache;
78#ifndef Q_OS_ANDROID
79 diskCache->setMaximumCacheSize(1'000'000'000); // 1GB, when it's actually a user-wide shared cache
80#endif
81 m_nam->setCache(diskCache);
82 }
83
84 return m_nam;
85}
86
87#include "moc_wikidataquerymanager.cpp"
void setUserAgentEmailAddress(const QString &email)
Set the email address used in the User-Agent header.
void execute(Query *query)
Execute query.
Base class for Wikidata queries.
std::optional< QSqlQuery > query(const QString &queryStatement)
QString errorString() const const
void enableStrictTransportSecurityStore(bool enabled, const QString &storeDir)
QNetworkReply * get(const QNetworkRequest &request)
void setCache(QAbstractNetworkCache *cache)
void setRedirectPolicy(QNetworkRequest::RedirectPolicy policy)
void setStrictTransportSecurityEnabled(bool enabled)
void setCacheDirectory(const QString &cacheDir)
NetworkError error() const const
Q_EMITQ_EMIT
QMetaObject::Connection connect(const QObject *sender, PointerToMemberFunction signal, Functor functor)
void deleteLater()
void setParent(QObject *parent)
QString writableLocation(StandardLocation type)
bool isEmpty() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:06:15 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.