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 "qgeocodereply_nokia.h" |
38 | #include "qgeocodejsonparser.h" |
39 | #include "qgeoerror_messages.h" |
40 | |
41 | #include <QtPositioning/QGeoShape> |
42 | #include <QtCore/QCoreApplication> |
43 | |
44 | Q_DECLARE_METATYPE(QList<QGeoLocation>) |
45 | |
46 | QT_BEGIN_NAMESPACE |
47 | |
48 | // manualBoundsRequired will be true if the parser has to manually |
49 | // check if a given result lies within the viewport bounds, |
50 | // and false if the bounds information was able to be supplied |
51 | // to the server in the request (so it should not return any |
52 | // out-of-bounds results). |
53 | QGeoCodeReplyNokia::QGeoCodeReplyNokia(QNetworkReply *reply, int limit, int offset, |
54 | const QGeoShape &viewport, bool manualBoundsRequired, |
55 | QObject *parent) |
56 | : QGeoCodeReply(parent), m_parsing(false), m_manualBoundsRequired(manualBoundsRequired) |
57 | { |
58 | if (!reply) { |
59 | setError(error: UnknownError, QStringLiteral("Null reply" )); |
60 | return; |
61 | } |
62 | qRegisterMetaType<QList<QGeoLocation> >(); |
63 | |
64 | connect(sender: reply, SIGNAL(finished()), receiver: this, SLOT(networkFinished())); |
65 | connect(sender: reply, SIGNAL(errorOccurred(QNetworkReply::NetworkError)), |
66 | receiver: this, SLOT(networkError(QNetworkReply::NetworkError))); |
67 | connect(sender: this, signal: &QGeoCodeReply::aborted, receiver: reply, slot: &QNetworkReply::abort); |
68 | connect(sender: this, signal: &QGeoCodeReply::aborted, slot: [this](){ m_parsing = false; }); |
69 | connect(sender: this, signal: &QObject::destroyed, receiver: reply, slot: &QObject::deleteLater); |
70 | |
71 | |
72 | setLimit(limit); |
73 | setOffset(offset); |
74 | setViewport(viewport); |
75 | } |
76 | |
77 | QGeoCodeReplyNokia::~QGeoCodeReplyNokia() |
78 | { |
79 | } |
80 | |
81 | void QGeoCodeReplyNokia::networkFinished() |
82 | { |
83 | QNetworkReply *reply = qobject_cast<QNetworkReply *>(object: sender()); |
84 | reply->deleteLater(); |
85 | |
86 | if (reply->error() != QNetworkReply::NoError) |
87 | return; |
88 | |
89 | QGeoCodeJsonParser *parser = new QGeoCodeJsonParser; // QRunnable, autoDelete = true. |
90 | if (m_manualBoundsRequired) |
91 | parser->setBounds(viewport()); |
92 | connect(sender: parser, SIGNAL(results(QList<QGeoLocation>)), |
93 | receiver: this, SLOT(appendResults(QList<QGeoLocation>))); |
94 | connect(sender: parser, SIGNAL(error(QString)), receiver: this, SLOT(parseError(QString))); |
95 | |
96 | m_parsing = true; |
97 | parser->parse(data: reply->readAll()); |
98 | } |
99 | |
100 | void QGeoCodeReplyNokia::networkError(QNetworkReply::NetworkError error) |
101 | { |
102 | Q_UNUSED(error); |
103 | |
104 | QNetworkReply *reply = qobject_cast<QNetworkReply *>(object: sender()); |
105 | reply->deleteLater(); |
106 | setError(error: QGeoCodeReply::CommunicationError, errorString: reply->errorString()); |
107 | } |
108 | |
109 | void QGeoCodeReplyNokia::appendResults(const QList<QGeoLocation> &locations) |
110 | { |
111 | if (!m_parsing) |
112 | return; |
113 | |
114 | m_parsing = false; |
115 | setLocations(locations); |
116 | setFinished(true); |
117 | } |
118 | |
119 | void QGeoCodeReplyNokia::parseError(const QString &errorString) |
120 | { |
121 | Q_UNUSED(errorString); |
122 | |
123 | setError(error: QGeoCodeReply::ParseError, |
124 | errorString: QCoreApplication::translate(context: NOKIA_PLUGIN_CONTEXT_NAME, key: RESPONSE_NOT_RECOGNIZABLE)); |
125 | } |
126 | |
127 | QT_END_NAMESPACE |
128 | |