Commit 90c236d7 authored by Volker Krause's avatar Volker Krause
Browse files

Remove more of the AbstractBackend/Backend duplication

This is mostly historic, due to Backend being retro-fitted on the existing
internal-only backend classes. Backend is now the central instance and
holds the implementation details internally, avoiding the double storage
in Manager. This also further prepares the way towards the new coverage
area model.
parent a6683d70
......@@ -6,6 +6,7 @@
#include "backend.h"
#include "backend_p.h"
#include "backends/abstractbackend.h"
#include "datatypes_p.h"
#include "json_p.h"
......@@ -14,10 +15,26 @@
using namespace KPublicTransport;
KPUBLICTRANSPORT_MAKE_GADGET(Backend)
KPUBLICTRANSPORT_MAKE_PROPERTY(Backend, QString, identifier, setIdentifier)
KPUBLICTRANSPORT_MAKE_PROPERTY(Backend, QString, name, setName)
KPUBLICTRANSPORT_MAKE_PROPERTY(Backend, QString, description, setDescription)
KPUBLICTRANSPORT_MAKE_PROPERTY(Backend, bool, isSecure, setIsSecure)
QString Backend::identifier() const
{
return d->m_backendImpl ? d->m_backendImpl->backendId() : QString();
}
QString Backend::name() const
{
return d->name;
}
QString Backend::description() const
{
return d->description;
}
bool Backend::isSecure() const
{
return d->m_backendImpl && d->m_backendImpl->capabilities() & AbstractBackend::Secure;
}
QString Backend::primaryCountryCode() const
{
......@@ -27,10 +44,19 @@ QString Backend::primaryCountryCode() const
return {};
}
Backend BackendPrivate::fromJson(const QJsonObject &obj, const QString &backendId)
const AbstractBackend* BackendPrivate::impl(const Backend &b)
{
return b.d->m_backendImpl.get();
}
void BackendPrivate::setImpl(Backend &b, std::unique_ptr<AbstractBackend> &&impl)
{
b.d->m_backendImpl = std::move(impl);
}
Backend BackendPrivate::fromJson(const QJsonObject &obj)
{
Backend b;
b.d->identifier = backendId;
const auto jsonMetaData = obj.value(QLatin1String("KPlugin")).toObject();
b.d->name = Json::translatedValue(jsonMetaData, QStringLiteral("Name"));
b.d->description = Json::translatedValue(jsonMetaData, QStringLiteral("Description"));
......
......@@ -23,15 +23,15 @@ class KPUBLICTRANSPORT_EXPORT Backend
/** Internal identifier of this backend.
* Not intended for display, use for storing per-backend settings for example.
*/
KPUBLICTRANSPORT_PROPERTY(QString, identifier, setIdentifier)
Q_PROPERTY(QString identifier READ identifier)
/** Short, humand readable name of the backend. Typically the company providing the service */
KPUBLICTRANSPORT_PROPERTY(QString, name, setName)
Q_PROPERTY(QString name READ name)
/** Humand readable description of this backend. */
KPUBLICTRANSPORT_PROPERTY(QString, description, setDescription)
Q_PROPERTY(QString description READ description)
/** Supports secrure network access.
* Yes, even in 2019 that is unfortunately not guaranteed.
*/
KPUBLICTRANSPORT_PROPERTY(bool, isSecure, setIsSecure)
Q_PROPERTY(bool isSecure READ isSecure)
/** ISO 3166-2 alpha 2 code of the country this backend service is primarity addressing.
* Returns an empty value for global/international services.
......@@ -39,6 +39,10 @@ class KPUBLICTRANSPORT_EXPORT Backend
Q_PROPERTY(QString primaryCountryCode READ primaryCountryCode)
public:
QString identifier() const;
QString name() const;
QString description() const;
bool isSecure() const;
QString primaryCountryCode() const;
};
......
......@@ -10,20 +10,24 @@
#include <QSharedData>
#include <QString>
#include <memory>
class QJsonObject;
namespace KPublicTransport {
class AbstractBackend;
class Backend;
class BackendPrivate : public QSharedData {
public:
static Backend fromJson(const QJsonObject &obj, const QString &backendId);
static const AbstractBackend* impl(const Backend &b);
static void setImpl(Backend &b, std::unique_ptr<AbstractBackend> &&impl);
static Backend fromJson(const QJsonObject &obj);
QString identifier;
std::unique_ptr<AbstractBackend> m_backendImpl;
QString name;
QString description;
bool isSecure;
};
}
......
......@@ -87,9 +87,8 @@ public:
Manager *q = nullptr;
QNetworkAccessManager *m_nam = nullptr;
std::vector<std::unique_ptr<AbstractBackend>> m_backends;
std::vector<Backend> m_backends;
std::vector<Attribution> m_attributions;
std::vector<Backend> m_backendMetaData;
// we store both explicitly to have a third state, backends with the enabled state being the "default" (whatever that might eventually be)
QStringList m_enabledBackends;
......@@ -141,20 +140,15 @@ void ManagerPrivate::loadNetworks()
m_attributions.push_back(net->attribution());
}
auto metaData = BackendPrivate::fromJson(doc.object(), net->backendId());
metaData.setIsSecure(net->capabilities() & AbstractBackend::Secure);
m_backendMetaData.push_back(std::move(metaData));
m_backends.push_back(std::move(net));
auto b = BackendPrivate::fromJson(doc.object());
BackendPrivate::setImpl(b, std::move(net));
m_backends.push_back(std::move(b));
} else {
qCWarning(Log) << "Failed to load public transport network configuration config:" << it.fileName();
}
}
std::stable_sort(m_backends.begin(), m_backends.end(), [](const auto &lhs, const auto &rhs) {
return lhs->backendId() < rhs->backendId();
});
std::stable_sort(m_backendMetaData.begin(), m_backendMetaData.end(), [](const auto &lhs, const auto &rhs) {
return lhs.identifier() < rhs.identifier();
});
......@@ -546,7 +540,7 @@ JourneyReply* Manager::queryJourney(const JourneyRequest &req) const
// first time/direct query
if (req.contexts().empty()) {
for (const auto &backend : d->m_backends) {
if (d->queryJourney(backend.get(), req, reply)) {
if (d->queryJourney(BackendPrivate::impl(backend), req, reply)) {
++pendingOps;
}
}
......@@ -602,7 +596,7 @@ StopoverReply* Manager::queryStopover(const StopoverRequest &req) const
// first time/direct query
if (req.contexts().empty()) {
for (const auto &backend : d->m_backends) {
if (d->queryStopover(backend.get(), req, reply)) {
if (d->queryStopover(BackendPrivate::impl(backend), req, reply)) {
++pendingOps;
}
}
......@@ -653,8 +647,9 @@ LocationReply* Manager::queryLocation(const LocationRequest &req) const
return reply;
}
for (const auto &backend : d->m_backends) {
if (d->shouldSkipBackend(backend.get(), req)) {
for (const auto &b : d->m_backends) {
const auto backend = BackendPrivate::impl(b);
if (d->shouldSkipBackend(backend, req)) {
continue;
}
if (req.hasCoordinate() && backend->isLocationExcluded(req.location())) {
......@@ -697,8 +692,9 @@ VehicleLayoutReply* Manager::queryVehicleLayout(const VehicleLayoutRequest &req)
return reply;
}
for (const auto &backend : d->m_backends) {
if (d->shouldSkipBackend(backend.get(), req)) {
for (const auto &b : d->m_backends) {
const auto backend = BackendPrivate::impl(b);
if (d->shouldSkipBackend(backend, req)) {
continue;
}
if (req.stopover().stopPoint().hasCoordinate() && backend->isLocationExcluded(req.stopover().stopPoint())) {
......@@ -750,7 +746,7 @@ QVariantList Manager::attributionsVariant() const
const std::vector<Backend>& Manager::backends() const
{
return d->m_backendMetaData;
return d->m_backends;
}
bool Manager::isBackendEnabled(const QString &backendId) const
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment