summaryrefslogtreecommitdiff
path: root/src/plugins/geoservices/nokia/placesv2/jsonparserhelpers.cpp
blob: 8e6858fea54397acf8162bc81d4d8ff1fcf85cf0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/****************************************************************************
**
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the QtLocation module of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 3 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL3 included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 3 requirements
** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 2.0 or (at your option) the GNU General
** Public license version 3 or any later version approved by the KDE Free
** Qt Foundation. The licenses are as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-2.0.html and
** https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include "jsonparserhelpers.h"
#include "../qplacemanagerengine_nokiav2.h"

#include <QtCore/QCoreApplication>
#include <QtCore/QDateTime>
#include <QtCore/QJsonObject>
#include <QtCore/QJsonArray>
#include <QtCore/QVariantMap>
#include <QtPositioning/QGeoCoordinate>
#include <QtLocation/QPlaceContentRequest>
#include <QtLocation/QPlaceIcon>
#include <QtLocation/QPlaceSupplier>
#include <QtLocation/QPlaceContent>
#include <QtLocation/QPlaceUser>
#include <QtLocation/QPlaceContactDetail>
#include <QtLocation/QPlaceCategory>

QT_BEGIN_NAMESPACE

QGeoCoordinate parseCoordinate(const QJsonArray &coordinateArray)
{
    return QGeoCoordinate(coordinateArray.at(0).toDouble(), coordinateArray.at(1).toDouble());
}

QPlaceSupplier parseSupplier(const QJsonObject &supplierObject,
                             const QPlaceManagerEngineNokiaV2 *engine)
{
    Q_ASSERT(engine);

    QPlaceSupplier supplier;
    supplier.setName(supplierObject.value(QStringLiteral("title")).toString());
    supplier.setUrl(supplierObject.value(QStringLiteral("href")).toString());

    supplier.setIcon(engine->icon(supplierObject.value(QStringLiteral("icon")).toString()));

    return supplier;
}

QPlaceCategory parseCategory(const QJsonObject &categoryObject,
                             const QPlaceManagerEngineNokiaV2 *engine)
{
    Q_ASSERT(engine);

    QPlaceCategory category;

    category.setName(categoryObject.value(QStringLiteral("title")).toString());

    const QUrl href(categoryObject.value(QStringLiteral("href")).toString());
    const QString hrefPath(href.path());
    category.setCategoryId(hrefPath.mid(hrefPath.lastIndexOf(QLatin1Char('/')) + 1));


    category.setIcon(engine->icon(categoryObject.value(QStringLiteral("icon")).toString()));
    return category;
}

QList<QPlaceCategory> parseCategories(const QJsonArray &categoryArray,
                                     const QPlaceManagerEngineNokiaV2 *engine)
{
    Q_ASSERT(engine);

    QList<QPlaceCategory> categoryList;
    for (int i = 0; i < categoryArray.count(); ++i)
        categoryList.append(parseCategory(categoryArray.at(i).toObject(),
                                          engine));

    return categoryList;
}

QList<QPlaceContactDetail> parseContactDetails(const QJsonArray &contacts)
{
    QList<QPlaceContactDetail> contactDetails;

    for (int i = 0; i < contacts.count(); ++i) {
        QJsonObject contact = contacts.at(i).toObject();

        QPlaceContactDetail detail;
        detail.setLabel(contact.value(QStringLiteral("label")).toString());
        detail.setValue(contact.value(QStringLiteral("value")).toString());

        contactDetails.append(detail);
    }

    return contactDetails;
}

QPlaceContent parseImage(const QJsonObject &imageObject,
                         const QPlaceManagerEngineNokiaV2 *engine)
{
    Q_ASSERT(engine);

    QPlaceContent image(QPlaceContent::ImageType);

    image.setValue(QPlaceContent::ContentAttribution, imageObject.value(
                   QStringLiteral("attribution")).toString());
    image.setValue(QPlaceContent::ImageUrl, imageObject.value(
                   QStringLiteral("src")).toString());
    image.setValue(QPlaceContent::ContentSupplier, QVariant::fromValue(parseSupplier(
                   imageObject.value(QStringLiteral("supplier")).toObject(), engine)));

    return image;
}

QPlaceContent parseReview(const QJsonObject &reviewObject,
                          const QPlaceManagerEngineNokiaV2 *engine)
{
    Q_ASSERT(engine);

    QPlaceContent review(QPlaceContent::ReviewType);

    review.setValue(QPlaceContent::ReviewDateTime, QDateTime::fromString(
                    reviewObject.value(QStringLiteral("date")).toString()));

    if (reviewObject.contains(QStringLiteral("title"))) {
        review.setValue(QPlaceContent::ReviewTitle, reviewObject.value(
                        QStringLiteral("title")).toString());
    }

    if (reviewObject.contains(QStringLiteral("rating"))) {
        review.setValue(QPlaceContent::ReviewRating, reviewObject.value(
                        QStringLiteral("rating")).toDouble());
    }

    review.setValue(QPlaceContent::ReviewText, reviewObject.value(
                    QStringLiteral("description")).toString());

    QJsonObject userObject = reviewObject.value(QStringLiteral("user")).toObject();

    QPlaceUser user;
    user.setUserId(userObject.value(QStringLiteral("id")).toString());
    user.setName(userObject.value(QStringLiteral("title")).toString());
    review.setValue(QPlaceContent::ContentUser, QVariant::fromValue(user));

    review.setValue(QPlaceContent::ContentAttribution, reviewObject.value(
                    QStringLiteral("attribution")).toString());

    review.setValue(QPlaceContent::ReviewLanguage, reviewObject.value(
                    QStringLiteral("language")).toString());

    review.setValue(QPlaceContent::ContentSupplier, QVariant::fromValue(parseSupplier(
                    reviewObject.value(QStringLiteral("supplier")).toObject(), engine)));

    //if (reviewObject.contains(QStringLiteral("via"))) {
    //    QJsonObject viaObject = reviewObject.value(QStringLiteral("via")).toObject();
    //}

    return review;
}

QPlaceContent parseEditorial(const QJsonObject &editorialObject,
                             const QPlaceManagerEngineNokiaV2 *engine)
{
    Q_ASSERT(engine);

    QPlaceContent editorial(QPlaceContent::EditorialType);

    editorial.setValue(QPlaceContent::ContentAttribution, editorialObject.value(QStringLiteral("attribution")).toString());

    //if (editorialObject.contains(QStringLiteral("via"))) {
    //    QJsonObject viaObject = editorialObject.value(QStringLiteral("via")).toObject();
    //}

    editorial.setValue(QPlaceContent::ContentSupplier, QVariant::fromValue(parseSupplier(
                       editorialObject.value(QStringLiteral("supplier")).toObject(), engine)));
    editorial.setValue(QPlaceContent::EditorialLanguage, editorialObject.value(
                       QStringLiteral("language")).toString());
    editorial.setValue(QPlaceContent::EditorialText, editorialObject.value(
                       QStringLiteral("description")).toString());

    return editorial;
}

void parseCollection(QPlaceContent::Type type, const QJsonObject &object,
                     QPlaceContent::Collection *collection, int *totalCount,
                     QPlaceContentRequest *previous, QPlaceContentRequest *next,
                     const QPlaceManagerEngineNokiaV2 *engine)
{
    Q_ASSERT(engine);

    if (totalCount)
        *totalCount = object.value(QStringLiteral("available")).toDouble();

    int offset = 0;
    if (object.contains(QStringLiteral("offset")))
        offset = object.value(QStringLiteral("offset")).toDouble();

    if (previous && object.contains(QStringLiteral("previous"))) {
        previous->setContentType(type);
        previous->setContentContext(QUrl(object.value(QStringLiteral("previous")).toString()));
    }

    if (next && object.contains(QStringLiteral("next"))) {
        next->setContentType(type);
        next->setContentContext(QUrl(object.value(QStringLiteral("next")).toString()));
    }

    if (collection) {
        QJsonArray items = object.value(QStringLiteral("items")).toArray();
        for (int i = 0; i < items.count(); ++i) {
            QJsonObject itemObject = items.at(i).toObject();

            switch (type) {
            case QPlaceContent::ImageType:
                collection->insert(offset + i, parseImage(itemObject, engine));
                break;
            case QPlaceContent::ReviewType:
                collection->insert(offset + i, parseReview(itemObject, engine));
                break;
            case QPlaceContent::EditorialType:
                collection->insert(offset + i, parseEditorial(itemObject, engine));
                break;
            case QPlaceContent::NoType:
            default:
                break;
            }
        }
    }
}

QT_END_NAMESPACE