code_app/app/scanner/dialog_updateprogress.cpp

140 lines
3.6 KiB
C++
Raw Normal View History

#include "dialog_updateprogress.h"
#include "ui_dialog_updateprogress.h"
#include "base/HGUtility.h"
2022-07-13 11:01:42 +00:00
#include "base/HGMd5.h"
#include <QMessageBox>
#include <QFile>
#include <QDir>
#include <QCryptographicHash>
2022-07-13 11:01:42 +00:00
Dialog_updateProgress::Dialog_updateProgress(HGDll dll, HGVersionMgr mgr, const QString &url,
const QString &versionNum, const QString &md5, QWidget *parent) :
QDialog(parent)
2022-07-13 11:01:42 +00:00
, m_versionDll(dll)
, m_versionMgr(mgr)
, m_url(url)
2022-07-13 11:01:42 +00:00
, m_versionNum(versionNum)
, m_md5(md5)
, ui(new Ui::Dialog_updateProgress)
{
ui->setupUi(this);
ui->progressBar->setValue(0);
connect(this, SIGNAL(updateProgress(int)), this, SLOT(on_updateProgress(int)), Qt::QueuedConnection);
2022-07-13 11:01:42 +00:00
connect(this, SIGNAL(finish()), this, SLOT(on_finish()), Qt::QueuedConnection);
m_stopThread = false;
HGBase_OpenThread(ThreadFunc, this, &m_thread);
}
Dialog_updateProgress::~Dialog_updateProgress()
{
if (nullptr != m_thread)
{
HGBase_CloseThread(m_thread);
m_thread = nullptr;
}
delete ui;
}
2022-07-13 11:01:42 +00:00
HGInt Dialog_updateProgress::HttpDownloadThreadFunc(HGULonglong totalSize, HGULonglong nowSize, HGPointer param)
{
Dialog_updateProgress *p = (Dialog_updateProgress *)param;
if (p->m_stopThread)
{
return 1;
}
if (totalSize != 0)
{
emit p->updateProgress(((double)nowSize / totalSize) * 100);
}
return 0;
}
void Dialog_updateProgress::ThreadFunc(HGThread thread, HGPointer param)
{
(void)thread;
Dialog_updateProgress *p = (Dialog_updateProgress *)param;
HGChar cfgPath[512];
HGBase_GetConfigPath(cfgPath, 512);
HGBase_CreateDir(cfgPath);
2022-07-13 11:01:42 +00:00
strcat(cfgPath, (p->m_versionNum + QString("%1").arg(".exe")).toLatin1().data());
QFile cfgFile(cfgPath);
cfgFile.open(QFile::ReadOnly);
QByteArray fileMsg = cfgFile.readAll();
cfgFile.close();
QString md5 = QCryptographicHash::hash(fileMsg , QCryptographicHash::Md5).toHex();
bool ret = true;
2022-07-13 11:53:37 +00:00
QFile f(cfgPath);
if(!f.exists() || md5 != p->m_md5)
2022-07-13 11:01:42 +00:00
{
typedef HGResult (HGAPI *Func)(HGVersionMgr, const HGChar *, const HGChar *, HGHttpDownloadFunc, HGPointer);
Func func = NULL;
ret = HGBase_GetDllProcAddress(p->m_versionDll, "HGVersion_HttpDownload", (HGPointer *)&func);
if (NULL != func)
{
func(p->m_versionMgr, p->m_url.toStdString().c_str(), cfgPath, HttpDownloadThreadFunc, p);
}
if (ret)
{
QString curPath = QDir::currentPath();
QString tmpPath = QDir::tempPath();
QFile file(curPath);
file.copy("HuagoScanUpgrade.exe", tmpPath);
#if defined(HG_CMP_MSC)
file.copy("msvcp140.dll", tmpPath);
file.copy("Qt5Core.dll", tmpPath);
file.copy("Qt5Gui.dll", tmpPath);
file.copy("Qt5Widgets.dll", tmpPath);
file.copy("vcruntime140.dll", tmpPath);
#endif
emit p->finish();
emit p->upgradeApp();
}
else
{
emit p->finish();
QFile::remove(cfgPath);
}
}
else
{
emit p->finish();
emit p->upgradeApp();
}
}
void Dialog_updateProgress::on_updateProgress(int value)
{
ui->progressBar->setValue(value);
}
2022-07-13 11:01:42 +00:00
void Dialog_updateProgress::on_finish()
{
close();
}
void Dialog_updateProgress::on_pushButton_clicked()
{
m_stopThread = true;
HGBase_CloseThread(m_thread);
m_thread = nullptr;
close();
}
void Dialog_updateProgress::closeEvent(QCloseEvent* e)
{
(void)e;
m_stopThread = true;
HGBase_CloseThread(m_thread);
m_thread = nullptr;
2022-07-13 11:01:42 +00:00
}