From b9a32343711008f8db4fb6d26fafe85698161052 Mon Sep 17 00:00:00 2001 From: Tomaz Canabrava Date: Mon, 1 Jun 2015 15:47:25 -0300 Subject: 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 Signed-off-by: Dirk Hohndel --- divesitehelpers.cpp | 37 ++++++++++++++++++++++++++++++------- 1 file changed, 30 insertions(+), 7 deletions(-) (limited to 'divesitehelpers.cpp') diff --git a/divesitehelpers.cpp b/divesitehelpers.cpp index 8564ce2dc..62b48ba94 100644 --- a/divesitehelpers.cpp +++ b/divesitehelpers.cpp @@ -16,6 +16,7 @@ #include #include #include +#include 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(); -- cgit v1.2.3-70-g09d2