KPublicTransport

locationreply.cpp
1 /*
2  SPDX-FileCopyrightText: 2018 Volker Krause <[email protected]>
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 
19 using namespace KPublicTransport;
20 
21 namespace KPublicTransport {
22 class LocationReplyPrivate: public ReplyPrivate {
23 public:
24  void finalizeResult() override;
25 
26  LocationRequest request;
27  std::vector<Location> locations;
28 };
29 }
30 
31 void 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 
56 LocationReply::LocationReply(const LocationRequest &req, QObject *parent)
57  : Reply(new LocationReplyPrivate, parent)
58 {
59  Q_D(LocationReply);
60  d->request = req;
61 }
62 
63 LocationReply::~LocationReply() = default;
64 
66 {
67  Q_D(const LocationReply);
68  return d->request;
69 }
70 
71 const std::vector<Location>& LocationReply::result() const
72 {
73  Q_D(const LocationReply);
74  return d->locations;
75 }
76 
77 std::vector<Location>&& LocationReply::takeResult()
78 {
79  Q_D(LocationReply);
80  return std::move(d->locations);
81 }
82 
83 void LocationReply::addResult(std::vector<Location> &&res)
84 {
85  Q_D(LocationReply);
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 
110 void LocationReply::addError(const AbstractBackend *backend, Reply::Error error, const QString &errorMsg)
111 {
112  if (error == Reply::NotFoundError) {
113  Cache::addNegativeLocationCacheEntry(backend->backendId(), request().cacheKey());
114  } else {
115  qCDebug(Log) << backend->backendId() << error << errorMsg;
116  }
117  Reply::addError(error, errorMsg);
118 }
Query operations and data types for accessing realtime public transport information from online servi...
Definition: attribution.cpp:16
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:262
std::vector< Location > && takeResult()
Returns the found locations for moving elsewhere.
void error(QWidget *parent, const QString &text, const QString &caption=QString(), Options options=Notify)
static Location merge(const Location &lhs, const Location &rhs)
Merge two departure instances.
Definition: location.cpp:368
The requested journey/departure/place could not be found.
Definition: reply.h:34
LocationRequest request() const
The request this is the reply for.
Location query reply.
Definition: locationreply.h:22
Error
Error types.
Definition: reply.h:31
Nothing went wrong.
Definition: reply.h:32
QString cacheKey() const
Unique string representation used for caching results.
const std::vector< Location > & result() const
Returns the found locations.
Describes a location search.
Query response base class.
Definition: reply.h:24
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Fri Oct 15 2021 23:07:28 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.