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 "jsonparserhelpers.h"
38 #include "qplacecontentreplyimpl.h"
39 #include "../qplacemanagerengine_nokiav2.h"
40 #include "../qgeoerror_messages.h"
41
42 #include <QCoreApplication>
43 #include <QtCore/QJsonDocument>
44 #include <QtCore/QJsonObject>
45
46 QT_BEGIN_NAMESPACE
47
QPlaceContentReplyImpl(const QPlaceContentRequest & request,QNetworkReply * reply,QPlaceManagerEngineNokiaV2 * engine)48 QPlaceContentReplyImpl::QPlaceContentReplyImpl(const QPlaceContentRequest &request,
49 QNetworkReply *reply,
50 QPlaceManagerEngineNokiaV2 *engine)
51 : QPlaceContentReply(engine), m_engine(engine)
52 {
53 Q_ASSERT(engine);
54 if (!reply) {
55 setError(UnknownError, QStringLiteral("Null reply"));
56 return;
57 }
58 setRequest(request);
59
60 connect(reply, SIGNAL(finished()), this, SLOT(replyFinished()));
61 connect(reply, SIGNAL(errorOccurred(QNetworkReply::NetworkError)),
62 this, SLOT(replyError(QNetworkReply::NetworkError)));
63 connect(this, &QPlaceReply::aborted, reply, &QNetworkReply::abort);
64 connect(this, &QObject::destroyed, reply, &QObject::deleteLater);
65 }
66
~QPlaceContentReplyImpl()67 QPlaceContentReplyImpl::~QPlaceContentReplyImpl()
68 {
69 }
70
setError(QPlaceReply::Error error_,const QString & errorString)71 void QPlaceContentReplyImpl::setError(QPlaceReply::Error error_, const QString &errorString)
72 {
73 QPlaceContentReply::setError(error_, errorString);
74 emit error(error_, errorString);
75 setFinished(true);
76 emit finished();
77 }
78
replyFinished()79 void QPlaceContentReplyImpl::replyFinished()
80 {
81 QNetworkReply *reply = static_cast<QNetworkReply *>(sender());
82 reply->deleteLater();
83
84 if (reply->error() != QNetworkReply::NoError)
85 return;
86
87 QJsonDocument document = QJsonDocument::fromJson(reply->readAll());
88 if (!document.isObject()) {
89 setError(ParseError, QCoreApplication::translate(NOKIA_PLUGIN_CONTEXT_NAME, PARSE_ERROR));
90 return;
91 }
92
93 QJsonObject object = document.object();
94
95 QPlaceContent::Collection collection;
96 int totalCount;
97 QPlaceContentRequest previous;
98 QPlaceContentRequest next;
99
100 parseCollection(request().contentType(), object, &collection, &totalCount,
101 &previous, &next, m_engine);
102
103 setTotalCount(totalCount);
104 setContent(collection);
105 setPreviousPageRequest(previous);
106 setNextPageRequest(next);
107
108 setFinished(true);
109 emit finished();
110 }
111
replyError(QNetworkReply::NetworkError error)112 void QPlaceContentReplyImpl::replyError(QNetworkReply::NetworkError error)
113 {
114 QNetworkReply *reply = static_cast<QNetworkReply *>(sender());
115 reply->deleteLater();
116 if (error == QNetworkReply::OperationCanceledError)
117 setError(QPlaceReply::CancelError, QStringLiteral("Request cancelled"));
118 else
119 setError(QPlaceReply::CommunicationError, reply->errorString());
120 }
121
122 QT_END_NAMESPACE
123