7#include "stopoverreply.h"
10#include "requestcontext_p.h"
11#include "stopoverrequest.h"
12#include "backends/abstractbackend.h"
13#include "backends/cache.h"
14#include "datatypes/stopoverutil_p.h"
16#include <KPublicTransport/Stopover>
18#include <QNetworkReply>
23class StopoverReplyPrivate :
public ReplyPrivate {
25 void finalizeResult()
override;
26 bool needToWaitForAssets()
const override;
31 std::vector<Stopover> result;
35void StopoverReplyPrivate::finalizeResult()
43 std::sort(result.begin(), result.end(), [
this](
const auto &lhs,
const auto &rhs) {
44 return StopoverUtil::timeLessThan(request, lhs, rhs);
47 for (
auto it = result.begin(); it != result.end(); ++it) {
48 for (
auto mergeIt = it + 1; mergeIt != result.end();) {
49 if (!StopoverUtil::timeEqual(request, (*it), (*mergeIt))) {
55 mergeIt = result.erase(mergeIt);
62 nextRequest.purgeLoops(request);
63 prevRequest.purgeLoops(request);
66bool StopoverReplyPrivate::needToWaitForAssets()
const
72 :
Reply(new StopoverReplyPrivate, parent)
80StopoverReply::~StopoverReply() =
default;
97 return std::move(d->result);
100void StopoverReply::addResult(
const AbstractBackend *backend, std::vector<Stopover> &&res)
107 auto context = d->nextRequest.context(backend);
108 context.type = RequestContext::Next;
109 for (
const auto &dep : res) {
110 context.dateTime = std::max(context.dateTime, dep.scheduledDepartureTime());
112 d->nextRequest.setContext(backend, std::move(context));
114 if (backend->hasCapability(AbstractBackend::CanQueryPreviousDeparture)) {
115 context = d->prevRequest.context(backend);
116 context.type = RequestContext::Previous;
117 context.dateTime = res[0].scheduledDepartureTime();
118 for (
const auto &jny : res) {
119 context.dateTime = std::min(context.dateTime, jny.scheduledDepartureTime());
121 d->prevRequest.setContext(backend, std::move(context));
125 auto context = d->nextRequest.context(backend);
126 context.type = RequestContext::Next;
127 for (
const auto &dep : res) {
128 context.dateTime = std::max(context.dateTime, dep.scheduledArrivalTime());
130 d->nextRequest.setContext(backend, std::move(context));
132 if (backend->hasCapability(AbstractBackend::CanQueryPreviousDeparture)) {
133 context = d->prevRequest.context(backend);
134 context.type = RequestContext::Previous;
135 context.dateTime = res[0].scheduledArrivalTime();
136 for (
const auto &jny : res) {
137 context.dateTime = std::min(context.dateTime, jny.scheduledArrivalTime());
139 d->prevRequest.setContext(backend, std::move(context));
144 if (backend->timeZone().isValid()) {
145 for (
auto &dep : res) {
146 StopoverUtil::applyTimeZone(dep, backend->timeZone());
150 for (
auto &
stop : res) {
151 StopoverUtil::propagateTimeZone(
stop);
155 for (
auto &dep : res) {
156 dep.applyMetaData(
request().downloadAssets());
160 addAttribution(backend->attribution());
164 Cache::addNegativeStopoverCacheEntry(backend->backendId(),
request().cacheKey());
167 if (d->result.empty()) {
168 d->result = std::move(res);
170 d->result.insert(d->result.end(), res.begin(), res.end());
174 d->emitUpdated(
this);
175 d->emitFinishedIfDone(
this);
181 if (d->nextRequest.contexts().empty()) {
184 return d->nextRequest;
190 if (d->prevRequest.contexts().empty()) {
193 return d->prevRequest;
196void StopoverReply::setNextContext(
const AbstractBackend *backend,
const QVariant &data)
199 auto context = d->nextRequest.context(backend);
200 context.type = RequestContext::Next;
201 context.backendData = data;
202 d->nextRequest.setContext(backend, std::move(context));
205void StopoverReply::setPreviousContext(
const AbstractBackend *backend,
const QVariant &data)
208 auto context = d->prevRequest.context(backend);
209 context.type = RequestContext::Previous;
210 context.backendData = data;
211 d->prevRequest.setContext(backend, std::move(context));
214void StopoverReply::addError(
const AbstractBackend *backend,
Reply::Error error,
const QString &errorMsg)
217 Cache::addNegativeStopoverCacheEntry(backend->backendId(),
request().cacheKey());
219 qCDebug(
Log) << backend->backendId() <<
error << errorMsg;
221 Reply::addError(
error, errorMsg);
Query response base class.
Error error() const
Error code.
@ NoError
Nothing went wrong.
@ NotFoundError
The requested journey/departure/place could not be found.
Departure or arrival query reply.
StopoverRequest previousRequest() const
Returns a request object for querying departures preceding the ones returned by this reply.
StopoverRequest nextRequest() const
Returns a request object for querying departures following the ones returned by this reply.
StopoverRequest request() const
The request this is the reply for.
std::vector< Stopover > && takeResult()
Returns the found arrival or departure information for moving elsewhere.
const std::vector< Stopover > & result() const
Returns the found arrival or departure information.
Describes an arrival or departure search.
@ QueryDeparture
Search for departures.
@ QueryArrival
Search for arrivals.
bool downloadAssets
Enable downloading of graphic assets such as line logos for the data requested here.
static Stopover merge(const Stopover &lhs, const Stopover &rhs)
Merge two departure instances.
static bool isSame(const Stopover &lhs, const Stopover &rhs)
Checks if to instances refer to the same departure (which does not necessarily mean they are exactly ...
void stop(Ekos::AlignState mode)
void error(QWidget *parent, const QString &text, const QString &title, const KGuiItem &buttonOk, Options options=Notify)
Query operations and data types for accessing realtime public transport information from online servi...