KPublicTransport

tripreply.cpp
1/*
2 SPDX-FileCopyrightText: 2025 Volker Krause <vkrause@kde.org>
3 SPDX-License-Identifier: LGPL-2.0-or-later
4*/
5
6#include "tripreply.h"
7
8#include "logging.h"
9#include "reply_p.h"
10#include "triprequest.h"
11
12#include "backends/abstractbackend.h"
13#include "backends/cache.h"
14#include "datatypes/journeyutil_p.h"
15
16#include <KPublicTransport/Journey>
17#include <KPublicTransport/Stopover>
18
19namespace KPublicTransport {
20class TripReplyPrivate: public ReplyPrivate {
21public:
22 void finalizeResult() override {}
23
24 TripRequest request;
25 JourneySection trip;
26 qsizetype beginIdx = -1;
27 qsizetype endIdx = -1;
28};
29}
30
31using namespace KPublicTransport;
32
33TripReply::TripReply(const TripRequest &req, QObject *parent)
34 : Reply(new TripReplyPrivate, parent)
35{
37 d->request = req;
38 d->trip = req.journeySection();
39 d->beginIdx = 0;
40 d->endIdx = (qsizetype)d->trip.intermediateStops().size() + 1;
41}
42
43TripReply::~TripReply() = default;
44
46{
47 Q_D(const TripReply);
48 return d->request;
49}
50
51JourneySection TripReply::trip() const
52{
53 Q_D(const TripReply);
54 return d->trip;
55}
56
58{
59 Q_D(const TripReply);
60 auto partialTrip = d->request.journeySection();
61
62 // do we have enough to go on to cut out a partial trip? is that even needed?
63 if (partialTrip.from().name().isEmpty() || partialTrip.to().name().isEmpty() || JourneySection::isSame(partialTrip, d->trip) || d->trip.intermediateStops().empty()) {
64 return d->trip;
65 }
66
67 if (d->beginIdx < 0 || d->endIdx < 0) {
68 return d->trip;
69 }
70 partialTrip = d->trip.subsection(d->beginIdx, d->endIdx);
71 partialTrip.setRoute(d->trip.route());
72 return partialTrip;
73}
74
76{
77 Q_D(const TripReply);
78 return d->beginIdx;
79}
80
82{
83 Q_D(const TripReply);
84 return d->endIdx;
85}
86
87void TripReply::addResult(const AbstractBackend *backend, JourneySection &&journeySection)
88{
90 JourneyUtil::propagateTimeZones(journeySection);
91
93 d->trip = JourneySection::merge(d->trip, journeySection);
94 } else {
95 auto route = Route::merge(d->trip.route(), journeySection.route());
96 d->trip = journeySection;
97 d->trip.setRoute(route);
98 }
99 d->beginIdx = d->trip.indexOfStopover(d->request.journeySection().departure());
100 d->endIdx = d->trip.indexOfStopover(d->request.journeySection().arrival());
101
102 JourneyUtil::postProcessPath(d->trip);
103 d->trip.applyMetaData(request().downloadAssets());
104
105 // apply static attributions if @p backend contributed to the results (can be nullptr for emulated results!)
106 if (backend) {
107 addAttribution(backend->attribution());
108 }
109
110 d->pendingOps--;
111 d->emitFinishedIfDone(this);
112}
113
114void TripReply::addError(const AbstractBackend *backend, Reply::Error error, const QString &errorMsg)
115{
116 if (error == Reply::NotFoundError) {
117 // TODO add negative cache entry
118 } else {
119 qCDebug(Log) << backend->backendId() << error << errorMsg;
120 }
121 Reply::addError(error, errorMsg);
122}
123
124#include "moc_tripreply.cpp"
A segment of a journey plan.
Definition journey.h:39
static JourneySection merge(const JourneySection &lhs, const JourneySection &rhs)
Merge two instances.
Definition journey.cpp:738
static bool isSame(const JourneySection &lhs, const JourneySection &rhs)
Checks if two instances refer to the same journey section (which does not necessarily mean they are e...
Definition journey.cpp:688
KPublicTransport::Route route
Route to take on this segment.
Definition journey.h:94
QVariantList intermediateStops
Intermediate stops for consumption by QML.
Definition journey.h:120
Query response base class.
Definition reply.h:25
Error
Error types.
Definition reply.h:33
@ NotFoundError
The requested journey/departure/place could not be found.
Definition reply.h:36
static Route merge(const Route &lhs, const Route &rhs)
Merge two Route instances.
Definition line.cpp:276
Reply to a trip query.
Definition tripreply.h:23
qsizetype journeySectionBegin() const
Index at which journeySection() begins in trip().
Definition tripreply.cpp:75
qsizetype journeySectionEnd() const
Index at which journeySection() ends in trip().
Definition tripreply.cpp:81
JourneySection journeySection() const
The sub-trip matching the JourneySection used in the request.
Definition tripreply.cpp:57
TripRequest request() const
The request this is the reply for.
Definition tripreply.cpp:45
Request for a single trip.
Definition triprequest.h:29
KPublicTransport::JourneySection journeySection
A JourneySection for which the full trip is requested.
Definition triprequest.h:32
Query operations and data types for accessing realtime public transport information from online servi...
Q_D(Todo)
This file is part of the KDE documentation.
Documentation copyright © 1996-2025 The KDE developers.
Generated on Fri Feb 21 2025 11:47:40 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.