File indexing completed on 2024-11-03 13:05:05

0001 /*
0002     SPDX-FileCopyrightText: 2018 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "locationrequest.h"
0008 #include "datatypes/datatypes_p.h"
0009 #include "datatypes/json_p.h"
0010 #include "datatypes/locationutil_p.h"
0011 
0012 #include <QCryptographicHash>
0013 #include <QDebug>
0014 #include <QMetaEnum>
0015 #include <QSharedData>
0016 #include <QStringList>
0017 
0018 #include <cmath>
0019 
0020 using namespace KPublicTransport;
0021 
0022 namespace KPublicTransport {
0023 class LocationRequestPrivate : public QSharedData {
0024 public:
0025     Location location;
0026     QStringList backendIds;
0027     int maximumDistance = 1000;
0028     int maximumResults = 10;
0029     Location::Types types = Location::Stop | Location::RentedVehicleStation;
0030 };
0031 }
0032 
0033 KPUBLICTRANSPORT_MAKE_GADGET(LocationRequest)
0034 KPUBLICTRANSPORT_MAKE_PROPERTY(LocationRequest, int, maximumDistance, setMaximumDistance)
0035 KPUBLICTRANSPORT_MAKE_PROPERTY(LocationRequest, int, maximumResults, setMaximumResults)
0036 KPUBLICTRANSPORT_MAKE_PROPERTY(LocationRequest, Location::Types, types, setTypes)
0037 
0038 LocationRequest::LocationRequest(const Location &locaction)
0039     : d(new LocationRequestPrivate)
0040 {
0041     d->location = locaction;
0042 }
0043 
0044 bool LocationRequest::isValid() const
0045 {
0046     return hasCoordinate() || !d->location.name().isEmpty();
0047 }
0048 
0049 Location LocationRequest::location() const
0050 {
0051     return d->location;
0052 }
0053 
0054 void LocationRequest::setLocation(const Location &location)
0055 {
0056     d.detach();
0057     d->location = location;
0058 }
0059 
0060 float LocationRequest::latitude() const
0061 {
0062     return d->location.latitude();
0063 }
0064 
0065 void LocationRequest::setLatitude(float lat)
0066 {
0067     d.detach();
0068     d->location.setLatitude(lat);
0069 }
0070 
0071 float LocationRequest::longitude() const
0072 {
0073     return d->location.longitude();
0074 }
0075 
0076 void LocationRequest::setLongitude(float lon)
0077 {
0078     d.detach();
0079     d->location.setLongitude(lon);
0080 }
0081 
0082 void LocationRequest::setCoordinate(float lat, float lon)
0083 {
0084     d.detach();
0085     d->location.setCoordinate(lat, lon);
0086 }
0087 
0088 bool LocationRequest::hasCoordinate() const
0089 {
0090     return d->location.hasCoordinate();
0091 }
0092 
0093 QString LocationRequest::name() const
0094 {
0095     if (d->location.name().isEmpty()) {
0096         return QString(d->location.locality() + QLatin1Char(' ') + d->location.streetAddress()).trimmed();
0097     }
0098     return d->location.name();
0099 }
0100 
0101 void LocationRequest::setName(const QString &name)
0102 {
0103     d.detach();
0104     d->location.setName(name);
0105 }
0106 
0107 QString LocationRequest::cacheKey() const
0108 {
0109     QCryptographicHash hash(QCryptographicHash::Sha1);
0110     hash.addData(LocationUtil::cacheKey(d->location).toUtf8());
0111     const auto me = QMetaEnum::fromType<Location::Types>();
0112     hash.addData(me.valueToKeys(types()));
0113     hash.addData(QByteArray::number(maximumDistance()));
0114     hash.addData(QByteArray::number(maximumResults()));
0115     return QString::fromUtf8(hash.result().toHex());
0116 }
0117 
0118 QJsonObject LocationRequest::toJson(const LocationRequest &req)
0119 {
0120     auto obj = Json::toJson(req);
0121     obj.insert(QLatin1String("location"), Location::toJson(req.location()));
0122     return obj;
0123 }
0124 
0125 QStringList LocationRequest::backendIds() const
0126 {
0127     return d->backendIds;
0128 }
0129 
0130 void LocationRequest::setBackendIds(const QStringList &backendIds)
0131 {
0132     d.detach();
0133     d->backendIds = backendIds;
0134 }
0135 
0136 #include "moc_locationrequest.cpp"