latitudeservice.cpp 7.17 KB
Newer Older
1 2 3
/*
    Copyright (C) 2012  Jan Grulich <grulja@gmail.com>

4 5 6 7 8 9 10 11 12
    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) version 3, or any
    later version accepted by the membership of KDE e.V. (or its
    successor approved by the membership of KDE e.V.), which shall
    act as a proxy defined in Section 6 of version 3 of the license.

    This library is distributed in the hope that it will be useful,
13 14
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
    GNU Lesser General Public License for more details.
16

17 18
    You should have received a copy of the GNU Lesser General Public
    License along with this library.  If not, see <http://www.gnu.org/licenses/>.
19 20 21 22 23 24 25
*/


#include "latitudeservice.h"
#include "location.h"


26
#include <QJsonDocument>
27 28 29 30 31 32 33

namespace KGAPI2 {

namespace LatitudeService {

namespace Private
{
34
    LocationPtr parseLocation(const QVariantMap &map);
Daniel Vrátil's avatar
Daniel Vrátil committed
35

Laurent Montel's avatar
Laurent Montel committed
36 37 38
    static const QUrl GoogleApisUrl(QStringLiteral("https://www.googleapis.com"));
    static const QString LocationBasePath(QStringLiteral("/latitude/v1/location"));
    static const QString CurrentLocationBasePath(QStringLiteral("/latitude/v1/currentLocation"));
39 40 41 42
}

LocationPtr JSONToLocation(const QByteArray & jsonData)
{
43 44
    QJsonDocument document = QJsonDocument::fromJson(jsonData);
    if (document.isNull()) {
45 46
        return LocationPtr();
    }
47
    const QVariantMap data = document.toVariant().toMap();
Laurent Montel's avatar
Laurent Montel committed
48
    const QVariantMap info = data.value(QStringLiteral("data")).toMap();
49 50 51 52 53 54 55 56

    return Private::parseLocation(info);
}

QByteArray locationToJSON(const LocationPtr &location)
{
    QVariantMap map, output;

Laurent Montel's avatar
Laurent Montel committed
57 58 59
    map.insert(QStringLiteral("kind"), QStringLiteral("latitude#location"));
    map.insert(QStringLiteral("latitude"), QString::number(location->latitude()));
    map.insert(QStringLiteral("longitude"), QString::number(location->longitude()));
60 61

    if (location->timestamp() != 0) {
Laurent Montel's avatar
Laurent Montel committed
62
        map.insert(QStringLiteral("timestampMs"), location->timestamp());
63 64
    }
    if (location->accuracy() != -1) {
Laurent Montel's avatar
Laurent Montel committed
65
        map.insert(QStringLiteral("accuracy"), location->accuracy());
66 67
    }
    if (location->speed() != -1) {
Laurent Montel's avatar
Laurent Montel committed
68
        map.insert(QStringLiteral("speed"), location->speed());
69 70
    }
    if (location->heading() != -1) {
Laurent Montel's avatar
Laurent Montel committed
71
        map.insert(QStringLiteral("heading"), location->heading());
72 73
    }

Laurent Montel's avatar
Laurent Montel committed
74
    map.insert(QStringLiteral("altitude"), location->altitude());
75 76

    if (location->altitudeAccuracy() != 0) {
Laurent Montel's avatar
Laurent Montel committed
77
        map.insert(QStringLiteral("altitudeAccuracy"), location->altitudeAccuracy());
78 79
    }

Laurent Montel's avatar
Laurent Montel committed
80
    output.insert(QStringLiteral("data"), map);
81

82
    QJsonDocument document = QJsonDocument::fromVariant(output);
83
    return document.toJson(QJsonDocument::Compact);
84 85 86 87 88 89 90 91
}

ObjectsList parseLocationJSONFeed(const QByteArray & jsonFeed, FeedData & feedData)
{
    Q_UNUSED(feedData);

    ObjectsList output;

92 93
    QJsonDocument document = QJsonDocument::fromJson(jsonFeed);
    const QVariantMap map = document.toVariant().toMap();
Laurent Montel's avatar
Laurent Montel committed
94 95
    const QVariantMap data = map.value(QStringLiteral("data")).toMap();
    const QVariantList items = data.value(QStringLiteral("items")).toList();
96
    output.reserve(items.size());
Laurent Montel's avatar
Laurent Montel committed
97
    for (const QVariant &c : items) {
98 99 100 101 102 103 104
        QVariantMap location = c.toMap();
        output << Private::parseLocation(location).dynamicCast<Object>();
    }

    return output;
}

105
LocationPtr Private::parseLocation(const QVariantMap &map)
106 107 108
{
    LocationPtr location(new Location);

Laurent Montel's avatar
Laurent Montel committed
109 110
    if (map.contains(QStringLiteral("timestampMs"))) {
        location->setTimestamp(map.value(QStringLiteral("timestampMs")).toULongLong());
111
    }
Laurent Montel's avatar
Laurent Montel committed
112 113
    if (map.contains(QStringLiteral("latitude"))) {
        location->setLatitude(map.value(QStringLiteral("latitude")).toFloat());
114
    }
Laurent Montel's avatar
Laurent Montel committed
115 116
    if (map.contains(QStringLiteral("longitude"))) {
        location->setLongitude(map.value(QStringLiteral("longitude")).toFloat());
117
    }
Laurent Montel's avatar
Laurent Montel committed
118 119
    if (map.contains(QStringLiteral("accuracy"))) {
        location->setAccuracy(map.value(QStringLiteral("accuracy")).toInt());
120
    }
Laurent Montel's avatar
Laurent Montel committed
121 122
    if (map.contains(QStringLiteral("speed"))) {
        location->setSpeed(map.value(QStringLiteral("speed")).toInt());
123
    }
Laurent Montel's avatar
Laurent Montel committed
124 125
    if (map.contains(QStringLiteral("heading"))) {
        location->setHeading(map.value(QStringLiteral("heading")).toInt());
126
    }
Laurent Montel's avatar
Laurent Montel committed
127 128
    if (map.contains(QStringLiteral("altitude"))) {
        location->setAltitude(map.value(QStringLiteral("altitude")).toInt());
129
    }
Laurent Montel's avatar
Laurent Montel committed
130 131
    if (map.contains(QStringLiteral("altitudeAccuracy"))) {
        location->setAltitudeAccuracy(map.value(QStringLiteral("altitudeAccuracy")).toInt());
132 133 134 135 136 137 138 139
    }

    return location;
}


QString APIVersion()
{
Laurent Montel's avatar
Laurent Montel committed
140
    return QStringLiteral("1");
141 142 143 144
}

QUrl retrieveCurrentLocationUrl(const Latitude::Granularity granularity)
{
Daniel Vrátil's avatar
Daniel Vrátil committed
145 146
    QUrl url(Private::GoogleApisUrl);
    url.setPath(Private::CurrentLocationBasePath);
147
    if (granularity == Latitude::City) {
Laurent Montel's avatar
Laurent Montel committed
148
        url.addQueryItem(QStringLiteral("granularity"), QStringLiteral("city"));
149
    } else if (granularity == Latitude::Best) {
Laurent Montel's avatar
Laurent Montel committed
150
        url.addQueryItem(QStringLiteral("granularity"), QStringLiteral("best"));
151 152
    }

Daniel Vrátil's avatar
Daniel Vrátil committed
153
    return url;
154 155 156 157
}

QUrl deleteCurrentLocationUrl()
{
Daniel Vrátil's avatar
Daniel Vrátil committed
158 159 160
    QUrl url(Private::GoogleApisUrl);
    url.setPath(Private::CurrentLocationBasePath);
    return url;
161 162 163 164
}

QUrl insertCurrentLocationUrl()
{
Daniel Vrátil's avatar
Daniel Vrátil committed
165 166 167
    QUrl url(Private::GoogleApisUrl);
    url.setPath(Private::CurrentLocationBasePath);
    return url;
168 169 170 171 172
}

QUrl locationHistoryUrl(const Latitude::Granularity granularity, const int maxResults,
                        const qlonglong maxTime, const qlonglong minTime)
{
Daniel Vrátil's avatar
Daniel Vrátil committed
173 174
    QUrl url(Private::GoogleApisUrl);
    url.setPath(Private::LocationBasePath);
175 176

    if (granularity == Latitude::City) {
Laurent Montel's avatar
Laurent Montel committed
177
        url.addQueryItem(QStringLiteral("granularity"), QStringLiteral("city"));
178
    } else if (granularity == Latitude::Best) {
Laurent Montel's avatar
Laurent Montel committed
179
        url.addQueryItem(QStringLiteral("granularity"), QStringLiteral("best"));
180 181 182
    }

    if (maxResults > 0) {
Laurent Montel's avatar
Laurent Montel committed
183
        url.addQueryItem(QStringLiteral("max-results"), QString::number(maxResults));
184 185 186
    }

    if ((maxTime > 0) && (maxTime >= minTime)) {
Laurent Montel's avatar
Laurent Montel committed
187
        url.addQueryItem(QStringLiteral("max-time"), QString::number(maxTime));
188 189 190
    }

    if ((minTime > 0) && (minTime <= maxTime)) {
Laurent Montel's avatar
Laurent Montel committed
191
        url.addQueryItem(QStringLiteral("min-time"), QString::number(minTime));
192 193 194 195 196 197 198
    }

    return url;
}

QUrl retrieveLocationUrl(const qlonglong id, const Latitude::Granularity granularity)
{
Daniel Vrátil's avatar
Daniel Vrátil committed
199 200
    QUrl url(Private::GoogleApisUrl);
    url.setPath(Private::LocationBasePath % QLatin1Char('/') % QString::number(id));
201 202

     if (granularity == Latitude::City) {
Laurent Montel's avatar
Laurent Montel committed
203
        url.addQueryItem(QStringLiteral("granularity"), QStringLiteral("city"));
204
     } else if (granularity == Latitude::Best) {
Laurent Montel's avatar
Laurent Montel committed
205
        url.addQueryItem(QStringLiteral("granularity"), QStringLiteral("best"));
206 207 208 209 210 211 212
     }

    return url;
}

QUrl insertLocationUrl()
{
Daniel Vrátil's avatar
Daniel Vrátil committed
213 214 215
    QUrl url(Private::GoogleApisUrl);
    url.setPath(Private::LocationBasePath);
    return url;
216 217 218 219
}

QUrl deleteLocationUrl(const qlonglong id)
{
220
    Q_UNUSED(id);
Daniel Vrátil's avatar
Daniel Vrátil committed
221 222
    QUrl url(Private::GoogleApisUrl);
    url.setPath(Private::LocationBasePath);
223 224 225 226 227 228
    return url;
}

} // namespace LatitudeService

} // namespace KGAPI2