Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
Loading...
Searching...
No Matches
qplacedetailsreplyimpl.cpp
Go to the documentation of this file.
1// Copyright (C) 2015 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
5#include "jsonparserhelpers.h"
8
9#include <QCoreApplication>
10#include <QtCore/QJsonDocument>
11#include <QtCore/QJsonObject>
12#include <QtCore/QJsonArray>
13#include <QtNetwork/QNetworkReply>
14#include <QtPositioning/QGeoAddress>
15#include <QtPositioning/QGeoCoordinate>
16#include <QtPositioning/QGeoLocation>
17#include <QtPositioning/QGeoRectangle>
18#include <QtLocation/QPlace>
19#include <QtLocation/QPlaceAttribute>
20#include <QtLocation/QPlaceContactDetail>
21#include <QtLocation/QPlaceManager>
22#include <QtLocation/QPlaceSupplier>
23#include <QtLocation/QPlaceIcon>
24#include <QtLocation/QPlaceRatings>
25#include <QtLocation/QPlaceUser>
26
28
29// These countries format the street address as: {house number} {street name}
30// All other countries format it as: {street name} {house number}
31static const char COUNTRY_TABLE_string[] =
32 "CAN\0"
33 "NZL\0"
34 "GBR\0"
35 "AUS\0"
36 "LKA\0"
37 "USA\0"
38 "SGP\0"
39 "FRA\0"
40 "BHS\0"
41 "CHN\0"
42 "IND\0"
43 "IRL\0"
44 "ARE\0"
45 "\0";
46
47static const int COUNTRY_TABLE_indices[] = {
48 0, 4, 8, 12, 16, 20, 24, 28,
49 32, 36, 40, 44, 48, -1
50};
51
52static bool countryTableContains(const QString &countryCode)
53{
54 for (int i = 0; COUNTRY_TABLE_indices[i] != -1; ++i) {
56 return true;
57 }
58
59 return false;
60}
61
64: QPlaceDetailsReply(parent), m_engine(parent)
65{
66 if (!reply) {
67 setError(UnknownError, QStringLiteral("Null reply"));
68 return;
69 }
71 this, &QPlaceDetailsReplyImpl::replyFinished);
73 this, &QPlaceDetailsReplyImpl::replyError);
76}
77
81
82void QPlaceDetailsReplyImpl::setError(QPlaceReply::Error error_, const QString &errorString)
83{
86 setFinished(true);
87 emit finished();
88}
89
90void QPlaceDetailsReplyImpl::replyFinished()
91{
92 QNetworkReply *reply = static_cast<QNetworkReply *>(sender());
94
96 return;
97
99 if (!document.isObject()) {
101 return;
102 }
103
104 QJsonObject object = document.object();
105
107
108 place.setPlaceId(object.value(QLatin1String("placeId")).toString());
109
110 //const QUrl view = object.value(QLatin1String("view")).toString();
111
112 place.setName(object.value(QLatin1String("name")).toString());
113
114 //if (object.contains(QLatin1String("distance")))
115 // double distance = object.value(QLatin1String("distance")).toDouble();
116
117 //if (object.contains(QLatin1String("alternativeNames"))) {
118 // QJsonArray alternativeNames = object.value(QLatin1String("alternativeNames")).toArray();
119 //}
120
122
123 QJsonObject locationObject = object.value(QLatin1String("location")).toObject();
124
125 //if (locationObject.contains(QLatin1String("locationId")))
126 // const QString locationId = locationObject.value(QLatin1String("locationId")).toString();
127
128 QJsonArray position = locationObject.value(QLatin1String("position")).toArray();
129 location.setCoordinate(QGeoCoordinate(position.at(0).toDouble(), position.at(1).toDouble()));
130
132
133 QJsonObject addressObject = locationObject.value(QLatin1String("address")).toObject();
134
135 address.setText(addressObject.value(QLatin1String("text")).toString());
136
137 address.setCountry(addressObject.value(QLatin1String("country")).toString());
138 address.setCountryCode(addressObject.value(QLatin1String("countryCode")).toString());
139
140 QString house;
141 QString street;
142
143 if (addressObject.contains(QLatin1String("house")))
144 house = addressObject.value(QLatin1String("house")).toString();
145 if (addressObject.contains(QLatin1String("street")))
146 street = addressObject.value(QLatin1String("street")).toString();
147
148 if (countryTableContains(address.countryCode())) {
149 if (!house.isEmpty() && !street.startsWith(house))
150 street = house + QLatin1Char(' ') + street;
151 } else {
152 if (!house.isEmpty() && !street.endsWith(house))
153 street += QLatin1Char(' ') + house;
154 }
155
156 address.setStreet(street);
157
158 if (addressObject.contains(QLatin1String("city")))
159 address.setCity(addressObject.value(QLatin1String("city")).toString());
160 if (addressObject.contains(QLatin1String("district")))
161 address.setDistrict(addressObject.value(QLatin1String("district")).toString());
162 if (addressObject.contains(QLatin1String("state")))
163 address.setState(addressObject.value(QLatin1String("state")).toString());
164 if (addressObject.contains(QLatin1String("county")))
165 address.setCounty(addressObject.value(QLatin1String("county")).toString());
166 if (addressObject.contains(QLatin1String("postalCode")))
167 address.setPostalCode(addressObject.value(QLatin1String("postalCode")).toString());
168
169 location.setAddress(address);
170
171 if (locationObject.contains(QLatin1String("bbox"))) {
172 QJsonArray bbox = locationObject.value(QLatin1String("bbox")).toArray();
173 QGeoRectangle box(QGeoCoordinate(bbox.at(3).toDouble(), bbox.at(0).toDouble()),
174 QGeoCoordinate(bbox.at(1).toDouble(), bbox.at(2).toDouble()));
175 location.setBoundingShape(box);
176 }
177
179
180 place.setCategories(parseCategories(object.value(QLatin1String("categories")).toArray(),
181 m_engine));
182
183 place.setIcon(m_engine->icon(object.value(QLatin1String("icon")).toString(),
184 place.categories()));
185
186 if (object.contains(QLatin1String("contacts"))) {
187 QJsonObject contactsObject = object.value(QLatin1String("contacts")).toObject();
188
189 if (contactsObject.contains(QLatin1String("phone"))) {
191 parseContactDetails(contactsObject.value(QLatin1String("phone")).toArray()));
192 }
193 if (contactsObject.contains(QLatin1String("fax"))) {
195 parseContactDetails(contactsObject.value(QLatin1String("fax")).toArray()));
196 }
197 if (contactsObject.contains(QLatin1String("website"))) {
199 parseContactDetails(contactsObject.value(QLatin1String("website")).toArray()));
200 }
201 if (contactsObject.contains(QLatin1String("email"))) {
203 parseContactDetails(contactsObject.value(QLatin1String("email")).toArray()));
204 }
205 }
206
207 //if (object.contains(QLatin1String("verifiedByOwner")))
208 // bool verifiedByOwner = object.value(QLatin1String("verifiedByOwner")).toBool();
209
210 if (object.contains(QLatin1String("attribution")))
211 place.setAttribution(object.value(QLatin1String("attribution")).toString());
212
213 if (object.contains(QLatin1String("supplier"))) {
214 place.setSupplier(parseSupplier(object.value(QLatin1String("supplier")).toObject(),
215 m_engine));
216 }
217
218 if (object.contains(QLatin1String("ratings"))) {
219 QJsonObject ratingsObject = object.value(QLatin1String("ratings")).toObject();
220
221 QPlaceRatings ratings;
222 ratings.setAverage(ratingsObject.value(QLatin1String("average")).toDouble());
223 ratings.setCount(ratingsObject.value(QLatin1String("count")).toDouble());
224 ratings.setMaximum(5.0);
225
226 place.setRatings(ratings);
227 }
228
229 if (object.contains(QLatin1String("extended"))) {
230 QJsonObject extendedObject = object.value(QLatin1String("extended")).toObject();
231
232 for (auto it = extendedObject.constBegin(), end = extendedObject.constEnd(); it != end; ++it) {
233 QJsonObject attributeObject = it.value().toObject();
234
236
237 attribute.setLabel(attributeObject.value(QLatin1String("label")).toString());
238 attribute.setText(attributeObject.value(QLatin1String("text")).toString());
239
240 QString key = it.key();
241 if (key == QLatin1String("payment"))
243 else if (key == QLatin1String("openingHours"))
245 else
247 }
248 }
249
250 if (object.contains(QLatin1String("media"))) {
251 QJsonObject mediaObject = object.value(QLatin1String("media")).toObject();
252
253 if (mediaObject.contains(QLatin1String("images"))) {
254 QPlaceContent::Collection collection;
255 int totalCount = 0;
256
258 mediaObject.value(QLatin1String("images")).toObject(),
259 &collection, &totalCount, 0, 0, m_engine);
260
263 }
264 if (mediaObject.contains(QLatin1String("editorials"))) {
265 QPlaceContent::Collection collection;
266 int totalCount = 0;
267
269 mediaObject.value(QLatin1String("editorials")).toObject(),
270 &collection, &totalCount, 0, 0, m_engine);
271
274 }
275 if (mediaObject.contains(QLatin1String("reviews"))) {
276 QPlaceContent::Collection collection;
277 int totalCount = 0;
278
280 mediaObject.value(QLatin1String("reviews")).toObject(),
281 &collection, &totalCount, 0, 0, m_engine);
282
285 }
286 }
287
288 //if (object.contains(QLatin1String("related"))) {
289 // QJsonObject relatedObject = object.value(QLatin1String("related")).toObject();
290 //}
291
292 QPlaceAttribute provider;
293 provider.setText(QLatin1String("here"));
295
299
300 setFinished(true);
301 emit finished();
302}
303
304void QPlaceDetailsReplyImpl::replyError(QNetworkReply::NetworkError error)
305{
306 QNetworkReply *reply = static_cast<QNetworkReply *>(sender());
309 setError(QPlaceReply::CancelError, QStringLiteral("Request cancelled"));
312 QString::fromLatin1("The id, %1, does not reference an existing place")
313 .arg(m_placeId));
314 } else {
316 }
317}
318
static QString translate(const char *context, const char *key, const char *disambiguation=nullptr, int n=-1)
\threadsafe
\inmodule QtPositioning
Definition qgeoaddress.h:18
\inmodule QtPositioning
\inmodule QtPositioning
\inmodule QtPositioning
QByteArray readAll()
Reads all remaining data from the device, and returns it as a byte array.
QString errorString() const
Returns a human-readable description of the last device error that occurred.
\inmodule QtCore\reentrant
Definition qjsonarray.h:18
\inmodule QtCore\reentrant
static QJsonDocument fromJson(const QByteArray &json, QJsonParseError *error=nullptr)
Parses json as a UTF-8 encoded JSON document, and creates a QJsonDocument from it.
\inmodule QtCore\reentrant
Definition qjsonobject.h:20
QJsonValue value(const QString &key) const
Returns a QJsonValue representing the value for the key key.
QJsonObject toObject() const
This is an overloaded member function, provided for convenience. It differs from the above function o...
The QNetworkReply class contains the data and headers for a request sent with QNetworkAccessManager.
void errorOccurred(QNetworkReply::NetworkError)
NetworkError error() const
Returns the error that was found during the processing of this request.
virtual void abort()=0
Aborts the operation immediately and close down any network connections still open.
NetworkError
Indicates all possible error conditions found during the processing of the request.
void finished()
This signal is emitted when the reply has finished processing.
static QMetaObject::Connection connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
\threadsafe
Definition qobject.cpp:2960
QObject * sender() const
Returns a pointer to the object that sent the signal, if called in a slot activated by a signal; othe...
Definition qobject.cpp:2658
void destroyed(QObject *=nullptr)
This signal is emitted immediately before the object obj is destroyed, after any instances of QPointe...
void deleteLater()
\threadsafe
Definition qobject.cpp:2435
\inmodule QtLocation
static const QString OpeningHours
\qmltype ExtendedAttributes \instantiates QQmlPropertyMap \inqmlmodule QtLocation
void setText(const QString &text)
static const QString Provider
\variable QPlaceAttribute::Provider The constant to specify an attribute that defines which provider ...
static const QString Payment
\variable QPlaceAttribute::Payment The constant to specify an attribute that defines the methods of p...
static const QString Fax
\variable QPlaceContactDetail::Fax The constant used to specify fax contact details.
static const QString Website
\variable QPlaceContactDetail::Website The constant used to specify website contact details.
static const QString Phone
\qmlvaluetype contactDetail \inqmlmodule QtLocation
static const QString Email
\variable QPlaceContactDetail::Email The constant to specify email contact details.
QPlaceDetailsReplyImpl(QNetworkReply *reply, QPlaceManagerEngineNokiaV2 *parent)
\inmodule QtLocation
QPlace place() const
Returns the place that was fetched.
void setPlace(const QPlace &place)
Sets the fetched place of the reply.
QPlaceIcon icon(const QString &remotePath, const QList< QPlaceCategory > &categories=QList< QPlaceCategory >()) const
\inmodule QtLocation
void setCount(int count)
void setMaximum(qreal max)
void setAverage(qreal average)
QPlaceReply::Error error() const
Returns the error code.
void errorOccurred(QPlaceReply::Error error, const QString &errorString=QString())
This signal is emitted when an error has been detected in the processing of this reply.
void finished()
This signal is emitted when this reply has finished processing.
Error
Describes an error which occurred during an operation.
Definition qplacereply.h:18
@ PlaceDoesNotExistError
Definition qplacereply.h:20
void aborted()
QString errorString() const
Returns the error string of the reply.
void setError(QPlaceReply::Error error, const QString &errorString)
Sets the error and errorString of the reply.
void setFinished(bool finished)
Sets the status of whether the reply is finished or not.
\inmodule QtLocation
Definition qplace.h:25
void setVisibility(QLocation::Visibility visibility)
Sets the visibility of the place to visibility.
Definition qplace.cpp:528
void setDetailsFetched(bool fetched)
Sets whether the details of this place have been fetched or not.
Definition qplace.cpp:418
void setIcon(const QPlaceIcon &icon)
Sets the icon of the place.
Definition qplace.cpp:347
void setContactDetails(const QString &contactType, QList< QPlaceContactDetail > details)
Sets the contact details of a specified contactType.
Definition qplace.cpp:495
void setTotalContentCount(QPlaceContent::Type type, int total)
Sets the totalCount of content objects of the given type.
Definition qplace.cpp:280
void setPlaceId(const QString &identifier)
Sets the identifier of the place.
Definition qplace.cpp:314
void setAttribution(const QString &attribution)
Sets the attribution string of the place.
Definition qplace.cpp:331
void setRatings(const QPlaceRatings &ratings)
Sets the aggregated rating of the place.
Definition qplace.cpp:213
void setContent(QPlaceContent::Type type, const QPlaceContent::Collection &content)
Sets a collection of content for the given type.
Definition qplace.cpp:249
QList< QPlaceCategory > categories() const
Returns categories that this place belongs to.
Definition qplace.cpp:164
void setLocation(const QGeoLocation &location)
Sets the location of the place.
Definition qplace.cpp:197
void setCategories(const QList< QPlaceCategory > &categories)
Sets the categories that this place belongs to.
Definition qplace.cpp:181
void setExtendedAttribute(const QString &attributeType, const QPlaceAttribute &attribute)
Assigns an attribute of the given attributeType to a place.
Definition qplace.cpp:448
void setSupplier(const QPlaceSupplier &supplier)
Sets the supplier of this place to supplier.
Definition qplace.cpp:229
void setName(const QString &name)
Sets the name of the place.
Definition qplace.cpp:296
const_iterator constBegin() const noexcept
Definition qset.h:139
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
bool startsWith(const QString &s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the string starts with s; otherwise returns false.
Definition qstring.cpp:5455
static QString fromLatin1(QByteArrayView ba)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qstring.cpp:5871
bool endsWith(const QString &s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the string ends with s; otherwise returns false.
Definition qstring.cpp:5506
QSet< QString >::iterator it
QList< QPlaceContactDetail > parseContactDetails(const QJsonArray &contacts)
QList< QPlaceCategory > parseCategories(const QJsonArray &categoryArray, const QPlaceManagerEngineNokiaV2 *engine)
QPlaceSupplier parseSupplier(const QJsonObject &supplierObject, const QPlaceManagerEngineNokiaV2 *engine)
void parseCollection(QPlaceContent::Type type, const QJsonObject &object, QPlaceContent::Collection *collection, int *totalCount, QPlaceContentRequest *previous, QPlaceContentRequest *next, const QPlaceManagerEngineNokiaV2 *engine)
@ PublicVisibility
Definition qlocation.h:21
Combined button and popup list for selecting options.
emscripten::val document()
Definition qwasmdom.h:49
DBusConnection const char DBusError * error
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
EGLOutputLayerEXT EGLint attribute
QT_BEGIN_NAMESPACE const char NOKIA_PLUGIN_CONTEXT_NAME[]
const char PARSE_ERROR[]
static bool contains(const QJsonArray &haystack, unsigned needle)
Definition qopengl.cpp:116
GLint location
GLuint64 key
GLuint GLuint end
GLsizei const GLint * box
GLuint GLuint64EXT address
static bool countryTableContains(const QString &countryCode)
static const int COUNTRY_TABLE_indices[]
static QT_BEGIN_NAMESPACE const char COUNTRY_TABLE_string[]
static qreal position(const QQuickItem *item, QQuickAnchors::Anchor anchorLine)
SSL_CTX int void * arg
QLatin1StringView QLatin1String
Definition qstringfwd.h:31
#define QStringLiteral(str)
#define emit
static double toDouble(Value v)
QNetworkReply * reply
char * toString(const MyType &t)
[31]
\inmodule QtCore \reentrant
Definition qchar.h:18