code_app/app/fwupgrade/mainwindow.cpp

412 lines
12 KiB
C++
Raw Normal View History

2022-11-10 02:22:58 +00:00
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QLabel>
#include <QMovie>
#include <QMessageBox>
2022-11-10 09:23:16 +00:00
#include <QFileDialog>
#include "dialog_upgradefirmware.h"
#include "base/HGBase.h"
#define PASSWORD_KEY 4
2022-11-10 02:22:58 +00:00
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
2022-11-10 09:23:16 +00:00
m_curDevName.clear();
m_curDevHandle = nullptr;
m_curFwVersion.clear();
ui->editFilePath->setEnabled(false);
ui->editOldPassword->setEchoMode(QLineEdit::EchoMode::Password);
ui->editNewPassword->setEchoMode(QLineEdit::EchoMode::Password);
ui->editNewPassword_2->setEchoMode(QLineEdit::EchoMode::Password);
ui->btnGetVersionList->setEnabled(false);
ui->btnDownloadUpgrade->setEnabled(false);
ui->btnOpenFilePath->setEnabled(false);
ui->btnUpgrade->setEnabled(false);
ui->btnClearRollCount->setEnabled(false);
connect(this, SIGNAL(sane_dev_arrive(QString, bool)), this, SLOT(on_sane_dev_arrive(QString, bool)), Qt::QueuedConnection);
connect(this, SIGNAL(sane_dev_remove(QString)), this, SLOT(on_sane_dev_remove(QString)), Qt::QueuedConnection);
m_versionMgr = nullptr;
HGVersion_CreateMgr(&m_versionMgr);
m_versionInfo = nullptr;
m_versionCount = 0;
SANE_Int v = 0;
sane_init_ex(&v, sane_ex_callback, this);
2022-11-10 02:22:58 +00:00
}
MainWindow::~MainWindow()
{
2022-11-10 09:23:16 +00:00
if (nullptr != m_curDevHandle)
{
sane_close(m_curDevHandle);
m_curDevHandle = nullptr;
}
m_curFwVersion.clear();
m_curDevName.clear();
sane_exit();
HGVersion_ReleaseVersionList(m_versionInfo, m_versionCount);
m_versionInfo = nullptr;
m_versionCount = 0;
HGVersion_DestroyMgr(m_versionMgr);
m_versionMgr = nullptr;
2022-11-10 02:22:58 +00:00
delete ui;
}
2022-11-10 09:23:16 +00:00
QString MainWindow::passwordEncrypt(const QString& password)
{
QString p = password;
int num = PASSWORD_KEY - p.length() % PASSWORD_KEY;
for (int i = 0; i < num; i++)
p.append("+");
int rows = p.length() / 4;
QString transcode;
for (int i = 0; i < PASSWORD_KEY; i++)
for (int j = 0; j < rows; j++)
transcode.append(p[i + j * PASSWORD_KEY]);
return transcode;
}
QString MainWindow::passwordDecrypt(const QString& transcode)
{
QString t = transcode;
int cols = t.length() / 4;
QString password;
for (int i = 0; i < cols; i++)
for (int j = 0; j < PASSWORD_KEY; j++)
password.append(t[i + j * cols]);
password.remove("+");
return password;
}
void MainWindow::on_sane_dev_arrive(QString devName, bool opened)
{
int idx = -1;
int count = ui->comboDevList->count();
for (int i = 0; i < count; ++i)
{
QString name = ui->comboDevList->itemText(i);
if (name == devName)
{
idx = i;
break;
}
}
if (-1 == idx)
{
ui->comboDevList->addItem(devName);
ui->comboDevList2->addItem(devName);
}
}
void MainWindow::on_sane_dev_remove(QString devName)
{
int idx = -1;
int count = ui->comboDevList->count();
for (int i = 0; i < count; ++i)
{
QString name = ui->comboDevList->itemText(i);
if (name == devName)
{
idx = i;
break;
}
}
if (-1 != idx)
{
ui->comboDevList->removeItem(idx);
ui->comboDevList2->removeItem(idx);
}
}
void MainWindow::on_btnExit_clicked()
{
close();
}
int MainWindow::sane_ex_callback(SANE_Handle hdev, int code, void *data, unsigned int* len, void *param)
{
(void)hdev;
(void)len;
MainWindow *p = (MainWindow *)param;
switch (code)
{
case SANE_EVENT_DEVICE_ARRIVED:
{
SANE_Device_Ex* sane_dev = (SANE_Device_Ex*)data;
emit p->sane_dev_arrive(sane_dev->name, sane_dev->openned == SANE_TRUE);
}
break;
case SANE_EVENT_DEVICE_LEFT:
{
SANE_Device* sane_dev = (SANE_Device*)data;
emit p->sane_dev_remove(sane_dev->name);
}
break;
}
return 0;
}
void MainWindow::on_comboDevList_currentIndexChanged(int index)
{
ui->comboDevList2->setCurrentIndex(index);
HGVersion_ReleaseVersionList(m_versionInfo, m_versionCount);
m_versionInfo = nullptr;
m_versionCount = 0;
ui->comboVersionList->clear();
ui->editFilePath->clear();
ui->labelRollCount->setText("0");
ui->labelScanCount->setText("0");
if (nullptr != m_curDevHandle)
{
sane_close(m_curDevHandle);
m_curDevHandle = nullptr;
}
m_curFwVersion.clear();
m_curDevName.clear();
ui->btnGetVersionList->setEnabled(false);
ui->btnDownloadUpgrade->setEnabled(false);
ui->btnOpenFilePath->setEnabled(false);
ui->btnUpgrade->setEnabled(false);
ui->btnClearRollCount->setEnabled(false);
if (-1 != index)
{
QString name = ui->comboDevList->itemText(index);
if (SANE_STATUS_GOOD == sane_open(name.toStdString().c_str(), &m_curDevHandle))
{
m_curDevName = name;
std::string versionNum;
unsigned int versionNumLen = 0;
if(SANE_STATUS_NO_MEM == sane_io_control(m_curDevHandle, IO_CTRL_CODE_GET_HARDWARE_VERSION, nullptr, &versionNumLen)
&& versionNumLen)
{
versionNum.resize(versionNumLen);
sane_io_control(m_curDevHandle, IO_CTRL_CODE_GET_HARDWARE_VERSION, &versionNum[0], &versionNumLen);
}
m_curFwVersion = QString::fromStdString(versionNum.c_str());
2022-11-11 03:56:19 +00:00
ui->labelDevInfo->setText(QString(tr("open device: %1, firmware version: %2")).arg(name).arg(m_curFwVersion));
2022-11-10 09:23:16 +00:00
2022-11-11 03:56:19 +00:00
SANE_Int historyScanNum = 0;
unsigned int historyScanNumLen = sizeof(SANE_Int);
SANE_Status ret = sane_io_control(m_curDevHandle, IO_CTRL_CODE_GET_HISTORY_NUM, &historyScanNum, &historyScanNumLen);
if(ret == SANE_STATUS_GOOD && historyScanNum >= 0)
{
ui->labelRollCount->setText(QString(historyScanNum));
ui->labelScanCount->setText(QString(historyScanNum));
}
else
{
ui->labelRollCount->setText(tr("do not support"));
ui->labelScanCount->setText(tr("do not support"));
}
2022-11-10 09:23:16 +00:00
ui->btnGetVersionList->setEnabled(true);
ui->btnOpenFilePath->setEnabled(true);
ui->btnClearRollCount->setEnabled(true);
}
else
{
2022-11-11 03:56:19 +00:00
ui->labelDevInfo->setText(QString(tr("open device error: %1")).arg(name));
2022-11-10 09:23:16 +00:00
}
}
else
{
2022-11-11 03:56:19 +00:00
ui->labelDevInfo->setText(tr("no device opened"));
2022-11-10 09:23:16 +00:00
}
}
void MainWindow::on_btnGetVersionList_clicked()
{
HGVersion_ReleaseVersionList(m_versionInfo, m_versionCount);
m_versionInfo = nullptr;
m_versionCount = 0;
ui->comboVersionList->clear();
std::string devType;
if (m_curFwVersion.left(2) == "G1")
devType = "G100";
else if (m_curFwVersion.left(2) == "G2")
devType = "G200";
else if (m_curFwVersion.left(2) == "G3")
devType = "G300";
else if (m_curFwVersion.left(2) == "G4")
devType = "G400";
HGVersion_GetDriverVersionList(m_versionMgr, devType.c_str(), &m_versionInfo, &m_versionCount);
if (0 != m_versionCount)
{
for (HGUInt i = 0; i < m_versionCount; ++i)
{
ui->comboVersionList->addItem(m_versionInfo[i].version);
}
}
else
{
QMessageBox msg(QMessageBox::Information, tr("tips"), tr("no version available"), QMessageBox::Yes, this);
msg.setButtonText(QMessageBox::Yes, tr("yes"));
msg.exec();
}
}
void MainWindow::on_comboVersionList_currentIndexChanged(int index)
{
if (-1 != index)
{
ui->btnDownloadUpgrade->setEnabled(true);
}
else
{
ui->btnDownloadUpgrade->setEnabled(false);
}
}
void MainWindow::on_editFilePath_textChanged(const QString &arg1)
{
if (arg1.isEmpty())
{
ui->btnUpgrade->setEnabled(false);
}
else
{
ui->btnUpgrade->setEnabled(true);
}
}
void MainWindow::on_btnOpenFilePath_clicked()
{
QString filePath = QFileDialog::getOpenFileName(this, tr("Open File"), ".", tr("Zip Files(*.zip *.zip)"));
if (!filePath.isEmpty())
{
ui->editFilePath->setText(filePath);
}
}
void MainWindow::on_btnDownloadUpgrade_clicked()
{
int idx = ui->comboVersionList->currentIndex();
if (-1 != idx)
{
Dialog_upgradeFirmware dlg(m_versionMgr, m_curDevHandle, m_versionInfo[idx].url, m_versionInfo[idx].version,
m_versionInfo[idx].md5, this);
dlg.exec();
if (0 == dlg.getUpgradeStatus())
{
2022-11-11 03:56:19 +00:00
ui->labelDevInfo->setText(QString(tr("device: %1 upgrade firmware success")).arg(m_curDevName));
2022-11-10 09:23:16 +00:00
}
else if (1 == dlg.getUpgradeStatus())
{
2022-11-11 03:56:19 +00:00
ui->labelDevInfo->setText(QString(tr("device: %1 upgrade firmware failed, download file fail")).arg(m_curDevName));
2022-11-10 09:23:16 +00:00
}
else if (2 == dlg.getUpgradeStatus())
{
2022-11-11 03:56:19 +00:00
ui->labelDevInfo->setText(QString(tr("device: %1 upgrade firmware failed, io error")).arg(m_curDevName));
2022-11-10 09:23:16 +00:00
}
}
}
void MainWindow::on_btnUpgrade_clicked()
{
std::string filePath = ui->editFilePath->text().toStdString();
if (!filePath.empty())
{
Dialog_upgradeFirmware dlg(m_curDevHandle, filePath, this);
dlg.exec();
if (0 == dlg.getUpgradeStatus())
{
2022-11-11 03:56:19 +00:00
ui->labelDevInfo->setText(QString(tr("device: %1 upgrade firmware success")).arg(m_curDevName));
2022-11-10 09:23:16 +00:00
}
else if (2 == dlg.getUpgradeStatus())
{
2022-11-11 03:56:19 +00:00
ui->labelDevInfo->setText(QString(tr("device: %1 upgrade firmware failed, io error")).arg(m_curDevName));
2022-11-10 09:23:16 +00:00
}
}
}
void MainWindow::on_comboDevList2_currentIndexChanged(int index)
{
ui->comboDevList->setCurrentIndex(index);
}
void MainWindow::on_btnClearRollCount_clicked()
{
unsigned int count = 0;
int ret = sane_io_control(m_curDevHandle, IO_CTRL_CODE_CLEAR_ROLLER_COUNT, nullptr, &count);
QString info;
if(ret == SANE_STATUS_GOOD)
info = tr("Roller scanned count has been set to 0.");
else
info = tr("Roller scanned count reset failed.");
QMessageBox msg(QMessageBox::Information, tr("tips"), info, QMessageBox::Yes, this);
msg.setButtonText(QMessageBox::Yes, tr("yes"));
msg.exec();
}
void MainWindow::on_btnModifyPassword_clicked()
{
HGChar cfgPath[256]= {0};
HGBase_GetConfigPath(cfgPath, 256);
strcat(cfgPath, "config.ini");
HGChar str[256] = {0};
HGBase_GetProfileString(cfgPath, "login", "password", "", str, 256);
QString password = (0 == *str) ? "123456" : MainWindow::passwordDecrypt(str);
if (password != ui->editOldPassword->text())
{
2022-11-11 03:56:19 +00:00
QMessageBox msg(QMessageBox::Information, tr("tips"), tr("old password is wrong"), QMessageBox::Yes, this);
2022-11-10 09:23:16 +00:00
msg.setButtonText(QMessageBox::Yes, tr("yes"));
msg.exec();
return;
}
if (ui->editNewPassword->text().isEmpty())
{
2022-11-11 03:56:19 +00:00
QMessageBox msg(QMessageBox::Information, tr("tips"), tr("new password can not be empty"), QMessageBox::Yes, this);
2022-11-10 09:23:16 +00:00
msg.setButtonText(QMessageBox::Yes, tr("yes"));
msg.exec();
return;
}
if (ui->editNewPassword->text() != ui->editNewPassword_2->text())
{
2022-11-11 03:56:19 +00:00
QMessageBox msg(QMessageBox::Information, tr("tips"), tr("new password is inconsistent"), QMessageBox::Yes, this);
2022-11-10 09:23:16 +00:00
msg.setButtonText(QMessageBox::Yes, tr("yes"));
msg.exec();
return;
}
if (HGBASE_ERR_OK != HGBase_SetProfileString(cfgPath, "login", "password", passwordEncrypt(ui->editNewPassword->text()).toStdString().c_str()))
{
2022-11-11 03:56:19 +00:00
QMessageBox msg(QMessageBox::Information, tr("tips"), tr("modify password fail"), QMessageBox::Yes, this);
2022-11-10 09:23:16 +00:00
msg.setButtonText(QMessageBox::Yes, tr("yes"));
msg.exec();
return;
}
2022-11-11 03:56:19 +00:00
QMessageBox msg(QMessageBox::Information, tr("tips"), tr("modify password success"), QMessageBox::Yes, this);
2022-11-10 09:23:16 +00:00
msg.setButtonText(QMessageBox::Yes, tr("yes"));
msg.exec();
}