2017-04-27 18:26:05 +00:00
// SPDX-License-Identifier: GPL-2.0
2015-10-01 22:48:57 +00:00
# include "preferences_network.h"
# include "ui_preferences_network.h"
# include "subsurfacewebservices.h"
2016-04-05 05:02:03 +00:00
# include "core/cloudstorage.h"
2018-06-16 08:08:34 +00:00
# include "core/dive.h"
2018-07-05 18:37:53 +00:00
# include "core/settings/qPref.h"
2015-10-01 22:48:57 +00:00
# include <QNetworkProxy>
2017-11-29 09:57:08 +00:00
PreferencesNetwork : : PreferencesNetwork ( ) : AbstractPreferencesWidget ( tr ( " Network " ) , QIcon ( " :preferences-system-network-icon " ) , 9 ) , ui ( new Ui : : PreferencesNetwork ( ) )
2015-10-01 22:48:57 +00:00
{
ui - > setupUi ( this ) ;
ui - > proxyType - > clear ( ) ;
ui - > proxyType - > addItem ( tr ( " No proxy " ) , QNetworkProxy : : NoProxy ) ;
ui - > proxyType - > addItem ( tr ( " System proxy " ) , QNetworkProxy : : DefaultProxy ) ;
ui - > proxyType - > addItem ( tr ( " HTTP proxy " ) , QNetworkProxy : : HttpProxy ) ;
ui - > proxyType - > addItem ( tr ( " SOCKS proxy " ) , QNetworkProxy : : Socks5Proxy ) ;
ui - > proxyType - > setCurrentIndex ( - 1 ) ;
connect ( ui - > proxyType , SIGNAL ( currentIndexChanged ( int ) ) , this , SLOT ( proxyType_changed ( int ) ) ) ;
}
PreferencesNetwork : : ~ PreferencesNetwork ( )
{
delete ui ;
}
void PreferencesNetwork : : refreshSettings ( )
{
ui - > proxyHost - > setText ( prefs . proxy_host ) ;
ui - > proxyPort - > setValue ( prefs . proxy_port ) ;
ui - > proxyAuthRequired - > setChecked ( prefs . proxy_auth ) ;
ui - > proxyUsername - > setText ( prefs . proxy_user ) ;
ui - > proxyPassword - > setText ( prefs . proxy_pass ) ;
ui - > proxyType - > setCurrentIndex ( ui - > proxyType - > findData ( prefs . proxy_type ) ) ;
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 ) ;
2017-10-26 07:15:10 +00:00
updateCloudAuthenticationState ( ) ;
2015-10-01 22:48:57 +00:00
}
void PreferencesNetwork : : syncSettings ( )
{
2018-07-27 19:55:49 +00:00
auto cloud = qPrefCloudStorage : : instance ( ) ;
auto proxy = qPrefProxy : : instance ( ) ;
2016-08-10 16:27:03 +00:00
2018-07-27 19:55:49 +00:00
proxy - > set_proxy_type ( ui - > proxyType - > itemData ( ui - > proxyType - > currentIndex ( ) ) . toInt ( ) ) ;
proxy - > set_proxy_host ( ui - > proxyHost - > text ( ) ) ;
proxy - > set_proxy_port ( ui - > proxyPort - > value ( ) ) ;
proxy - > set_proxy_auth ( ui - > proxyAuthRequired - > isChecked ( ) ) ;
proxy - > set_proxy_user ( ui - > proxyUsername - > text ( ) ) ;
proxy - > set_proxy_pass ( ui - > proxyPassword - > text ( ) ) ;
2015-10-01 22:48:57 +00:00
QString email = ui - > cloud_storage_email - > text ( ) ;
QString password = ui - > cloud_storage_password - > text ( ) ;
QString newpassword = ui - > cloud_storage_new_passwd - > text ( ) ;
2016-08-10 16:43:39 +00:00
//TODO: Change this to the Cloud Storage Stuff, not preferences.
2018-07-05 18:37:53 +00:00
if ( prefs . cloud_verification_status = = qPref : : CS_VERIFIED & & ! newpassword . isEmpty ( ) ) {
2015-10-01 22:48:57 +00:00
// 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 ( ) ) ) {
2018-01-16 12:30:02 +00:00
report_error ( qPrintable ( tr ( " Change ignored. Cloud storage email and password can only consist of letters, numbers, and '.', '-', '_', and '+'. " ) ) ) ;
return ;
2018-01-16 13:15:31 +00:00
}
if ( ! reg . match ( email ) . hasMatch ( ) | | ( ! newpassword . isEmpty ( ) & & ! reg . match ( newpassword ) . hasMatch ( ) ) ) {
2018-01-16 12:30:02 +00:00
report_error ( qPrintable ( tr ( " Change ignored. Cloud storage email and new password can only consist of letters, numbers, and '.', '-', '_', and '+'. " ) ) ) ;
ui - > cloud_storage_new_passwd - > setText ( " " ) ;
return ;
2015-10-01 22:48:57 +00:00
}
2018-01-16 13:15:31 +00:00
CloudStorageAuthenticate * cloudAuth = new CloudStorageAuthenticate ( this ) ;
connect ( cloudAuth , & CloudStorageAuthenticate : : finishedAuthenticate , this , & PreferencesNetwork : : updateCloudAuthenticationState ) ;
connect ( cloudAuth , & CloudStorageAuthenticate : : passwordChangeSuccessful , this , & PreferencesNetwork : : passwordUpdateSuccessful ) ;
cloudAuth - > backend ( email , password , " " , newpassword ) ;
ui - > cloud_storage_new_passwd - > setText ( " " ) ;
2018-07-14 14:52:25 +00:00
cloud - > set_cloud_storage_newpassword ( newpassword ) ;
2015-10-01 22:48:57 +00:00
}
2018-07-05 18:37:53 +00:00
} else if ( prefs . cloud_verification_status = = qPref : : CS_UNKNOWN | |
prefs . cloud_verification_status = = qPref : : CS_INCORRECT_USER_PASSWD | |
2015-10-01 22:48:57 +00:00
email ! = prefs . cloud_storage_email | |
password ! = prefs . cloud_storage_password ) {
// different credentials - reset verification status
2018-07-14 14:52:25 +00:00
int oldVerificationStatus = cloud - > cloud_verification_status ( ) ;
cloud - > set_cloud_verification_status ( qPref : : CS_UNKNOWN ) ;
2015-10-01 22:48:57 +00:00
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 ( ) ) ) {
report_error ( qPrintable ( tr ( " Cloud storage email and password can only consist of letters, numbers, and '.', '-', '_', and '+'. " ) ) ) ;
2018-07-14 14:52:25 +00:00
cloud - > set_cloud_verification_status ( oldVerificationStatus ) ;
2018-01-16 12:30:02 +00:00
return ;
2015-10-01 22:48:57 +00:00
}
2018-01-16 13:15:31 +00:00
CloudStorageAuthenticate * cloudAuth = new CloudStorageAuthenticate ( this ) ;
connect ( cloudAuth , & CloudStorageAuthenticate : : finishedAuthenticate , this , & PreferencesNetwork : : updateCloudAuthenticationState ) ;
cloudAuth - > backend ( email , password ) ;
2015-10-01 22:48:57 +00:00
}
2018-07-05 18:37:53 +00:00
} else if ( prefs . cloud_verification_status = = qPref : : CS_NEED_TO_VERIFY ) {
2015-10-01 22:48:57 +00:00
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 '+'. " ) ) ) ;
2018-01-16 12:30:02 +00:00
return ;
2015-10-01 22:48:57 +00:00
}
CloudStorageAuthenticate * cloudAuth = new CloudStorageAuthenticate ( this ) ;
2017-10-26 07:15:10 +00:00
connect ( cloudAuth , SIGNAL ( finishedAuthenticate ( ) ) , this , SLOT ( updateCloudAuthenticationState ( ) ) ) ;
2015-11-07 21:00:14 +00:00
cloudAuth - > backend ( email , password , pin ) ;
2015-10-01 22:48:57 +00:00
}
}
2018-07-14 14:52:25 +00:00
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 ) ;
cloud - > set_cloud_base_url ( prefs . cloud_base_url ) ;
2015-10-01 22:48:57 +00:00
}
2017-10-26 07:15:10 +00:00
void PreferencesNetwork : : updateCloudAuthenticationState ( )
2015-10-01 22:48:57 +00:00
{
2018-07-05 18:37:53 +00:00
ui - > cloud_storage_pin - > setEnabled ( prefs . cloud_verification_status = = qPref : : CS_NEED_TO_VERIFY ) ;
ui - > cloud_storage_pin - > setVisible ( prefs . cloud_verification_status = = qPref : : CS_NEED_TO_VERIFY ) ;
ui - > cloud_storage_pin_label - > setEnabled ( prefs . cloud_verification_status = = qPref : : CS_NEED_TO_VERIFY ) ;
ui - > cloud_storage_pin_label - > setVisible ( prefs . cloud_verification_status = = qPref : : CS_NEED_TO_VERIFY ) ;
ui - > cloud_storage_new_passwd - > setEnabled ( prefs . cloud_verification_status = = qPref : : CS_VERIFIED ) ;
ui - > cloud_storage_new_passwd - > setVisible ( prefs . cloud_verification_status = = qPref : : CS_VERIFIED ) ;
ui - > cloud_storage_new_passwd_label - > setEnabled ( prefs . cloud_verification_status = = qPref : : CS_VERIFIED ) ;
ui - > cloud_storage_new_passwd_label - > setVisible ( prefs . cloud_verification_status = = qPref : : CS_VERIFIED ) ;
if ( prefs . cloud_verification_status = = qPref : : CS_VERIFIED ) {
2015-10-01 22:48:57 +00:00
ui - > cloudStorageGroupBox - > setTitle ( tr ( " Subsurface cloud storage (credentials verified) " ) ) ;
2018-07-05 18:37:53 +00:00
} else if ( prefs . cloud_verification_status = = qPref : : CS_INCORRECT_USER_PASSWD ) {
2017-10-26 07:15:10 +00:00
ui - > cloudStorageGroupBox - > setTitle ( tr ( " Subsurface cloud storage (incorrect password) " ) ) ;
2018-07-05 18:37:53 +00:00
} else if ( prefs . cloud_verification_status = = qPref : : CS_NEED_TO_VERIFY ) {
2017-10-26 07:15:10 +00:00
ui - > cloudStorageGroupBox - > setTitle ( tr ( " Subsurface cloud storage (PIN required) " ) ) ;
2015-10-01 22:48:57 +00:00
} else {
ui - > cloudStorageGroupBox - > setTitle ( tr ( " Subsurface cloud storage " ) ) ;
}
2015-11-10 21:45:13 +00:00
emit settingsChanged ( ) ;
2015-10-01 22:48:57 +00:00
}
void PreferencesNetwork : : proxyType_changed ( int idx )
{
if ( idx = = - 1 ) {
return ;
}
int proxyType = ui - > proxyType - > itemData ( idx ) . toInt ( ) ;
bool hpEnabled = ( proxyType = = QNetworkProxy : : Socks5Proxy | | proxyType = = QNetworkProxy : : HttpProxy ) ;
ui - > proxyHost - > setEnabled ( hpEnabled ) ;
ui - > proxyPort - > setEnabled ( hpEnabled ) ;
ui - > proxyAuthRequired - > setEnabled ( hpEnabled ) ;
ui - > proxyUsername - > setEnabled ( hpEnabled & ui - > proxyAuthRequired - > isChecked ( ) ) ;
ui - > proxyPassword - > setEnabled ( hpEnabled & ui - > proxyAuthRequired - > isChecked ( ) ) ;
ui - > proxyAuthRequired - > setChecked ( ui - > proxyAuthRequired - > isChecked ( ) ) ;
}
2017-11-19 12:28:57 +00:00
void PreferencesNetwork : : passwordUpdateSuccessful ( )
2015-10-01 22:48:57 +00:00
{
ui - > cloud_storage_password - > setText ( prefs . cloud_storage_password ) ;
2015-11-07 21:00:14 +00:00
}