KPublicTransport

restonboardbackend.cpp
1/*
2 SPDX-FileCopyrightText: 2022 Volker Krause <vkrause@kde.org>
3 SPDX-License-Identifier: LGPL-2.0-or-later
4*/
5
6#include "restonboardbackend_p.h"
7#include "logging.h"
8#include "positiondata_p.h"
9
10#include "../../lib/json/jsonp_p.h"
11
12#include <KPublicTransport/Journey>
13
14#include <QFile>
15#include <QFileInfo>
16#include <QJsonArray>
17#include <QJsonDocument>
18#include <QJsonObject>
19#include <QNetworkAccessManager>
20#include <QNetworkReply>
21
22using namespace KPublicTransport;
23
24RestOnboardBackend::RestOnboardBackend(QObject *parent)
25 : AbstractOnboardBackend(parent)
26{
27}
28
29RestOnboardBackend::~RestOnboardBackend() = default;
30
31static QString fakeResponseFile(QLatin1String key)
32{
33 QFile configFile(qEnvironmentVariable("KPUBLICTRANSPORT_ONBOARD_FAKE_CONFIG"));
34 if (!configFile.open(QFile::ReadOnly)) {
35 qCWarning(Log) << configFile.errorString() << configFile.fileName();
36 return {};
37 }
38
39 const auto path = QJsonDocument::fromJson(configFile.readAll()).object().value(key).toString();
40 if (QFileInfo(path).isAbsolute()) {
41 return path;
42 }
43
44 return QFileInfo(configFile.fileName()).absolutePath() + QLatin1Char('/') + path;
45}
46
47static QJsonObject fakeResponse(QLatin1String key)
48{
49 QFile f(fakeResponseFile(key));
50 if (!f.open(QFile::ReadOnly)) {
51 qCWarning(Log) << f.errorString() << f.fileName();
52 return {};
53 }
54
55 const auto doc = QJsonDocument::fromJson(JsonP::decode(f.readAll()));
56 return doc.object();
57}
58
59void RestOnboardBackend::requestPosition(QNetworkAccessManager *nam)
60{
61 if (Q_UNLIKELY(qEnvironmentVariableIsSet("KPUBLICTRANSPORT_ONBOARD_FAKE_CONFIG"))) {
62 Q_EMIT positionReceived(parsePositionData(fakeResponse(QLatin1String("positionResponse"))));
63 return;
64 }
65
66 if (!supportsPosition()) {
67 return;
68 }
69
70 auto reply = nam->get(createPositionRequest());
71 connect(reply, &QNetworkReply::finished, this, [this, reply]() {
72 reply->deleteLater();
73 if (reply->error() != QNetworkReply::NoError) {
74 qCWarning(Log) << reply->url() << reply->errorString();
75 Q_EMIT positionReceived({});
76 return;
77 }
78
79 const auto doc = QJsonDocument::fromJson(JsonP::decode(reply->readAll()));
80 if (doc.isArray()) {
81 Q_EMIT positionReceived(parsePositionData(doc.array()));
82 } else {
83 Q_EMIT positionReceived(parsePositionData(doc.object()));
84 }
85 });
86}
87
88void RestOnboardBackend::requestJourney(QNetworkAccessManager *nam)
89{
90 if (Q_UNLIKELY(qEnvironmentVariableIsSet("KPUBLICTRANSPORT_ONBOARD_FAKE_CONFIG"))) {
91 Q_EMIT journeyReceived(parseJourneyData(fakeResponse(QLatin1String("journeyResponse"))));
92 return;
93 }
94
95 if (!supportsJourney()) {
96 return;
97 }
98
99 auto reply = nam->get(createJourneyRequest());
100 connect(reply, &QNetworkReply::finished, this, [this, reply]() {
101 reply->deleteLater();
102 if (reply->error() != QNetworkReply::NoError) {
103 qCWarning(Log) << reply->url() << reply->errorString();
104 Q_EMIT journeyReceived({});
105 return;
106 }
107
108 const auto doc = QJsonDocument::fromJson(reply->readAll());
109 if (doc.isArray()) {
110 Q_EMIT journeyReceived(parseJourneyData(doc.array()));
111 } else {
112 Q_EMIT journeyReceived(parseJourneyData(doc.object()));
113 }
114 });
115}
QString path(const QString &relativePath)
Query operations and data types for accessing realtime public transport information from online servi...
QString absolutePath() const const
QJsonDocument fromJson(const QByteArray &json, QJsonParseError *error)
QJsonObject object() const const
QJsonValue value(QLatin1StringView key) const const
QString toString() const const
QNetworkReply * get(const QNetworkRequest &request)
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 18 2024 12:07:52 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.