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
qplacesearchsuggestionreplyimpl.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
5#include "../qgeoerror_messages.h"
6
7#include <QCoreApplication>
8#include <QtCore/QJsonDocument>
9#include <QtCore/QJsonObject>
10#include <QtCore/QJsonArray>
11
13
14QPlaceSearchSuggestionReplyImpl::QPlaceSearchSuggestionReplyImpl(QNetworkReply *reply,
17{
18 if (!reply) {
19 setError(UnknownError, QStringLiteral("Null reply"));
20 return;
21 }
22 connect(reply, &QNetworkReply::finished,
23 this, &QPlaceSearchSuggestionReplyImpl::replyFinished);
24 connect(reply, &QNetworkReply::errorOccurred,
25 this, &QPlaceSearchSuggestionReplyImpl::replyError);
26 connect(this, &QPlaceReply::aborted, reply, &QNetworkReply::abort);
27 connect(this, &QObject::destroyed, reply, &QObject::deleteLater);
28}
29
30QPlaceSearchSuggestionReplyImpl::~QPlaceSearchSuggestionReplyImpl()
31{
32}
33
34void QPlaceSearchSuggestionReplyImpl::setError(QPlaceReply::Error error_,
35 const QString &errorString)
36{
37 QPlaceReply::setError(error_, errorString);
38 emit errorOccurred(error_, errorString);
39 setFinished(true);
40 emit finished();
41}
42
43void QPlaceSearchSuggestionReplyImpl::replyFinished()
44{
45 QNetworkReply *reply = static_cast<QNetworkReply *>(sender());
46 reply->deleteLater();
47
48 if (reply->error() != QNetworkReply::NoError)
49 return;
50
51 QJsonDocument document = QJsonDocument::fromJson(reply->readAll());
52 if (!document.isObject()) {
53 setError(ParseError, QCoreApplication::translate(NOKIA_PLUGIN_CONTEXT_NAME, PARSE_ERROR));
54 emit errorOccurred(error(), errorString());
55 return;
56 }
57
58 QJsonObject object = document.object();
59
60 QJsonArray suggestions = object.value(QStringLiteral("suggestions")).toArray();
61
62 QStringList s;
63 for (int i = 0; i < suggestions.count(); ++i) {
64 QJsonValue v = suggestions.at(i);
65 if (v.isString())
66 s.append(v.toString());
67 }
68
69 setSuggestions(s);
70
71 setFinished(true);
72 emit finished();
73}
74
75void QPlaceSearchSuggestionReplyImpl::replyError(QNetworkReply::NetworkError error)
76{
77 QNetworkReply *reply = static_cast<QNetworkReply *>(sender());
78 reply->deleteLater();
79 if (error == QNetworkReply::OperationCanceledError)
80 setError(QPlaceReply::CancelError, QCoreApplication::translate(NOKIA_PLUGIN_CONTEXT_NAME, CANCEL_ERROR));
81 else
82 setError(QPlaceReply::CommunicationError, QCoreApplication::translate(NOKIA_PLUGIN_CONTEXT_NAME, NETWORK_ERROR));
83}
84
85QT_END_NAMESPACE
The QNetworkReply class contains the data and headers for a request sent with QNetworkAccessManager.
QObject * parent
Definition qobject.h:73
Combined button and popup list for selecting options.