code_app/app/upgrade/mainwindow.cpp

115 lines
3.0 KiB
C++

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "base/HGDef.h"
#include "base/HGInc.h"
#include "base/HGInfo.h"
#include <QLabel>
#include <QMovie>
#include <QMessageBox>
#if defined(HG_CMP_MSC)
#include <shellapi.h>
#include <tlhelp32.h>
#endif
MainWindow::MainWindow(const std::string &appName, const std::string& pkgPath, QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
, m_appName(appName)
, m_pkgPath(pkgPath)
, m_thread(nullptr)
, m_success(false)
{
ui->setupUi(this);
setWindowTitle(tr("Installation"));
setWindowFlags(Qt::CustomizeWindowHint | Qt::WindowTitleHint | Qt::WindowMinMaxButtonsHint);
ui->label_text->setText(tr("Installation in progress, please wait..."));
QMovie *movie = new QMovie(":images/image_rsc/logo/waiting.gif");
ui->label_gif->setMovie(movie);
movie->setCacheMode(QMovie::CacheAll);
movie->setScaledSize(QSize(ui->label_gif->width(), ui->label_gif->height()));
movie->start();
ui->label_gif->show();
connect(this, SIGNAL(closeWnd()), this, SLOT(close()), Qt::QueuedConnection);
HGBase_OpenThread(ThreadFunc, this, &m_thread);
}
MainWindow::~MainWindow()
{
HGBase_CloseThread(m_thread);
m_thread = nullptr;
delete ui;
}
bool MainWindow::isInstallSuccess()
{
return m_success;
}
QString MainWindow::getUpgradeFailInfo()
{
return m_upgradeFailInfo;
}
bool MainWindow::Upgrade(const std::string& pkgPath, QString &failInfo)
{
bool ret = false;
failInfo.clear();
#if defined(HG_CMP_MSC)
// PROCESS_INFORMATION ProcessInfo;
// STARTUPINFOA StartupInfo;
// ZeroMemory(&StartupInfo, sizeof(StartupInfo));
// StartupInfo.cb = sizeof(StartupInfo);
// char command[256];
// sprintf(command, "%s %s", pkgPath.c_str(), "/verysilent");
SHELLEXECUTEINFOA sei = {0};
sei.cbSize = sizeof(SHELLEXECUTEINFOA);
sei.fMask = SEE_MASK_NOCLOSEPROCESS;
sei.nShow = SW_SHOWNORMAL;
sei.lpFile = pkgPath.c_str();
sei.lpParameters = "/verysilent";
sei.lpVerb = "runas";
// if (CreateProcessA(nullptr, command, nullptr, nullptr, FALSE, 0, nullptr, nullptr, &StartupInfo, &ProcessInfo))
if (ShellExecuteExA(&sei))
{
WaitForSingleObject(sei.hProcess, INFINITE);
DWORD dwCode = 0;
GetExitCodeProcess(sei.hProcess, &dwCode);
failInfo = QString("ExitCode=%1").arg((int)dwCode);
//CloseHandle(ProcessInfo.hThread);
CloseHandle(sei.hProcess);
ret = (0 == dwCode);
}
#else
std::string cmd = "pkexec dpkg -i \"" + pkgPath + "\"";
int status = system(cmd.c_str());
if (-1 != status && WIFEXITED(status) && 0 == WEXITSTATUS(status))
ret = true;
#endif
return ret;
}
void HGAPI MainWindow::ThreadFunc(HGThread thread, HGPointer param)
{
(void)thread;
MainWindow* p = (MainWindow*)param;
p->m_success = Upgrade(p->m_pkgPath, p->m_upgradeFailInfo);
emit p->closeWnd();
}