2019-12-06 13:53:14 +00:00
// SPDX-License-Identifier: GPL-2.0
# include "preferences_cloud.h"
# include "ui_preferences_cloud.h"
# include "subsurfacewebservices.h"
# include "core/cloudstorage.h"
# include "core/errorhelper.h"
# include "core/settings/qPrefCloudStorage.h"
# include <QRegularExpression>
2022-07-17 23:46:15 +00:00
# include <QMessageBox>
2024-01-25 14:51:40 +00:00
# include <QDesktopServices>
2019-12-06 13:53:14 +00:00
PreferencesCloud : : PreferencesCloud ( ) : AbstractPreferencesWidget ( tr ( " Cloud " ) , QIcon ( " :preferences-cloud-icon " ) , 9 ) , ui ( new Ui : : PreferencesCloud ( ) )
{
ui - > setupUi ( this ) ;
ui - > label_help2 - > setWordWrap ( true ) ;
ui - > label_help3 - > setWordWrap ( true ) ;
ui - > label_help4 - > setWordWrap ( true ) ;
}
PreferencesCloud : : ~ PreferencesCloud ( )
{
delete ui ;
}
2024-01-25 14:51:40 +00:00
void PreferencesCloud : : on_resetPassword_clicked ( )
{
QDesktopServices : : openUrl ( QUrl ( " https://cloud.subsurface-divelog.org/passwordreset " ) ) ;
}
2019-12-06 13:53:14 +00:00
void PreferencesCloud : : refreshSettings ( )
{
2024-06-13 20:59:32 +00:00
ui - > cloud_storage_email - > setText ( QString : : fromStdString ( prefs . cloud_storage_email ) ) ;
ui - > cloud_storage_password - > setText ( QString : : fromStdString ( prefs . cloud_storage_password ) ) ;
2019-12-06 13:53:14 +00:00
ui - > save_password_local - > setChecked ( prefs . save_password_local ) ;
updateCloudAuthenticationState ( ) ;
}
void PreferencesCloud : : syncSettings ( )
{
auto cloud = qPrefCloudStorage : : instance ( ) ;
QString email = ui - > cloud_storage_email - > text ( ) . toLower ( ) ;
QString password = ui - > cloud_storage_password - > text ( ) ;
QString newpassword = ui - > cloud_storage_new_passwd - > text ( ) ;
2022-07-19 09:41:34 +00:00
QString emailpasswordformatwarning = tr ( " Change ignored. Cloud storage email and new password can only consist of letters, numbers, and '.', '-', '_', and '+'. " ) ;
2019-12-06 13:53:14 +00:00
//TODO: Change this to the Cloud Storage Stuff, not preferences.
if ( prefs . cloud_verification_status = = qPrefCloudStorage : : CS_VERIFIED & & ! newpassword . isEmpty ( ) ) {
// deal with password change
if ( ! email . isEmpty ( ) & & ! password . isEmpty ( ) ) {
// connect to backend server to check / create credentials
QRegularExpression reg ( " ^[a-zA-Z0-9@.+_-]+$ " ) ;
if ( ! reg . match ( email ) . hasMatch ( ) | | ( ! password . isEmpty ( ) & & ! reg . match ( password ) . hasMatch ( ) ) ) {
2022-07-17 23:46:15 +00:00
QMessageBox : : warning ( this , tr ( " Warning " ) , emailpasswordformatwarning ) ;
2019-12-06 13:53:14 +00:00
return ;
}
if ( ! reg . match ( email ) . hasMatch ( ) | | ( ! newpassword . isEmpty ( ) & & ! reg . match ( newpassword ) . hasMatch ( ) ) ) {
2022-07-17 23:46:15 +00:00
QMessageBox : : warning ( this , tr ( " Warning " ) , emailpasswordformatwarning ) ;
2024-06-13 20:59:32 +00:00
ui - > cloud_storage_new_passwd - > setText ( QString ( ) ) ;
2019-12-06 13:53:14 +00:00
return ;
}
CloudStorageAuthenticate * cloudAuth = new CloudStorageAuthenticate ( this ) ;
connect ( cloudAuth , & CloudStorageAuthenticate : : finishedAuthenticate , this , & PreferencesCloud : : updateCloudAuthenticationState ) ;
connect ( cloudAuth , & CloudStorageAuthenticate : : passwordChangeSuccessful , this , & PreferencesCloud : : passwordUpdateSuccessful ) ;
cloudAuth - > backend ( email , password , " " , newpassword ) ;
2024-06-13 20:59:32 +00:00
ui - > cloud_storage_new_passwd - > setText ( QString ( ) ) ;
2019-12-06 13:53:14 +00:00
}
} else if ( prefs . cloud_verification_status = = qPrefCloudStorage : : CS_UNKNOWN | |
prefs . cloud_verification_status = = qPrefCloudStorage : : CS_INCORRECT_USER_PASSWD | |
2024-06-13 20:59:32 +00:00
email . toStdString ( ) ! = prefs . cloud_storage_email | |
password . toStdString ( ) ! = prefs . cloud_storage_password ) {
2019-12-06 13:53:14 +00:00
// different credentials - reset verification status
int oldVerificationStatus = cloud - > cloud_verification_status ( ) ;
cloud - > set_cloud_verification_status ( qPrefCloudStorage : : CS_UNKNOWN ) ;
if ( ! email . isEmpty ( ) & & ! password . isEmpty ( ) ) {
// connect to backend server to check / create credentials
QRegularExpression reg ( " ^[a-zA-Z0-9@.+_-]+$ " ) ;
if ( ! reg . match ( email ) . hasMatch ( ) | | ( ! password . isEmpty ( ) & & ! reg . match ( password ) . hasMatch ( ) ) ) {
2022-07-17 23:46:15 +00:00
QMessageBox : : warning ( this , tr ( " Warning " ) , emailpasswordformatwarning ) ;
2019-12-06 13:53:14 +00:00
cloud - > set_cloud_verification_status ( oldVerificationStatus ) ;
return ;
}
CloudStorageAuthenticate * cloudAuth = new CloudStorageAuthenticate ( this ) ;
connect ( cloudAuth , & CloudStorageAuthenticate : : finishedAuthenticate , this , & PreferencesCloud : : updateCloudAuthenticationState ) ;
cloudAuth - > backend ( email , password ) ;
}
} else if ( prefs . cloud_verification_status = = qPrefCloudStorage : : CS_NEED_TO_VERIFY ) {
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 ( ) ) {
2022-07-17 23:46:15 +00:00
QMessageBox : : warning ( this , tr ( " Warning " ) , emailpasswordformatwarning ) ;
2019-12-06 13:53:14 +00:00
return ;
}
CloudStorageAuthenticate * cloudAuth = new CloudStorageAuthenticate ( this ) ;
2024-03-09 08:22:34 +00:00
connect ( cloudAuth , & CloudStorageAuthenticate : : finishedAuthenticate , this , & PreferencesCloud : : updateCloudAuthenticationState ) ;
2019-12-06 13:53:14 +00:00
cloudAuth - > backend ( email , password , pin ) ;
}
}
cloud - > set_cloud_storage_email ( email ) ;
cloud - > set_save_password_local ( ui - > save_password_local - > isChecked ( ) ) ;
cloud - > set_cloud_storage_password ( password ) ;
cloud - > set_cloud_verification_status ( prefs . cloud_verification_status ) ;
2024-06-13 20:59:32 +00:00
cloud - > set_cloud_base_url ( QString : : fromStdString ( prefs . cloud_base_url ) ) ;
2019-12-06 13:53:14 +00:00
}
void PreferencesCloud : : updateCloudAuthenticationState ( )
{
ui - > cloud_storage_pin - > setEnabled ( prefs . cloud_verification_status = = qPrefCloudStorage : : CS_NEED_TO_VERIFY ) ;
ui - > cloud_storage_pin - > setVisible ( prefs . cloud_verification_status = = qPrefCloudStorage : : CS_NEED_TO_VERIFY ) ;
ui - > cloud_storage_pin_label - > setEnabled ( prefs . cloud_verification_status = = qPrefCloudStorage : : CS_NEED_TO_VERIFY ) ;
ui - > cloud_storage_pin_label - > setVisible ( prefs . cloud_verification_status = = qPrefCloudStorage : : CS_NEED_TO_VERIFY ) ;
ui - > cloud_storage_new_passwd - > setEnabled ( prefs . cloud_verification_status = = qPrefCloudStorage : : CS_VERIFIED ) ;
ui - > cloud_storage_new_passwd - > setVisible ( prefs . cloud_verification_status = = qPrefCloudStorage : : CS_VERIFIED ) ;
ui - > cloud_storage_new_passwd_label - > setEnabled ( prefs . cloud_verification_status = = qPrefCloudStorage : : CS_VERIFIED ) ;
ui - > cloud_storage_new_passwd_label - > setVisible ( prefs . cloud_verification_status = = qPrefCloudStorage : : CS_VERIFIED ) ;
if ( prefs . cloud_verification_status = = qPrefCloudStorage : : CS_VERIFIED ) {
ui - > cloudStorageGroupBox - > setTitle ( tr ( " Subsurface cloud storage (credentials verified) " ) ) ;
} else if ( prefs . cloud_verification_status = = qPrefCloudStorage : : CS_INCORRECT_USER_PASSWD ) {
ui - > cloudStorageGroupBox - > setTitle ( tr ( " Subsurface cloud storage (incorrect password) " ) ) ;
} else if ( prefs . cloud_verification_status = = qPrefCloudStorage : : CS_NEED_TO_VERIFY ) {
ui - > cloudStorageGroupBox - > setTitle ( tr ( " Subsurface cloud storage (PIN required) " ) ) ;
} else {
ui - > cloudStorageGroupBox - > setTitle ( tr ( " Subsurface cloud storage " ) ) ;
}
emit settingsChanged ( ) ;
}
void PreferencesCloud : : passwordUpdateSuccessful ( )
{
2024-06-13 20:59:32 +00:00
ui - > cloud_storage_password - > setText ( QString : : fromStdString ( prefs . cloud_storage_password ) ) ;
2019-12-06 13:53:14 +00:00
}