2015-02-23 17:09:48 +00:00
|
|
|
//
|
|
|
|
// infrastructure to deal with dive sites
|
|
|
|
//
|
2015-05-10 15:44:35 +00:00
|
|
|
|
|
|
|
#include "divesitehelpers.h"
|
|
|
|
|
2015-02-23 17:09:48 +00:00
|
|
|
#include "divesite.h"
|
|
|
|
#include "helpers.h"
|
|
|
|
#include "usersurvey.h"
|
|
|
|
#include "membuffer.h"
|
|
|
|
#include <QJsonDocument>
|
|
|
|
#include <QJsonArray>
|
|
|
|
#include <QJsonObject>
|
|
|
|
#include <QNetworkReply>
|
|
|
|
#include <QNetworkRequest>
|
|
|
|
#include <QNetworkAccessManager>
|
|
|
|
#include <QUrlQuery>
|
|
|
|
#include <QEventLoop>
|
|
|
|
|
2015-05-15 21:27:08 +00:00
|
|
|
struct GeoLookupInfo {
|
2015-05-10 15:44:35 +00:00
|
|
|
degrees_t lat;
|
|
|
|
degrees_t lon;
|
|
|
|
uint32_t uuid;
|
|
|
|
};
|
|
|
|
|
2015-05-15 21:27:08 +00:00
|
|
|
QVector<GeoLookupInfo> geo_lookup_data;
|
2015-05-10 15:44:35 +00:00
|
|
|
|
2015-05-15 21:27:08 +00:00
|
|
|
ReverseGeoLookupThread* ReverseGeoLookupThread::instance() {
|
|
|
|
static ReverseGeoLookupThread* self = new ReverseGeoLookupThread();
|
2015-05-10 15:44:35 +00:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2015-05-15 21:27:08 +00:00
|
|
|
ReverseGeoLookupThread::ReverseGeoLookupThread(QObject *obj) : QThread(obj)
|
2015-02-23 17:09:48 +00:00
|
|
|
{
|
2015-05-10 15:44:35 +00:00
|
|
|
}
|
|
|
|
|
2015-05-15 21:27:08 +00:00
|
|
|
void ReverseGeoLookupThread::run() {
|
|
|
|
if (geo_lookup_data.isEmpty())
|
2015-05-10 15:44:35 +00:00
|
|
|
return;
|
|
|
|
|
2015-02-23 17:09:48 +00:00
|
|
|
QNetworkRequest request;
|
|
|
|
QNetworkAccessManager *rgl = new QNetworkAccessManager();
|
|
|
|
request.setRawHeader("Accept", "text/json");
|
|
|
|
request.setRawHeader("User-Agent", getUserAgent().toUtf8());
|
|
|
|
QEventLoop loop;
|
2015-05-10 15:44:35 +00:00
|
|
|
QString apiCall("http://open.mapquestapi.com/nominatim/v1/reverse.php?format=json&accept-language=%1&lat=%2&lon=%3");
|
2015-05-15 21:27:08 +00:00
|
|
|
Q_FOREACH (const GeoLookupInfo& info, geo_lookup_data ) {
|
2015-05-10 15:44:35 +00:00
|
|
|
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();
|
2015-02-23 17:09:48 +00:00
|
|
|
}
|
2015-05-10 15:44:35 +00:00
|
|
|
rgl->deleteLater();
|
2015-02-23 17:09:48 +00:00
|
|
|
}
|
|
|
|
|
2015-05-15 21:27:08 +00:00
|
|
|
extern "C" void add_geo_information_for_lookup(degrees_t latitude, degrees_t longitude, uint32_t uuid) {
|
|
|
|
GeoLookupInfo info;
|
2015-05-10 15:44:35 +00:00
|
|
|
info.lat = latitude;
|
|
|
|
info.lon = longitude;
|
|
|
|
info.uuid = uuid;
|
|
|
|
|
2015-05-15 21:27:08 +00:00
|
|
|
geo_lookup_data.append(info);
|
2015-05-10 15:44:35 +00:00
|
|
|
}
|