#include "mainwindow.h" #include "ui_mainwindow.h" #include #include #include #include #include #include #include "dialog_upgradefirmware.h" #include "dialog_updateprogress.h" #include "base/HGBase.h" #include "HGString.h" #include #define PASSWORD_KEY 4 MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent) , ui(new Ui::MainWindow) { ui->setupUi(this); 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); SANE_Int v = 0; sane_init_ex(&v, sane_ex_callback, this); } MainWindow::~MainWindow() { if (nullptr != m_curDevHandle) { sane_close(m_curDevHandle); m_curDevHandle = nullptr; } m_curFwVersion.clear(); m_curDevName.clear(); sane_exit(); m_vVersion.clear(); HGVersion_DestroyMgr(m_versionMgr); m_versionMgr = nullptr; delete ui; } 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; } QString MainWindow::GetDevType(const QString& fwVersion, bool *canLock) { if (NULL != canLock) *canLock = false; QString devType; if (fwVersion.length() == 10) { QString dev = fwVersion.left(2); if (fwVersion.mid(2, 2) == "40" || fwVersion.mid(2, 2) == "50" || fwVersion.mid(2, 2) == "60" || fwVersion.mid(2, 2) == "70") { // 3288 QString ver = fwVersion.mid(2, 2); QString date = fwVersion.mid(4, 6); } else { // 3399 QString ver = fwVersion.mid(2, 3); QString date = fwVersion.mid(5, 5); if ((dev == "G1" || dev == "G2") && ver == "393") { if (date >= "A1220" && date <= "A1230") { devType = dev + ver + "/" + "A1220-A1230"; if (date >= "A1224") { if (NULL != canLock) *canLock = true; } } else if (date >= "B0326" && date <= "B0334") { devType = dev + ver + "/" + "B0326-B0334"; if (date >= "B0333") { if (NULL != canLock) *canLock = true; } } else if (date >= "B0429") { devType = dev + ver + "/" + "B0429-"; if (date >= "B1031" || date == "B0500") { if (NULL != canLock) *canLock = true; } } } else if (dev == "G4" && ver == "393") { devType = dev + ver + "/"; if (date >= "B1108") { if (NULL != canLock) *canLock = true; } } } } return devType; } 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) { assert(!opened); ui->comboDevList->addItem(devName); ui->comboDevList2->addItem(devName); } else if (opened) { ui->comboDevList->setCurrentIndex(-1); ui->comboDevList->setCurrentIndex(idx); } } 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); m_vVersion.clear(); 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()); QString devType = GetDevType(m_curFwVersion, NULL); ui->labelDevInfo->setText(QString(tr("device: %1, firmware version: %2, upgrade: %3")).arg(name) .arg(m_curFwVersion).arg(!devType.isEmpty() ? tr("support") : tr("do not support"))); SANE_Int rollCount = 0; unsigned int rollCountSize = sizeof(SANE_Int); SANE_Status ret = sane_io_control(m_curDevHandle, IO_CTRL_CODE_GET_HISTORY_ROLLER_NUM, &rollCount, &rollCountSize); if(ret == SANE_STATUS_GOOD && rollCount >= 0) { ui->labelRollCount->setText(QString("%1").arg(rollCount)); } else { ui->labelRollCount->setText(tr("do not support")); } SANE_Int scanCount = 0; unsigned int scanCountSize = sizeof(SANE_Int); ret = sane_io_control(m_curDevHandle, IO_CTRL_CODE_GET_SCANN_NUM, &scanCount, &scanCountSize); if (ret == SANE_STATUS_GOOD && scanCount >= 0) { ui->labelScanCount->setText(QString("%1").arg(scanCount)); } else { ui->labelScanCount->setText(tr("do not support")); } if (!devType.isEmpty()) { ui->btnGetVersionList->setEnabled(true); ui->btnOpenFilePath->setEnabled(true); } ui->btnClearRollCount->setEnabled(true); } else { ui->labelDevInfo->setText(QString(tr("open device error: %1")).arg(name)); } } else { ui->labelDevInfo->setText(tr("no device opened")); } } static bool Greater(const VersionInfo &info1, const VersionInfo &info2) { return info1.version > info2.version; } void MainWindow::on_btnGetVersionList_clicked() { m_vVersion.clear(); ui->comboVersionList->clear(); QString devType = GetDevType(m_curFwVersion, NULL); assert(!devType.isEmpty()); HGVersionInfo *versionInfo = nullptr; HGUInt versionCount = 0; HGVersion_GetDriverVersionList(m_versionMgr, devType.toStdString().c_str(), &versionInfo, &versionCount); if (0 != versionCount) { for (HGUInt i = 0; i < versionCount; ++i) { if (QString(versionInfo[i].version) > m_curFwVersion) { VersionInfo inf; inf.version = versionInfo[i].version; inf.url = versionInfo[i].url; inf.md5 = versionInfo[i].md5; m_vVersion.push_back(inf); } } } HGVersion_ReleaseVersionList(versionInfo, versionCount); std::sort(m_vVersion.begin(), m_vVersion.end(), Greater); for (int i = 0; i < m_vVersion.size(); ++i) { ui->comboVersionList->addItem(m_vVersion[i].version.c_str()); } if (m_vVersion.empty()) { 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()) { QString devType = GetDevType(m_curFwVersion, NULL); assert(!devType.isEmpty()); HGChar fileName[256] = {0}; HGBase_GetFileName(filePath.toStdString().c_str(), fileName, 256); HGChar fileVersion[256] = {0}; HGBase_GetFilePrefix(fileName, fileVersion, 256); if (devType == GetDevType(fileVersion, NULL)) { if (m_curFwVersion >= fileVersion) { QMessageBox msg(QMessageBox::Information, tr("tips"), tr("the selected firmware is not newer than the current version"), QMessageBox::Yes, this); msg.setButtonText(QMessageBox::Yes, tr("yes")); msg.exec(); } else { ui->editFilePath->setText(filePath); } } else { QMessageBox msg(QMessageBox::Question, tr("tips"), tr("firmware file mismatch, continue?"), QMessageBox::Yes | QMessageBox::No, this); msg.setButtonText(QMessageBox::Yes, tr("yes")); msg.setButtonText(QMessageBox::No, tr("no")); msg.exec(); if (msg.clickedButton() == msg.button(QMessageBox::Yes)) { ui->editFilePath->setText(filePath); } } } } void MainWindow::on_btnDownloadUpgrade_clicked() { int idx = ui->comboVersionList->currentIndex(); if (-1 != idx) { QString url = m_vVersion[idx].url.c_str(); QString md5 = m_vVersion[idx].md5.c_str(); QString versionNum = m_vVersion[idx].version.c_str(); if (m_curFwVersion >= versionNum) { QMessageBox msg(QMessageBox::Information, tr("tips"), tr("the selected firmware is not newer than the current version"), QMessageBox::Yes, this); msg.setButtonText(QMessageBox::Yes, tr("yes")); msg.exec(); return; } assert(!url.isEmpty() && !md5.isEmpty() && !versionNum.isEmpty()); HGChar suffix[64]; HGBase_GetFileSuffix(url.toStdString().c_str(), suffix, 64); HGChar savePath[512]; HGBase_GetTmpPath(savePath, 512); HGBase_CreateDir(savePath); HGChar fileName[128]; sprintf(fileName, "%s.%s", "{644759d6-7e4e-47c7-b42e-5105bb2de4a3}", suffix); strcat(savePath, fileName); QFile saveFile(savePath); saveFile.open(QFile::ReadOnly); QByteArray fileMsg = saveFile.readAll(); saveFile.close(); QString md5_2 = QCryptographicHash::hash(fileMsg , QCryptographicHash::Md5).toHex(); QFile f(savePath); if(!f.exists() || md5 != md5_2) { Dialog_updateProgress dlg(m_versionMgr, url, savePath, this); if (dlg.exec()) { QFile saveFile(savePath); saveFile.open(QFile::ReadOnly); QByteArray fileMsg = saveFile.readAll(); saveFile.close(); QString md5_2 = QCryptographicHash::hash(fileMsg , QCryptographicHash::Md5).toHex(); if (md5 == md5_2) { Dialog_upgradeFirmware dlg(m_curDevHandle, savePath, this); dlg.exec(); if (0 == dlg.getUpgradeStatus()) { ui->labelDevInfo->setText(QString(tr("device: %1 upgrade firmware success")).arg(m_curDevName)); } else { ui->labelDevInfo->setText(QString(tr("device: %1 upgrade firmware failed, io error")).arg(m_curDevName)); } } else { QMessageBox msg(QMessageBox::Information, tr("tips"), tr("download firmware fail"), QMessageBox::Yes, this); msg.setButtonText(QMessageBox::Yes, tr("yes")); msg.exec(); } } } else { Dialog_upgradeFirmware dlg(m_curDevHandle, savePath, this); dlg.exec(); if (0 == dlg.getUpgradeStatus()) { ui->labelDevInfo->setText(QString(tr("device: %1 upgrade firmware success")).arg(m_curDevName)); } else { ui->labelDevInfo->setText(QString(tr("device: %1 upgrade firmware failed, io error")).arg(m_curDevName)); } } HGBase_DeleteFile(savePath); } } void MainWindow::on_btnUpgrade_clicked() { std::string filePath = Utf8ToStdString(ui->editFilePath->text().toStdString()); if (!filePath.empty()) { Dialog_upgradeFirmware dlg(m_curDevHandle, filePath, this); dlg.exec(); if (0 == dlg.getUpgradeStatus()) { ui->labelDevInfo->setText(QString(tr("device: %1 upgrade firmware success")).arg(m_curDevName)); } else { ui->labelDevInfo->setText(QString(tr("device: %1 upgrade firmware failed, io error")).arg(m_curDevName)); } } } 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_SET_CLEAR_ROLLER_COUNT, nullptr, &count); QString info; if (ret == SANE_STATUS_GOOD) { ui->labelRollCount->setText("0"); 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); HGBase_CreateDir(cfgPath); 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()) { QMessageBox msg(QMessageBox::Information, tr("tips"), tr("old password is wrong"), QMessageBox::Yes, this); msg.setButtonText(QMessageBox::Yes, tr("yes")); msg.exec(); return; } if (ui->editNewPassword->text().isEmpty()) { QMessageBox msg(QMessageBox::Information, tr("tips"), tr("new password can not be empty"), QMessageBox::Yes, this); msg.setButtonText(QMessageBox::Yes, tr("yes")); msg.exec(); return; } if (ui->editNewPassword->text() != ui->editNewPassword_2->text()) { QMessageBox msg(QMessageBox::Information, tr("tips"), tr("new password is inconsistent"), QMessageBox::Yes, this); 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())) { QMessageBox msg(QMessageBox::Information, tr("tips"), tr("modify password fail"), QMessageBox::Yes, this); msg.setButtonText(QMessageBox::Yes, tr("yes")); msg.exec(); return; } ui->editOldPassword->setText(""); ui->editNewPassword->setText(""); ui->editNewPassword_2->setText(""); QMessageBox msg(QMessageBox::Information, tr("tips"), tr("modify password success"), QMessageBox::Yes, this); msg.setButtonText(QMessageBox::Yes, tr("yes")); msg.exec(); }