KPublicTransport

stopoverrequest.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 "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 <QCryptographicHash>
16#include <QDateTime>
17#include <QMetaEnum>
18#include <QSharedData>
19
20using namespace KPublicTransport;
21
22enum { DepartureCacheTimeResolution = 60 }; // in seconds
23
24namespace KPublicTransport {
25class StopoverRequestPrivate : public QSharedData {
26public:
27 Location stop;
28 QDateTime dateTime;
30 std::vector<RequestContext> contexts;
31 QStringList backendIds;
32 std::vector<Line::Mode> lineModes;
33 bool downloadAssets = false;
34 int maximumResults = 12;
35};
36}
37
38KPUBLICTRANSPORT_MAKE_GADGET(StopoverRequest)
39KPUBLICTRANSPORT_MAKE_PROPERTY(StopoverRequest, Location, stop, setStop)
40KPUBLICTRANSPORT_MAKE_PROPERTY(StopoverRequest, StopoverRequest::Mode, mode, setMode)
41KPUBLICTRANSPORT_MAKE_PROPERTY(StopoverRequest, bool, downloadAssets, setDownloadAssets)
42KPUBLICTRANSPORT_MAKE_PROPERTY(StopoverRequest, int, maximumResults, setMaximumResults)
43
45 : d(new StopoverRequestPrivate)
46{
47 d->stop = stop;
48}
49
51{
52 return !d->stop.isEmpty();
53}
54
56{
57 if (!d->dateTime.isValid()) {
58 d->dateTime = QDateTime::currentDateTime();
59 }
60 return d->dateTime;
61}
62
63void StopoverRequest::setDateTime(const QDateTime &dt)
64{
65 d.detach();
66 d->dateTime = dt;
67}
68
69const std::vector<Line::Mode>& StopoverRequest::lineModes() const
70{
71 return d->lineModes;
72}
73
74void StopoverRequest::setLineModes(std::vector<Line::Mode> &&lineModes)
75{
76 d.detach();
77 d->lineModes = std::move(lineModes);
78 std::sort(d->lineModes.begin(), d->lineModes.end());
79 d->lineModes.erase(std::unique(d->lineModes.begin(), d->lineModes.end()), d->lineModes.end());
80}
81
82QVariantList StopoverRequest::lineModesVariant() const
83{
84 QVariantList l;
85 l.reserve(d->lineModes.size());
86 std::transform(d->lineModes.begin(), d->lineModes.end(), std::back_inserter(l), [](const Line::Mode &value) {
87 return QVariant::fromValue<Line::Mode>(value);
88 });
89 return l;
90}
91
92void StopoverRequest::setLineModesVariant(const QVariantList &lineModes)
93{
94 auto l = std::move(d->lineModes);
95 l.clear();
96 l.reserve(lineModes.size());
97 std::transform(lineModes.begin(), lineModes.end(), std::back_inserter(l), [](const auto &mode) { return static_cast<Line::Mode>(mode.toInt()); });
98 setLineModes(std::move(l));
99}
100
101RequestContext StopoverRequest::context(const AbstractBackend *backend) const
102{
103 const auto it = std::lower_bound(d->contexts.begin(), d->contexts.end(), backend);
104 if (it != d->contexts.end() && (*it).backend == backend) {
105 return *it;
106 }
107
108 RequestContext context;
109 context.backend = backend;
110 return context;
111}
112
113const std::vector<RequestContext>& StopoverRequest::contexts() const
114{
115 return d->contexts;
116}
117
118void StopoverRequest::setContext(const AbstractBackend *backend, RequestContext &&context)
119{
120 d.detach();
121 const auto it = std::lower_bound(d->contexts.begin(), d->contexts.end(), backend);
122 if (it != d->contexts.end() && (*it).backend == backend) {
123 (*it) = std::move(context);
124 } else {
125 d->contexts.insert(it, std::move(context));
126 }
127}
128
129void StopoverRequest::purgeLoops(const StopoverRequest &baseRequest)
130{
131 RequestContext::purgeLoops(d->contexts, baseRequest.contexts());
132}
133
134QJsonObject StopoverRequest::toJson(const StopoverRequest &req)
135{
136 auto obj = Json::toJson(req);
137 obj.insert(QLatin1String("stop"), Location::toJson(req.stop()));
138 return obj;
139}
140
142{
143 return d->backendIds;
144}
145
147{
148 d.detach();
149 d->backendIds = backendIds;
150}
151
153{
155 hash.addData(QByteArray::number(d->dateTime.toSecsSinceEpoch() / DepartureCacheTimeResolution));
156 hash.addData(LocationUtil::cacheKey(d->stop).toUtf8());
157 hash.addData(d->mode == StopoverRequest::QueryArrival ? "A" : "D");
158
159 hash.addData("MODES");
160 for (const auto &mode : d->lineModes) {
161 hash.addData(QMetaEnum::fromType<Line::Mode>().valueToKey(mode));
162 }
163
164 return QString::fromUtf8(hash.result().toHex());
165}
166
167#include "moc_stopoverrequest.cpp"
Mode
Mode of transportation.
Definition line.h:27
static QJsonObject toJson(const Location &loc)
Serializes one Location object to JSON.
Definition location.cpp:494
Describes an arrival or departure search.
Mode
Query departures or arrivals?
@ QueryDeparture
Search for departures.
@ QueryArrival
Search for arrivals.
bool isValid() const
Returns true if this is a valid request, ie.
QVariantList lineModes
Line modes.
QDateTime dateTime
Date/time at which the search should start.
void setBackendIds(const QStringList &backendIds)
Set identifiers of backends that should be queried.
QString cacheKey() const
Unique string representation used for caching results.
KPublicTransport::Location stop
The location at which to search for departures/arrivals.
QStringList backendIds() const
Identifiers of the backends that should be queried.
Mode mode
Controls whether to search for arrivals or departures.
void setLineModes(std::vector< Line::Mode > &&modes)
Sets the requested line modes.
void stop(Ekos::AlignState mode)
Query operations and data types for accessing realtime public transport information from online servi...
QByteArray number(double n, char format, int precision)
QByteArray toHex(char separator) const const
bool addData(QIODevice *device)
QByteArray result() const const
QDateTime currentDateTime()
QMetaEnum fromType()
QString fromUtf8(QByteArrayView str)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 29 2024 11:57:19 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.