2021-01-09 19:55:16 +03:00
# include <QApplication>
2021-01-26 15:01:15 +03:00
# include <QClipboard>
2021-01-09 19:55:16 +03:00
# include <QDesktopServices>
2021-02-25 21:16:00 +03:00
# include <QHBoxLayout>
2021-01-26 15:01:15 +03:00
# include <QJsonDocument>
# include <QJsonObject>
2020-12-16 06:02:22 +03:00
# include <QKeyEvent>
2021-01-09 19:55:16 +03:00
# include <QMenu>
2020-12-16 06:02:22 +03:00
# include <QMessageBox>
2021-01-09 19:55:16 +03:00
# include <QMetaEnum>
2020-12-26 15:03:51 +03:00
# include <QSysInfo>
# include <QThread>
2021-01-06 17:12:24 +03:00
# include <QTimer>
2020-12-16 06:02:22 +03:00
2021-01-06 17:12:24 +03:00
# include "core/errorstrings.h"
2020-12-30 17:03:05 +03:00
# include "core/openvpnconfigurator.h"
# include "core/servercontroller.h"
2021-02-24 21:58:32 +03:00
# include "ui/qautostart.h"
2021-01-06 17:12:24 +03:00
2020-12-16 06:02:22 +03:00
# include "debug.h"
# include "defines.h"
2020-11-23 16:20:25 +03:00
# include "mainwindow.h"
# include "ui_mainwindow.h"
2020-12-26 15:03:51 +03:00
# include "utils.h"
# include "vpnconnection.h"
2020-11-23 16:20:25 +03:00
2021-01-03 23:55:04 +03:00
# ifdef Q_OS_MAC
# include "ui/macos_util.h"
# endif
2020-12-26 15:03:51 +03:00
MainWindow : : MainWindow ( QWidget * parent ) :
2021-01-10 16:50:38 +03:00
# ifdef Q_OS_WIN
2021-01-03 21:18:20 +03:00
CFramelessWindow ( parent ) ,
2021-01-10 16:50:38 +03:00
# else
2021-01-03 23:55:04 +03:00
QMainWindow ( parent ) ,
2021-01-10 16:50:38 +03:00
# endif
2020-12-26 15:03:51 +03:00
ui ( new Ui : : MainWindow ) ,
2021-01-26 15:01:15 +03:00
m_vpnConnection ( nullptr )
2020-11-23 16:20:25 +03:00
{
ui - > setupUi ( this ) ;
2021-01-10 18:47:29 +03:00
ui - > label_error_text - > clear ( ) ;
2021-01-03 19:14:54 +03:00
ui - > widget_tittlebar - > installEventFilter ( this ) ;
ui - > stackedWidget_main - > setSpeed ( 200 ) ;
ui - > stackedWidget_main - > setAnimation ( QEasingCurve : : Linear ) ;
2020-12-04 00:45:21 +03:00
2021-01-03 23:55:04 +03:00
# ifdef Q_OS_MAC
ui - > widget_tittlebar - > hide ( ) ;
2021-02-24 13:41:32 -08:00
resize ( width ( ) , height ( ) - ui - > stackedWidget_main - > y ( ) ) ;
2021-01-03 23:55:04 +03:00
ui - > stackedWidget_main - > move ( 0 , 0 ) ;
fixWidget ( this ) ;
# endif
2020-12-16 06:02:22 +03:00
// Post initialization
2020-12-30 17:03:05 +03:00
2021-01-26 15:01:15 +03:00
if ( m_settings . haveAuthData ( ) ) {
2021-01-09 19:55:16 +03:00
goToPage ( Page : : Vpn , true , false ) ;
2020-12-30 17:03:05 +03:00
} else {
2021-01-09 19:55:16 +03:00
goToPage ( Page : : Start , true , false ) ;
2020-12-30 17:03:05 +03:00
}
2020-12-16 06:02:22 +03:00
2021-01-09 19:55:16 +03:00
setupTray ( ) ;
setupUiConnections ( ) ;
2020-12-16 06:02:22 +03:00
2021-01-26 15:01:15 +03:00
connect ( ui - > lineEdit_sites_add_custom , & QLineEdit : : returnPressed , [ & ] ( ) {
ui - > pushButton_sites_add_custom - > click ( ) ;
} ) ;
2021-02-18 15:00:41 +03:00
updateSettings ( ) ;
2021-01-26 15:01:15 +03:00
2021-02-18 15:00:41 +03:00
//ui->pushButton_general_settings_exit->hide();
2021-01-16 15:08:27 +03:00
2020-12-16 06:02:22 +03:00
setFixedSize ( width ( ) , height ( ) ) ;
2020-12-26 15:03:51 +03:00
qInfo ( ) . noquote ( ) < < QString ( " Started %1 version %2 " ) . arg ( APPLICATION_NAME ) . arg ( APP_VERSION ) ;
qInfo ( ) . noquote ( ) < < QString ( " %1 (%2) " ) . arg ( QSysInfo : : prettyProductName ( ) ) . arg ( QSysInfo : : currentCpuArchitecture ( ) ) ;
2020-12-26 23:17:20 +03:00
Utils : : initializePath ( Utils : : configPath ( ) ) ;
2020-12-26 15:03:51 +03:00
2020-12-26 23:17:20 +03:00
m_vpnConnection = new VpnConnection ( this ) ;
2020-12-26 15:03:51 +03:00
connect ( m_vpnConnection , SIGNAL ( bytesChanged ( quint64 , quint64 ) ) , this , SLOT ( onBytesChanged ( quint64 , quint64 ) ) ) ;
connect ( m_vpnConnection , SIGNAL ( connectionStateChanged ( VpnProtocol : : ConnectionState ) ) , this , SLOT ( onConnectionStateChanged ( VpnProtocol : : ConnectionState ) ) ) ;
2021-01-08 16:51:58 +03:00
connect ( m_vpnConnection , SIGNAL ( vpnProtocolError ( amnezia : : ErrorCode ) ) , this , SLOT ( onVpnProtocolError ( amnezia : : ErrorCode ) ) ) ;
2020-12-26 15:03:51 +03:00
onConnectionStateChanged ( VpnProtocol : : ConnectionState : : Disconnected ) ;
2020-12-26 23:17:20 +03:00
2021-02-24 21:58:32 +03:00
if ( m_settings . isAutoConnect ( ) & & m_settings . haveAuthData ( ) ) {
QTimer : : singleShot ( 1000 , this , [ this ] ( ) {
ui - > pushButton_connect - > setEnabled ( false ) ;
onConnect ( ) ;
} ) ;
}
2020-12-26 23:17:20 +03:00
qDebug ( ) . noquote ( ) < < QString ( " Default config: %1 " ) . arg ( Utils : : defaultVpnConfigFileName ( ) ) ;
2021-02-24 21:58:32 +03:00
2021-02-25 18:03:24 +03:00
m_ipAddressValidator . setRegExp ( Utils : : ipAddressRegExp ( ) ) ;
2021-02-24 21:58:32 +03:00
ui - > lineEdit_new_server_ip - > setValidator ( & m_ipAddressValidator ) ;
ui - > lineEdit_network_settings_dns1 - > setValidator ( & m_ipAddressValidator ) ;
ui - > lineEdit_network_settings_dns2 - > setValidator ( & m_ipAddressValidator ) ;
2020-11-23 16:20:25 +03:00
}
MainWindow : : ~ MainWindow ( )
{
2020-12-26 15:03:51 +03:00
hide ( ) ;
m_vpnConnection - > disconnectFromVpn ( ) ;
for ( int i = 0 ; i < 50 ; i + + ) {
qApp - > processEvents ( QEventLoop : : ExcludeUserInputEvents ) ;
QThread : : msleep ( 100 ) ;
2021-02-18 15:00:41 +03:00
if ( m_vpnConnection - > isDisconnected ( ) ) {
2020-12-26 15:03:51 +03:00
break ;
}
}
2020-12-26 23:17:20 +03:00
delete m_vpnConnection ;
delete ui ;
qDebug ( ) < < " Application closed " ;
2020-12-16 06:02:22 +03:00
}
2021-01-09 19:55:16 +03:00
void MainWindow : : goToPage ( Page page , bool reset , bool slide )
{
if ( reset ) {
if ( page = = Page : : NewServer ) {
ui - > label_new_server_wait_info - > hide ( ) ;
ui - > label_new_server_wait_info - > clear ( ) ;
ui - > progressBar_new_server_connection - > setMinimum ( 0 ) ;
ui - > progressBar_new_server_connection - > setMaximum ( 300 ) ;
}
if ( page = = Page : : ServerSettings ) {
ui - > label_server_settings_wait_info - > hide ( ) ;
ui - > label_server_settings_wait_info - > clear ( ) ;
ui - > label_server_settings_server - > setText ( QString ( " %1@%2:%3 " )
2021-01-26 15:01:15 +03:00
. arg ( m_settings . userName ( ) )
. arg ( m_settings . serverName ( ) )
. arg ( m_settings . serverPort ( ) ) ) ;
2021-01-09 19:55:16 +03:00
}
}
if ( slide )
ui - > stackedWidget_main - > slideInWidget ( getPageWidget ( page ) ) ;
else
ui - > stackedWidget_main - > setCurrentWidget ( getPageWidget ( page ) ) ;
}
QWidget * MainWindow : : getPageWidget ( MainWindow : : Page page )
2020-12-16 06:02:22 +03:00
{
2021-01-09 19:55:16 +03:00
switch ( page ) {
case ( Page : : Start ) : return ui - > page_start ;
case ( Page : : NewServer ) : return ui - > page_new_server ;
case ( Page : : Vpn ) : return ui - > page_vpn ;
case ( Page : : GeneralSettings ) : return ui - > page_general_settings ;
2021-02-24 21:58:32 +03:00
case ( Page : : AppSettings ) : return ui - > page_app_settings ;
case ( Page : : NetworkSettings ) : return ui - > page_network_settings ;
2021-01-09 19:55:16 +03:00
case ( Page : : ServerSettings ) : return ui - > page_server_settings ;
case ( Page : : ShareConnection ) : return ui - > page_share_connection ;
case ( Page : : Sites ) : return ui - > page_sites ;
}
return nullptr ;
2021-01-03 19:14:54 +03:00
}
2021-01-09 19:55:16 +03:00
2021-01-03 19:14:54 +03:00
bool MainWindow : : eventFilter ( QObject * obj , QEvent * event )
{
if ( obj = = ui - > widget_tittlebar ) {
QMouseEvent * mouseEvent = static_cast < QMouseEvent * > ( event ) ;
if ( ! mouseEvent )
return false ;
if ( event - > type ( ) = = QEvent : : MouseButtonPress ) {
offset = mouseEvent - > pos ( ) ;
canMove = true ;
}
if ( event - > type ( ) = = QEvent : : MouseButtonRelease ) {
canMove = false ;
}
if ( event - > type ( ) = = QEvent : : MouseMove ) {
if ( canMove & & ( mouseEvent - > buttons ( ) & Qt : : LeftButton ) ) {
move ( mapToParent ( mouseEvent - > pos ( ) - offset ) ) ;
}
event - > ignore ( ) ;
return false ;
}
}
return QMainWindow : : eventFilter ( obj , event ) ;
2020-12-16 06:02:22 +03:00
}
void MainWindow : : keyPressEvent ( QKeyEvent * event )
{
switch ( event - > key ( ) ) {
2020-12-26 15:03:51 +03:00
case Qt : : Key_L :
if ( ! Debug : : openLogsFolder ( ) ) {
QMessageBox : : warning ( this , APPLICATION_NAME , tr ( " Cannot open logs folder! " ) ) ;
}
2020-12-16 06:02:22 +03:00
break ;
2020-12-26 15:03:51 +03:00
default :
2020-12-16 06:02:22 +03:00
;
}
}
2021-01-09 19:55:16 +03:00
void MainWindow : : closeEvent ( QCloseEvent * event )
{
event - > ignore ( ) ;
hide ( ) ;
}
2021-01-16 21:04:16 +03:00
void MainWindow : : showEvent ( QShowEvent * event )
{
# ifdef Q_OS_MACX
if ( ! event - > spontaneous ( ) ) {
setDockIconVisible ( true ) ;
}
# endif
}
void MainWindow : : hideEvent ( QHideEvent * event )
{
# ifdef Q_OS_MACX
if ( ! event - > spontaneous ( ) ) {
setDockIconVisible ( false ) ;
}
# endif
}
2021-01-09 19:55:16 +03:00
void MainWindow : : onPushButtonNewServerConnectWithNewData ( bool )
2020-12-30 17:03:05 +03:00
{
if ( ui - > lineEdit_new_server_ip - > text ( ) . isEmpty ( ) | |
ui - > lineEdit_new_server_login - > text ( ) . isEmpty ( ) | |
ui - > lineEdit_new_server_password - > text ( ) . isEmpty ( ) ) {
QMessageBox : : warning ( this , APPLICATION_NAME , tr ( " Please fill in all fields " ) ) ;
return ;
2021-01-06 17:12:24 +03:00
}
qDebug ( ) < < " Start connection with new data " ;
ServerCredentials serverCredentials ;
serverCredentials . hostName = ui - > lineEdit_new_server_ip - > text ( ) ;
if ( serverCredentials . hostName . contains ( " : " ) ) {
serverCredentials . port = serverCredentials . hostName . split ( " : " ) . at ( 1 ) . toInt ( ) ;
serverCredentials . hostName = serverCredentials . hostName . split ( " : " ) . at ( 0 ) ;
}
serverCredentials . userName = ui - > lineEdit_new_server_login - > text ( ) ;
serverCredentials . password = ui - > lineEdit_new_server_password - > text ( ) ;
2021-01-09 19:55:16 +03:00
bool ok = installServer ( serverCredentials ,
2021-01-10 16:50:38 +03:00
ui - > page_new_server ,
ui - > progressBar_new_server_connection ,
ui - > pushButton_new_server_connect_with_new_data ,
ui - > label_new_server_wait_info ) ;
2021-01-09 19:55:16 +03:00
if ( ok ) {
2021-01-26 15:01:15 +03:00
m_settings . setServerCredentials ( serverCredentials ) ;
2021-01-09 19:55:16 +03:00
goToPage ( Page : : Vpn ) ;
qApp - > processEvents ( ) ;
onConnect ( ) ;
}
}
2021-01-06 17:12:24 +03:00
2021-01-26 15:01:15 +03:00
void MainWindow : : onPushButtonNewServerConnectWithExistingCode ( bool )
{
QString s = ui - > lineEdit_start_existing_code - > text ( ) ;
s . replace ( " vpn:// " , " " ) ;
QJsonObject o = QJsonDocument : : fromJson ( QByteArray : : fromBase64 ( s . toUtf8 ( ) ) ) . object ( ) ;
ServerCredentials credentials ;
credentials . hostName = o . value ( " h " ) . toString ( ) ;
credentials . port = o . value ( " p " ) . toInt ( ) ;
credentials . userName = o . value ( " u " ) . toString ( ) ;
credentials . password = o . value ( " w " ) . toString ( ) ;
m_settings . setServerCredentials ( credentials ) ;
goToPage ( Page : : Vpn ) ;
qDebug ( ) < < QString ( " Added server %3@%1:%2 " ) .
arg ( credentials . hostName ) .
arg ( credentials . port ) .
arg ( credentials . userName ) ;
}
2021-01-09 19:55:16 +03:00
bool MainWindow : : installServer ( ServerCredentials credentials ,
QWidget * page , QProgressBar * progress , QPushButton * button , QLabel * info )
{
page - > setEnabled ( false ) ;
button - > setVisible ( false ) ;
info - > setVisible ( true ) ;
info - > setText ( tr ( " Please wait, configuring process may take up to 5 minutes " ) ) ;
2021-01-06 17:12:24 +03:00
QTimer timer ;
2021-01-09 19:55:16 +03:00
connect ( & timer , & QTimer : : timeout , [ progress ] ( ) {
progress - > setValue ( progress - > value ( ) + 1 ) ;
2021-01-06 17:12:24 +03:00
} ) ;
2021-01-09 19:55:16 +03:00
progress - > setValue ( 0 ) ;
2021-01-06 17:12:24 +03:00
timer . start ( 1000 ) ;
2021-02-18 15:00:41 +03:00
ErrorCode e = ServerController : : setupServer ( credentials , Protocol : : Any ) ;
2021-02-25 18:03:24 +03:00
qDebug ( ) < < " Setup server finished with code " < < e ;
2021-01-06 17:12:24 +03:00
if ( e ) {
2021-01-09 19:55:16 +03:00
page - > setEnabled ( true ) ;
button - > setVisible ( true ) ;
info - > setVisible ( false ) ;
2021-01-06 17:12:24 +03:00
QMessageBox : : warning ( this , APPLICATION_NAME ,
tr ( " Error occurred while configuring server. " ) + " \n " +
2021-01-10 20:37:57 +03:00
errorString ( e ) ) ;
2020-12-30 17:03:05 +03:00
2021-01-09 19:55:16 +03:00
return false ;
2021-01-06 17:12:24 +03:00
}
2021-01-03 19:14:54 +03:00
2021-01-06 17:12:24 +03:00
// just ui progressbar tweak
timer . stop ( ) ;
2021-01-03 19:14:54 +03:00
2021-01-09 19:55:16 +03:00
int remaining_val = progress - > maximum ( ) - progress - > value ( ) ;
2021-01-03 19:14:54 +03:00
2021-01-06 17:12:24 +03:00
if ( remaining_val > 0 ) {
QTimer timer1 ;
QEventLoop loop1 ;
connect ( & timer1 , & QTimer : : timeout , [ & ] ( ) {
2021-01-09 19:55:16 +03:00
progress - > setValue ( progress - > value ( ) + 1 ) ;
if ( progress - > value ( ) > = progress - > maximum ( ) ) {
2021-01-06 17:12:24 +03:00
loop1 . quit ( ) ;
2021-01-03 19:14:54 +03:00
}
2021-01-06 17:12:24 +03:00
} ) ;
2021-01-03 19:14:54 +03:00
2021-01-06 17:12:24 +03:00
timer1 . start ( 5 ) ;
loop1 . exec ( ) ;
2020-12-30 17:03:05 +03:00
}
2021-01-06 17:12:24 +03:00
2021-01-09 19:55:16 +03:00
button - > show ( ) ;
page - > setEnabled ( true ) ;
info - > setText ( tr ( " Amnezia server installed " ) ) ;
2020-12-30 17:03:05 +03:00
2021-01-09 19:55:16 +03:00
return true ;
2020-12-26 15:03:51 +03:00
}
2021-01-09 19:55:16 +03:00
void MainWindow : : onPushButtonReinstallServer ( bool )
2020-12-30 17:03:05 +03:00
{
2021-01-09 19:55:16 +03:00
onDisconnect ( ) ;
2021-01-26 15:01:15 +03:00
installServer ( m_settings . serverCredentials ( ) ,
2021-01-09 19:55:16 +03:00
ui - > page_server_settings ,
ui - > progressBar_server_settings_reinstall ,
ui - > pushButton_server_settings_reinstall ,
ui - > label_server_settings_wait_info ) ;
2020-12-30 17:03:05 +03:00
}
2021-01-09 19:55:16 +03:00
void MainWindow : : onPushButtonClearServer ( bool )
2020-12-30 17:03:05 +03:00
{
2021-01-09 19:55:16 +03:00
onDisconnect ( ) ;
2020-12-30 17:03:05 +03:00
2021-01-26 15:01:15 +03:00
ErrorCode e = ServerController : : removeServer ( m_settings . serverCredentials ( ) , Protocol : : Any ) ;
2021-01-09 19:55:16 +03:00
if ( e ) {
QMessageBox : : warning ( this , APPLICATION_NAME ,
tr ( " Error occurred while configuring server. " ) + " \n " +
errorString ( e ) + " \n " +
tr ( " See logs for details. " ) ) ;
2020-12-16 06:02:22 +03:00
2021-01-09 19:55:16 +03:00
return ;
}
else {
ui - > label_server_settings_wait_info - > show ( ) ;
ui - > label_server_settings_wait_info - > setText ( tr ( " Amnezia server successfully uninstalled " ) ) ;
}
2020-12-16 06:02:22 +03:00
}
2021-01-09 19:55:16 +03:00
void MainWindow : : onPushButtonForgetServer ( bool )
2020-12-16 06:02:22 +03:00
{
2021-01-09 19:55:16 +03:00
onDisconnect ( ) ;
2021-01-26 15:01:15 +03:00
m_settings . setUserName ( " " ) ;
m_settings . setPassword ( " " ) ;
m_settings . setServerName ( " " ) ;
m_settings . setServerPort ( ) ;
2021-01-09 19:55:16 +03:00
goToPage ( Page : : Start ) ;
2020-11-23 16:20:25 +03:00
}
2021-01-09 19:55:16 +03:00
void MainWindow : : onBytesChanged ( quint64 receivedData , quint64 sentData )
2020-12-16 06:02:22 +03:00
{
2021-01-09 19:55:16 +03:00
ui - > label_speed_received - > setText ( VpnConnection : : bytesPerSecToText ( receivedData ) ) ;
ui - > label_speed_sent - > setText ( VpnConnection : : bytesPerSecToText ( sentData ) ) ;
2020-12-16 06:02:22 +03:00
}
2020-12-26 15:03:51 +03:00
void MainWindow : : onConnectionStateChanged ( VpnProtocol : : ConnectionState state )
{
2021-01-15 23:36:35 +03:00
qDebug ( ) < < " MainWindow::onConnectionStateChanged " < < VpnProtocol : : textConnectionState ( state ) ;
2020-12-26 15:03:51 +03:00
bool pushButtonConnectEnabled = false ;
2021-02-18 15:00:41 +03:00
bool radioButtonsModeEnabled = false ;
2020-12-26 15:03:51 +03:00
ui - > label_state - > setText ( VpnProtocol : : textConnectionState ( state ) ) ;
2021-01-09 19:55:16 +03:00
setTrayState ( state ) ;
2020-12-26 15:03:51 +03:00
switch ( state ) {
case VpnProtocol : : ConnectionState : : Disconnected :
onBytesChanged ( 0 , 0 ) ;
ui - > pushButton_connect - > setChecked ( false ) ;
pushButtonConnectEnabled = true ;
2021-02-18 15:00:41 +03:00
radioButtonsModeEnabled = true ;
2020-12-26 15:03:51 +03:00
break ;
case VpnProtocol : : ConnectionState : : Preparing :
pushButtonConnectEnabled = false ;
2021-02-18 15:00:41 +03:00
radioButtonsModeEnabled = false ;
2020-12-26 15:03:51 +03:00
break ;
case VpnProtocol : : ConnectionState : : Connecting :
pushButtonConnectEnabled = false ;
2021-02-18 15:00:41 +03:00
radioButtonsModeEnabled = false ;
2020-12-26 15:03:51 +03:00
break ;
case VpnProtocol : : ConnectionState : : Connected :
pushButtonConnectEnabled = true ;
2021-02-18 15:00:41 +03:00
radioButtonsModeEnabled = false ;
2020-12-26 15:03:51 +03:00
break ;
case VpnProtocol : : ConnectionState : : Disconnecting :
pushButtonConnectEnabled = false ;
2021-02-18 15:00:41 +03:00
radioButtonsModeEnabled = false ;
2020-12-26 15:03:51 +03:00
break ;
2021-02-18 15:00:41 +03:00
case VpnProtocol : : ConnectionState : : Reconnecting :
2021-01-09 19:55:16 +03:00
pushButtonConnectEnabled = true ;
2021-02-18 15:00:41 +03:00
radioButtonsModeEnabled = false ;
2020-12-26 15:03:51 +03:00
break ;
case VpnProtocol : : ConnectionState : : Error :
2021-02-18 15:00:41 +03:00
ui - > pushButton_connect - > setChecked ( false ) ;
2020-12-26 15:03:51 +03:00
pushButtonConnectEnabled = true ;
2021-02-18 15:00:41 +03:00
radioButtonsModeEnabled = true ;
2020-12-26 15:03:51 +03:00
break ;
case VpnProtocol : : ConnectionState : : Unknown :
pushButtonConnectEnabled = true ;
2021-02-18 15:00:41 +03:00
radioButtonsModeEnabled = true ;
2020-12-26 15:03:51 +03:00
}
ui - > pushButton_connect - > setEnabled ( pushButtonConnectEnabled ) ;
2021-02-18 15:00:41 +03:00
ui - > radioButton_mode_all_sites - > setEnabled ( radioButtonsModeEnabled ) ;
ui - > radioButton_mode_selected_sites - > setEnabled ( radioButtonsModeEnabled ) ;
2020-12-26 15:03:51 +03:00
}
2021-01-08 16:51:58 +03:00
void MainWindow : : onVpnProtocolError ( ErrorCode errorCode )
{
2021-01-10 18:47:29 +03:00
ui - > label_error_text - > setText ( errorString ( errorCode ) ) ;
2021-01-08 16:51:58 +03:00
}
2021-01-09 19:55:16 +03:00
void MainWindow : : onPushButtonConnectClicked ( bool checked )
2020-12-16 06:02:22 +03:00
{
2020-12-26 15:03:51 +03:00
if ( checked ) {
2021-01-09 19:55:16 +03:00
onConnect ( ) ;
2020-12-26 15:03:51 +03:00
} else {
2021-01-09 19:55:16 +03:00
onDisconnect ( ) ;
}
}
void MainWindow : : setupTray ( )
{
m_menu = new QMenu ( ) ;
2021-02-24 13:41:32 -08:00
m_menu - > addAction ( QIcon ( " :/images/tray/application.png " ) , tr ( " Show " ) + " " + APPLICATION_NAME , this , [ this ] ( ) {
show ( ) ;
raise ( ) ;
} ) ;
2021-01-09 19:55:16 +03:00
m_menu - > addSeparator ( ) ;
m_trayActionConnect = m_menu - > addAction ( tr ( " Connect " ) , this , SLOT ( onConnect ( ) ) ) ;
m_trayActionDisconnect = m_menu - > addAction ( tr ( " Disconnect " ) , this , SLOT ( onDisconnect ( ) ) ) ;
m_menu - > addSeparator ( ) ;
m_menu - > addAction ( QIcon ( " :/images/tray/link.png " ) , tr ( " Visit Website " ) , [ & ] ( ) {
QDesktopServices : : openUrl ( QUrl ( " https://amnezia.org " ) ) ;
} ) ;
m_menu - > addAction ( QIcon ( " :/images/tray/cancel.png " ) , tr ( " Quit " ) + " " + APPLICATION_NAME , this , [ & ] ( ) {
2021-01-16 21:04:16 +03:00
// QMessageBox::question(this, QMessageBox::question(this, tr("Exit"), tr("Do you really want to quit?"), QMessageBox::Yes | QMessageBox::No, );
2021-01-09 19:55:16 +03:00
QMessageBox msgBox ( QMessageBox : : Question , tr ( " Exit " ) , tr ( " Do you really want to quit? " ) ,
2021-01-16 21:04:16 +03:00
QMessageBox : : Yes | QMessageBox : : No , this , Qt : : Dialog | Qt : : MSWindowsFixedSizeDialogHint | Qt : : WindowStaysOnTopHint ) ;
msgBox . setDefaultButton ( QMessageBox : : Yes ) ;
2021-01-09 19:55:16 +03:00
msgBox . raise ( ) ;
if ( msgBox . exec ( ) = = QMessageBox : : Yes ) {
qApp - > quit ( ) ;
}
} ) ;
m_tray . setContextMenu ( m_menu ) ;
setTrayState ( VpnProtocol : : ConnectionState : : Disconnected ) ;
m_tray . show ( ) ;
connect ( & m_tray , & QSystemTrayIcon : : activated , this , & MainWindow : : onTrayActivated ) ;
}
void MainWindow : : setTrayIcon ( const QString & iconPath )
{
m_tray . setIcon ( QIcon ( QPixmap ( iconPath ) . scaled ( 128 , 128 ) ) ) ;
}
MainWindow : : Page MainWindow : : currentPage ( )
{
QWidget * currentPage = ui - > stackedWidget_main - > currentWidget ( ) ;
QMetaEnum e = QMetaEnum : : fromType < MainWindow : : Page > ( ) ;
for ( int k = 0 ; k < e . keyCount ( ) ; k + + ) {
Page p = static_cast < MainWindow : : Page > ( e . value ( k ) ) ;
if ( currentPage = = getPageWidget ( p ) ) return p ;
}
return Page : : Start ;
}
void MainWindow : : setupUiConnections ( )
{
connect ( ui - > pushButton_close , & QPushButton : : clicked , this , [ this ] ( ) {
if ( currentPage ( ) = = Page : : Start | | currentPage ( ) = = Page : : NewServer ) qApp - > quit ( ) ;
else hide ( ) ;
} ) ;
connect ( ui - > pushButton_general_settings_exit , & QPushButton : : clicked , this , [ & ] ( ) { qApp - > quit ( ) ; } ) ;
2021-01-09 20:05:16 +03:00
connect ( ui - > pushButton_new_server_get_info , & QPushButton : : clicked , this , [ & ] ( ) {
QDesktopServices : : openUrl ( QUrl ( " https://amnezia.org " ) ) ;
} ) ;
2021-01-09 19:55:16 +03:00
connect ( ui - > pushButton_connect , SIGNAL ( clicked ( bool ) ) , this , SLOT ( onPushButtonConnectClicked ( bool ) ) ) ;
connect ( ui - > pushButton_new_server_setup , & QPushButton : : clicked , this , [ this ] ( ) { goToPage ( Page : : NewServer ) ; } ) ;
connect ( ui - > pushButton_new_server_connect_with_new_data , SIGNAL ( clicked ( bool ) ) , this , SLOT ( onPushButtonNewServerConnectWithNewData ( bool ) ) ) ;
2021-01-26 15:01:15 +03:00
connect ( ui - > pushButton_new_server_connect , SIGNAL ( clicked ( bool ) ) , this , SLOT ( onPushButtonNewServerConnectWithExistingCode ( bool ) ) ) ;
2021-01-09 19:55:16 +03:00
connect ( ui - > pushButton_server_settings_reinstall , SIGNAL ( clicked ( bool ) ) , this , SLOT ( onPushButtonReinstallServer ( bool ) ) ) ;
connect ( ui - > pushButton_server_settings_clear , SIGNAL ( clicked ( bool ) ) , this , SLOT ( onPushButtonClearServer ( bool ) ) ) ;
connect ( ui - > pushButton_server_settings_forget , SIGNAL ( clicked ( bool ) ) , this , SLOT ( onPushButtonForgetServer ( bool ) ) ) ;
2021-02-18 15:00:41 +03:00
connect ( ui - > pushButton_vpn_add_site , & QPushButton : : clicked , this , [ this ] ( ) { goToPage ( Page : : Sites ) ; } ) ;
2021-01-09 19:55:16 +03:00
connect ( ui - > pushButton_settings , & QPushButton : : clicked , this , [ this ] ( ) { goToPage ( Page : : GeneralSettings ) ; } ) ;
2021-02-24 21:58:32 +03:00
connect ( ui - > pushButton_app_settings , & QPushButton : : clicked , this , [ this ] ( ) { goToPage ( Page : : AppSettings ) ; } ) ;
connect ( ui - > pushButton_network_settings , & QPushButton : : clicked , this , [ this ] ( ) { goToPage ( Page : : NetworkSettings ) ; } ) ;
2021-01-09 19:55:16 +03:00
connect ( ui - > pushButton_server_settings , & QPushButton : : clicked , this , [ this ] ( ) { goToPage ( Page : : ServerSettings ) ; } ) ;
2021-01-26 15:01:15 +03:00
connect ( ui - > pushButton_share_connection , & QPushButton : : clicked , this , [ this ] ( ) {
goToPage ( Page : : ShareConnection ) ;
updateShareCode ( ) ;
} ) ;
connect ( ui - > pushButton_copy_sharing_code , & QPushButton : : clicked , this , [ this ] ( ) {
QGuiApplication : : clipboard ( ) - > setText ( ui - > textEdit_sharing_code - > toPlainText ( ) ) ;
ui - > pushButton_copy_sharing_code - > setText ( tr ( " Copied " ) ) ;
2021-02-24 21:58:32 +03:00
QTimer : : singleShot ( 3000 , this , [ this ] ( ) {
2021-01-26 15:01:15 +03:00
ui - > pushButton_copy_sharing_code - > setText ( tr ( " Copy " ) ) ;
} ) ;
} ) ;
2021-01-09 19:55:16 +03:00
connect ( ui - > pushButton_back_from_sites , & QPushButton : : clicked , this , [ this ] ( ) { goToPage ( Page : : Vpn ) ; } ) ;
connect ( ui - > pushButton_back_from_settings , & QPushButton : : clicked , this , [ this ] ( ) { goToPage ( Page : : Vpn ) ; } ) ;
connect ( ui - > pushButton_back_from_new_server , & QPushButton : : clicked , this , [ this ] ( ) { goToPage ( Page : : Start ) ; } ) ;
2021-02-24 21:58:32 +03:00
connect ( ui - > pushButton_back_from_app_settings , & QPushButton : : clicked , this , [ this ] ( ) { goToPage ( Page : : GeneralSettings ) ; } ) ;
connect ( ui - > pushButton_back_from_network_settings , & QPushButton : : clicked , this , [ this ] ( ) { goToPage ( Page : : GeneralSettings ) ; } ) ;
2021-01-09 19:55:16 +03:00
connect ( ui - > pushButton_back_from_server_settings , & QPushButton : : clicked , this , [ this ] ( ) { goToPage ( Page : : GeneralSettings ) ; } ) ;
connect ( ui - > pushButton_back_from_share , & QPushButton : : clicked , this , [ this ] ( ) { goToPage ( Page : : GeneralSettings ) ; } ) ;
2021-01-26 15:01:15 +03:00
connect ( ui - > pushButton_sites_add_custom , & QPushButton : : clicked , this , [ this ] ( ) { onPushButtonAddCustomSitesClicked ( ) ; } ) ;
2021-02-18 15:00:41 +03:00
connect ( ui - > radioButton_mode_selected_sites , & QRadioButton : : toggled , ui - > pushButton_vpn_add_site , & QPushButton : : setEnabled ) ;
connect ( ui - > radioButton_mode_selected_sites , & QRadioButton : : toggled , this , [ this ] ( bool toggled ) {
m_settings . setCustomRouting ( toggled ) ;
} ) ;
2021-02-24 21:58:32 +03:00
connect ( ui - > checkBox_autostart , & QCheckBox : : stateChanged , this , [ this ] ( int state ) {
if ( state = = Qt : : Unchecked ) {
ui - > checkBox_autoconnect - > setChecked ( false ) ;
}
Autostart : : setAutostart ( state = = Qt : : Checked ) ;
} ) ;
connect ( ui - > checkBox_autoconnect , & QCheckBox : : stateChanged , this , [ this ] ( int state ) {
m_settings . setAutoConnect ( state = = Qt : : Checked ) ;
} ) ;
connect ( ui - > pushButton_network_settings_resetdns1 , & QPushButton : : clicked , this , [ this ] ( ) {
m_settings . setPrimaryDns ( m_settings . cloudFlareNs1 ( ) ) ;
updateSettings ( ) ;
} ) ;
connect ( ui - > pushButton_network_settings_resetdns2 , & QPushButton : : clicked , this , [ this ] ( ) {
m_settings . setPrimaryDns ( m_settings . cloudFlareNs2 ( ) ) ;
updateSettings ( ) ;
} ) ;
connect ( ui - > lineEdit_network_settings_dns1 , & QLineEdit : : textEdited , this , [ this ] ( const QString & newText ) {
if ( m_ipAddressValidator . regExp ( ) . exactMatch ( newText ) ) {
m_settings . setPrimaryDns ( newText ) ;
}
} ) ;
connect ( ui - > lineEdit_network_settings_dns2 , & QLineEdit : : textEdited , this , [ this ] ( const QString & newText ) {
if ( m_ipAddressValidator . regExp ( ) . exactMatch ( newText ) ) {
m_settings . setSecondaryDns ( newText ) ;
}
} ) ;
2021-01-09 19:55:16 +03:00
}
void MainWindow : : setTrayState ( VpnProtocol : : ConnectionState state )
{
QString resourcesPath = " :/images/tray/%1 " ;
m_trayActionDisconnect - > setEnabled ( state = = VpnProtocol : : ConnectionState : : Connected ) ;
m_trayActionConnect - > setEnabled ( state = = VpnProtocol : : ConnectionState : : Disconnected ) ;
switch ( state ) {
case VpnProtocol : : ConnectionState : : Disconnected :
setTrayIcon ( QString ( resourcesPath ) . arg ( DisconnectedTrayIconName ) ) ;
break ;
case VpnProtocol : : ConnectionState : : Preparing :
setTrayIcon ( QString ( resourcesPath ) . arg ( DisconnectedTrayIconName ) ) ;
break ;
case VpnProtocol : : ConnectionState : : Connecting :
setTrayIcon ( QString ( resourcesPath ) . arg ( DisconnectedTrayIconName ) ) ;
break ;
case VpnProtocol : : ConnectionState : : Connected :
setTrayIcon ( QString ( resourcesPath ) . arg ( ConnectedTrayIconName ) ) ;
break ;
case VpnProtocol : : ConnectionState : : Disconnecting :
setTrayIcon ( QString ( resourcesPath ) . arg ( DisconnectedTrayIconName ) ) ;
break ;
2021-02-18 15:00:41 +03:00
case VpnProtocol : : ConnectionState : : Reconnecting :
2021-01-09 19:55:16 +03:00
setTrayIcon ( QString ( resourcesPath ) . arg ( DisconnectedTrayIconName ) ) ;
break ;
case VpnProtocol : : ConnectionState : : Error :
setTrayIcon ( QString ( resourcesPath ) . arg ( ErrorTrayIconName ) ) ;
break ;
case VpnProtocol : : ConnectionState : : Unknown :
default :
setTrayIcon ( QString ( resourcesPath ) . arg ( DisconnectedTrayIconName ) ) ;
}
2021-01-10 16:50:38 +03:00
//#ifdef Q_OS_MAC
// // Get theme from current user (note, this app can be launched as root application and in this case this theme can be different from theme of real current user )
// bool darkTaskBar = MacOSFunctions::instance().isMenuBarUseDarkTheme();
// darkTaskBar = forceUseBrightIcons ? true : darkTaskBar;
// resourcesPath = ":/images_mac/tray_icon/%1";
// useIconName = useIconName.replace(".png", darkTaskBar ? "@2x.png" : " dark@2x.png");
//#endif
2021-01-09 19:55:16 +03:00
}
void MainWindow : : onTrayActivated ( QSystemTrayIcon : : ActivationReason reason )
{
2021-01-16 21:04:16 +03:00
# ifndef Q_OS_MAC
2021-01-09 19:55:16 +03:00
if ( reason = = QSystemTrayIcon : : DoubleClick | | reason = = QSystemTrayIcon : : Trigger ) {
show ( ) ;
raise ( ) ;
setWindowState ( Qt : : WindowActive ) ;
}
2021-01-16 21:04:16 +03:00
# endif
2021-01-09 19:55:16 +03:00
}
void MainWindow : : onConnect ( )
{
2021-01-10 18:47:29 +03:00
ui - > label_error_text - > clear ( ) ;
2021-01-09 19:55:16 +03:00
ui - > pushButton_connect - > setChecked ( true ) ;
qApp - > processEvents ( ) ;
// TODO: Call connectToVpn with restricted server account
2021-01-26 15:01:15 +03:00
ServerCredentials credentials = m_settings . serverCredentials ( ) ;
2021-01-09 19:55:16 +03:00
ErrorCode errorCode = m_vpnConnection - > connectToVpn ( credentials ) ;
if ( errorCode ) {
//ui->pushButton_connect->setChecked(false);
QMessageBox : : critical ( this , APPLICATION_NAME , errorString ( errorCode ) ) ;
return ;
2020-12-26 15:03:51 +03:00
}
2021-01-15 23:36:35 +03:00
2021-01-09 19:55:16 +03:00
ui - > pushButton_connect - > setEnabled ( false ) ;
}
void MainWindow : : onDisconnect ( )
{
ui - > pushButton_connect - > setChecked ( false ) ;
m_vpnConnection - > disconnectFromVpn ( ) ;
2020-12-16 06:02:22 +03:00
}
2021-01-09 19:55:16 +03:00
void MainWindow : : onTrayActionConnect ( )
2021-01-03 19:14:54 +03:00
{
2021-01-09 19:55:16 +03:00
if ( m_trayActionConnect - > text ( ) = = tr ( " Connect " ) ) {
onConnect ( ) ;
} else if ( m_trayActionConnect - > text ( ) = = tr ( " Disconnect " ) ) {
onDisconnect ( ) ;
}
2021-01-03 19:14:54 +03:00
}
2021-01-09 19:55:16 +03:00
2021-01-26 15:01:15 +03:00
void MainWindow : : onPushButtonAddCustomSitesClicked ( )
{
QString newSite = ui - > lineEdit_sites_add_custom - > text ( ) ;
if ( newSite . isEmpty ( ) ) return ;
if ( ! newSite . contains ( " . " ) ) return ;
// get domain name if it present
newSite . replace ( " https:// " , " " ) ;
newSite . replace ( " http:// " , " " ) ;
newSite . replace ( " ftp:// " , " " ) ;
newSite = newSite . split ( " / " , QString : : SkipEmptyParts ) . first ( ) ;
QStringList customSites = m_settings . customSites ( ) ;
if ( ! customSites . contains ( newSite ) ) {
customSites . append ( newSite ) ;
m_settings . setCustomSites ( customSites ) ;
QString newIp = Utils : : getIPAddress ( newSite ) ;
QStringList customIps = m_settings . customIps ( ) ;
if ( ! newIp . isEmpty ( ) & & ! customIps . contains ( newIp ) ) {
customIps . append ( newIp ) ;
m_settings . setCustomIps ( customIps ) ;
2021-02-18 15:00:41 +03:00
if ( m_vpnConnection - > connectionState ( ) = = VpnProtocol : : ConnectionState : : Connected ) {
IpcClient : : Interface ( ) - > routeAddList ( m_vpnConnection - > vpnProtocol ( ) - > vpnGateway ( ) ,
QStringList ( ) < < newIp ) ;
}
2021-01-26 15:01:15 +03:00
}
2021-02-18 15:00:41 +03:00
updateSettings ( ) ;
2021-01-26 15:01:15 +03:00
ui - > lineEdit_sites_add_custom - > clear ( ) ;
}
else {
qDebug ( ) < < " customSites already contains " < < newSite ;
}
}
2021-02-25 21:16:00 +03:00
void MainWindow : : onPushButtonDeleteCustomSiteClicked ( const QString & siteToDelete )
2021-01-26 15:01:15 +03:00
{
if ( siteToDelete . isEmpty ( ) ) {
return ;
}
QString ipToDelete = Utils : : getIPAddress ( siteToDelete ) ;
QStringList customSites = m_settings . customSites ( ) ;
customSites . removeAll ( siteToDelete ) ;
qDebug ( ) < < " Deleted custom site: " < < siteToDelete ;
m_settings . setCustomSites ( customSites ) ;
QStringList customIps = m_settings . customIps ( ) ;
customIps . removeAll ( ipToDelete ) ;
qDebug ( ) < < " Deleted custom ip: " < < ipToDelete ;
m_settings . setCustomIps ( customIps ) ;
2021-02-18 15:00:41 +03:00
updateSettings ( ) ;
2021-01-26 15:01:15 +03:00
2021-02-18 15:00:41 +03:00
if ( m_vpnConnection - > connectionState ( ) = = VpnProtocol : : ConnectionState : : Connected ) {
IpcClient : : Interface ( ) - > routeDelete ( ipToDelete ) ;
IpcClient : : Interface ( ) - > flushDns ( ) ;
}
2021-01-26 15:01:15 +03:00
}
2021-02-18 15:00:41 +03:00
void MainWindow : : updateSettings ( )
2021-01-26 15:01:15 +03:00
{
2021-02-18 15:00:41 +03:00
ui - > radioButton_mode_selected_sites - > setChecked ( m_settings . customRouting ( ) ) ;
ui - > pushButton_vpn_add_site - > setEnabled ( m_settings . customRouting ( ) ) ;
2021-02-24 21:58:32 +03:00
ui - > checkBox_autostart - > setChecked ( Autostart : : isAutostart ( ) ) ;
ui - > checkBox_autoconnect - > setChecked ( m_settings . isAutoConnect ( ) ) ;
ui - > lineEdit_network_settings_dns1 - > setText ( m_settings . primaryDns ( ) ) ;
ui - > lineEdit_network_settings_dns2 - > setText ( m_settings . secondaryDns ( ) ) ;
2021-02-25 21:16:00 +03:00
ui - > listWidget_sites - > clear ( ) ;
for ( const QString & site : m_settings . customSites ( ) ) {
makeSitesListItem ( ui - > listWidget_sites , site ) ;
}
2021-01-26 15:01:15 +03:00
}
void MainWindow : : updateShareCode ( )
{
QJsonObject o ;
o . insert ( " h " , m_settings . serverName ( ) ) ;
o . insert ( " p " , m_settings . serverPort ( ) ) ;
o . insert ( " u " , m_settings . userName ( ) ) ;
o . insert ( " w " , m_settings . password ( ) ) ;
QByteArray ba = QJsonDocument ( o ) . toJson ( ) . toBase64 ( QByteArray : : Base64UrlEncoding | QByteArray : : OmitTrailingEquals ) ;
ui - > textEdit_sharing_code - > setText ( QString ( " vpn://%1 " ) . arg ( QString ( ba ) ) ) ;
2021-02-18 15:00:41 +03:00
//qDebug() << "Share code" << QJsonDocument(o).toJson();
2021-01-26 15:01:15 +03:00
}
2021-02-25 21:16:00 +03:00
void MainWindow : : makeSitesListItem ( QListWidget * listWidget , const QString & address )
{
2021-02-25 23:11:46 +03:00
QSize size ( 310 , 25 ) ;
2021-02-25 21:16:00 +03:00
QWidget * widget = new QWidget ;
widget - > resize ( size ) ;
QLabel * label = new QLabel ( address , widget ) ;
label - > resize ( size ) ;
QPushButton * btn = new QPushButton ( widget ) ;
btn - > resize ( size ) ;
QPushButton * btn1 = new QPushButton ( widget ) ;
btn1 - > resize ( 30 , 25 ) ;
2021-02-25 23:11:46 +03:00
btn1 - > move ( 280 , 0 ) ;
2021-02-25 21:16:00 +03:00
btn1 - > setCursor ( QCursor ( Qt : : PointingHandCursor ) ) ;
connect ( btn1 , & QPushButton : : clicked , this , [ this , label ] ( ) {
onPushButtonDeleteCustomSiteClicked ( label - > text ( ) ) ;
return ;
} ) ;
QListWidgetItem * item = new QListWidgetItem ( listWidget ) ;
item - > setSizeHint ( size ) ;
listWidget - > setItemWidget ( item , widget ) ;
widget - > setStyleSheet ( styleSheet ( ) ) ;
}