KPublicTransport

stopoverrequest.cpp
1 /*
2  SPDX-FileCopyrightText: 2018 Volker Krause <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "stopoverrequest.h"
8 #include "requestcontext_p.h"
9 #include "datatypes/datatypes_p.h"
10 #include "datatypes/json_p.h"
11 #include "datatypes/locationutil_p.h"
12 
13 #include <KPublicTransport/Location>
14 
15 #include <QDateTime>
16 #include <QSharedData>
17 
18 using namespace KPublicTransport;
19 
20 enum { DepartureCacheTimeResolution = 60 }; // in seconds
21 
22 namespace KPublicTransport {
23 class StopoverRequestPrivate : public QSharedData {
24 public:
25  Location stop;
26  QDateTime dateTime;
28  std::vector<RequestContext> contexts;
29  QStringList backendIds;
30  bool downloadAssets = false;
31  int maximumResults = 12;
32 };
33 }
34 
35 KPUBLICTRANSPORT_MAKE_GADGET(StopoverRequest)
36 KPUBLICTRANSPORT_MAKE_PROPERTY(StopoverRequest, Location, stop, setStop)
37 KPUBLICTRANSPORT_MAKE_PROPERTY(StopoverRequest, StopoverRequest::Mode, mode, setMode)
38 KPUBLICTRANSPORT_MAKE_PROPERTY(StopoverRequest, bool, downloadAssets, setDownloadAssets)
39 KPUBLICTRANSPORT_MAKE_PROPERTY(StopoverRequest, int, maximumResults, setMaximumResults)
40 
41 StopoverRequest::StopoverRequest(const Location &stop)
42  : d(new StopoverRequestPrivate)
43 {
44  d->stop = stop;
45 }
46 
48 {
49  return !d->stop.isEmpty();
50 }
51 
53 {
54  if (!d->dateTime.isValid()) {
55  d->dateTime = QDateTime::currentDateTime();
56  }
57  return d->dateTime;
58 }
59 
60 void StopoverRequest::setDateTime(const QDateTime &dt)
61 {
62  d.detach();
63  d->dateTime = dt;
64 }
65 
66 RequestContext StopoverRequest::context(const AbstractBackend *backend) const
67 {
68  const auto it = std::lower_bound(d->contexts.begin(), d->contexts.end(), backend);
69  if (it != d->contexts.end() && (*it).backend == backend) {
70  return *it;
71  }
72 
73  RequestContext context;
74  context.backend = backend;
75  return context;
76 }
77 
78 const std::vector<RequestContext>& StopoverRequest::contexts() const
79 {
80  return d->contexts;
81 }
82 
83 void StopoverRequest::setContext(const AbstractBackend *backend, RequestContext &&context)
84 {
85  d.detach();
86  const auto it = std::lower_bound(d->contexts.begin(), d->contexts.end(), backend);
87  if (it != d->contexts.end() && (*it).backend == backend) {
88  (*it) = std::move(context);
89  } else {
90  d->contexts.insert(it, std::move(context));
91  }
92 }
93 
94 void StopoverRequest::purgeLoops(const StopoverRequest &baseRequest)
95 {
96  RequestContext::purgeLoops(d->contexts, baseRequest.contexts());
97 }
98 
99 QJsonObject StopoverRequest::toJson(const StopoverRequest &req)
100 {
101  auto obj = Json::toJson(req);
102  obj.insert(QStringLiteral("stop"), Location::toJson(req.stop()));
103  return obj;
104 }
105 
107 {
108  return d->backendIds;
109 }
110 
112 {
113  d.detach();
114  d->backendIds = backendIds;
115 }
116 
118 {
119  return QString::number(d->dateTime.toSecsSinceEpoch() / DepartureCacheTimeResolution) + QLatin1Char('_')
120  + LocationUtil::cacheKey(d->stop)
121  + QLatin1Char('_') + (d->mode == StopoverRequest::QueryArrival ? QLatin1Char('A') : QLatin1Char('D'));
122 }
123 
124 #include "moc_stopoverrequest.cpp"
Query operations and data types for accessing realtime public transport information from online servi...
Definition: attribution.cpp:16
QString cacheKey() const
Unique string representation used for caching results.
void setBackendIds(const QStringList &backendIds)
Set identifiers of backends that should be queried.
QStringList backendIds() const
Identifiers of the backends that should be queried.
QString number(int n, int base)
bool isValid() const
Returns true if this is a valid request, ie.
KGuiItem stop()
QDateTime dateTime
Date/time at which the search should start.
Describes an arrival or departure search.
KPublicTransport::Location stop
The location at which to search for departures/arrivals.
Mode
Query departures or arrivals?
QDateTime currentDateTime()
static QJsonObject toJson(const Location &loc)
Serializes one Location object to JSON.
Definition: location.cpp:444
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Sat Oct 23 2021 23:05:21 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.