1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 The Qt Company Ltd.
4 ** Contact: http://www.qt.io/licensing/
5 **
6 ** This file is part of the QtLocation module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL3$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and The Qt Company. For licensing terms
14 ** and conditions see http://www.qt.io/terms-conditions. For further
15 ** information use the contact form at http://www.qt.io/contact-us.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 3 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPLv3 included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 3 requirements
23 ** will be met: https://www.gnu.org/licenses/lgpl.html.
24 **
25 ** GNU General Public License Usage
26 ** Alternatively, this file may be used under the terms of the GNU
27 ** General Public License version 2.0 or later as published by the Free
28 ** Software Foundation and appearing in the file LICENSE.GPL included in
29 ** the packaging of this file. Please review the following information to
30 ** ensure the GNU General Public License version 2.0 requirements will be
31 ** met: http://www.gnu.org/licenses/gpl-2.0.html.
32 **
33 ** $QT_END_LICENSE$
34 **
35 ****************************************************************************/
36 
37 #include "jsonparserhelpers.h"
38 #include "../qplacemanagerengine_nokiav2.h"
39 
40 #include <QtCore/QCoreApplication>
41 #include <QtCore/QDateTime>
42 #include <QtCore/QJsonObject>
43 #include <QtCore/QJsonArray>
44 #include <QtCore/QVariantMap>
45 #include <QtPositioning/QGeoCoordinate>
46 #include <QtLocation/QPlaceImage>
47 #include <QtLocation/QPlaceReview>
48 #include <QtLocation/QPlaceEditorial>
49 #include <QtLocation/QPlaceUser>
50 #include <QtLocation/QPlaceContactDetail>
51 #include <QtLocation/QPlaceCategory>
52 
53 QT_BEGIN_NAMESPACE
54 
parseCoordinate(const QJsonArray & coordinateArray)55 QGeoCoordinate parseCoordinate(const QJsonArray &coordinateArray)
56 {
57     return QGeoCoordinate(coordinateArray.at(0).toDouble(), coordinateArray.at(1).toDouble());
58 }
59 
parseSupplier(const QJsonObject & supplierObject,const QPlaceManagerEngineNokiaV2 * engine)60 QPlaceSupplier parseSupplier(const QJsonObject &supplierObject,
61                              const QPlaceManagerEngineNokiaV2 *engine)
62 {
63     Q_ASSERT(engine);
64 
65     QPlaceSupplier supplier;
66     supplier.setName(supplierObject.value(QStringLiteral("title")).toString());
67     supplier.setUrl(supplierObject.value(QStringLiteral("href")).toString());
68 
69     supplier.setIcon(engine->icon(supplierObject.value(QStringLiteral("icon")).toString()));
70 
71     return supplier;
72 }
73 
parseCategory(const QJsonObject & categoryObject,const QPlaceManagerEngineNokiaV2 * engine)74 QPlaceCategory parseCategory(const QJsonObject &categoryObject,
75                              const QPlaceManagerEngineNokiaV2 *engine)
76 {
77     Q_ASSERT(engine);
78 
79     QPlaceCategory category;
80 
81     category.setName(categoryObject.value(QStringLiteral("title")).toString());
82 
83     const QUrl href(categoryObject.value(QStringLiteral("href")).toString());
84     const QString hrefPath(href.path());
85     category.setCategoryId(hrefPath.mid(hrefPath.lastIndexOf(QLatin1Char('/')) + 1));
86 
87 
88     category.setIcon(engine->icon(categoryObject.value(QStringLiteral("icon")).toString()));
89     return category;
90 }
91 
parseCategories(const QJsonArray & categoryArray,const QPlaceManagerEngineNokiaV2 * engine)92 QList<QPlaceCategory> parseCategories(const QJsonArray &categoryArray,
93                                      const QPlaceManagerEngineNokiaV2 *engine)
94 {
95     Q_ASSERT(engine);
96 
97     QList<QPlaceCategory> categoryList;
98     for (int i = 0; i < categoryArray.count(); ++i)
99         categoryList.append(parseCategory(categoryArray.at(i).toObject(),
100                                           engine));
101 
102     return categoryList;
103 }
104 
parseContactDetails(const QJsonArray & contacts)105 QList<QPlaceContactDetail> parseContactDetails(const QJsonArray &contacts)
106 {
107     QList<QPlaceContactDetail> contactDetails;
108 
109     for (int i = 0; i < contacts.count(); ++i) {
110         QJsonObject contact = contacts.at(i).toObject();
111 
112         QPlaceContactDetail detail;
113         detail.setLabel(contact.value(QStringLiteral("label")).toString());
114         detail.setValue(contact.value(QStringLiteral("value")).toString());
115 
116         contactDetails.append(detail);
117     }
118 
119     return contactDetails;
120 }
121 
parseImage(const QJsonObject & imageObject,const QPlaceManagerEngineNokiaV2 * engine)122 QPlaceImage parseImage(const QJsonObject &imageObject,
123                        const QPlaceManagerEngineNokiaV2 *engine)
124 {
125     Q_ASSERT(engine);
126 
127     QPlaceImage image;
128 
129     image.setAttribution(imageObject.value(QStringLiteral("attribution")).toString());
130     image.setUrl(imageObject.value(QStringLiteral("src")).toString());
131     image.setSupplier(parseSupplier(imageObject.value(QStringLiteral("supplier")).toObject(),
132                                     engine));
133 
134     return image;
135 }
136 
parseReview(const QJsonObject & reviewObject,const QPlaceManagerEngineNokiaV2 * engine)137 QPlaceReview parseReview(const QJsonObject &reviewObject,
138                          const QPlaceManagerEngineNokiaV2 *engine)
139 {
140     Q_ASSERT(engine);
141 
142     QPlaceReview review;
143 
144     review.setDateTime(QDateTime::fromString(reviewObject.value(QStringLiteral("date")).toString()));
145 
146     if (reviewObject.contains(QStringLiteral("title")))
147         review.setTitle(reviewObject.value(QStringLiteral("title")).toString());
148 
149     if (reviewObject.contains(QStringLiteral("rating")))
150         review.setRating(reviewObject.value(QStringLiteral("rating")).toDouble());
151 
152     review.setText(reviewObject.value(QStringLiteral("description")).toString());
153 
154     QJsonObject userObject = reviewObject.value(QStringLiteral("user")).toObject();
155 
156     QPlaceUser user;
157     user.setUserId(userObject.value(QStringLiteral("id")).toString());
158     user.setName(userObject.value(QStringLiteral("title")).toString());
159     review.setUser(user);
160 
161     review.setAttribution(reviewObject.value(QStringLiteral("attribution")).toString());
162 
163     review.setLanguage(reviewObject.value(QStringLiteral("language")).toString());
164 
165     review.setSupplier(parseSupplier(reviewObject.value(QStringLiteral("supplier")).toObject(),
166                                      engine));
167 
168     //if (reviewObject.contains(QStringLiteral("via"))) {
169     //    QJsonObject viaObject = reviewObject.value(QStringLiteral("via")).toObject();
170     //}
171 
172     return review;
173 }
174 
parseEditorial(const QJsonObject & editorialObject,const QPlaceManagerEngineNokiaV2 * engine)175 QPlaceEditorial parseEditorial(const QJsonObject &editorialObject,
176                                const QPlaceManagerEngineNokiaV2 *engine)
177 {
178     Q_ASSERT(engine);
179 
180     QPlaceEditorial editorial;
181 
182     editorial.setAttribution(editorialObject.value(QStringLiteral("attribution")).toString());
183 
184     //if (editorialObject.contains(QStringLiteral("via"))) {
185     //    QJsonObject viaObject = editorialObject.value(QStringLiteral("via")).toObject();
186     //}
187 
188     editorial.setSupplier(parseSupplier(editorialObject.value(QStringLiteral("supplier")).toObject(),
189                                         engine));
190     editorial.setLanguage(editorialObject.value(QStringLiteral("language")).toString());
191     editorial.setText(editorialObject.value(QStringLiteral("description")).toString());
192 
193     return editorial;
194 }
195 
parseCollection(QPlaceContent::Type type,const QJsonObject & object,QPlaceContent::Collection * collection,int * totalCount,QPlaceContentRequest * previous,QPlaceContentRequest * next,const QPlaceManagerEngineNokiaV2 * engine)196 void parseCollection(QPlaceContent::Type type, const QJsonObject &object,
197                      QPlaceContent::Collection *collection, int *totalCount,
198                      QPlaceContentRequest *previous, QPlaceContentRequest *next,
199                      const QPlaceManagerEngineNokiaV2 *engine)
200 {
201     Q_ASSERT(engine);
202 
203     if (totalCount)
204         *totalCount = object.value(QStringLiteral("available")).toDouble();
205 
206     int offset = 0;
207     if (object.contains(QStringLiteral("offset")))
208         offset = object.value(QStringLiteral("offset")).toDouble();
209 
210     if (previous && object.contains(QStringLiteral("previous"))) {
211         previous->setContentType(type);
212         previous->setContentContext(QUrl(object.value(QStringLiteral("previous")).toString()));
213     }
214 
215     if (next && object.contains(QStringLiteral("next"))) {
216         next->setContentType(type);
217         next->setContentContext(QUrl(object.value(QStringLiteral("next")).toString()));
218     }
219 
220     if (collection) {
221         QJsonArray items = object.value(QStringLiteral("items")).toArray();
222         for (int i = 0; i < items.count(); ++i) {
223             QJsonObject itemObject = items.at(i).toObject();
224 
225             switch (type) {
226             case QPlaceContent::ImageType:
227                 collection->insert(offset + i, parseImage(itemObject, engine));
228                 break;
229             case QPlaceContent::ReviewType:
230                 collection->insert(offset + i, parseReview(itemObject, engine));
231                 break;
232             case QPlaceContent::EditorialType:
233                 collection->insert(offset + i, parseEditorial(itemObject, engine));
234                 break;
235             case QPlaceContent::NoType:
236             default:
237                 break;
238             }
239         }
240     }
241 }
242 
243 QT_END_NAMESPACE
244