diff options
author | Paolo Angelelli <paolo.angelelli@qt.io> | 2016-09-14 16:28:38 +0200 |
---|---|---|
committer | Edward Welbourne <edward.welbourne@qt.io> | 2016-11-29 10:52:47 +0000 |
commit | 0a552f285fdc3760d9755109ceef3b87392a308d (patch) | |
tree | 943e169f0c002d8e9f74badd0f3ad7a452229e4d /src/plugins/geoservices/osm/qgeocodereplyosm.cpp | |
parent | 9b76e151b897698e285e4e39158b5934dc498bd1 (diff) | |
download | qtlocation-0a552f285fdc3760d9755109ceef3b87392a308d.tar.gz |
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 <alexander.blasche@qt.io>
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Diffstat (limited to 'src/plugins/geoservices/osm/qgeocodereplyosm.cpp')
-rw-r--r-- | src/plugins/geoservices/osm/qgeocodereplyosm.cpp | 48 |
1 files changed, 16 insertions, 32 deletions
diff --git a/src/plugins/geoservices/osm/qgeocodereplyosm.cpp b/src/plugins/geoservices/osm/qgeocodereplyosm.cpp index 15b1724e..a30601d0 100644 --- a/src/plugins/geoservices/osm/qgeocodereplyosm.cpp +++ b/src/plugins/geoservices/osm/qgeocodereplyosm.cpp @@ -50,31 +50,23 @@ QT_BEGIN_NAMESPACE QGeoCodeReplyOsm::QGeoCodeReplyOsm(QNetworkReply *reply, QObject *parent) -: QGeoCodeReply(parent), m_reply(reply) +: QGeoCodeReply(parent) { - connect(m_reply, SIGNAL(finished()), this, SLOT(networkReplyFinished())); - connect(m_reply, SIGNAL(error(QNetworkReply::NetworkError)), + if (!reply) { + setError(UnknownError, QStringLiteral("Null reply")); + return; + } + connect(reply, SIGNAL(finished()), this, SLOT(networkReplyFinished())); + connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(networkReplyError(QNetworkReply::NetworkError))); - + connect(this, &QGeoCodeReply::aborted, reply, &QNetworkReply::abort); + connect(this, &QObject::destroyed, reply, &QObject::deleteLater); setLimit(1); setOffset(0); } QGeoCodeReplyOsm::~QGeoCodeReplyOsm() { - if (m_reply) - m_reply->deleteLater(); -} - -void QGeoCodeReplyOsm::abort() -{ - if (!m_reply) - return; - - m_reply->abort(); - - m_reply->deleteLater(); - m_reply = 0; } static QGeoAddress parseAddressObject(const QJsonObject &object) @@ -108,14 +100,14 @@ static QGeoAddress parseAddressObject(const QJsonObject &object) void QGeoCodeReplyOsm::networkReplyFinished() { - if (!m_reply) - return; + QNetworkReply *reply = static_cast<QNetworkReply *>(sender()); + reply->deleteLater(); - if (m_reply->error() != QNetworkReply::NoError) + if (reply->error() != QNetworkReply::NoError) return; QList<QGeoLocation> locations; - QJsonDocument document = QJsonDocument::fromJson(m_reply->readAll()); + QJsonDocument document = QJsonDocument::fromJson(reply->readAll()); if (document.isObject()) { QJsonObject object = document.object(); @@ -169,22 +161,14 @@ void QGeoCodeReplyOsm::networkReplyFinished() setLocations(locations); setFinished(true); - - m_reply->deleteLater(); - m_reply = 0; } void QGeoCodeReplyOsm::networkReplyError(QNetworkReply::NetworkError error) { Q_UNUSED(error) - - if (!m_reply) - return; - - setError(QGeoCodeReply::CommunicationError, m_reply->errorString()); - - m_reply->deleteLater(); - m_reply = 0; + QNetworkReply *reply = static_cast<QNetworkReply *>(sender()); + reply->deleteLater(); + setError(QGeoCodeReply::CommunicationError, reply->errorString()); } QT_END_NAMESPACE |