Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qplacecontentreplyimpl.cpp
Go to the documentation of this file.
1// Copyright (C) 2015 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
7#include "../qplacemanagerengine_nokiav2.h"
8#include "../qgeoerror_messages.h"
9
10
11#include <QCoreApplication>
12#include <QtCore/QJsonDocument>
13#include <QtCore/QJsonObject>
14
16
17QPlaceContentReplyImpl::QPlaceContentReplyImpl(const QPlaceContentRequest &request,
18 QNetworkReply *reply,
19 QPlaceManagerEngineNokiaV2 *engine)
20 : QPlaceContentReply(engine), m_engine(engine)
21{
22 Q_ASSERT(engine);
23 if (!reply) {
24 setError(UnknownError, QStringLiteral("Null reply"));
25 return;
26 }
27 setRequest(request);
28
29 connect(reply, &QNetworkReply::finished,
30 this, &QPlaceContentReplyImpl::replyFinished);
31 connect(reply, &QNetworkReply::errorOccurred,
32 this, &QPlaceContentReplyImpl::replyError);
33 connect(this, &QPlaceReply::aborted, reply, &QNetworkReply::abort);
34 connect(this, &QObject::destroyed, reply, &QObject::deleteLater);
35}
36
40
41void QPlaceContentReplyImpl::setError(QPlaceReply::Error error_, const QString &errorString)
42{
43 QPlaceContentReply::setError(error_, errorString);
44 emit errorOccurred(error_, errorString);
45 setFinished(true);
46 emit finished();
47}
48
49void QPlaceContentReplyImpl::replyFinished()
50{
51 QNetworkReply *reply = static_cast<QNetworkReply *>(sender());
52 reply->deleteLater();
53
54 if (reply->error() != QNetworkReply::NoError)
55 return;
56
57 QJsonDocument document = QJsonDocument::fromJson(reply->readAll());
58 if (!document.isObject()) {
59 setError(ParseError, QCoreApplication::translate(NOKIA_PLUGIN_CONTEXT_NAME, PARSE_ERROR));
60 return;
61 }
62
63 QJsonObject object = document.object();
64
65 QPlaceContent::Collection collection;
66 int totalCount;
67 QPlaceContentRequest previous;
68 QPlaceContentRequest next;
69
70 parseCollection(request().contentType(), object, &collection, &totalCount,
71 &previous, &next, m_engine);
72
73 setTotalCount(totalCount);
74 setContent(collection);
75 setPreviousPageRequest(previous);
76 setNextPageRequest(next);
77
78 setFinished(true);
79 emit finished();
80}
81
82void QPlaceContentReplyImpl::replyError(QNetworkReply::NetworkError error)
83{
84 QNetworkReply *reply = static_cast<QNetworkReply *>(sender());
85 reply->deleteLater();
86 if (error == QNetworkReply::OperationCanceledError)
87 setError(QPlaceReply::CancelError, QStringLiteral("Request cancelled"));
88 else
89 setError(QPlaceReply::CommunicationError, reply->errorString());
90}
91
92QT_END_NAMESPACE