diff options
-rw-r--r-- | parse-xml.c | 6 | ||||
-rw-r--r-- | qthelper.cpp | 38 |
2 files changed, 43 insertions, 1 deletions
diff --git a/parse-xml.c b/parse-xml.c index 4675dad01..ecef235ef 100644 --- a/parse-xml.c +++ b/parse-xml.c @@ -1162,6 +1162,7 @@ static void gps_location(char *buffer, struct dive_site *ds) /* this is in qthelper.cpp, so including the .h file is a pain */ extern const char *printGPSCoords(int lat, int lon); +extern void reverseGeoLookup(degrees_t, degrees_t, uint32_t); static void gps_in_dive(char *buffer, struct dive *dive) { @@ -1179,10 +1180,11 @@ static void gps_in_dive(char *buffer, struct dive *dive) } else { fprintf(stderr, "found no uuid in dive, no existing dive site with these coordinates, creating a new divesite without name and above GPS\n"); dive->dive_site_uuid = create_dive_site_with_gps("", latitude, longitude); + ds = get_dive_site_by_uuid(dive->dive_site_uuid); } } else { fprintf(stderr, "found uuid in dive, checking to see if we should add GPS\n"); - struct dive_site *ds = get_dive_site_by_uuid(uuid); + ds = get_dive_site_by_uuid(uuid); if (dive_site_has_gps_location(ds) && (latitude.udeg != 0 || longitude.udeg != 0) && (ds->latitude.udeg != latitude.udeg || ds->longitude.udeg != longitude.udeg)) { @@ -1198,6 +1200,8 @@ static void gps_in_dive(char *buffer, struct dive *dive) ds->longitude = longitude; } } + if (ds && (!ds->notes || strstr(ds->notes, "countrytag:") == NULL)) + reverseGeoLookup(latitude, longitude, dive->dive_site_uuid); } static void add_dive_site(char *buffer, struct dive *dive) diff --git a/qthelper.cpp b/qthelper.cpp index 8d4703b0a..a4b644304 100644 --- a/qthelper.cpp +++ b/qthelper.cpp @@ -1,6 +1,10 @@ #include "qthelper.h" +#include "helpers.h" #include "gettextfromc.h" #include "statistics.h" +#include "subsurfacewebservices.h" +#include "usersurvey.h" +#include "membuffer.h" #include <exif.h> #include "file.h" #include <QFile> @@ -8,6 +12,15 @@ #include <QDir> #include <QDebug> #include <QSettings> +#include <QJsonDocument> +#include <QJsonArray> +#include <QJsonObject> +#include <QNetworkReply> +#include <QNetworkRequest> +#include <QNetworkAccessManager> +#include <QUrlQuery> +#include <QEventLoop> + #include <libxslt/documents.h> #define translate(_context, arg) trGettext(arg) @@ -342,3 +355,28 @@ void selectedDivesGasUsed(QVector<QPair<QString, int> > &gasUsedOrdered) } qSort(gasUsedOrdered.begin(), gasUsedOrdered.end(), lessThan); } + +extern "C" void reverseGeoLookup(degrees_t latitude, degrees_t longitude, uint32_t uuid) +{ + 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", UserSurvey::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()); + } +} |