KPublicTransport

locationreply.cpp
1/*
2 SPDX-FileCopyrightText: 2018 Volker Krause <vkrause@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "locationreply.h"
8#include "reply_p.h"
9#include "locationrequest.h"
10#include "datatypes/locationutil_p.h"
11#include "logging.h"
12#include "backends/abstractbackend.h"
13#include "backends/cache.h"
14
15#include <KPublicTransport/Location>
16
17#include <QDebug>
18
19using namespace KPublicTransport;
20
21namespace KPublicTransport {
22class LocationReplyPrivate: public ReplyPrivate {
23public:
24 void finalizeResult() override;
25
26 LocationRequest request;
27 std::vector<Location> locations;
28};
29}
30
31void LocationReplyPrivate::finalizeResult()
32{
33 if (locations.empty()) {
34 return;
35 }
37 errorMsg.clear();
38
39 // merge all duplicates, as there is no natural order for name searches this is done in O(n²) for now
40 for (auto it = locations.begin(); it != locations.end(); ++it) {
41 for (auto mergeIt = it + 1; mergeIt != locations.end();) {
42 if (Location::isSame(*it, *mergeIt)) {
43 *it = Location::merge(*it, *mergeIt);
44 mergeIt = locations.erase(mergeIt);
45 } else {
46 ++mergeIt;
47 }
48 }
49 }
50
51 std::sort(locations.begin(), locations.end(), [this](const auto &lhs, const auto &rhs) {
52 return LocationUtil::sortLessThan(request, lhs, rhs);
53 });
54}
55
56LocationReply::LocationReply(const LocationRequest &req, QObject *parent)
57 : Reply(new LocationReplyPrivate, parent)
58{
60 d->request = req;
61}
62
63LocationReply::~LocationReply() = default;
64
66{
67 Q_D(const LocationReply);
68 return d->request;
69}
70
71const std::vector<Location>& LocationReply::result() const
72{
73 Q_D(const LocationReply);
74 return d->locations;
75}
76
77std::vector<Location>&& LocationReply::takeResult()
78{
80 return std::move(d->locations);
81}
82
83void LocationReply::addResult(std::vector<Location> &&res)
84{
86 // remove implausible results
87 for (auto it = res.begin(); it != res.end();) {
88 // we sometimes seem to get bogus places in Antarctica
89 if ((*it).hasCoordinate() && (*it).latitude() < -65.0) {
90 qCDebug(Log) << "Dropping location in Antarctica" << (*it).name() << (*it).latitude() << (*it).longitude();
91 it = res.erase(it);
92 continue;
93 }
94 ++it;
95 }
96
97 if (!res.empty()) {
98 if (d->locations.empty()) {
99 d->locations = std::move(res);
100 } else {
101 d->locations.insert(d->locations.end(), res.begin(), res.end());
102 }
103 d->emitUpdated(this);
104 }
105
106 d->pendingOps--;
107 d->emitFinishedIfDone(this);
108}
109
110void LocationReply::addError(const AbstractBackend *backend, Reply::Error error, const QString &errorMsg)
111{
113 Cache::addNegativeLocationCacheEntry(backend->backendId(), request().cacheKey());
114 } else {
115 qCDebug(Log) << backend->backendId() << error << errorMsg;
116 }
117 Reply::addError(error, errorMsg);
118}
LocationRequest request() const
The request this is the reply for.
std::vector< Location > && takeResult()
Returns the found locations for moving elsewhere.
const std::vector< Location > & result() const
Returns the found locations.
Describes a location search.
static Location merge(const Location &lhs, const Location &rhs)
Merge two departure instances.
Definition location.cpp:369
static bool isSame(const Location &lhs, const Location &rhs)
Checks if to instances refer to the same location (which does not necessarily mean they are exactly e...
Definition location.cpp:263
Query response base class.
Definition reply.h:25
Error error() const
Error code.
Definition reply.cpp:46
Error
Error types.
Definition reply.h:31
@ NoError
Nothing went wrong.
Definition reply.h:32
@ NotFoundError
The requested journey/departure/place could not be found.
Definition reply.h:34
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...
Q_D(Todo)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:13:06 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.