260 lines
10 KiB
C++
260 lines
10 KiB
C++
#include "ctrlpwdpanel.h"
|
|
#include "gutil/qgui.h"
|
|
#include "gutil/qnetwork.h"
|
|
#include "gutil/qwaitingdlg.h"
|
|
#include "QFileDialog"
|
|
#include <QMessageBox>
|
|
#include <QJsonObject>
|
|
#include "main.h"
|
|
#include "tools.h"
|
|
#include "deviceitem.h"
|
|
#include "devicepanel.h"
|
|
|
|
CtrlPwdPanel::CtrlPwdPanel() {
|
|
auto vBox = new VBox(this);
|
|
vBox->setContentsMargins(6,6,6,0);
|
|
|
|
lbPwdConfig = new QLabel;
|
|
auto font = lbPwdConfig->font();
|
|
font.setPixelSize(16);
|
|
font.setBold(true);
|
|
lbPwdConfig->setFont(font);
|
|
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] {
|
|
if(gSelCards.isEmpty()) {
|
|
QMessageBox::information(this, translate("","Tip"), translate("","Please select screen first"));
|
|
return;
|
|
}
|
|
if(fdOldPwd->isVisible() && fdOldPwd->text().isEmpty()) {
|
|
QMessageBox::information(this, translate("","Tip"), tr("InputOriginalPasswordTip"));
|
|
return;
|
|
}
|
|
if(fdNewPwd->text().isEmpty()) {
|
|
QMessageBox::information(this, translate("","Tip"), tr("InputNewPasswordTip"));
|
|
return;
|
|
}
|
|
if(fdPwdAgain->text().isEmpty()) {
|
|
QMessageBox::information(this, translate("","Tip"), tr("InputRepeatPasswordTip"));
|
|
return;
|
|
}
|
|
if(fdNewPwd->text() != fdPwdAgain->text()) {
|
|
QMessageBox::information(this, translate("","Tip"), tr("InputRepeatPasswordNotSameTip"));
|
|
return;
|
|
}
|
|
auto res = QMessageBox::information(this, translate("","Tip"), 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);
|
|
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());
|
|
if(gSelCards.count() == 1) {
|
|
auto waitingDlg = new WaitingDlg(this, tr("SetControllerPassword")+" ...");
|
|
Def_CtrlReqPre
|
|
connect(reply, &QNetworkReply::finished, this, [=] {
|
|
JValue json;
|
|
QString err = errStrWithJson(reply, &json);
|
|
if(! err.isEmpty()) {
|
|
waitingDlg->close();
|
|
QMessageBox::critical(this, translate("","Error"), err);
|
|
return;
|
|
}
|
|
if(json["result"].toInt()) {
|
|
waitingDlg->close();
|
|
QMessageBox::critical(this, translate("","Tip"), tr("OriginalPasswordErrorTip"));
|
|
return;
|
|
}
|
|
waitingDlg->success();
|
|
lbOldPwd->show();
|
|
fdOldPwd->show();
|
|
btnPwdClear->show();
|
|
btnPwdSet->setText(tr("Modify password"));
|
|
auto item = findItem(card.id);
|
|
if(item) {
|
|
item->mCard.hasPassword = true;
|
|
item->init();
|
|
}
|
|
fdNewPwd->clear();
|
|
fdPwdAgain->clear();
|
|
fdOldPwd->clear();
|
|
});
|
|
} else {
|
|
foreach(auto card, gSelCards) {
|
|
auto reply = NetReq("http://"+card.ip+":2016/settings").timeout(120000).post(json);
|
|
connect(reply, &QNetworkReply::finished, this, [=] {
|
|
JValue json;
|
|
auto err = errStrWithJson(reply, &json);
|
|
if(err.isEmpty()) {
|
|
if(json["result"].toInt()) err = tr("OriginalPasswordErrorTip");
|
|
else {
|
|
err = translate("","Success");
|
|
lbOldPwd->show();
|
|
fdOldPwd->show();
|
|
btnPwdClear->show();
|
|
btnPwdSet->setText(tr("Modify password"));
|
|
auto item = findItem(card.id);
|
|
if(item) {
|
|
item->mCard.hasPassword = true;
|
|
item->init();
|
|
}
|
|
}
|
|
}
|
|
gFdResInfo->append(card.id+" "+tr("SetControllerPassword")+" "+err);
|
|
});
|
|
}
|
|
}
|
|
});
|
|
hBox->addWidget(btnPwdSet);
|
|
|
|
btnPwdClear = new QPushButton;
|
|
btnPwdClear->setMinimumSize(60, 30);
|
|
btnPwdClear->setProperty("ssType", "progManageTool");
|
|
connect(btnPwdClear, &QPushButton::clicked, this, [this] {
|
|
if(gSelCards.isEmpty()) {
|
|
QMessageBox::information(this, translate("","Tip"), translate("","Please select screen first"));
|
|
return;
|
|
}
|
|
if(fdOldPwd->isVisible() && fdOldPwd->text().isEmpty()) {
|
|
QMessageBox::information(this, translate("","Tip"), tr("InputOriginalPasswordTip"));
|
|
return;
|
|
}
|
|
QJsonObject json;
|
|
json.insert("_id", "SetControllerPassword");
|
|
json.insert("_type", "SetControllerPassword");
|
|
json.insert("pwd", fdOldPwd->text());
|
|
json.insert("newPwd","");
|
|
if(gSelCards.count() == 1) {
|
|
auto waitingDlg = new WaitingDlg(this, tr("SetControllerPassword")+" ...");
|
|
Def_CtrlReqPre
|
|
connect(reply, &QNetworkReply::finished, this, [=] {
|
|
JValue json;
|
|
auto err = errStrWithJson(reply, &json);
|
|
if(! err.isEmpty()) {
|
|
waitingDlg->close();
|
|
QMessageBox::critical(this, translate("","Error"), err);
|
|
return;
|
|
}
|
|
if(json["result"].toInt()) {
|
|
waitingDlg->close();
|
|
QMessageBox::critical(this, translate("","Tip"), tr("OriginalPasswordErrorTip"));
|
|
return;
|
|
}
|
|
waitingDlg->success();
|
|
lbOldPwd->hide();
|
|
fdOldPwd->hide();
|
|
btnPwdClear->hide();
|
|
btnPwdSet->setText(tr("Set encryption"));
|
|
auto item = findItem(card.id);
|
|
if(item) {
|
|
item->mCard.hasPassword = false;
|
|
item->init();
|
|
}
|
|
fdNewPwd->clear();
|
|
fdPwdAgain->clear();
|
|
fdOldPwd->clear();
|
|
});
|
|
} else {
|
|
foreach(auto card, gSelCards) {
|
|
auto reply = NetReq("http://"+card.ip+":2016/settings").timeout(120000).post(json);
|
|
connect(reply, &QNetworkReply::finished, this, [=] {
|
|
JValue json;
|
|
auto err = errStrWithJson(reply, &json);
|
|
if(err.isEmpty()) {
|
|
if(json["result"].toInt()) err = tr("OriginalPasswordErrorTip");
|
|
else {
|
|
err = translate("","Success");
|
|
lbOldPwd->hide();
|
|
fdOldPwd->hide();
|
|
btnPwdClear->hide();
|
|
btnPwdSet->setText(tr("Set encryption"));
|
|
auto item = findItem(card.id);
|
|
if(item) {
|
|
item->mCard.hasPassword = false;
|
|
item->init();
|
|
}
|
|
}
|
|
}
|
|
gFdResInfo->append(card.id+" "+tr("SetControllerPassword")+" "+err);
|
|
});
|
|
}
|
|
}
|
|
});
|
|
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() {
|
|
bool isSingle = gSelCards.count()==1;
|
|
if(! isSingle) return;
|
|
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"));
|
|
}
|
|
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"));
|
|
}
|