| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2017 Mapbox, Inc. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the QtFoo 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 "qplacesearchreplymapbox.h" |
| 41 | #include "qplacemanagerenginemapbox.h" |
| 42 | #include "qmapboxcommon.h" |
| 43 | |
| 44 | #include <QtCore/QJsonDocument> |
| 45 | #include <QtCore/QJsonArray> |
| 46 | #include <QtCore/QJsonObject> |
| 47 | #include <QtNetwork/QNetworkReply> |
| 48 | #include <QtPositioning/QGeoCircle> |
| 49 | #include <QtPositioning/QGeoRectangle> |
| 50 | #include <QtLocation/QPlaceResult> |
| 51 | #include <QtLocation/QPlaceSearchRequest> |
| 52 | #include <QtLocation/QPlaceContactDetail> |
| 53 | |
| 54 | #include <algorithm> |
| 55 | |
| 56 | QT_BEGIN_NAMESPACE |
| 57 | |
| 58 | namespace { |
| 59 | |
| 60 | // https://www.mapbox.com/api-documentation/#response-object |
| 61 | QPlaceResult parsePlaceResult(const QJsonObject &response, const QString &attribution) |
| 62 | { |
| 63 | QPlace place; |
| 64 | |
| 65 | place.setAttribution(attribution); |
| 66 | place.setPlaceId(response.value(QStringLiteral("id" )).toString()); |
| 67 | place.setVisibility(QLocation::PublicVisibility); |
| 68 | |
| 69 | QString placeName = response.value(QStringLiteral("text" )).toString(); |
| 70 | if (placeName.isEmpty()) |
| 71 | placeName = response.value(QStringLiteral("place_name" )).toString(); |
| 72 | |
| 73 | place.setName(placeName); |
| 74 | place.setDetailsFetched(true); |
| 75 | |
| 76 | // Unused data: type, place_type, relevance, properties.short_code, |
| 77 | // properties.landmark, properties.wikidata |
| 78 | |
| 79 | // The property object is unstable and only Carmen GeoJSON properties are |
| 80 | // guaranteed. This implementation should check for the presence of these |
| 81 | // values in a response before it attempts to use them. |
| 82 | if (response.value(QStringLiteral("properties" )).isObject()) { |
| 83 | const QJsonObject properties = response.value(QStringLiteral("properties" )).toObject(); |
| 84 | |
| 85 | const QString makiString = properties.value(QStringLiteral("maki" )).toString(); |
| 86 | if (!makiString.isEmpty()) { |
| 87 | QVariantMap iconParameters; |
| 88 | iconParameters.insert(akey: QPlaceIcon::SingleUrl, |
| 89 | avalue: QUrl::fromLocalFile(QStringLiteral(":/mapbox/" ) + makiString + QStringLiteral(".svg" ))); |
| 90 | |
| 91 | QPlaceIcon icon; |
| 92 | icon.setParameters(iconParameters); |
| 93 | place.setIcon(icon); |
| 94 | } |
| 95 | |
| 96 | const QString phoneString = properties.value(QStringLiteral("tel" )).toString(); |
| 97 | if (!phoneString.isEmpty()) { |
| 98 | QPlaceContactDetail phoneDetail; |
| 99 | phoneDetail.setLabel(QPlaceContactDetail::Phone); |
| 100 | phoneDetail.setValue(phoneString); |
| 101 | place.setContactDetails(contactType: QPlaceContactDetail::Phone, details: QList<QPlaceContactDetail>() << phoneDetail); |
| 102 | } |
| 103 | |
| 104 | const QString categoryString = properties.value(QStringLiteral("category" )).toString(); |
| 105 | if (!categoryString.isEmpty()) { |
| 106 | QList<QPlaceCategory> categories; |
| 107 | for (const QString &categoryId : categoryString.split(QStringLiteral(", " ), behavior: Qt::SkipEmptyParts)) { |
| 108 | QPlaceCategory category; |
| 109 | category.setName(QMapboxCommon::mapboxNameForCategory(category: categoryId)); |
| 110 | category.setCategoryId(categoryId); |
| 111 | categories.append(t: category); |
| 112 | } |
| 113 | place.setCategories(categories); |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | // XXX: matching_text, matching_place_name |
| 118 | // XXX: text_{language}, place_name_{language} |
| 119 | // XXX: language, language_{language} |
| 120 | |
| 121 | place.setLocation(QMapboxCommon::parseGeoLocation(response)); |
| 122 | |
| 123 | // XXX: geometry, geometry.type, geometry.coordinates, geometry.interpolated |
| 124 | |
| 125 | QPlaceResult result; |
| 126 | result.setPlace(place); |
| 127 | result.setTitle(place.name()); |
| 128 | |
| 129 | return result; |
| 130 | } |
| 131 | |
| 132 | } // namespace |
| 133 | |
| 134 | QPlaceSearchReplyMapbox::QPlaceSearchReplyMapbox(const QPlaceSearchRequest &request, QNetworkReply *reply, QPlaceManagerEngineMapbox *parent) |
| 135 | : QPlaceSearchReply(parent) |
| 136 | { |
| 137 | Q_ASSERT(parent); |
| 138 | if (!reply) { |
| 139 | setError(errorCode: UnknownError, QStringLiteral("Null reply" )); |
| 140 | return; |
| 141 | } |
| 142 | setRequest(request); |
| 143 | |
| 144 | connect(sender: reply, signal: &QNetworkReply::finished, receiver: this, slot: &QPlaceSearchReplyMapbox::onReplyFinished); |
| 145 | connect(sender: reply, signal: &QNetworkReply::errorOccurred, receiver: this, slot: &QPlaceSearchReplyMapbox::onNetworkError); |
| 146 | |
| 147 | connect(sender: this, signal: &QPlaceReply::aborted, receiver: reply, slot: &QNetworkReply::abort); |
| 148 | connect(sender: this, signal: &QObject::destroyed, receiver: reply, slot: &QObject::deleteLater); |
| 149 | } |
| 150 | |
| 151 | QPlaceSearchReplyMapbox::~QPlaceSearchReplyMapbox() |
| 152 | { |
| 153 | } |
| 154 | |
| 155 | void QPlaceSearchReplyMapbox::setError(QPlaceReply::Error errorCode, const QString &errorString) |
| 156 | { |
| 157 | QPlaceReply::setError(error: errorCode, errorString); |
| 158 | emit error(error: errorCode, errorString); |
| 159 | |
| 160 | setFinished(true); |
| 161 | emit finished(); |
| 162 | } |
| 163 | |
| 164 | void QPlaceSearchReplyMapbox::onReplyFinished() |
| 165 | { |
| 166 | QNetworkReply *reply = static_cast<QNetworkReply *>(sender()); |
| 167 | reply->deleteLater(); |
| 168 | |
| 169 | if (reply->error() != QNetworkReply::NoError) |
| 170 | return; |
| 171 | |
| 172 | const QJsonDocument document = QJsonDocument::fromJson(json: reply->readAll()); |
| 173 | if (!document.isObject()) { |
| 174 | setError(errorCode: ParseError, errorString: tr(s: "Response parse error" )); |
| 175 | return; |
| 176 | } |
| 177 | |
| 178 | const QJsonArray features = document.object().value(QStringLiteral("features" )).toArray(); |
| 179 | const QString attribution = document.object().value(QStringLiteral("attribution" )).toString(); |
| 180 | |
| 181 | const QGeoCoordinate searchCenter = request().searchArea().center(); |
| 182 | const QList<QPlaceCategory> categories = request().categories(); |
| 183 | |
| 184 | QList<QPlaceSearchResult> results; |
| 185 | for (const QJsonValue &feature : features) { |
| 186 | QPlaceResult placeResult = parsePlaceResult(response: feature.toObject(), attribution); |
| 187 | |
| 188 | if (!categories.isEmpty()) { |
| 189 | const QList<QPlaceCategory> placeCategories = placeResult.place().categories(); |
| 190 | bool categoryMatch = false; |
| 191 | if (!placeCategories.isEmpty()) { |
| 192 | for (const QPlaceCategory &placeCategory : placeCategories) { |
| 193 | if (categories.contains(t: placeCategory)) { |
| 194 | categoryMatch = true; |
| 195 | break; |
| 196 | } |
| 197 | } |
| 198 | } |
| 199 | if (!categoryMatch) |
| 200 | continue; |
| 201 | } |
| 202 | placeResult.setDistance(searchCenter.distanceTo(other: placeResult.place().location().coordinate())); |
| 203 | results.append(t: placeResult); |
| 204 | } |
| 205 | |
| 206 | if (request().relevanceHint() == QPlaceSearchRequest::DistanceHint) { |
| 207 | std::sort(first: results.begin(), last: results.end(), comp: [](const QPlaceResult &a, const QPlaceResult &b) -> bool { |
| 208 | return a.distance() < b.distance(); |
| 209 | }); |
| 210 | } else if (request().relevanceHint() == QPlaceSearchRequest::LexicalPlaceNameHint) { |
| 211 | std::sort(first: results.begin(), last: results.end(), comp: [](const QPlaceResult &a, const QPlaceResult &b) -> bool { |
| 212 | return a.place().name() < b.place().name(); |
| 213 | }); |
| 214 | } |
| 215 | |
| 216 | setResults(results); |
| 217 | |
| 218 | setFinished(true); |
| 219 | emit finished(); |
| 220 | } |
| 221 | |
| 222 | void QPlaceSearchReplyMapbox::onNetworkError(QNetworkReply::NetworkError error) |
| 223 | { |
| 224 | Q_UNUSED(error); |
| 225 | QNetworkReply *reply = static_cast<QNetworkReply *>(sender()); |
| 226 | reply->deleteLater(); |
| 227 | setError(errorCode: CommunicationError, errorString: reply->errorString()); |
| 228 | } |
| 229 | |
| 230 | QT_END_NAMESPACE |
| 231 | |