2023-04-18 14:14:46 +08:00
# include "ctrlpwdpanel.h"
# include "gutil/qgui.h"
2023-05-15 16:06:10 +08:00
# include "gutil/qnetwork.h"
2023-10-23 15:08:45 +08:00
# include "gutil/qwaitingdlg.h"
2023-04-18 14:14:46 +08:00
# include "QFileDialog"
# include <QMessageBox>
# include <QJsonObject>
2023-05-15 16:06:10 +08:00
# include "globaldefine.h"
2023-04-18 14:14:46 +08:00
# include "tools.h"
2023-05-15 16:06:10 +08:00
# include "deviceitem.h"
# include "devicepanel.h"
2023-04-18 14:14:46 +08:00
2023-08-01 11:42:41 +08:00
CtrlPwdPanel : : CtrlPwdPanel ( ) {
2023-04-18 14:14:46 +08:00
auto vBox = new VBox ( this ) ;
2023-10-23 15:08:45 +08:00
vBox - > setContentsMargins ( 6 , 6 , 6 , 0 ) ;
2023-04-18 14:14:46 +08:00
lbPwdConfig = new QLabel ;
2023-10-23 15:08:45 +08:00
auto font = lbPwdConfig - > font ( ) ;
font . setPixelSize ( 16 ) ;
font . setBold ( true ) ;
lbPwdConfig - > setFont ( font ) ;
2023-04-18 14:14:46 +08:00
lbPwdConfig - > setAlignment ( Qt : : AlignCenter ) ;
vBox - > addWidget ( lbPwdConfig ) ;
vBox - > addSpacing ( 20 ) ;
auto grid = new Grid ( vBox ) ;
grid - > setColumnStretch ( 0 , 1 ) ;
grid - > setColumnStretch ( 3 , 1 ) ;
lbOldPwd = new QLabel ;
grid - > addWidget ( lbOldPwd , 0 , 1 ) ;
lbNewPwd = new QLabel ;
grid - > addWidget ( lbNewPwd , 1 , 1 ) ;
lbPwdAgain = new QLabel ;
grid - > addWidget ( lbPwdAgain , 2 , 1 ) ;
fdOldPwd = new QLineEdit ;
fdOldPwd - > setFixedSize ( 160 , 30 ) ;
fdOldPwd - > setEchoMode ( QLineEdit : : PasswordEchoOnEdit ) ;
grid - > addWidget ( fdOldPwd , 0 , 2 ) ;
fdNewPwd = new QLineEdit ;
fdNewPwd - > setFixedSize ( 160 , 30 ) ;
fdNewPwd - > setEchoMode ( QLineEdit : : PasswordEchoOnEdit ) ;
grid - > addWidget ( fdNewPwd , 1 , 2 ) ;
fdPwdAgain = new QLineEdit ;
fdPwdAgain - > setFixedSize ( 160 , 30 ) ;
fdPwdAgain - > setEchoMode ( QLineEdit : : PasswordEchoOnEdit ) ;
grid - > addWidget ( fdPwdAgain , 2 , 2 ) ;
vBox - > addSpacing ( 20 ) ;
auto hBox = new HBox ( vBox ) ;
hBox - > addStretch ( ) ;
btnPwdSet = new QPushButton ;
btnPwdSet - > setMinimumSize ( 60 , 30 ) ;
btnPwdSet - > setProperty ( " ssType " , " progManageTool " ) ;
connect ( btnPwdSet , & QPushButton : : clicked , this , [ this ] {
2023-05-15 16:06:10 +08:00
if ( gSelCards . isEmpty ( ) ) {
2023-05-11 11:47:00 +08:00
QMessageBox : : information ( this , tr ( " Tip " ) , tr ( " NoSelectedController " ) ) ;
2023-04-18 14:14:46 +08:00
return ;
}
if ( fdOldPwd - > isVisible ( ) & & fdOldPwd - > text ( ) . isEmpty ( ) ) {
2023-05-11 11:47:00 +08:00
QMessageBox : : information ( this , tr ( " Tip " ) , tr ( " InputOriginalPasswordTip " ) ) ;
2023-04-18 14:14:46 +08:00
return ;
}
if ( fdNewPwd - > text ( ) . isEmpty ( ) ) {
2023-05-11 11:47:00 +08:00
QMessageBox : : information ( this , tr ( " Tip " ) , tr ( " InputNewPasswordTip " ) ) ;
2023-04-18 14:14:46 +08:00
return ;
}
if ( fdPwdAgain - > text ( ) . isEmpty ( ) ) {
2023-05-11 11:47:00 +08:00
QMessageBox : : information ( this , tr ( " Tip " ) , tr ( " InputRepeatPasswordTip " ) ) ;
2023-04-18 14:14:46 +08:00
return ;
}
if ( fdNewPwd - > text ( ) ! = fdPwdAgain - > text ( ) ) {
2023-05-11 11:47:00 +08:00
QMessageBox : : information ( this , tr ( " Tip " ) , tr ( " InputRepeatPasswordNotSameTip " ) ) ;
2023-04-18 14:14:46 +08:00
return ;
}
2023-05-11 11:47:00 +08:00
auto res = QMessageBox : : information ( this , tr ( " Tip Info " ) , tr ( " After setting the password, please remember the password and record it. If you forget the password, the device will be unable to operate. Are you sure you want to continue with this operation? " ) , QMessageBox : : Ok , QMessageBox : : Cancel ) ;
2023-04-18 14:14:46 +08:00
if ( res ! = QMessageBox : : Ok ) return ;
QJsonObject json ;
json . insert ( " _id " , " SetControllerPassword " ) ;
json . insert ( " _type " , " SetControllerPassword " ) ;
json . insert ( " pwd " , fdOldPwd - > text ( ) ) ;
json . insert ( " newPwd " , fdNewPwd - > text ( ) ) ;
2023-05-15 16:06:10 +08:00
if ( gSelCards . count ( ) = = 1 ) {
2023-04-18 14:14:46 +08:00
auto waitingDlg = new WaitingDlg ( this , tr ( " SetControllerPassword " ) + " ... " ) ;
Def_CtrlReqPre
2023-05-15 16:06:10 +08:00
connect ( reply , & QNetworkReply : : finished , this , [ = ] {
2023-04-18 14:14:46 +08:00
QJsonDocument json ;
QString err = checkReplyForJson ( reply , & json ) ;
if ( ! err . isEmpty ( ) ) {
waitingDlg - > close ( ) ;
2023-05-11 11:47:00 +08:00
QMessageBox : : critical ( this , tr ( " Error " ) , err ) ;
2023-04-18 14:14:46 +08:00
return ;
}
if ( json [ " result " ] . toInt ( ) ) {
waitingDlg - > close ( ) ;
2023-05-11 11:47:00 +08:00
QMessageBox : : critical ( this , tr ( " Tip " ) , tr ( " OriginalPasswordErrorTip " ) ) ;
2023-04-18 14:14:46 +08:00
return ;
}
waitingDlg - > success ( ) ;
lbOldPwd - > show ( ) ;
fdOldPwd - > show ( ) ;
btnPwdClear - > show ( ) ;
btnPwdSet - > setText ( tr ( " Modify password " ) ) ;
2023-05-15 16:06:10 +08:00
auto item = findItem ( card . id ) ;
if ( item ) {
item - > mCard . hasPassword = true ;
item - > init ( ) ;
}
2023-04-18 14:14:46 +08:00
fdNewPwd - > clear ( ) ;
fdPwdAgain - > clear ( ) ;
fdOldPwd - > clear ( ) ;
} ) ;
} else {
2023-05-15 16:06:10 +08:00
foreach ( auto card , gSelCards ) {
auto reply = NetReq ( " http:// " + card . ip + " :2016/settings " ) . timeout ( 120000 ) . post ( json ) ;
connect ( reply , & QNetworkReply : : finished , this , [ = ] {
2023-04-18 14:14:46 +08:00
QJsonDocument json ;
QString err = checkReplyForJson ( reply , & json ) ;
if ( err . isEmpty ( ) ) {
if ( json [ " result " ] . toInt ( ) ) err = tr ( " OriginalPasswordErrorTip " ) ;
else {
err = tr ( " Success " ) ;
lbOldPwd - > show ( ) ;
fdOldPwd - > show ( ) ;
btnPwdClear - > show ( ) ;
btnPwdSet - > setText ( tr ( " Modify password " ) ) ;
2023-05-15 16:06:10 +08:00
auto item = findItem ( card . id ) ;
if ( item ) {
item - > mCard . hasPassword = true ;
item - > init ( ) ;
}
2023-04-18 14:14:46 +08:00
}
}
2023-05-15 16:06:10 +08:00
gFdResInfo - > append ( card . id + " " + tr ( " SetControllerPassword " ) + " " + err ) ;
2023-04-18 14:14:46 +08:00
} ) ;
}
}
} ) ;
hBox - > addWidget ( btnPwdSet ) ;
btnPwdClear = new QPushButton ;
btnPwdClear - > setMinimumSize ( 60 , 30 ) ;
btnPwdClear - > setProperty ( " ssType " , " progManageTool " ) ;
connect ( btnPwdClear , & QPushButton : : clicked , this , [ this ] {
2023-05-15 16:06:10 +08:00
if ( gSelCards . isEmpty ( ) ) {
2023-05-11 11:47:00 +08:00
QMessageBox : : information ( this , tr ( " Tip " ) , tr ( " NoSelectedController " ) ) ;
2023-04-18 14:14:46 +08:00
return ;
}
if ( fdOldPwd - > isVisible ( ) & & fdOldPwd - > text ( ) . isEmpty ( ) ) {
2023-05-11 11:47:00 +08:00
QMessageBox : : information ( this , tr ( " Tip " ) , tr ( " InputOriginalPasswordTip " ) ) ;
2023-04-18 14:14:46 +08:00
return ;
}
QJsonObject json ;
json . insert ( " _id " , " SetControllerPassword " ) ;
json . insert ( " _type " , " SetControllerPassword " ) ;
json . insert ( " pwd " , fdOldPwd - > text ( ) ) ;
json . insert ( " newPwd " , " " ) ;
2023-05-15 16:06:10 +08:00
if ( gSelCards . count ( ) = = 1 ) {
2023-04-18 14:14:46 +08:00
auto waitingDlg = new WaitingDlg ( this , tr ( " SetControllerPassword " ) + " ... " ) ;
Def_CtrlReqPre
2023-05-15 16:06:10 +08:00
connect ( reply , & QNetworkReply : : finished , this , [ = ] {
2023-04-18 14:14:46 +08:00
QJsonDocument json ;
QString err = checkReplyForJson ( reply , & json ) ;
if ( ! err . isEmpty ( ) ) {
waitingDlg - > close ( ) ;
2023-05-11 11:47:00 +08:00
QMessageBox : : critical ( this , tr ( " Error " ) , err ) ;
2023-04-18 14:14:46 +08:00
return ;
}
if ( json [ " result " ] . toInt ( ) ) {
waitingDlg - > close ( ) ;
2023-05-11 11:47:00 +08:00
QMessageBox : : critical ( this , tr ( " Tip " ) , tr ( " OriginalPasswordErrorTip " ) ) ;
2023-04-18 14:14:46 +08:00
return ;
}
waitingDlg - > success ( ) ;
lbOldPwd - > hide ( ) ;
fdOldPwd - > hide ( ) ;
btnPwdClear - > hide ( ) ;
btnPwdSet - > setText ( tr ( " Set encryption " ) ) ;
2023-05-15 16:06:10 +08:00
auto item = findItem ( card . id ) ;
if ( item ) {
item - > mCard . hasPassword = false ;
item - > init ( ) ;
}
2023-04-18 14:14:46 +08:00
fdNewPwd - > clear ( ) ;
fdPwdAgain - > clear ( ) ;
fdOldPwd - > clear ( ) ;
} ) ;
} else {
2023-05-15 16:06:10 +08:00
foreach ( auto card , gSelCards ) {
auto reply = NetReq ( " http:// " + card . ip + " :2016/settings " ) . timeout ( 120000 ) . post ( json ) ;
connect ( reply , & QNetworkReply : : finished , this , [ = ] {
2023-04-18 14:14:46 +08:00
QJsonDocument json ;
QString err = checkReplyForJson ( reply , & json ) ;
if ( err . isEmpty ( ) ) {
if ( json [ " result " ] . toInt ( ) ) err = tr ( " OriginalPasswordErrorTip " ) ;
else {
err = tr ( " Success " ) ;
lbOldPwd - > hide ( ) ;
fdOldPwd - > hide ( ) ;
btnPwdClear - > hide ( ) ;
btnPwdSet - > setText ( tr ( " Set encryption " ) ) ;
2023-05-15 16:06:10 +08:00
auto item = findItem ( card . id ) ;
if ( item ) {
item - > mCard . hasPassword = false ;
item - > init ( ) ;
}
2023-04-18 14:14:46 +08:00
}
}
2023-05-15 16:06:10 +08:00
gFdResInfo - > append ( card . id + " " + tr ( " SetControllerPassword " ) + " " + err ) ;
2023-04-18 14:14:46 +08:00
} ) ;
}
}
} ) ;
hBox - > addWidget ( btnPwdClear ) ;
hBox - > addStretch ( ) ;
vBox - > addStretch ( ) ;
connect ( gDevicePanel , & DevicePanel : : sigSelectedDeviceList , this , [ this ] {
if ( isVisible ( ) ) init ( ) ;
} ) ;
transUi ( ) ;
}
void CtrlPwdPanel : : showEvent ( QShowEvent * event ) {
QWidget : : showEvent ( event ) ;
init ( ) ;
}
void CtrlPwdPanel : : init ( ) {
2023-05-15 16:06:10 +08:00
bool isSingle = gSelCards . count ( ) = = 1 ;
2023-04-18 14:14:46 +08:00
if ( ! isSingle ) return ;
2023-05-15 16:06:10 +08:00
auto card = gSelCards [ 0 ] ;
lbOldPwd - > setVisible ( card . hasPassword ) ;
fdOldPwd - > setVisible ( card . hasPassword ) ;
btnPwdClear - > setVisible ( card . hasPassword ) ;
btnPwdSet - > setText ( card . hasPassword ? tr ( " Modify password " ) : tr ( " Set encryption " ) ) ;
2023-04-18 14:14:46 +08:00
}
void CtrlPwdPanel : : changeEvent ( QEvent * event ) {
QWidget : : changeEvent ( event ) ;
if ( event - > type ( ) = = QEvent : : LanguageChange ) transUi ( ) ;
}
void CtrlPwdPanel : : transUi ( ) {
lbPwdConfig - > setText ( tr ( " Set Password " ) ) ;
lbOldPwd - > setText ( tr ( " Original password " ) ) ;
lbNewPwd - > setText ( tr ( " New password " ) ) ;
lbPwdAgain - > setText ( tr ( " Enter again " ) ) ;
fdOldPwd - > setPlaceholderText ( tr ( " original password " ) ) ;
fdNewPwd - > setPlaceholderText ( tr ( " New password " ) ) ;
fdPwdAgain - > setPlaceholderText ( tr ( " Repeat new password " ) ) ;
btnPwdSet - > setText ( tr ( " Set encryption " ) ) ;
btnPwdClear - > setText ( tr ( " Cancel encryption " ) ) ;
}