diff options
author | Tomaz Canabrava <tomaz.canabrava@intel.com> | 2015-06-01 15:47:25 -0300 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2015-06-01 13:13:45 -0700 |
commit | b9a32343711008f8db4fb6d26fafe85698161052 (patch) | |
tree | 8d0ead08bd3f63b7984bf56eb476dbc15da74aee /divesitehelpers.cpp | |
parent | d0b5f355ab9805d5f236d20b1b92ef6f71c1f9ad (diff) | |
download | subsurface-b9a32343711008f8db4fb6d26fafe85698161052.tar.gz |
Add a timeout (500ms) to GeoLoccation discovery
GeoLocation discovery would hang if on an incorrectly configured proxy -
this way we will finish as soon as it tries to get everything.
I also need to see what to do with the dive sites if it fails to find any.
Signed-off-by: Tomaz Canabrava <tomaz.canabrava@intel.com>
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
Diffstat (limited to 'divesitehelpers.cpp')
-rw-r--r-- | divesitehelpers.cpp | 37 |
1 files changed, 30 insertions, 7 deletions
diff --git a/divesitehelpers.cpp b/divesitehelpers.cpp index 8564ce2dc..62b48ba94 100644 --- a/divesitehelpers.cpp +++ b/divesitehelpers.cpp @@ -16,6 +16,7 @@ #include <QNetworkAccessManager> #include <QUrlQuery> #include <QEventLoop> +#include <QTimer> struct GeoLookupInfo { degrees_t lat; @@ -46,21 +47,43 @@ void ReverseGeoLookupThread::run() { QString apiCall("http://open.mapquestapi.com/nominatim/v1/reverse.php?format=json&accept-language=%1&lat=%2&lon=%3"); Q_FOREACH (const GeoLookupInfo& info, geo_lookup_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())); + QTimer timer; + timer.setSingleShot(true); + + QEventLoop loop; + connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit())); + connect(reply, SIGNAL(finished()), &loop, SLOT(quit())); + timer.start(500); // 30 secs. timeout loop.exec(); - QJsonParseError errorObject; - QJsonDocument jsonDoc = QJsonDocument::fromJson(reply->readAll(), &errorObject); - if (errorObject.error != QJsonParseError::NoError) { - qDebug() << errorObject.errorString(); - } else { + + if(timer.isActive()) { + timer.stop(); + if(reply->error() > 0) + goto clear_reply; + + int v = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt(); + if (v < 200 || v >= 300) + goto clear_reply; + + QJsonParseError errorObject; + QJsonDocument jsonDoc = QJsonDocument::fromJson(reply->readAll(), &errorObject); + if (errorObject.error != QJsonParseError::NoError) + goto clear_reply; + 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()); + + } else { + disconnect(reply, SIGNAL(finished()), &loop, SLOT(quit())); + reply->abort(); } + clear_reply: reply->deleteLater(); } rgl->deleteLater(); |