| 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 "qplacesearchreplyhere.h" |
| 38 | #include "jsonparserhelpers.h" |
| 39 | #include "../qplacemanagerengine_nokiav2.h" |
| 40 | #include "../qgeoerror_messages.h" |
| 41 | |
| 42 | #include <QCoreApplication> |
| 43 | #include <QtCore/QJsonDocument> |
| 44 | #include <QtCore/QJsonObject> |
| 45 | #include <QtCore/QJsonArray> |
| 46 | #include <QtLocation/QPlaceIcon> |
| 47 | #include <QtLocation/QPlaceResult> |
| 48 | #include <QtLocation/QPlaceProposedSearchResult> |
| 49 | #include <QtLocation/private/qplacesearchrequest_p.h> |
| 50 | |
| 51 | #include <QtCore/QDebug> |
| 52 | |
| 53 | QT_BEGIN_NAMESPACE |
| 54 | |
| 55 | QPlaceSearchReplyHere::QPlaceSearchReplyHere(const QPlaceSearchRequest &request, |
| 56 | QNetworkReply *reply, |
| 57 | QPlaceManagerEngineNokiaV2 *parent) |
| 58 | : QPlaceSearchReply(parent), m_engine(parent) |
| 59 | { |
| 60 | if (!reply) { |
| 61 | setError(error_: UnknownError, QStringLiteral("Null reply" )); |
| 62 | return; |
| 63 | } |
| 64 | setRequest(request); |
| 65 | |
| 66 | connect(sender: reply, SIGNAL(finished()), receiver: this, SLOT(replyFinished())); |
| 67 | connect(sender: reply, SIGNAL(errorOccurred(QNetworkReply::NetworkError)), |
| 68 | receiver: this, SLOT(replyError(QNetworkReply::NetworkError))); |
| 69 | connect(sender: this, signal: &QPlaceReply::aborted, receiver: reply, slot: &QNetworkReply::abort); |
| 70 | connect(sender: this, signal: &QObject::destroyed, receiver: reply, slot: &QObject::deleteLater); |
| 71 | } |
| 72 | |
| 73 | QPlaceSearchReplyHere::~QPlaceSearchReplyHere() |
| 74 | { |
| 75 | } |
| 76 | |
| 77 | void QPlaceSearchReplyHere::setError(QPlaceReply::Error error_, const QString &errorString) |
| 78 | { |
| 79 | QPlaceReply::setError(error: error_, errorString); |
| 80 | emit error(error: error_, errorString); |
| 81 | setFinished(true); |
| 82 | emit finished(); |
| 83 | } |
| 84 | |
| 85 | void QPlaceSearchReplyHere::replyFinished() |
| 86 | { |
| 87 | QNetworkReply *reply = static_cast<QNetworkReply *>(sender()); |
| 88 | reply->deleteLater(); |
| 89 | |
| 90 | if (reply->error() != QNetworkReply::NoError) |
| 91 | return; |
| 92 | |
| 93 | QJsonDocument document = QJsonDocument::fromJson(json: reply->readAll()); |
| 94 | if (!document.isObject()) { |
| 95 | setError(error_: ParseError, errorString: QCoreApplication::translate(context: NOKIA_PLUGIN_CONTEXT_NAME, key: PARSE_ERROR)); |
| 96 | return; |
| 97 | } |
| 98 | |
| 99 | QJsonObject resultsObject = document.object(); |
| 100 | |
| 101 | if (resultsObject.contains(QStringLiteral("results" ))) |
| 102 | resultsObject = resultsObject.value(QStringLiteral("results" )).toObject(); |
| 103 | |
| 104 | QJsonArray items = resultsObject.value(QStringLiteral("items" )).toArray(); |
| 105 | |
| 106 | QList<QPlaceSearchResult> results; |
| 107 | for (int i = 0; i < items.count(); ++i) { |
| 108 | QJsonObject item = items.at(i).toObject(); |
| 109 | |
| 110 | const QString type = item.value(QStringLiteral("type" )).toString(); |
| 111 | if (type == QStringLiteral("urn:nlp-types:place" )) |
| 112 | results.append(t: parsePlaceResult(item)); |
| 113 | else if (type == QStringLiteral("urn:nlp-types:search" )) |
| 114 | results.append(t: parseSearchResult(item)); |
| 115 | } |
| 116 | |
| 117 | QPlaceSearchRequest r_orig = request(); |
| 118 | QPlaceSearchRequestPrivate *rpimpl_orig = QPlaceSearchRequestPrivate::get(request&: r_orig); |
| 119 | |
| 120 | if (resultsObject.contains(QStringLiteral("next" ))) { |
| 121 | QPlaceSearchRequest request; |
| 122 | request.setSearchContext(QUrl(resultsObject.value(QStringLiteral("next" )).toString())); |
| 123 | QPlaceSearchRequestPrivate *rpimpl = QPlaceSearchRequestPrivate::get(request); |
| 124 | rpimpl->related = true; |
| 125 | rpimpl->page = rpimpl_orig->page + 1; |
| 126 | setNextPageRequest(request); |
| 127 | } |
| 128 | |
| 129 | if (resultsObject.contains(QStringLiteral("previous" ))) { |
| 130 | QPlaceSearchRequest request; |
| 131 | request.setSearchContext(QUrl(resultsObject.value(QStringLiteral("previous" )).toString())); |
| 132 | QPlaceSearchRequestPrivate *rpimpl = QPlaceSearchRequestPrivate::get(request); |
| 133 | rpimpl->related = true; |
| 134 | rpimpl->page = rpimpl_orig->page - 1; |
| 135 | setPreviousPageRequest(request); |
| 136 | } |
| 137 | |
| 138 | setResults(results); |
| 139 | |
| 140 | setFinished(true); |
| 141 | emit finished(); |
| 142 | } |
| 143 | |
| 144 | QPlaceResult QPlaceSearchReplyHere::parsePlaceResult(const QJsonObject &item) const |
| 145 | { |
| 146 | QPlaceResult result; |
| 147 | |
| 148 | if (item.contains(QStringLiteral("distance" ))) |
| 149 | result.setDistance(item.value(QStringLiteral("distance" )).toDouble()); |
| 150 | |
| 151 | QPlace place; |
| 152 | |
| 153 | QGeoLocation location; |
| 154 | |
| 155 | location.setCoordinate(parseCoordinate(coordinateArray: item.value(QStringLiteral("position" )).toArray())); |
| 156 | |
| 157 | const QString vicinity = item.value(QStringLiteral("vicinity" )).toString(); |
| 158 | QGeoAddress address; |
| 159 | address.setText(vicinity); |
| 160 | location.setAddress(address); |
| 161 | |
| 162 | if (item.contains(QStringLiteral("bbox" ))) { |
| 163 | QJsonArray bbox = item.value(QStringLiteral("bbox" )).toArray(); |
| 164 | QGeoRectangle box(QGeoCoordinate(bbox.at(i: 3).toDouble(), bbox.at(i: 0).toDouble()), |
| 165 | QGeoCoordinate(bbox.at(i: 1).toDouble(), bbox.at(i: 2).toDouble())); |
| 166 | location.setBoundingBox(box); |
| 167 | } |
| 168 | |
| 169 | place.setLocation(location); |
| 170 | |
| 171 | QPlaceRatings ratings; |
| 172 | ratings.setAverage(item.value(QStringLiteral("averageRating" )).toDouble()); |
| 173 | ratings.setMaximum(5.0); |
| 174 | place.setRatings(ratings); |
| 175 | |
| 176 | const QString title = item.value(QStringLiteral("title" )).toString(); |
| 177 | place.setName(title); |
| 178 | result.setTitle(title); |
| 179 | |
| 180 | QPlaceIcon icon = m_engine->icon(remotePath: item.value(QStringLiteral("icon" )).toString()); |
| 181 | place.setIcon(icon); |
| 182 | result.setIcon(icon); |
| 183 | |
| 184 | place.setCategory(parseCategory(categoryObject: item.value(QStringLiteral("category" )).toObject(), |
| 185 | engine: m_engine)); |
| 186 | |
| 187 | //QJsonArray having = item.value(QStringLiteral("having")).toArray(); |
| 188 | |
| 189 | result.setSponsored(item.value(QStringLiteral("sponsored" )).toBool()); |
| 190 | |
| 191 | QUrl href = item.value(QStringLiteral("href" )).toString(); |
| 192 | //QUrl type = item.value(QStringLiteral("type")).toString(); |
| 193 | |
| 194 | place.setPlaceId(href.path().mid(position: 18, n: 41)); |
| 195 | |
| 196 | QPlaceAttribute provider; |
| 197 | provider.setText(QStringLiteral("here" )); |
| 198 | place.setExtendedAttribute(attributeType: QPlaceAttribute::Provider, attribute: provider); |
| 199 | place.setVisibility(QLocation::PublicVisibility); |
| 200 | |
| 201 | result.setPlace(place); |
| 202 | |
| 203 | return result; |
| 204 | } |
| 205 | |
| 206 | QPlaceProposedSearchResult QPlaceSearchReplyHere::parseSearchResult(const QJsonObject &item) const |
| 207 | { |
| 208 | QPlaceProposedSearchResult result; |
| 209 | |
| 210 | result.setTitle(item.value(QStringLiteral("title" )).toString()); |
| 211 | |
| 212 | QPlaceIcon icon = m_engine->icon(remotePath: item.value(QStringLiteral("icon" )).toString()); |
| 213 | result.setIcon(icon); |
| 214 | |
| 215 | QPlaceSearchRequest request; |
| 216 | request.setSearchContext(QUrl(item.value(key: "href" ).toString())); |
| 217 | |
| 218 | result.setSearchRequest(request); |
| 219 | |
| 220 | return result; |
| 221 | } |
| 222 | |
| 223 | void QPlaceSearchReplyHere::replyError(QNetworkReply::NetworkError error) |
| 224 | { |
| 225 | QNetworkReply *reply = static_cast<QNetworkReply *>(sender()); |
| 226 | reply->deleteLater(); |
| 227 | if (error == QNetworkReply::OperationCanceledError) { |
| 228 | setError(error_: QPlaceReply::CancelError, errorString: QCoreApplication::translate(context: NOKIA_PLUGIN_CONTEXT_NAME, key: CANCEL_ERROR)); |
| 229 | } else if (error == QNetworkReply::ContentNotFoundError) { |
| 230 | setError(error_: QPlaceReply::PlaceDoesNotExistError, |
| 231 | errorString: QString::fromLatin1(str: "The id, %1, does not reference an existing place" ) |
| 232 | .arg(a: request().recommendationId())); |
| 233 | } else { |
| 234 | setError(error_: QPlaceReply::CommunicationError, errorString: QCoreApplication::translate(context: NOKIA_PLUGIN_CONTEXT_NAME, key: NETWORK_ERROR)); |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | QT_END_NAMESPACE |
| 239 | |