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
Q_DECLARE_METATYPE(QList<QGeoLocation>)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(UnknownError, QStringLiteral("Null reply"));
60 return;
61 }
62 qRegisterMetaType<QList<QGeoLocation> >();
63
64 connect(reply, SIGNAL(finished()), this, SLOT(networkFinished()));
65 connect(reply, SIGNAL(errorOccurred(QNetworkReply::NetworkError)),
66 this, SLOT(networkError(QNetworkReply::NetworkError)));
67 connect(this, &QGeoCodeReply::aborted, reply, &QNetworkReply::abort);
68 connect(this, &QGeoCodeReply::aborted, [this](){ m_parsing = false; });
69 connect(this, &QObject::destroyed, reply, &QObject::deleteLater);
70
71
72 setLimit(limit);
73 setOffset(offset);
74 setViewport(viewport);
75 }
76
~QGeoCodeReplyNokia()77 QGeoCodeReplyNokia::~QGeoCodeReplyNokia()
78 {
79 }
80
networkFinished()81 void QGeoCodeReplyNokia::networkFinished()
82 {
83 QNetworkReply *reply = qobject_cast<QNetworkReply *>(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(parser, SIGNAL(results(QList<QGeoLocation>)),
93 this, SLOT(appendResults(QList<QGeoLocation>)));
94 connect(parser, SIGNAL(error(QString)), this, SLOT(parseError(QString)));
95
96 m_parsing = true;
97 parser->parse(reply->readAll());
98 }
99
networkError(QNetworkReply::NetworkError error)100 void QGeoCodeReplyNokia::networkError(QNetworkReply::NetworkError error)
101 {
102 Q_UNUSED(error);
103
104 QNetworkReply *reply = qobject_cast<QNetworkReply *>(sender());
105 reply->deleteLater();
106 setError(QGeoCodeReply::CommunicationError, reply->errorString());
107 }
108
appendResults(const QList<QGeoLocation> & locations)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
parseError(const QString & errorString)119 void QGeoCodeReplyNokia::parseError(const QString &errorString)
120 {
121 Q_UNUSED(errorString);
122
123 setError(QGeoCodeReply::ParseError,
124 QCoreApplication::translate(NOKIA_PLUGIN_CONTEXT_NAME, RESPONSE_NOT_RECOGNIZABLE));
125 }
126
127 QT_END_NAMESPACE
128