Kgapi

latitudeservice.cpp
1/*
2 SPDX-FileCopyrightText: 2012 Jan Grulich <grulja@gmail.com>
3
4 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
5*/
6
7#include "latitudeservice.h"
8#include "location.h"
9
10#include <QJsonDocument>
11#include <QUrlQuery>
12#include <QVariant>
13#include <QVariantMap>
14
15namespace KGAPI2
16{
17
18namespace LatitudeService
19{
20
21namespace Private
22{
23LocationPtr parseLocation(const QVariantMap &map);
24
25static const QUrl GoogleApisUrl(QStringLiteral("https://www.googleapis.com"));
26static const QString LocationBasePath(QStringLiteral("/latitude/v1/location"));
27static const QString CurrentLocationBasePath(QStringLiteral("/latitude/v1/currentLocation"));
28}
29
31{
32 QJsonDocument document = QJsonDocument::fromJson(jsonData);
33 if (document.isNull()) {
34 return LocationPtr();
35 }
36 const QVariantMap data = document.toVariant().toMap();
37 const QVariantMap info = data.value(QStringLiteral("data")).toMap();
38
39 return Private::parseLocation(info);
40}
41
43{
44 QVariantMap map;
45 QVariantMap output;
46
47 map.insert(QStringLiteral("kind"), QStringLiteral("latitude#location"));
48 map.insert(QStringLiteral("latitude"), QString::number(location->latitude()));
49 map.insert(QStringLiteral("longitude"), QString::number(location->longitude()));
50
51 if (location->timestamp() != 0) {
52 map.insert(QStringLiteral("timestampMs"), location->timestamp());
53 }
54 if (location->accuracy() != -1) {
55 map.insert(QStringLiteral("accuracy"), location->accuracy());
56 }
57 if (location->speed() != -1) {
58 map.insert(QStringLiteral("speed"), location->speed());
59 }
60 if (location->heading() != -1) {
61 map.insert(QStringLiteral("heading"), location->heading());
62 }
63
64 map.insert(QStringLiteral("altitude"), location->altitude());
65
66 if (location->altitudeAccuracy() != 0) {
67 map.insert(QStringLiteral("altitudeAccuracy"), location->altitudeAccuracy());
68 }
69
70 output.insert(QStringLiteral("data"), map);
71
73 return document.toJson(QJsonDocument::Compact);
74}
75
77{
78 Q_UNUSED(feedData)
79
80 ObjectsList output;
81
82 QJsonDocument document = QJsonDocument::fromJson(jsonFeed);
83 const QVariantMap map = document.toVariant().toMap();
84 const QVariantMap data = map.value(QStringLiteral("data")).toMap();
85 const QVariantList items = data.value(QStringLiteral("items")).toList();
86 output.reserve(items.size());
87 for (const QVariant &c : items) {
88 QVariantMap location = c.toMap();
89 output << Private::parseLocation(location).dynamicCast<Object>();
90 }
91
92 return output;
93}
94
95LocationPtr Private::parseLocation(const QVariantMap &map)
96{
97 LocationPtr location(new Location);
98
99 if (map.contains(QStringLiteral("timestampMs"))) {
100 location->setTimestamp(map.value(QStringLiteral("timestampMs")).toULongLong());
101 }
102 if (map.contains(QStringLiteral("latitude"))) {
103 location->setLatitude(map.value(QStringLiteral("latitude")).toFloat());
104 }
105 if (map.contains(QStringLiteral("longitude"))) {
106 location->setLongitude(map.value(QStringLiteral("longitude")).toFloat());
107 }
108 if (map.contains(QStringLiteral("accuracy"))) {
109 location->setAccuracy(map.value(QStringLiteral("accuracy")).toInt());
110 }
111 if (map.contains(QStringLiteral("speed"))) {
112 location->setSpeed(map.value(QStringLiteral("speed")).toInt());
113 }
114 if (map.contains(QStringLiteral("heading"))) {
115 location->setHeading(map.value(QStringLiteral("heading")).toInt());
116 }
117 if (map.contains(QStringLiteral("altitude"))) {
118 location->setAltitude(map.value(QStringLiteral("altitude")).toInt());
119 }
120 if (map.contains(QStringLiteral("altitudeAccuracy"))) {
121 location->setAltitudeAccuracy(map.value(QStringLiteral("altitudeAccuracy")).toInt());
122 }
123
124 return location;
125}
126
128{
129 return QStringLiteral("1");
130}
131
132QUrl retrieveCurrentLocationUrl(const Latitude::Granularity granularity)
133{
134 QUrl url(Private::GoogleApisUrl);
135 url.setPath(Private::CurrentLocationBasePath);
136 QUrlQuery query(url);
137 if (granularity == Latitude::City) {
138 query.addQueryItem(QStringLiteral("granularity"), QStringLiteral("city"));
139 } else if (granularity == Latitude::Best) {
140 query.addQueryItem(QStringLiteral("granularity"), QStringLiteral("best"));
141 }
142 url.setQuery(query);
143
144 return url;
145}
146
148{
149 QUrl url(Private::GoogleApisUrl);
150 url.setPath(Private::CurrentLocationBasePath);
151 return url;
152}
153
155{
156 QUrl url(Private::GoogleApisUrl);
157 url.setPath(Private::CurrentLocationBasePath);
158 return url;
159}
160
161QUrl locationHistoryUrl(const Latitude::Granularity granularity, const int maxResults, const qlonglong maxTime, const qlonglong minTime)
162{
163 QUrl url(Private::GoogleApisUrl);
164 url.setPath(Private::LocationBasePath);
165 QUrlQuery query(url);
166 if (granularity == Latitude::City) {
167 query.addQueryItem(QStringLiteral("granularity"), QStringLiteral("city"));
168 } else if (granularity == Latitude::Best) {
169 query.addQueryItem(QStringLiteral("granularity"), QStringLiteral("best"));
170 }
171
172 if (maxResults > 0) {
173 query.addQueryItem(QStringLiteral("max-results"), QString::number(maxResults));
174 }
175
176 if ((maxTime > 0) && (maxTime >= minTime)) {
177 query.addQueryItem(QStringLiteral("max-time"), QString::number(maxTime));
178 }
179
180 if ((minTime > 0) && (minTime <= maxTime)) {
181 query.addQueryItem(QStringLiteral("min-time"), QString::number(minTime));
182 }
183 url.setQuery(query);
184
185 return url;
186}
187
188QUrl retrieveLocationUrl(const qlonglong id, const Latitude::Granularity granularity)
189{
190 QUrl url(Private::GoogleApisUrl);
191 url.setPath(Private::LocationBasePath % QLatin1Char('/') % QString::number(id));
192 QUrlQuery query(url);
193 if (granularity == Latitude::City) {
194 query.addQueryItem(QStringLiteral("granularity"), QStringLiteral("city"));
195 } else if (granularity == Latitude::Best) {
196 query.addQueryItem(QStringLiteral("granularity"), QStringLiteral("best"));
197 }
198 url.setQuery(query);
199
200 return url;
201}
202
204{
205 QUrl url(Private::GoogleApisUrl);
206 url.setPath(Private::LocationBasePath);
207 return url;
208}
209
210QUrl deleteLocationUrl(const qlonglong id)
211{
212 Q_UNUSED(id)
213 QUrl url(Private::GoogleApisUrl);
214 url.setPath(Private::LocationBasePath);
215 return url;
216}
217
218} // namespace LatitudeService
219
220} // namespace KGAPI2
Structure to store additional information about a feed.
Definition types.h:24
Represents a single record about geographical location provided by Google Latitude service.
Base class for all objects.
Definition object.h:31
QString APIVersion()
Supported version of API.
QUrl deleteLocationUrl(const qlonglong id)
Returns URL to delete a specific location.
ObjectsList parseLocationJSONFeed(const QByteArray &jsonFeed, FeedData &feedData)
Parses a JSON feed into list of Locations.
QUrl deleteCurrentLocationUrl()
Returns URL for deleting user's current location.
QUrl retrieveCurrentLocationUrl(const Latitude::Granularity granularity)
Returns URL for fetch current user's location.
QByteArray locationToJSON(const LocationPtr &location)
Serializes a Location object to JSON.
QUrl retrieveLocationUrl(const qlonglong id, const Latitude::Granularity granularity)
URL for fetching a specific location.
QUrl locationHistoryUrl(const Latitude::Granularity granularity, const int maxResults, const qlonglong maxTime, const qlonglong minTime)
Returns URL for fetching history of user's locations.
QUrl insertCurrentLocationUrl()
Returns URL for uploading user's current location.
LocationPtr JSONToLocation(const QByteArray &jsonData)
Parses JSON data into a Location object.
QUrl insertLocationUrl()
Returns URL for uploading a location.
A job to fetch a single map tile described by a StaticMapUrl.
Definition blog.h:16
QVariant location(const QVariant &res)
QJsonDocument fromJson(const QByteArray &json, QJsonParseError *error)
QJsonDocument fromVariant(const QVariant &variant)
bool isNull() const const
QByteArray toJson(JsonFormat format) const const
QVariant toVariant() const const
void reserve(qsizetype size)
QString number(double n, char format, int precision)
QFuture< void > map(Iterator begin, Iterator end, MapFunctor &&function)
void setPath(const QString &path, ParsingMode mode)
void setQuery(const QString &query, ParsingMode mode)
QMap< QString, QVariant > toMap() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Nov 22 2024 12:08:13 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.