diff options
author | Dirk Hohndel <dirk@hohndel.org> | 2015-06-04 17:26:30 -0700 |
---|---|---|
committer | Dirk Hohndel <dirk@hohndel.org> | 2015-06-07 09:54:35 -0700 |
commit | 318bf5cccc9a8ac2c8ee18939e3c6c4a4e7a0fb3 (patch) | |
tree | 25ee266d20b6e8b85a225d562a5574ae4d4da785 /qt-ui | |
parent | d9801b67b4dc7b925b356d2574cafaa03e598a3b (diff) | |
download | subsurface-318bf5cccc9a8ac2c8ee18939e3c6c4a4e7a0fb3.tar.gz |
Cloud storage: first stab at creating an account on the backend
This triggers when the email address / password is changed in the
preferences. It opens an https connection with the backend server (the URL
is hardcoded) which should create an account with these credentials.
Signed-off-by: Dirk Hohndel <dirk@hohndel.org>
Diffstat (limited to 'qt-ui')
-rw-r--r-- | qt-ui/preferences.cpp | 17 | ||||
-rw-r--r-- | qt-ui/subsurfacewebservices.cpp | 40 | ||||
-rw-r--r-- | qt-ui/subsurfacewebservices.h | 15 |
3 files changed, 70 insertions, 2 deletions
diff --git a/qt-ui/preferences.cpp b/qt-ui/preferences.cpp index f0f1212a2..8342dff78 100644 --- a/qt-ui/preferences.cpp +++ b/qt-ui/preferences.cpp @@ -9,6 +9,8 @@ #include <QNetworkProxy> #include <QNetworkCookieJar> +#include "subsurfacewebservices.h" + #if defined(FBSUPPORT) #include "socialnetworks.h" #endif @@ -360,10 +362,21 @@ void PreferencesDialog::syncSettings() s.endGroup(); s.beginGroup("CloudStorage"); - SAVE_OR_REMOVE("email", default_prefs.cloud_storage_email, ui.cloud_storage_email->text()); + QString email = ui.cloud_storage_email->text(); + QString password = ui.cloud_storage_password->text(); + if (email != prefs.cloud_storage_email || password != prefs.cloud_storage_password) { + // connect to backend server to check / create credentials + QRegularExpression reg("^[a-zA-Z0-9@.+_-]+$"); + if (!reg.match(email).hasMatch() || !reg.match(password).hasMatch()) { + report_error(qPrintable(tr("Cloud storage email and password can only consist of letters, numbers, and '.', '-', '_', and '+'."))); + } + CloudStorageAuthenticate *cloudAuth = new CloudStorageAuthenticate(this); + QNetworkReply *reply = cloudAuth->authenticate(email, password); + } + SAVE_OR_REMOVE("email", default_prefs.cloud_storage_email, email); SAVE_OR_REMOVE("save_password_local", default_prefs.save_password_local, ui.save_password_local->isChecked()); if (ui.save_password_local->isChecked()) - SAVE_OR_REMOVE("password", default_prefs.cloud_storage_password, ui.cloud_storage_password->text()); + SAVE_OR_REMOVE("password", default_prefs.cloud_storage_password, password); else s.remove("password"); s.endGroup(); diff --git a/qt-ui/subsurfacewebservices.cpp b/qt-ui/subsurfacewebservices.cpp index 0159977a7..5681974ec 100644 --- a/qt-ui/subsurfacewebservices.cpp +++ b/qt-ui/subsurfacewebservices.cpp @@ -928,3 +928,43 @@ QNetworkReply* UserSurveyServices::sendSurvey(QString values) reply = manager()->get(request); return reply; } + +CloudStorageAuthenticate::CloudStorageAuthenticate(QObject *parent) : QObject(parent) +{ + userAgent = getUserAgent(); + +} + +#define CLOUDBACKEND "https://cloud.subsurface-divelog.org/storage" + +QNetworkReply* CloudStorageAuthenticate::authenticate(QString email, QString password) +{ + QNetworkRequest *request = new QNetworkRequest(QUrl(CLOUDBACKEND)); + request->setRawHeader("Accept", "text/xml, text/plain"); + request->setRawHeader("User-Agent", userAgent.toUtf8()); + request->setHeader(QNetworkRequest::ContentTypeHeader, "text/plain"); + reply = WebServices::manager()->post(*request, qPrintable(QString(email + " " + password))); + connect(reply, SIGNAL(finished()), this, SLOT(uploadFinished())); + connect(reply, SIGNAL(sslErrors(QList<QSslError>)), this, SLOT(sslErrors(QList<QSslError>))); + connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, + SLOT(uploadError(QNetworkReply::NetworkError))); + return reply; +} + +void CloudStorageAuthenticate::uploadFinished() +{ + qDebug() << "Completed connection with cloud storage backend, response" << reply->readAll(); +} + +void CloudStorageAuthenticate::uploadError(QNetworkReply::NetworkError error) +{ + qDebug() << "Received error response from cloud storage backend:" << reply->errorString(); +} + +void CloudStorageAuthenticate::sslErrors(QList<QSslError> errorList) +{ + qDebug() << "Received error response trying to set up https connection with cloud storage backend:"; + Q_FOREACH (QSslError err, errorList) { + qDebug() << err.errorString(); + } +} diff --git a/qt-ui/subsurfacewebservices.h b/qt-ui/subsurfacewebservices.h index a9735382c..48f93da92 100644 --- a/qt-ui/subsurfacewebservices.h +++ b/qt-ui/subsurfacewebservices.h @@ -109,6 +109,21 @@ slots: virtual void startDownload() { } virtual void startUpload() { } virtual void buttonClicked(QAbstractButton *button) { } +}; + +class CloudStorageAuthenticate : QObject { + Q_OBJECT +public: + QNetworkReply* authenticate(QString email, QString password); + explicit CloudStorageAuthenticate(QObject *parent); +private +slots: + void uploadError(QNetworkReply::NetworkError error); + void sslErrors(QList<QSslError> errorList); + void uploadFinished(); +private: + QNetworkReply *reply; + QString userAgent; }; |