KPublicTransport

backend.cpp
1 /*
2  SPDX-FileCopyrightText: 2019 Volker Krause <[email protected]>
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 "coveragearea.h"
11 #include "datatypes_p.h"
12 #include "json_p.h"
13 
14 #include <QJsonObject>
15 
16 using namespace KPublicTransport;
17 
18 KPUBLICTRANSPORT_MAKE_GADGET(Backend)
19 
20 QString Backend::identifier() const
21 {
22  return d->m_backendImpl ? d->m_backendImpl->backendId() : QString();
23 }
24 
25 QString Backend::name() const
26 {
27  return d->name;
28 }
29 
30 QString Backend::description() const
31 {
32  return d->description;
33 }
34 
35 bool Backend::isSecure() const
36 {
37  return d->m_backendImpl && d->m_backendImpl->hasCapability(AbstractBackend::Secure);
38 }
39 
40 CoverageArea Backend::coverageArea(CoverageArea::Type coverageType) const
41 {
42  return d->coverage[coverageType];
43 }
44 
45 const AbstractBackend* BackendPrivate::impl(const Backend &b)
46 {
47  return b.d->m_backendImpl.get();
48 }
49 
50 void BackendPrivate::setImpl(Backend &b, std::unique_ptr<AbstractBackend> &&impl)
51 {
52  b.d->m_backendImpl = std::move(impl);
53 }
54 
55 struct {
56  const char *name;
57  CoverageArea::Type type;
58 } static constexpr const coverage_area_map[] = {
59  { "realtimeCoverage", CoverageArea::Realtime },
60  { "regularCoverage", CoverageArea::Regular },
61  { "anyCoverage", CoverageArea::Any },
62 };
63 
64 Backend 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  const auto coverage = obj.value(QLatin1String("coverage")).toObject();
72  for (const auto &c : coverage_area_map) {
73  const auto covObj = coverage.value(QLatin1String(c.name)).toObject();
74  if (covObj.empty()) {
75  continue;
76  }
77  b.d->coverage[c.type] = CoverageArea::fromJson(covObj);
78  b.d->coverage[c.type].setType(c.type);
79  }
80 
81  return b;
82 }
83 
84 #include "moc_backend.cpp"
Describes the area a specific KPublicTransport::Backend can provide information for.
Definition: coveragearea.h:22
Information about a backend service queried for location/departure/journey data.
Definition: backend.h:21
Query operations and data types for accessing realtime public transport information from online servi...
Definition: attribution.cpp:16
QString name
Short, humand readable name of the backend.
Definition: backend.h:29
QString description
Humand readable description of this backend.
Definition: backend.h:31
static CoverageArea fromJson(const QJsonObject &obj)
Read a single coverage area information from a JSON object in Transport API Repository format...
QJsonObject toObject() const const
Type
Coverage quality as defined by the Transport API Repository format.
Definition: coveragearea.h:27
QJsonValue value(const QString &key) const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sat Oct 23 2021 23:05:20 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.