mirror of
https://github.com/subsurface/subsurface.git
synced 2025-02-19 22:16:15 +00:00
Location service: retrieve the userid using the cloud storage API
This should actually not be in the mobile section at all. This needs to be available on the desktop as well. Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
This commit is contained in:
parent
18e52c1da4
commit
74b15922f3
3 changed files with 73 additions and 1 deletions
|
@ -11,6 +11,9 @@
|
||||||
#include <QApplication>
|
#include <QApplication>
|
||||||
#include <QTimer>
|
#include <QTimer>
|
||||||
|
|
||||||
|
#define GPS_FIX_ADD_URL "http://api.subsurface-divelog.org/api/dive/add/"
|
||||||
|
#define GET_WEBSERVICE_UID_URL "https://cloud.subsurface-divelog.org/webuserid/"
|
||||||
|
|
||||||
GpsLocation::GpsLocation(QObject *parent)
|
GpsLocation::GpsLocation(QObject *parent)
|
||||||
{
|
{
|
||||||
// create a QSettings object that's separate from the main application settings
|
// create a QSettings object that's separate from the main application settings
|
||||||
|
@ -79,6 +82,44 @@ void GpsLocation::status(QString msg)
|
||||||
qmlUiShowMessage(qPrintable(msg));
|
qmlUiShowMessage(qPrintable(msg));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
QString GpsLocation::getUserid(QString user, QString passwd)
|
||||||
|
{
|
||||||
|
qDebug() << "called getUserid";
|
||||||
|
QEventLoop loop;
|
||||||
|
QTimer timer;
|
||||||
|
timer.setSingleShot(true);
|
||||||
|
|
||||||
|
QNetworkAccessManager *manager = new QNetworkAccessManager(qApp);
|
||||||
|
QUrl url(GET_WEBSERVICE_UID_URL);
|
||||||
|
QString data;
|
||||||
|
data = user + " " + passwd;
|
||||||
|
QNetworkRequest request;
|
||||||
|
request.setUrl(url);
|
||||||
|
request.setRawHeader("Accept", "text/html");
|
||||||
|
request.setRawHeader("Content-type", "application/x-www-form-urlencoded");
|
||||||
|
reply = manager->post(request, data.toUtf8());
|
||||||
|
connect(&timer, &QTimer::timeout, &loop, &QEventLoop::quit);
|
||||||
|
connect(reply, &QNetworkReply::finished, &loop, &QEventLoop::quit);
|
||||||
|
connect(reply, SIGNAL(error(QNetworkReply::NetworkError)),
|
||||||
|
this, SLOT(getUseridError(QNetworkReply::NetworkError)));
|
||||||
|
timer.start(10000);
|
||||||
|
loop.exec();
|
||||||
|
if (timer.isActive()) {
|
||||||
|
timer.stop();
|
||||||
|
if (reply->error() == QNetworkReply::NoError) {
|
||||||
|
QString result = reply->readAll();
|
||||||
|
status(QString("received ") + result);
|
||||||
|
result.remove("WebserviceID:");
|
||||||
|
reply->deleteLater();
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
status("Getting Web service ID timed out");
|
||||||
|
}
|
||||||
|
reply->deleteLater();
|
||||||
|
return QString();
|
||||||
|
}
|
||||||
|
|
||||||
int GpsLocation::getGpsNum() const
|
int GpsLocation::getGpsNum() const
|
||||||
{
|
{
|
||||||
return geoSettings->value("count", 0).toInt();
|
return geoSettings->value("count", 0).toInt();
|
||||||
|
@ -223,6 +264,11 @@ void GpsLocation::postError(QNetworkReply::NetworkError error)
|
||||||
status(QString("error when sending a GPS fix: %1").arg(reply->errorString()));
|
status(QString("error when sending a GPS fix: %1").arg(reply->errorString()));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void GpsLocation::getUseridError(QNetworkReply::NetworkError error)
|
||||||
|
{
|
||||||
|
status(QString("error when retrieving Subsurface webservice user id: %1").arg(reply->errorString()));
|
||||||
|
}
|
||||||
|
|
||||||
void GpsLocation::uploadToServer()
|
void GpsLocation::uploadToServer()
|
||||||
{
|
{
|
||||||
// we want to do this one at a time (the server prefers that)
|
// we want to do this one at a time (the server prefers that)
|
||||||
|
@ -231,7 +277,7 @@ void GpsLocation::uploadToServer()
|
||||||
timer.setSingleShot(true);
|
timer.setSingleShot(true);
|
||||||
|
|
||||||
QNetworkAccessManager *manager = new QNetworkAccessManager(qApp);
|
QNetworkAccessManager *manager = new QNetworkAccessManager(qApp);
|
||||||
QUrl url("http://api.subsurface-divelog.org/api/dive/add/");
|
QUrl url(GPS_FIX_ADD_URL);
|
||||||
int count = geoSettings->value("count", 0).toInt();
|
int count = geoSettings->value("count", 0).toInt();
|
||||||
for (int i = 0; i < count; i++) {
|
for (int i = 0; i < count; i++) {
|
||||||
QDateTime dt;
|
QDateTime dt;
|
||||||
|
|
|
@ -16,6 +16,7 @@ public:
|
||||||
GpsLocation(QObject *parent);
|
GpsLocation(QObject *parent);
|
||||||
bool applyLocations();
|
bool applyLocations();
|
||||||
int getGpsNum() const;
|
int getGpsNum() const;
|
||||||
|
QString getUserid(QString user, QString passwd);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QGeoPositionInfo lastPos;
|
QGeoPositionInfo lastPos;
|
||||||
|
@ -32,6 +33,7 @@ public slots:
|
||||||
void updateTimeout();
|
void updateTimeout();
|
||||||
void uploadToServer();
|
void uploadToServer();
|
||||||
void postError(QNetworkReply::NetworkError error);
|
void postError(QNetworkReply::NetworkError error);
|
||||||
|
void getUseridError(QNetworkReply::NetworkError error);
|
||||||
void clearGpsData();
|
void clearGpsData();
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
|
@ -25,6 +25,12 @@ QMLManager::QMLManager() :
|
||||||
setCloudUserName(prefs.cloud_storage_email);
|
setCloudUserName(prefs.cloud_storage_email);
|
||||||
setCloudPassword(prefs.cloud_storage_password);
|
setCloudPassword(prefs.cloud_storage_password);
|
||||||
setSaveCloudPassword(prefs.save_password_local);
|
setSaveCloudPassword(prefs.save_password_local);
|
||||||
|
// if the cloud credentials are valid, we should get the GPS Webservice ID as well
|
||||||
|
if (!same_string(prefs.cloud_storage_email, "") &&
|
||||||
|
!same_string(prefs.cloud_storage_password, "") &&
|
||||||
|
same_string(prefs.userid, ""))
|
||||||
|
locationProvider->getUserid(prefs.cloud_storage_email, prefs.cloud_storage_password);
|
||||||
|
|
||||||
setDistanceThreshold(prefs.distance_threshold);
|
setDistanceThreshold(prefs.distance_threshold);
|
||||||
setTimeThreshold(prefs.time_threshold / 60);
|
setTimeThreshold(prefs.time_threshold / 60);
|
||||||
if (!same_string(prefs.cloud_storage_email, "") && !same_string(prefs.cloud_storage_password, ""))
|
if (!same_string(prefs.cloud_storage_email, "") && !same_string(prefs.cloud_storage_password, ""))
|
||||||
|
@ -38,6 +44,7 @@ QMLManager::~QMLManager()
|
||||||
void QMLManager::savePreferences()
|
void QMLManager::savePreferences()
|
||||||
{
|
{
|
||||||
QSettings s;
|
QSettings s;
|
||||||
|
bool cloudCredentialsChanged = false;
|
||||||
s.beginGroup("LocationService");
|
s.beginGroup("LocationService");
|
||||||
s.setValue("time_threshold", timeThreshold() * 60);
|
s.setValue("time_threshold", timeThreshold() * 60);
|
||||||
prefs.time_threshold = timeThreshold() * 60;
|
prefs.time_threshold = timeThreshold() * 60;
|
||||||
|
@ -53,15 +60,32 @@ void QMLManager::savePreferences()
|
||||||
if (!same_string(prefs.cloud_storage_email, qPrintable(cloudUserName()))) {
|
if (!same_string(prefs.cloud_storage_email, qPrintable(cloudUserName()))) {
|
||||||
free(prefs.cloud_storage_email);
|
free(prefs.cloud_storage_email);
|
||||||
prefs.cloud_storage_email = strdup(qPrintable(cloudUserName()));
|
prefs.cloud_storage_email = strdup(qPrintable(cloudUserName()));
|
||||||
|
cloudCredentialsChanged = true;
|
||||||
}
|
}
|
||||||
if (saveCloudPassword() != prefs.save_password_local)
|
if (saveCloudPassword() != prefs.save_password_local)
|
||||||
prefs.save_password_local = saveCloudPassword();
|
prefs.save_password_local = saveCloudPassword();
|
||||||
|
|
||||||
|
cloudCredentialsChanged |= !same_string(prefs.cloud_storage_password, qPrintable(cloudPassword()));
|
||||||
|
|
||||||
if (saveCloudPassword()) {
|
if (saveCloudPassword()) {
|
||||||
if (!same_string(prefs.cloud_storage_password, qPrintable(cloudPassword()))) {
|
if (!same_string(prefs.cloud_storage_password, qPrintable(cloudPassword()))) {
|
||||||
free(prefs.cloud_storage_password);
|
free(prefs.cloud_storage_password);
|
||||||
prefs.cloud_storage_password = strdup(qPrintable(cloudPassword()));
|
prefs.cloud_storage_password = strdup(qPrintable(cloudPassword()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// if the cloud credentials are valid, we should get the GPS Webservice ID as well
|
||||||
|
if (!same_string(prefs.cloud_storage_email, "") &&
|
||||||
|
!same_string(prefs.cloud_storage_password, "")) {
|
||||||
|
if (same_string(prefs.userid, "") || cloudCredentialsChanged) {
|
||||||
|
QString userid = locationProvider->getUserid(prefs.cloud_storage_email, prefs.cloud_storage_password);
|
||||||
|
if (!userid.isEmpty()) {
|
||||||
|
// overwrite the existing userid
|
||||||
|
free(prefs.userid);
|
||||||
|
prefs.userid = strdup(qPrintable(userid));
|
||||||
|
s.setValue("subsurface_webservice_uid", prefs.userid);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void QMLManager::loadDives()
|
void QMLManager::loadDives()
|
||||||
|
|
Loading…
Add table
Reference in a new issue