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 "qgeoroutereply_nokia.h"
38 #include "qgeoroutexmlparser.h"
39 #include "qgeoerror_messages.h"
40
41 #include <qgeorouterequest.h>
42
43 #include <QtCore/QCoreApplication>
44
Q_DECLARE_METATYPE(QList<QGeoRoute>)45 Q_DECLARE_METATYPE(QList<QGeoRoute>)
46
47 QT_BEGIN_NAMESPACE
48
49 QGeoRouteReplyNokia::QGeoRouteReplyNokia(const QGeoRouteRequest &request,
50 const QList<QNetworkReply *> &replies,
51 QObject *parent)
52 : QGeoRouteReply(request, parent), m_parsers(0)
53 {
54 qRegisterMetaType<QList<QGeoRoute> >();
55
56 bool failure = false;
57 foreach (QNetworkReply *reply, replies) {
58 if (!reply) {
59 failure = true;
60 continue;
61 }
62 connect(reply, SIGNAL(finished()), this, SLOT(networkFinished()));
63 connect(reply, SIGNAL(errorOccurred(QNetworkReply::NetworkError)),
64 this, SLOT(networkError(QNetworkReply::NetworkError)));
65 connect(this, &QGeoRouteReply::aborted, reply, &QNetworkReply::abort);
66 connect(this, &QObject::destroyed, reply, &QObject::deleteLater);
67 }
68 if (failure)
69 setError(UnknownError, QStringLiteral("Null reply"));
70 else
71 connect(this, &QGeoRouteReply::aborted, [this](){ m_parsers = 0; });
72 }
73
~QGeoRouteReplyNokia()74 QGeoRouteReplyNokia::~QGeoRouteReplyNokia()
75 {
76 }
77
networkFinished()78 void QGeoRouteReplyNokia::networkFinished()
79 {
80 QNetworkReply *reply = qobject_cast<QNetworkReply *>(sender());
81 reply->deleteLater();
82
83 if (reply->error() != QNetworkReply::NoError
84 && reply->error() != QNetworkReply::UnknownContentError) {
85 return;
86 }
87
88 QGeoRouteXmlParser *parser = new QGeoRouteXmlParser(request());
89 connect(parser, SIGNAL(results(QList<QGeoRoute>)),
90 this, SLOT(appendResults(QList<QGeoRoute>)));
91 connect(parser, SIGNAL(error(QString)), this, SLOT(parserError(QString)));
92
93 ++m_parsers;
94 parser->parse(reply->readAll());
95 }
96
networkError(QNetworkReply::NetworkError error)97 void QGeoRouteReplyNokia::networkError(QNetworkReply::NetworkError error)
98 {
99 if (error == QNetworkReply::UnknownContentError)
100 return;
101 QNetworkReply *reply = static_cast<QNetworkReply *>(sender());
102 reply->deleteLater();
103 setError(QGeoRouteReply::CommunicationError, reply->errorString());
104 if (error != QNetworkReply::OperationCanceledError) // Any error not caused by abort()
105 emit aborted(); // aborts all unfinished replies and sets m_parsers to 0
106 }
107
appendResults(const QList<QGeoRoute> & routes)108 void QGeoRouteReplyNokia::appendResults(const QList<QGeoRoute> &routes)
109 {
110 if (!m_parsers)
111 return;
112
113 --m_parsers;
114 addRoutes(routes);
115
116 if (!m_parsers)
117 setFinished(true);
118 }
119
parserError(const QString & errorString)120 void QGeoRouteReplyNokia::parserError(const QString &errorString)
121 {
122 Q_UNUSED(errorString);
123 emit aborted();
124 setError(QGeoRouteReply::ParseError,
125 QCoreApplication::translate(NOKIA_PLUGIN_CONTEXT_NAME, RESPONSE_NOT_RECOGNIZABLE));
126 }
127
128 QT_END_NAMESPACE
129