2015-06-06 09:40:08 +08:00
/*
Copyright © 2015 by The qTox Project
This file is part of qTox , a Qt - based graphical interface for Tox .
qTox is libre software : you can redistribute it and / or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
qTox is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU General Public License for more details .
You should have received a copy of the GNU General Public License
along with qTox . If not , see < http : //www.gnu.org/licenses/>.
*/
2015-06-04 01:04:28 +08:00
# include "loginscreen.h"
# include "ui_loginscreen.h"
2015-06-06 07:44:47 +08:00
# include "src/persistence/profile.h"
# include "src/persistence/profilelocker.h"
2015-06-04 08:10:06 +08:00
# include "src/nexus.h"
2015-06-06 07:44:47 +08:00
# include "src/persistence/settings.h"
2015-06-05 01:21:22 +08:00
# include "src/widget/form/setpassworddialog.h"
2015-06-06 07:44:47 +08:00
# include "src/widget/translator.h"
2015-06-04 08:10:06 +08:00
# include <QMessageBox>
2015-06-04 20:19:18 +08:00
# include <QDebug>
2015-06-04 01:04:28 +08:00
LoginScreen : : LoginScreen ( QWidget * parent ) :
QWidget ( parent ) ,
2015-06-05 21:45:43 +08:00
ui ( new Ui : : LoginScreen ) ,
quitShortcut { QKeySequence ( Qt : : CTRL + Qt : : Key_Q ) , this }
2015-06-04 01:04:28 +08:00
{
ui - > setupUi ( this ) ;
2015-06-04 01:28:16 +08:00
2015-08-04 04:14:29 +08:00
// permanently disables maximize button https://github.com/tux3/qTox/issues/1973
2015-08-21 05:27:13 +08:00
this - > setWindowFlags ( windowFlags ( ) & ! Qt : : WindowMaximizeButtonHint ) ;
2015-08-04 04:14:29 +08:00
this - > setFixedSize ( this - > size ( ) ) ;
2015-06-05 21:45:43 +08:00
connect ( & quitShortcut , & QShortcut : : activated , this , & LoginScreen : : close ) ;
2015-06-04 01:28:16 +08:00
connect ( ui - > newProfilePgbtn , & QPushButton : : clicked , this , & LoginScreen : : onNewProfilePageClicked ) ;
connect ( ui - > loginPgbtn , & QPushButton : : clicked , this , & LoginScreen : : onLoginPageClicked ) ;
connect ( ui - > createAccountButton , & QPushButton : : clicked , this , & LoginScreen : : onCreateNewProfile ) ;
2015-06-04 16:56:48 +08:00
connect ( ui - > newUsername , & QLineEdit : : returnPressed , this , & LoginScreen : : onCreateNewProfile ) ;
connect ( ui - > newPass , & QLineEdit : : returnPressed , this , & LoginScreen : : onCreateNewProfile ) ;
connect ( ui - > newPassConfirm , & QLineEdit : : returnPressed , this , & LoginScreen : : onCreateNewProfile ) ;
2015-06-04 01:28:16 +08:00
connect ( ui - > loginButton , & QPushButton : : clicked , this , & LoginScreen : : onLogin ) ;
2015-06-04 07:30:17 +08:00
connect ( ui - > loginUsernames , & QComboBox : : currentTextChanged , this , & LoginScreen : : onLoginUsernameSelected ) ;
2015-06-04 16:56:48 +08:00
connect ( ui - > loginPassword , & QLineEdit : : returnPressed , this , & LoginScreen : : onLogin ) ;
2015-06-05 01:21:22 +08:00
connect ( ui - > newPass , & QLineEdit : : textChanged , this , & LoginScreen : : onPasswordEdited ) ;
connect ( ui - > newPassConfirm , & QLineEdit : : textChanged , this , & LoginScreen : : onPasswordEdited ) ;
2015-06-05 21:24:02 +08:00
connect ( ui - > autoLoginCB , & QCheckBox : : stateChanged , this , & LoginScreen : : onAutoLoginToggled ) ;
2015-06-04 05:20:47 +08:00
2015-06-04 07:30:17 +08:00
reset ( ) ;
2015-06-06 03:37:01 +08:00
retranslateUi ( ) ;
Translator : : registerHandler ( std : : bind ( & LoginScreen : : retranslateUi , this ) , this ) ;
2015-06-04 07:30:17 +08:00
}
LoginScreen : : ~ LoginScreen ( )
{
2015-06-06 03:37:01 +08:00
Translator : : unregister ( this ) ;
2015-06-04 07:30:17 +08:00
delete ui ;
}
void LoginScreen : : reset ( )
{
ui - > newUsername - > clear ( ) ;
ui - > newPass - > clear ( ) ;
2015-06-05 00:51:27 +08:00
ui - > newPassConfirm - > clear ( ) ;
2015-06-04 07:30:17 +08:00
ui - > loginPassword - > clear ( ) ;
ui - > loginUsernames - > clear ( ) ;
2015-06-05 21:24:02 +08:00
2015-06-04 07:30:17 +08:00
Profile : : scanProfiles ( ) ;
2015-06-04 20:19:18 +08:00
QString lastUsed = Settings : : getInstance ( ) . getCurrentProfile ( ) ;
2015-06-04 05:20:47 +08:00
QVector < QString > profiles = Profile : : getProfiles ( ) ;
for ( QString profile : profiles )
2015-06-04 20:19:18 +08:00
{
2015-06-04 05:20:47 +08:00
ui - > loginUsernames - > addItem ( profile ) ;
2015-06-04 20:19:18 +08:00
if ( profile = = lastUsed )
ui - > loginUsernames - > setCurrentIndex ( ui - > loginUsernames - > count ( ) - 1 ) ;
}
2015-06-04 05:20:47 +08:00
if ( profiles . isEmpty ( ) )
2015-06-05 21:26:45 +08:00
{
2015-06-04 05:20:47 +08:00
ui - > stackedWidget - > setCurrentIndex ( 0 ) ;
2015-06-05 21:26:45 +08:00
ui - > newUsername - > setFocus ( ) ;
}
2015-06-04 05:20:47 +08:00
else
2015-06-05 21:26:45 +08:00
{
2015-06-04 05:20:47 +08:00
ui - > stackedWidget - > setCurrentIndex ( 1 ) ;
2015-06-05 21:26:45 +08:00
ui - > loginPassword - > setFocus ( ) ;
}
2015-06-05 21:24:02 +08:00
2015-10-09 08:10:51 +08:00
ui - > autoLoginCB - > blockSignals ( true ) ;
2015-06-05 21:24:02 +08:00
ui - > autoLoginCB - > setChecked ( Settings : : getInstance ( ) . getAutoLogin ( ) ) ;
2015-10-09 08:10:51 +08:00
ui - > autoLoginCB - > blockSignals ( false ) ;
2015-06-04 01:04:28 +08:00
}
2015-07-30 07:46:19 +08:00
# ifdef Q_OS_MAC
bool LoginScreen : : event ( QEvent * event )
{
if ( event - > type ( ) = = QEvent : : WindowActivate | | event - > type ( ) = = QEvent : : WindowStateChange )
emit windowStateChanged ( windowState ( ) ) ;
return QWidget : : event ( event ) ;
}
# endif
2015-06-04 01:28:16 +08:00
void LoginScreen : : onNewProfilePageClicked ( )
{
ui - > stackedWidget - > setCurrentIndex ( 0 ) ;
}
void LoginScreen : : onLoginPageClicked ( )
{
ui - > stackedWidget - > setCurrentIndex ( 1 ) ;
}
void LoginScreen : : onCreateNewProfile ( )
2015-08-21 05:27:13 +08:00
{
2015-06-04 08:10:06 +08:00
QString name = ui - > newUsername - > text ( ) ;
QString pass = ui - > newPass - > text ( ) ;
if ( name . isEmpty ( ) )
{
QMessageBox : : critical ( this , tr ( " Couldn't create a new profile " ) , tr ( " The username must not be empty. " ) ) ;
return ;
}
2015-06-04 01:28:16 +08:00
2015-06-05 00:41:58 +08:00
if ( pass . size ( ) ! = 0 & & pass . size ( ) < 6 )
2015-06-04 21:22:14 +08:00
{
2015-06-05 15:28:16 +08:00
QMessageBox : : critical ( this , tr ( " Couldn't create a new profile " ) , tr ( " The password must be at least 6 characters long. " ) ) ;
2015-06-04 21:22:14 +08:00
return ;
}
2015-06-04 08:10:06 +08:00
if ( ui - > newPassConfirm - > text ( ) ! = pass )
{
2015-06-05 15:28:16 +08:00
QMessageBox : : critical ( this , tr ( " Couldn't create a new profile " ) , tr ( " The passwords you've entered are different. \n Please make sure to enter same password twice. " ) ) ;
2015-06-04 08:10:06 +08:00
return ;
}
2015-06-05 18:44:22 +08:00
if ( Profile : : exists ( name ) )
2015-06-04 08:10:06 +08:00
{
2015-06-05 15:28:16 +08:00
QMessageBox : : critical ( this , tr ( " Couldn't create a new profile " ) , tr ( " A profile with this name already exists. " ) ) ;
2015-06-04 08:10:06 +08:00
return ;
}
Profile * profile = Profile : : createProfile ( name , pass ) ;
if ( ! profile )
{
// Unknown error
2015-06-05 15:28:16 +08:00
QMessageBox : : critical ( this , tr ( " Couldn't create a new profile " ) , tr ( " Unknown error: Couldn't create a new profile. \n If you encountered this error, please report it. " ) ) ;
2015-06-04 08:10:06 +08:00
return ;
}
Nexus & nexus = Nexus : : getInstance ( ) ;
nexus . setProfile ( profile ) ;
nexus . showMainGUI ( ) ;
2015-06-04 01:28:16 +08:00
}
2015-06-04 07:30:17 +08:00
void LoginScreen : : onLoginUsernameSelected ( const QString & name )
{
if ( name . isEmpty ( ) )
return ;
2015-06-04 08:10:06 +08:00
ui - > loginPassword - > clear ( ) ;
2015-06-04 21:16:25 +08:00
if ( Profile : : isEncrypted ( name ) )
2015-06-04 07:30:17 +08:00
{
ui - > loginPasswordLabel - > show ( ) ;
ui - > loginPassword - > show ( ) ;
}
else
{
ui - > loginPasswordLabel - > hide ( ) ;
ui - > loginPassword - > hide ( ) ;
}
}
2015-06-04 01:28:16 +08:00
void LoginScreen : : onLogin ( )
{
2015-06-04 08:10:06 +08:00
QString name = ui - > loginUsernames - > currentText ( ) ;
QString pass = ui - > loginPassword - > text ( ) ;
2015-10-18 05:08:30 +08:00
// name can be empty when there are no profiles
if ( name . isEmpty ( ) )
{
QMessageBox : : critical ( this , tr ( " Couldn't load profile " ) ,
tr ( " There is no selected profile. \n \n "
" You may want to create one. " ) ) ;
return ;
}
2015-06-04 08:10:06 +08:00
if ( ! ProfileLocker : : isLockable ( name ) )
{
2015-10-18 05:08:30 +08:00
QMessageBox : : critical ( this , tr ( " Couldn't load this profile " ) ,
tr ( " This profile is already in use. " ) ) ;
2015-06-04 08:10:06 +08:00
return ;
}
Profile * profile = Profile : : loadProfile ( name , pass ) ;
if ( ! profile )
{
2015-06-28 03:14:35 +08:00
if ( ! ProfileLocker : : isLockable ( name ) )
{
2015-10-18 05:08:30 +08:00
QMessageBox : : critical ( this , tr ( " Couldn't load this profile " ) ,
tr ( " Profile already in use. Close other clients. " ) ) ;
2015-06-28 03:14:35 +08:00
return ;
}
else
{
2015-10-18 05:08:30 +08:00
QMessageBox : : critical ( this , tr ( " Couldn't load this profile " ) ,
tr ( " Wrong password. " ) ) ;
2015-06-28 03:14:35 +08:00
return ;
}
2015-06-04 08:43:07 +08:00
}
2015-06-04 08:10:06 +08:00
Nexus & nexus = Nexus : : getInstance ( ) ;
2015-06-04 01:28:16 +08:00
2015-06-04 08:10:06 +08:00
nexus . setProfile ( profile ) ;
nexus . showMainGUI ( ) ;
2015-06-04 01:28:16 +08:00
}
2015-06-05 01:21:22 +08:00
void LoginScreen : : onPasswordEdited ( )
{
ui - > passStrengthMeter - > setValue ( SetPasswordDialog : : getPasswordStrength ( ui - > newPass - > text ( ) ) ) ;
}
2015-06-05 21:24:02 +08:00
void LoginScreen : : onAutoLoginToggled ( int state )
{
Qt : : CheckState cstate = static_cast < Qt : : CheckState > ( state ) ;
if ( cstate = = Qt : : CheckState : : Unchecked )
Settings : : getInstance ( ) . setAutoLogin ( false ) ;
else
Settings : : getInstance ( ) . setAutoLogin ( true ) ;
2015-06-05 21:27:28 +08:00
2015-06-06 21:54:58 +08:00
Settings : : getInstance ( ) . saveGlobal ( ) ;
2015-06-05 21:24:02 +08:00
}
2015-06-06 03:37:01 +08:00
void LoginScreen : : retranslateUi ( )
{
ui - > retranslateUi ( this ) ;
}