summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar Dirk Hohndel <dirk@hohndel.org>2016-01-10 17:39:13 -0800
committerGravatar Dirk Hohndel <dirk@hohndel.org>2016-01-10 20:44:21 -0800
commit69619ef5aa801b85707ca88dad993e1a744c8762 (patch)
tree4894ea98c36723e4cc2967f0e351edce15df8804
parent88291f605cec174de3e64d112bb5a44a16154162 (diff)
downloadsubsurface-69619ef5aa801b85707ca88dad993e1a744c8762.tar.gz
GPS data: delete fixes on the server
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
-rw-r--r--subsurface-core/gpslocation.cpp57
-rw-r--r--subsurface-core/gpslocation.h6
2 files changed, 60 insertions, 3 deletions
diff --git a/subsurface-core/gpslocation.cpp b/subsurface-core/gpslocation.cpp
index 05e934417..72f390df7 100644
--- a/subsurface-core/gpslocation.cpp
+++ b/subsurface-core/gpslocation.cpp
@@ -15,6 +15,7 @@
#include <QJsonArray>
#define GPS_FIX_ADD_URL "http://api.subsurface-divelog.org/api/dive/add/"
+#define GPS_FIX_DELETE_URL "http://api.subsurface-divelog.org/api/dive/delete/"
#define GPS_FIX_DOWNLOAD_URL "http://api.subsurface-divelog.org/api/dive/get/"
#define GET_WEBSERVICE_UID_URL "https://cloud.subsurface-divelog.org/webuserid/"
@@ -391,6 +392,8 @@ void GpsLocation::deleteFixFromStorage(gpsTracker &gt)
when = geoSettings->value(QString("gpsFix%1_time").arg(cnt - 1)).toLongLong();
struct gpsTracker movedTracker = m_trackers.value(when);
movedTracker.idx = gt.idx;
+ m_trackers.remove(movedTracker.when);
+ m_trackers.insert(movedTracker.when, movedTracker);
geoSettings->setValue(QString("gpsFix%1_time").arg(movedTracker.idx), when);
geoSettings->setValue(QString("gpsFix%1_lat").arg(movedTracker.idx), movedTracker.latitude.udeg);
geoSettings->setValue(QString("gpsFix%1_lon").arg(movedTracker.idx), movedTracker.longitude.udeg);
@@ -402,6 +405,7 @@ void GpsLocation::deleteFixFromStorage(gpsTracker &gt)
}
geoSettings->setValue("count", cnt - 1);
geoSettings->sync();
+ m_trackers.remove(gt.when);
}
void GpsLocation::deleteGpsFix(qint64 when)
@@ -413,7 +417,7 @@ void GpsLocation::deleteGpsFix(qint64 when)
return;
}
deleteFixFromStorage(deletedTracker);
- m_trackers.remove(when);
+ m_deletedTrackers.append(deletedTracker);
}
void GpsLocation::clearGpsData()
@@ -433,6 +437,55 @@ void GpsLocation::getUseridError(QNetworkReply::NetworkError error)
status(QString("error when retrieving Subsurface webservice user id: %1").arg(reply->errorString()));
}
+void GpsLocation::deleteFixesFromServer()
+{
+ QEventLoop loop;
+ QTimer timer;
+ timer.setSingleShot(true);
+
+ QNetworkAccessManager *manager = new QNetworkAccessManager(qApp);
+ QUrl url(GPS_FIX_DELETE_URL);
+ QList<qint64> keys = m_trackers.keys();
+ qint64 key;
+ while (!m_deletedTrackers.isEmpty()) {
+ gpsTracker gt = m_deletedTrackers.takeFirst();
+ int idx = gt.idx;
+ QDateTime dt;
+ QUrlQuery data;
+ dt.setTime_t(gt.when - gettimezoneoffset(gt.when));
+ qDebug() << dt.toString() << get_dive_date_string(gt.when);
+ data.addQueryItem("login", prefs.userid);
+ data.addQueryItem("dive_date", dt.toString("yyyy-MM-dd"));
+ data.addQueryItem("dive_time", dt.toString("hh:mm"));
+ status(data.toString(QUrl::FullyEncoded).toUtf8());
+ QNetworkRequest request;
+ request.setUrl(url);
+ request.setRawHeader("User-Agent", getUserAgent().toUtf8());
+ request.setRawHeader("Accept", "text/json");
+ request.setRawHeader("Content-type", "application/x-www-form-urlencoded");
+ reply = manager->post(request, data.toString(QUrl::FullyEncoded).toUtf8());
+ connect(&timer, &QTimer::timeout, &loop, &QEventLoop::quit);
+ connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
+ connect(reply, SIGNAL(error(QNetworkReply::NetworkError)),
+ this, SLOT(postError(QNetworkReply::NetworkError)));
+ timer.start(10000);
+ loop.exec();
+ if (timer.isActive()) {
+ timer.stop();
+ if (reply->error() != QNetworkReply::NoError) {
+ QString response = reply->readAll();
+ status(QString("Server response:") + reply->readAll());
+ }
+ } else {
+ status("Deleting on the server timed out");
+ break;
+ }
+ reply->deleteLater();
+ status(QString("completed deleting gps fix %1 - response: ").arg(gt.idx) + reply->readAll());
+ geoSettings->setValue(QString("gpsFix%1_uploaded").arg(idx), 1);
+ }
+}
+
void GpsLocation::uploadToServer()
{
// we want to do this one at a time (the server prefers that)
@@ -494,6 +547,8 @@ void GpsLocation::uploadToServer()
status(QString("completed sending gps fix %1 - response: ").arg(gt.idx) + reply->readAll());
geoSettings->setValue(QString("gpsFix%1_uploaded").arg(idx), 1);
}
+ // and now remove the ones that were locally deleted
+ deleteFixesFromServer();
}
void GpsLocation::downloadFromServer()
diff --git a/subsurface-core/gpslocation.h b/subsurface-core/gpslocation.h
index 9e5a25af7..20ed26ed9 100644
--- a/subsurface-core/gpslocation.h
+++ b/subsurface-core/gpslocation.h
@@ -45,10 +45,12 @@ private:
static GpsLocation *m_Instance;
bool waitingForPosition;
QMap<qint64, gpsTracker> m_trackers;
- void addFixToStorage(gpsTracker &gt);
- void deleteFixFromStorage(gpsTracker &gt);
+ QList<gpsTracker> m_deletedTrackers;
void loadFromStorage();
+ void addFixToStorage(gpsTracker &gt);
void replaceFixToStorage(gpsTracker &gt);
+ void deleteFixFromStorage(gpsTracker &gt);
+ void deleteFixesFromServer();
public slots:
void serviceEnable(bool toggle);