diff options
author | Tomaz Canabrava <tomaz.canabrava@gmail.com> | 2015-05-10 12:44:35 -0300 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2015-05-10 11:14:20 -0700 |
commit | 627de38c01f6bcba56221ed3703bd08d1d215168 (patch) | |
tree | 1bdd70a21eb5b707dcd11a7b0bf9a18db5ff5ca4 /divesitehelpers.cpp | |
parent | 412844c02b5180e67337de2404629163d0434574 (diff) | |
download | subsurface-627de38c01f6bcba56221ed3703bd08d1d215168.tar.gz |
Get the location information in a separate thread
This makes Subsurface usable faster for those without a good internet
connection when they are opening an older data file.
While parsing, we are only feeding an vector of locations, after the
parsing is done, we traverse the vector searching for the information on
the web.
I need to also add a way to stop if there`s no internet connection - but
this will be another patch.
Also, fixed two small memory leaks from the old imp.
[Dirk Hohndel: cleaned up the whitespace mess]
Signed-off-by: Tomaz Canabrava <tomaz.canabrava@gmail.com>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
Diffstat (limited to 'divesitehelpers.cpp')
-rw-r--r-- | divesitehelpers.cpp | 67 |
1 files changed, 51 insertions, 16 deletions
diff --git a/divesitehelpers.cpp b/divesitehelpers.cpp index b29ca3faa..cc45851f3 100644 --- a/divesitehelpers.cpp +++ b/divesitehelpers.cpp @@ -1,6 +1,9 @@ // // infrastructure to deal with dive sites // + +#include "divesitehelpers.h" + #include "divesite.h" #include "helpers.h" #include "usersurvey.h" @@ -14,28 +17,60 @@ #include <QUrlQuery> #include <QEventLoop> -extern "C" void reverseGeoLookup(degrees_t latitude, degrees_t longitude, uint32_t uuid) +struct GeoLoockupInfo { + degrees_t lat; + degrees_t lon; + uint32_t uuid; +}; + +QVector<GeoLoockupInfo> geo_loockup_data; + +ReverseGeoLoockupThread* ReverseGeoLoockupThread::instance() { + static ReverseGeoLoockupThread* self = new ReverseGeoLoockupThread(); + return self; +} + +ReverseGeoLoockupThread::ReverseGeoLoockupThread(QObject *obj) : QThread(obj) { +} + +void ReverseGeoLoockupThread::run() { + if (geo_loockup_data.isEmpty()) + return; + QNetworkRequest request; QNetworkAccessManager *rgl = new QNetworkAccessManager(); - request.setUrl(QString("http://open.mapquestapi.com/nominatim/v1/reverse.php?format=json&accept-language=%1&lat=%2&lon=%3") - .arg(uiLanguage(NULL)).arg(latitude.udeg / 1000000.0).arg(longitude.udeg / 1000000.0)); request.setRawHeader("Accept", "text/json"); request.setRawHeader("User-Agent", getUserAgent().toUtf8()); - QNetworkReply *reply = rgl->get(request); QEventLoop loop; - QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit())); - loop.exec(); - QJsonParseError errorObject; - QJsonDocument jsonDoc = QJsonDocument::fromJson(reply->readAll(), &errorObject); - if (errorObject.error != QJsonParseError::NoError) { - qDebug() << errorObject.errorString(); - } else { - QJsonObject obj = jsonDoc.object(); - QJsonObject address = obj.value("address").toObject(); - qDebug() << "found country:" << address.value("country").toString(); - struct dive_site *ds = get_dive_site_by_uuid(uuid); - ds->notes = add_to_string(ds->notes, "countrytag: %s", address.value("country").toString().toUtf8().data()); + QString apiCall("http://open.mapquestapi.com/nominatim/v1/reverse.php?format=json&accept-language=%1&lat=%2&lon=%3"); + Q_FOREACH (const GeoLoockupInfo& info, geo_loockup_data ) { + request.setUrl(apiCall.arg(uiLanguage(NULL)).arg(info.lat.udeg / 1000000.0).arg(info.lon.udeg / 1000000.0)); + QNetworkReply *reply = rgl->get(request); + QObject::connect(reply, SIGNAL(finished()), &loop, SLOT(quit())); + loop.exec(); + QJsonParseError errorObject; + QJsonDocument jsonDoc = QJsonDocument::fromJson(reply->readAll(), &errorObject); + if (errorObject.error != QJsonParseError::NoError) { + qDebug() << errorObject.errorString(); + } else { + QJsonObject obj = jsonDoc.object(); + QJsonObject address = obj.value("address").toObject(); + qDebug() << "found country:" << address.value("country").toString(); + struct dive_site *ds = get_dive_site_by_uuid(info.uuid); + ds->notes = add_to_string(ds->notes, "countrytag: %s", address.value("country").toString().toUtf8().data()); + } + + reply->deleteLater(); } + rgl->deleteLater(); } +extern "C" void add_geo_information_for_loockup(degrees_t latitude, degrees_t longitude, uint32_t uuid) { + GeoLoockupInfo info; + info.lat = latitude; + info.lon = longitude; + info.uuid = uuid; + + geo_loockup_data.append(info); +} |