Commit 882f763d authored by Volker Krause's avatar Volker Krause
Browse files

Make line meta data application public API

This helps with meta data access in a few cases:
- Deserialization of partial results such as a single JourneySection lost
meta data so far, with no way to work around that in user code.
- Asset download control was "all or nothing", which isn't ideal for code
selecting a single result and only wanting to downloda assets for that.

Now user code has explicit control over all this.
parent 40d08c89
Pipeline #92410 passed with stage
in 1 minute and 24 seconds
......@@ -346,6 +346,21 @@ QVariantList JourneySection::loadInformationVariant() const
return l;
}
void JourneySection::applyMetaData(bool download)
{
if (!from().hasCoordinate() || mode() != JourneySection::PublicTransport) {
return;
}
auto line = d->route.line();
line.applyMetaData(from(), download);
d->route.setLine(line);
// propagate to intermediate stops
for (auto &stop : d->intermediateStops) {
stop.setRoute(d->route);
}
}
bool JourneySection::isSame(const JourneySection &lhs, const JourneySection &rhs)
{
if (lhs.d->mode != rhs.d->mode) {
......@@ -518,6 +533,7 @@ JourneySection JourneySection::fromJson(const QJsonObject &obj)
section.setArrivalVehicleLayout(Vehicle::fromJson(obj.value(QLatin1String("arrivalVehicleLayout")).toObject()));
section.setArrivalPlatformLayout(Platform::fromJson(obj.value(QLatin1String("arrivalPlatformLayout")).toObject()));
section.setIndividualTransport(IndividualTransport::fromJson(obj.value(QLatin1String("individualTransport")).toObject()));
section.applyMetaData(false);
return section;
}
......@@ -609,6 +625,13 @@ Disruption::Effect Journey::disruptionEffect() const
return effect;
}
void Journey::applyMetaData(bool download)
{
for (auto &sec : d->sections) {
sec.applyMetaData(download);
}
}
static bool isTransportSection(JourneySection::Mode mode)
{
return mode == JourneySection::PublicTransport
......@@ -694,7 +717,6 @@ Journey Journey::fromJson(const QJsonObject &obj)
{
Journey j;
j.setSections(JourneySection::fromJson(obj.value(QLatin1String("sections")).toArray()));
JourneyUtil::applyMetaData(j, false);
return j;
}
......
......@@ -199,6 +199,11 @@ public:
/** Set the vehicle load information for this journey section. */
void setLoadInformation(std::vector<LoadInfo>&& loadInfo);
/** Augment line meta data.
* @param download if set to @p true, trigger the download of locally missing assets.
*/
void applyMetaData(bool download);
/** Checks if two instances refer to the same journey section (which does not necessarily mean they are exactly equal). */
static bool isSame(const JourneySection &lhs, const JourneySection &rhs);
......@@ -272,6 +277,11 @@ public:
int numberOfChanges() const;
Disruption::Effect disruptionEffect() const;
/** Augment line meta data.
* @param download if set to @p true, trigger the download of locally missing assets.
*/
void applyMetaData(bool download);
/** Checks if two instances refer to the same journey (which does not necessarily mean they are exactly equal). */
static bool isSame(const Journey &lhs, const Journey &rhs);
......
......@@ -5,7 +5,6 @@
*/
#include "journeyutil_p.h"
#include "lineutil_p.h"
#include "stopoverutil_p.h"
#include "timeutil_p.h"
......@@ -55,26 +54,3 @@ void JourneyUtil::applyTimeZone(Journey &jny, const QTimeZone &tz)
}
jny.setSections(std::move(sections));
}
void JourneyUtil::applyMetaData(Journey &jny, bool download)
{
auto sections = std::move(jny.takeSections());
for (auto &sec : sections) {
if (!sec.from().hasCoordinate() || sec.mode() != JourneySection::PublicTransport) {
continue;
}
auto route = sec.route();
auto line = route.line();
LineUtil::applyMetaData(line, sec.from(), download);
route.setLine(line);
sec.setRoute(route);
// propagate to intermediate stops
auto stops = sec.takeIntermediateStops();
for (auto &stop : stops) {
stop.setRoute(route);
}
sec.setIntermediateStops(std::move(stops));
}
jny.setSections(std::move(sections));
}
......@@ -29,8 +29,6 @@ namespace JourneyUtil
/** Reinterpret all floating times as times with the given timezone. */
void applyTimeZone(Journey &jny, const QTimeZone &tz);
/** Augment line meta data. */
void applyMetaData(Journey &jny, bool download);
}
}
......
......@@ -67,11 +67,6 @@ bool Line::hasTextColor() const
return d->textColor.isValid();
}
void Line::setMetaData(const LineMetaData &metaData)
{
d->metaData = metaData;
}
QString Line::logo() const
{
return AssetRepository::localFile(d->metaData.logoUrl());
......@@ -140,6 +135,20 @@ Line Line::merge(const Line &lhs, const Line &rhs)
return l;
}
void Line::applyMetaData(const Location &location, bool download)
{
if (name().isEmpty() || !location.hasCoordinate()) {
return;
}
d->metaData = LineMetaData::find(location.latitude(), location.longitude(), name(), mode());
if (download && AssetRepository::instance()) {
AssetRepository::instance()->download(d->metaData.logoUrl());
AssetRepository::instance()->download(d->metaData.modeLogoUrl());
}
}
QJsonObject Line::toJson(const Line &l)
{
auto obj = Json::toJson(l);
......
......@@ -14,10 +14,6 @@ namespace KPublicTransport {
class Line;
class LinePrivate;
class LineMetaData;
namespace LineUtil{
KPUBLICTRANSPORT_EXPORT void setMetaData(Line&, const LineMetaData&);
}
/** A public transport line. */
class KPUBLICTRANSPORT_EXPORT Line
......@@ -96,6 +92,12 @@ public:
QString modeLogo() const;
bool hasModeLogo() const;
/** Look up line meta data and apply what is found.
* @param location A location on or close to the line.
* @param download When set to @c true, not yet locally present logo URLs are retrieved.
*/
void applyMetaData(const Location &location, bool download);
/** Checks if to instances refer to the same line (which does not necessarily mean they are exactly equal). */
static bool isSame(const Line &lhs, const Line &rhs);
......@@ -106,12 +108,11 @@ public:
/** Serializes one object to JSON. */
static QJsonObject toJson(const Line &l);
/** Deserialize an object from JSON. */
/** Deserialize an object from JSON.
* @note Line meta data isn't serialized, so you might need to call applyLineMetaData() again
* after loading a line.
*/
static Line fromJson(const QJsonObject &obj);
private:
friend void LineUtil::setMetaData(Line&, const LineMetaData&);
void setMetaData(const LineMetaData &metaData);
};
class RoutePrivate;
......
......@@ -44,23 +44,3 @@ bool LineUtil::isCompatibleMode(Line::Mode lhs, Line::Mode rhs)
return false;
}
void LineUtil::applyMetaData(Line &line, const Location &loc, bool download)
{
if (line.name().isEmpty() || !loc.hasCoordinate()) {
return;
}
auto metaData = LineMetaData::find(loc.latitude(), loc.longitude(), line.name(), line.mode());
setMetaData(line, metaData);
if (download && AssetRepository::instance()) {
AssetRepository::instance()->download(metaData.logoUrl());
AssetRepository::instance()->download(metaData.modeLogoUrl());
}
}
void LineUtil::setMetaData(Line& line, const LineMetaData &metaData)
{
line.setMetaData(metaData);
}
......@@ -21,8 +21,6 @@ namespace LineUtil
bool isSameLineNameStrict(const QString &lhs, const QString &rhs);
bool isSameLineNameFuzzy(const QString &lhs, const QString &rhs);
bool isCompatibleMode(Line::Mode lhs, Line::Mode rhs);
void applyMetaData(Line &line, const Location &loc, bool download);
KPUBLICTRANSPORT_EXPORT void setMetaData(Line &line, const LineMetaData &metaData);
}
}
......
......@@ -149,6 +149,13 @@ QVariantList Stopover::loadInformationVariant() const
return l;
}
void Stopover::applyMetaData(bool download)
{
auto line = d->route.line();
line.applyMetaData(stopPoint(), download);
d->route.setLine(line);
}
bool Stopover::isSame(const Stopover &lhs, const Stopover &rhs)
{
// same time is mandatory
......@@ -246,7 +253,7 @@ Stopover Stopover::fromJson(const QJsonObject &obj)
stopover.setLoadInformation(LoadInfo::fromJson(obj.value(QLatin1String("load")).toArray()));
stopover.setVehicleLayout(Vehicle::fromJson(obj.value(QLatin1String("vehicleLayout")).toObject()));
stopover.setPlatformLayout(Platform::fromJson(obj.value(QLatin1String("platformLayout")).toObject()));
StopoverUtil::applyMetaData(stopover, false);
stopover.applyMetaData(false);
return stopover;
}
......
......@@ -97,6 +97,11 @@ public:
/** Set the expected vehicle load information for departing from this stopover. */
void setLoadInformation(std::vector<LoadInfo>&& loadInfo);
/** Augment line meta data.
* @param download when set to @c true trigger download of missing assets.
*/
void applyMetaData(bool download);
/** Checks if to instances refer to the same departure (which does not necessarily mean they are exactly equal). */
static bool isSame(const Stopover &lhs, const Stopover &rhs);
......
......@@ -5,7 +5,6 @@
*/
#include "stopoverutil_p.h"
#include "lineutil_p.h"
#include "timeutil_p.h"
#include <KPublicTransport/Stopover>
......@@ -47,12 +46,3 @@ void StopoverUtil::applyTimeZone(Stopover &dep, const QTimeZone &tz)
dep.setScheduledArrivalTime(TimeUtil::applyTimeZone(dep.scheduledArrivalTime(), tz));
dep.setExpectedArrivalTime(TimeUtil::applyTimeZone(dep.expectedArrivalTime(), tz));
}
void StopoverUtil::applyMetaData(Stopover &dep, bool download)
{
auto route = dep.route();
auto line = route.line();
LineUtil::applyMetaData(line, dep.stopPoint(), download);
route.setLine(line);
dep.setRoute(route);
}
......@@ -23,8 +23,6 @@ namespace StopoverUtil
/** Reinterpret all floating times as times with the given timezone. */
void applyTimeZone(Stopover &dep, const QTimeZone &tz);
/** Augment line meta data. */
void applyMetaData(Stopover &dep, bool download);
}
}
......
......@@ -245,7 +245,7 @@ void JourneyReply::addResult(const AbstractBackend *backend, std::vector<Journey
// apply line meta data
for (auto &jny : res) {
JourneyUtil::applyMetaData(jny, request().downloadAssets());
jny.applyMetaData(request().downloadAssets());
}
// cache negative hits, positive ones are too short-lived
......
......@@ -121,7 +121,7 @@ void StopoverReply::addResult(const AbstractBackend *backend, std::vector<Stopov
// augment line information
for (auto &dep : res) {
StopoverUtil::applyMetaData(dep, request().downloadAssets());
dep.applyMetaData(request().downloadAssets());
}
// apply static attributions if @p backend contributed to the results
......
......@@ -16,18 +16,11 @@ using namespace KPublicTransport;
Line LineMetaDataWrapper::lookup(const QString &name, double latitude, double longitude, int mode, bool download)
{
auto md = LineMetaData::find(latitude, longitude, name, static_cast<Line::Mode>(mode));
if (md.isNull()) {
return {};
}
Line line;
LineUtil::setMetaData(line, md);
if (download && AssetRepository::instance()) {
AssetRepository::instance()->download(md.logoUrl());
AssetRepository::instance()->download(md.modeLogoUrl());
}
line.setName(name);
line.setMode(static_cast<Line::Mode>(mode));
Location loc;
loc.setCoordinate(latitude, longitude);
line.applyMetaData(loc, download);
return line;
}
Supports Markdown
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