1 /****************************************************************************
2 **
3 ** Copyright (C) 2013-2018 Esri <contracts@esri.com>
4 ** Contact: https://www.qt.io/licensing/
5 **
6 ** This file is part of the QtLocation module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
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 https://www.qt.io/terms-conditions. For further
15 ** information use the contact form at https://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.LGPL3 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-3.0.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 (at your option) the GNU General
28 ** Public license version 3 or any later version approved by the KDE Free
29 ** Qt Foundation. The licenses are as published by the Free Software
30 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31 ** included in the packaging of this file. Please review the following
32 ** information to ensure the GNU General Public License requirements will
33 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34 ** https://www.gnu.org/licenses/gpl-3.0.html.
35 **
36 ** $QT_END_LICENSE$
37 **
38 ****************************************************************************/
39
40 #include "placesearchreply_esri.h"
41 #include "placemanagerengine_esri.h"
42
43 #include <QtCore/QJsonDocument>
44 #include <QtCore/QJsonArray>
45 #include <QtCore/QJsonObject>
46 #include <QtNetwork/QNetworkReply>
47 #include <QtPositioning/QGeoCircle>
48 #include <QtPositioning/QGeoRectangle>
49 #include <QtLocation/QPlaceResult>
50 #include <QtLocation/QPlaceSearchRequest>
51 #include <QtLocation/private/qplacesearchrequest_p.h>
52
53 static const QString kCandidatesKey(QStringLiteral("candidates"));
54 static const QString kAttributesKey(QStringLiteral("attributes"));
55 static const QString kAddressKey(QStringLiteral("address"));
56 static const QString kLongLabelKey(QStringLiteral("LongLabel"));
57 static const QString kCityKey(QStringLiteral("City"));
58 static const QString kCountryKey(QStringLiteral("Country"));
59 static const QString kRegionKey(QStringLiteral("Region"));
60 static const QString kPostalKey(QStringLiteral("Postal"));
61 static const QString kStAddrKey(QStringLiteral("StAddr"));
62 static const QString kStateKey(QStringLiteral("State"));
63 static const QString kDistrictKey(QStringLiteral("District"));
64 static const QString kLocationKey(QStringLiteral("location"));
65 static const QString kXKey(QStringLiteral("x"));
66 static const QString kYKey(QStringLiteral("y"));
67 static const QString kDistanceKey(QStringLiteral("Distance"));
68 static const QString kPhoneKey(QStringLiteral("Phone"));
69 static const QString kExtentKey(QStringLiteral("extent"));
70 static const QString kXminKey(QStringLiteral("xmin"));
71 static const QString kYminKey(QStringLiteral("ymin"));
72 static const QString kXmaxKey(QStringLiteral("xmax"));
73 static const QString kYmaxKey(QStringLiteral("ymax"));
74
75 QT_BEGIN_NAMESPACE
76
PlaceSearchReplyEsri(const QPlaceSearchRequest & request,QNetworkReply * reply,const QHash<QString,QString> & candidateFields,const QHash<QString,QString> & countries,PlaceManagerEngineEsri * parent)77 PlaceSearchReplyEsri::PlaceSearchReplyEsri(const QPlaceSearchRequest &request, QNetworkReply *reply,
78 const QHash<QString, QString> &candidateFields,
79 const QHash<QString, QString> &countries, PlaceManagerEngineEsri *parent) :
80 QPlaceSearchReply(parent), m_candidateFields(candidateFields), m_countries(countries)
81 {
82 Q_ASSERT(parent);
83 if (!reply) {
84 setError(UnknownError, QStringLiteral("Null reply"));
85 return;
86 }
87 setRequest(request);
88
89 connect(reply, SIGNAL(finished()), this, SLOT(replyFinished()));
90 connect(reply, SIGNAL(errorOccurred(QNetworkReply::NetworkError)), this, SLOT(networkError(QNetworkReply::NetworkError)));
91 connect(this, &QPlaceReply::aborted, reply, &QNetworkReply::abort);
92 connect(this, &QObject::destroyed, reply, &QObject::deleteLater);
93 }
94
~PlaceSearchReplyEsri()95 PlaceSearchReplyEsri::~PlaceSearchReplyEsri()
96 {
97 }
98
setError(QPlaceReply::Error errorCode,const QString & errorString)99 void PlaceSearchReplyEsri::setError(QPlaceReply::Error errorCode, const QString &errorString)
100 {
101 QPlaceReply::setError(errorCode, errorString);
102 emit error(errorCode, errorString);
103 setFinished(true);
104 emit finished();
105 }
106
replyFinished()107 void PlaceSearchReplyEsri::replyFinished()
108 {
109 QNetworkReply *reply = static_cast<QNetworkReply *>(sender());
110 reply->deleteLater();
111
112 if (reply->error() != QNetworkReply::NoError)
113 return;
114
115 QJsonDocument document = QJsonDocument::fromJson(reply->readAll());
116 if (!document.isObject())
117 {
118 setError(ParseError, tr("Response parse error"));
119 return;
120 }
121
122 QJsonValue suggestions = document.object().value(kCandidatesKey);
123 if (!suggestions.isArray())
124 {
125 setError(ParseError, tr("Response parse error"));
126 return;
127 }
128
129 QJsonArray resultsArray = suggestions.toArray();
130
131 QList<QPlaceSearchResult> results;
132 for (int i = 0; i < resultsArray.count(); ++i)
133 {
134 QJsonObject item = resultsArray.at(i).toObject();
135 QPlaceResult placeResult = parsePlaceResult(item);
136 results.append(placeResult);
137 }
138
139 setResults(results);
140 setFinished(true);
141 emit finished();
142 }
143
networkError(QNetworkReply::NetworkError error)144 void PlaceSearchReplyEsri::networkError(QNetworkReply::NetworkError error)
145 {
146 Q_UNUSED(error)
147 QNetworkReply *reply = static_cast<QNetworkReply *>(sender());
148 reply->deleteLater();
149 setError(QPlaceReply::CommunicationError, reply->errorString());
150 }
151
parsePlaceResult(const QJsonObject & item) const152 QPlaceResult PlaceSearchReplyEsri::parsePlaceResult(const QJsonObject &item) const
153 {
154 QPlace place;
155 QHash<QString, QString> keys;
156
157 // set attributes
158 const QJsonObject attributes = item.value(kAttributesKey).toObject();
159 for (const QString &key: attributes.keys())
160 {
161 const QString value = attributes.value(key).toVariant().toString();
162 if (!value.isEmpty())
163 {
164 QPlaceAttribute attribute;
165 attribute.setLabel(m_candidateFields.value(key, key)); // local name or key
166 attribute.setText(value);
167 place.setExtendedAttribute(key, attribute);
168 keys.insert(key, value);
169 }
170 }
171
172 if (keys.contains(kPhoneKey))
173 {
174 QPlaceContactDetail contactDetail;
175 contactDetail.setLabel(m_candidateFields.value(kPhoneKey, kPhoneKey)); // local name or key
176 contactDetail.setValue(keys.value(kPhoneKey));
177 place.appendContactDetail(QPlaceContactDetail::Phone, contactDetail);
178 }
179
180 // set address
181 QGeoAddress geoAddress;
182 geoAddress.setCity(keys.value(kCityKey));
183 geoAddress.setCountry(m_countries.value(keys.value(kCountryKey))); // mismatch code ISO2 vs ISO3
184 geoAddress.setCounty(keys.value(kRegionKey));
185 geoAddress.setPostalCode(keys.value(kPostalKey));
186 geoAddress.setStreet(keys.value(kStAddrKey));
187 geoAddress.setState(keys.value(kStateKey));
188 geoAddress.setDistrict(keys.value(kDistrictKey));
189
190 // set location
191 const QJsonObject location = item.value(kLocationKey).toObject();
192 const QGeoCoordinate coordinate = QGeoCoordinate(location.value(kYKey).toDouble(),
193 location.value(kXKey).toDouble());
194
195 // set boundingBox
196 const QJsonObject extent = item.value(kExtentKey).toObject();
197 const QGeoCoordinate topLeft(extent.value(kYminKey).toDouble(),
198 extent.value(kXminKey).toDouble());
199 const QGeoCoordinate bottomRight(extent.value(kYmaxKey).toDouble(),
200 extent.value(kXmaxKey).toDouble());
201 const QGeoRectangle boundingBox(topLeft, bottomRight);
202
203 // set geolocation
204 QGeoLocation geoLocation;
205 geoLocation.setCoordinate(coordinate);
206 geoLocation.setAddress(geoAddress);
207 geoLocation.setBoundingBox(boundingBox);
208
209 // set place
210 place.setName(keys.value(kLongLabelKey));
211 place.setLocation(geoLocation);
212 place.setPlaceId(attributes.value(kLongLabelKey).toString());
213
214 // set place result
215 QPlaceResult result;
216 result.setPlace(place);
217 result.setTitle(keys.value(kAddressKey));
218 result.setDistance(keys.value(kDistanceKey).toDouble());
219
220 return result;
221 }
222
223 QT_END_NAMESPACE
224