Merge branch 'cloudstorage'

This commit is contained in:
Dirk Hohndel 2015-06-09 17:16:31 -07:00
commit 8282ed2348
7 changed files with 131 additions and 5 deletions

1
pref.h
View file

@ -94,6 +94,7 @@ struct preferences {
char *cloud_storage_email;
char *cloud_storage_email_encoded;
bool save_password_local;
bool show_cloud_pin;
};
enum unit_system_values {
METRIC,

View file

@ -9,6 +9,8 @@
#include <QNetworkProxy>
#include <QNetworkCookieJar>
#include "subsurfacewebservices.h"
#if defined(FBSUPPORT)
#include "socialnetworks.h"
#endif
@ -57,7 +59,6 @@ PreferencesDialog::PreferencesDialog(QWidget *parent, Qt::WindowFlags f) : QDial
connect(ui.btnDisconnectFacebook, &QPushButton::clicked, fb, &FacebookManager::logout);
connect(fb, &FacebookManager::justLoggedOut, this, &PreferencesDialog::facebookDisconnect);
#endif
connect(ui.proxyType, SIGNAL(currentIndexChanged(int)), this, SLOT(proxyType_changed(int)));
connect(ui.buttonBox, SIGNAL(clicked(QAbstractButton *)), this, SLOT(buttonClicked(QAbstractButton *)));
connect(ui.gflow, SIGNAL(valueChanged(int)), this, SLOT(gflowChanged(int)));
@ -102,6 +103,14 @@ void PreferencesDialog::facebookDisconnect()
#endif
}
void PreferencesDialog::cloudPinNeeded(bool toggle)
{
ui.cloud_storage_pin->setEnabled(toggle);
ui.cloud_storage_pin->setVisible(toggle);
ui.cloud_storage_pin_label->setEnabled(toggle);
ui.cloud_storage_pin_label->setVisible(toggle);
}
#define DANGER_GF (gf > 100) ? "* { color: red; }" : ""
void PreferencesDialog::gflowChanged(int gf)
{
@ -208,6 +217,8 @@ void PreferencesDialog::setUiFromPrefs()
ui.cloud_storage_email->setText(prefs.cloud_storage_email);
ui.cloud_storage_password->setText(prefs.cloud_storage_password);
ui.save_password_local->setChecked(prefs.save_password_local);
ui.cloud_storage_pin->setVisible(prefs.show_cloud_pin);
ui.cloud_storage_pin_label->setVisible(prefs.show_cloud_pin);
}
void PreferencesDialog::restorePrefs()
@ -360,12 +371,36 @@ 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 (ui.cloud_storage_pin->isVisible()) {
QString pin = ui.cloud_storage_pin->text();
if (!pin.isEmpty()) {
// 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, pin);
}
} else 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);
connect(cloudAuth, SIGNAL(finishedAuthenticate(bool)), this, SLOT(cloudPinNeeded(bool)));
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");
SAVE_OR_REMOVE("show_cloud_pin", default_prefs.show_cloud_pin, prefs.show_cloud_pin);
s.endGroup();
loadSettings();
emit settingsChanged();
@ -482,6 +517,7 @@ void PreferencesDialog::loadSettings()
GET_TXT("password", cloud_storage_password);
GET_TXT("email", cloud_storage_email);
GET_BOOL("save_password_local", save_password_local);
GET_BOOL("show_cloud_pin", show_cloud_pin);
s.endGroup();
}

View file

@ -36,6 +36,7 @@ slots:
void on_btnUseDefaultFile_toggled(bool toggle);
void facebookLoggedIn();
void facebookDisconnect();
void cloudPinNeeded(bool toggle);
private:
explicit PreferencesDialog(QWidget *parent = 0, Qt::WindowFlags f = 0);
void setUiFromPrefs();

View file

@ -451,6 +451,20 @@
</property>
</widget>
</item>
<item row="0" column="2">
<widget class="QLabel" name="cloud_storage_pin_label">
<property name="text">
<string>Verification PIN</string>
</property>
</widget>
</item>
<item row="1" column="2">
<widget class="QLineEdit" name="cloud_storage_pin">
<property name="toolTip">
<string extracomment="One time verification PIN for Subsurface cloud storage infrastructure"/>
</property>
</widget>
</item>
</layout>
</widget>
</widget>

View file

@ -934,3 +934,61 @@ QNetworkReply* UserSurveyServices::sendSurvey(QString values)
reply = manager()->get(request);
return reply;
}
CloudStorageAuthenticate::CloudStorageAuthenticate(QObject *parent) : QObject(parent)
{
userAgent = getUserAgent();
}
#define CLOUDURL "https://cloud.subsurface-divelog.org/"
#define CLOUDBACKENDSTORAGE CLOUDURL "storage"
#define CLOUDBACKENDVERIFY CLOUDURL "verify"
QNetworkReply* CloudStorageAuthenticate::authenticate(QString email, QString password, QString pin)
{
QString payload(email + " " + password);
QUrl requestUrl;
if (pin == "") {
requestUrl = QUrl(CLOUDBACKENDSTORAGE);
} else {
requestUrl = QUrl(CLOUDBACKENDVERIFY);
payload += " " + pin;
}
QNetworkRequest *request = new QNetworkRequest(requestUrl);
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(payload));
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()
{
QString cloudAuthReply(reply->readAll());
qDebug() << "Completed connection with cloud storage backend, response" << cloudAuthReply;
if (cloudAuthReply == "[VERIFIED]") {
prefs.show_cloud_pin = false;
emit finishedAuthenticate(prefs.show_cloud_pin);
} else if (cloudAuthReply == "[VERIFY]") {
prefs.show_cloud_pin = true;
emit finishedAuthenticate(prefs.show_cloud_pin);
}
}
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();
}
}

View file

@ -109,6 +109,23 @@ slots:
virtual void startDownload() { }
virtual void startUpload() { }
virtual void buttonClicked(QAbstractButton *button) { }
};
class CloudStorageAuthenticate : public QObject {
Q_OBJECT
public:
QNetworkReply* authenticate(QString email, QString password, QString pin = "");
explicit CloudStorageAuthenticate(QObject *parent);
signals:
void finishedAuthenticate(bool toggle);
private
slots:
void uploadError(QNetworkReply::NetworkError error);
void sslErrors(QList<QSslError> errorList);
void uploadFinished();
private:
QNetworkReply *reply;
QString userAgent;
};

View file

@ -1024,8 +1024,7 @@ fraction_t string_to_fraction(const char *str)
int getCloudURL(QString &filename)
{
QString email = QString(prefs.cloud_storage_email);
email.replace("@", "_at_");
email.replace(QRegularExpression("[^a-zA-Z0-9._+-]"), "");
email.replace(QRegularExpression("[^a-zA-Z0-9@._+-]"), "");
if (email.isEmpty() || same_string(prefs.cloud_storage_password, ""))
return report_error("Please configure Cloud storage email and password in the preferences");
if (email != prefs.cloud_storage_email_encoded) {