KPublicTransport

backend.cpp
1/*
2 SPDX-FileCopyrightText: 2019 Volker Krause <vkrause@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "backend.h"
8#include "backend_p.h"
9#include "backends/abstractbackend.h"
10#include "datatypes_p.h"
11#include "json_p.h"
12
13#include <QJsonObject>
14
15using namespace Qt::Literals::StringLiterals;
16using namespace KPublicTransport;
17
18KPUBLICTRANSPORT_MAKE_GADGET(Backend)
19
21{
22 return d->m_backendImpl ? d->m_backendImpl->backendId() : QString();
23}
24
26{
27 return d->name;
28}
29
31{
32 return d->description;
33}
34
35bool Backend::isSecure() const
36{
37 return d->m_backendImpl && d->m_backendImpl->hasCapability(AbstractBackend::Secure);
38}
39
40CoverageArea Backend::coverageArea(CoverageArea::Type coverageType) const
41{
42 return d->coverage[coverageType];
43}
44
45const AbstractBackend* BackendPrivate::impl(const Backend &b)
46{
47 return b.d->m_backendImpl.get();
48}
49
50void BackendPrivate::setImpl(Backend &b, std::unique_ptr<AbstractBackend> &&impl)
51{
52 b.d->m_backendImpl = std::move(impl);
53}
54
55struct {
56 const char *name;
58} static constexpr const coverage_area_map[] = {
59 { "realtimeCoverage", CoverageArea::Realtime },
60 { "regularCoverage", CoverageArea::Regular },
61 { "anyCoverage", CoverageArea::Any },
62};
63
64Backend BackendPrivate::fromJson(const QJsonObject &obj)
65{
66 Backend b;
67 const auto jsonMetaData = obj.value(QLatin1String("KPlugin")).toObject();
68 b.d->name = Json::translatedValue(jsonMetaData, QStringLiteral("Name"));
69 b.d->description = Json::translatedValue(jsonMetaData, QStringLiteral("Description"));
70
71 // untranslated files for development purposes only
72 if (b.d->name.isEmpty() && b.d->description.isEmpty()) [[unlikely]] {
73 b.d->name = obj.value("name"_L1).toString();
74 b.d->description = obj.value("description"_L1).toString();
75 }
76
77 const auto coverage = obj.value(QLatin1String("coverage")).toObject();
78 for (const auto &c : coverage_area_map) {
79 const auto covObj = coverage.value(QLatin1String(c.name)).toObject();
80 if (covObj.empty()) {
81 continue;
82 }
83 b.d->coverage[c.type] = CoverageArea::fromJson(covObj);
84 b.d->coverage[c.type].setType(c.type);
85 }
86
87 return b;
88}
89
90#include "moc_backend.cpp"
Information about a backend service queried for location/departure/journey data.
Definition backend.h:22
QString description
Humand readable description of this backend.
Definition backend.h:31
QString name
Short, humand readable name of the backend.
Definition backend.h:29
bool isSecure
Supports secrure network access.
Definition backend.h:35
QString identifier
Internal identifier of this backend.
Definition backend.h:27
Describes the area a specific KPublicTransport::Backend can provide information for.
Type
Coverage quality as defined by the Transport API Repository format.
static CoverageArea fromJson(const QJsonObject &obj)
Read a single coverage area information from a JSON object in Transport API Repository format.
Query operations and data types for accessing realtime public transport information from online servi...
QJsonValue value(QLatin1StringView key) const const
QJsonObject toObject() const const
QString toString() const const
bool isEmpty() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 22 2024 12:10:39 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.