From 0a552f285fdc3760d9755109ceef3b87392a308d Mon Sep 17 00:00:00 2001 From: Paolo Angelelli Date: Wed, 14 Sep 2016 16:28:38 +0200 Subject: Refactoring: removing m_reply from GeoReply classes This patch tries to simplify the code removing the contained m_reply from all the Geo[Map,Route,Geocode,Places]Reply classes. The need for m_reply was associated to the "abort" method, but this can be solved by emitting a signal in the superclass abort() method, and connecting that to QNetworkReply::abort() in the constructor. Since QNetworkReplyHttpImpl always sends an OperationCanceledError it should then be safe to call deleteLater() on the network reply in the slot connected to QNetworkReply::error This patch also prevents the series of "QCoreApplication::postEvent: Unexpected null receiver" warnings that are generated due to deletingLater already deleted objects (abort() emits an error, the reply is destroyed inside the onError slot, but also in the abort() method). Finally, this patch removes the setFinished() call in QGeoRouteReply::abort() since the documentation does not mention this, and all the subclasses do not perform this operation and emit the corresponding signal. tst_qgeoroutereply has been adapted accordingly. Change-Id: I226ee163e7bed784dd7f0da1522e651459543bca Reviewed-by: Alex Blasche Reviewed-by: Edward Welbourne --- .../placesv2/qplacesearchsuggestionreplyimpl.cpp | 48 +++++++++++----------- 1 file changed, 24 insertions(+), 24 deletions(-) (limited to 'src/plugins/geoservices/nokia/placesv2/qplacesearchsuggestionreplyimpl.cpp') diff --git a/src/plugins/geoservices/nokia/placesv2/qplacesearchsuggestionreplyimpl.cpp b/src/plugins/geoservices/nokia/placesv2/qplacesearchsuggestionreplyimpl.cpp index 6ed8b5a2..9882545d 100644 --- a/src/plugins/geoservices/nokia/placesv2/qplacesearchsuggestionreplyimpl.cpp +++ b/src/plugins/geoservices/nokia/placesv2/qplacesearchsuggestionreplyimpl.cpp @@ -46,25 +46,23 @@ QT_BEGIN_NAMESPACE QPlaceSearchSuggestionReplyImpl::QPlaceSearchSuggestionReplyImpl(QNetworkReply *reply, QObject *parent) -: QPlaceSearchSuggestionReply(parent), m_reply(reply) +: QPlaceSearchSuggestionReply(parent) { - if (!m_reply) + if (!reply) { + setError(UnknownError, QStringLiteral("Null reply")); return; - - m_reply->setParent(this); - connect(m_reply, SIGNAL(finished()), this, SLOT(replyFinished())); + } + connect(reply, SIGNAL(finished()), this, SLOT(replyFinished())); + connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), + this, SLOT(replyError(QNetworkReply::NetworkError))); + connect(this, &QPlaceReply::aborted, reply, &QNetworkReply::abort); + connect(this, &QObject::destroyed, reply, &QObject::deleteLater); } QPlaceSearchSuggestionReplyImpl::~QPlaceSearchSuggestionReplyImpl() { } -void QPlaceSearchSuggestionReplyImpl::abort() -{ - if (m_reply) - m_reply->abort(); -} - void QPlaceSearchSuggestionReplyImpl::setError(QPlaceReply::Error error_, const QString &errorString) { @@ -76,18 +74,13 @@ void QPlaceSearchSuggestionReplyImpl::setError(QPlaceReply::Error error_, void QPlaceSearchSuggestionReplyImpl::replyFinished() { - if (m_reply->error() != QNetworkReply::NoError) { - switch (m_reply->error()) { - case QNetworkReply::OperationCanceledError: - setError(CancelError, "Request canceled."); - break; - default: - setError(CommunicationError, "Network error."); - } + QNetworkReply *reply = static_cast(sender()); + reply->deleteLater(); + + if (reply->error() != QNetworkReply::NoError) return; - } - QJsonDocument document = QJsonDocument::fromJson(m_reply->readAll()); + QJsonDocument document = QJsonDocument::fromJson(reply->readAll()); if (!document.isObject()) { setError(ParseError, QCoreApplication::translate(NOKIA_PLUGIN_CONTEXT_NAME, PARSE_ERROR)); emit error(error(), errorString()); @@ -107,11 +100,18 @@ void QPlaceSearchSuggestionReplyImpl::replyFinished() setSuggestions(s); - m_reply->deleteLater(); - m_reply = 0; - setFinished(true); emit finished(); } +void QPlaceSearchSuggestionReplyImpl::replyError(QNetworkReply::NetworkError error) +{ + QNetworkReply *reply = static_cast(sender()); + reply->deleteLater(); + if (error == QNetworkReply::OperationCanceledError) + setError(QPlaceReply::CancelError, QCoreApplication::translate(NOKIA_PLUGIN_CONTEXT_NAME, CANCEL_ERROR)); + else + setError(QPlaceReply::CommunicationError, QCoreApplication::translate(NOKIA_PLUGIN_CONTEXT_NAME, NETWORK_ERROR)); +} + QT_END_NAMESPACE -- cgit v1.2.1