diff options
author | Berthold Stoeger <bstoeger@mail.tuwien.ac.at> | 2018-10-11 20:07:20 +0200 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2018-10-11 16:25:02 -0700 |
commit | cb2f09a8e3673e420970d1d2c0f5c1688a0e1fbf (patch) | |
tree | a115d200a6bbb6d28b31ad4b27bb9d1e1c2d1d0d /core/divesitehelpers.cpp | |
parent | ce8199cdfdef97437ad85178c7104f307baf018b (diff) | |
download | subsurface-cb2f09a8e3673e420970d1d2c0f5c1688a0e1fbf.tar.gz |
Geo lookup: make the QNetworkAccessManager a static object
According to Qt's docs, the QNetworkAccessManager is supposed
to be a long-living object. Therefore, don't create one on
every geo-lookup, but a single object for all geo-lookups.
By making the object function-local it is only initiaized
on first use. Morover this limits the amount of concurrent
geo lookups.
Signed-off-by: Berthold Stoeger <bstoeger@mail.tuwien.ac.at>
Diffstat (limited to 'core/divesitehelpers.cpp')
-rw-r--r-- | core/divesitehelpers.cpp | 11 |
1 files changed, 6 insertions, 5 deletions
diff --git a/core/divesitehelpers.cpp b/core/divesitehelpers.cpp index b77bc8fec..7a7176624 100644 --- a/core/divesitehelpers.cpp +++ b/core/divesitehelpers.cpp @@ -22,8 +22,11 @@ void reverseGeoLookup() { + // By making the QNetworkAccessManager static and local to this function, + // only one manager exists for all geo-lookups and it is only initialized + // on first call to this function. + static QNetworkAccessManager rgl; QNetworkRequest request; - QNetworkAccessManager *rgl = new QNetworkAccessManager(); QEventLoop loop; QString geonamesURL("http://api.geonames.org/findNearbyPlaceNameJSON?lang=%1&lat=%2&lng=%3&radius=50&username=dirkhh"); QString geonamesOceanURL("http://api.geonames.org/oceanJSON?lang=%1&lat=%2&lng=%3&radius=50&username=dirkhh"); @@ -38,7 +41,7 @@ void reverseGeoLookup() // first check the findNearbyPlaces API from geonames - that should give us country, state, city request.setUrl(geonamesURL.arg(uiLanguage(NULL).section(QRegExp("[-_ ]"), 0, 0)).arg(ds->latitude.udeg / 1000000.0).arg(ds->longitude.udeg / 1000000.0)); - QNetworkReply *reply = rgl->get(request); + QNetworkReply *reply = rgl.get(request); timer.setSingleShot(true); QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit())); timer.start(5000); // 5 secs. timeout @@ -114,7 +117,7 @@ void reverseGeoLookup() } // next check the oceans API to figure out the body of water request.setUrl(geonamesOceanURL.arg(uiLanguage(NULL).section(QRegExp("[-_ ]"), 0, 0)).arg(ds->latitude.udeg / 1000000.0).arg(ds->longitude.udeg / 1000000.0)); - reply = rgl->get(request); + reply = rgl.get(request); QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit())); timer.start(5000); // 5 secs. timeout loop.exec(); @@ -161,6 +164,4 @@ void reverseGeoLookup() clear_reply: reply->deleteLater(); - - rgl->deleteLater(); } |