| 1 | // SPDX-FileCopyrightText: 2017 - 2022 UnionTech Software Technology Co., Ltd. |
| 2 | // |
| 3 | // SPDX-License-Identifier: GPL-3.0-only |
| 4 | |
| 5 | #include "bootmaker.h" |
| 6 | #include "../installer/qtinstallerfactory.h" |
| 7 | |
| 8 | #include "../util/sevenzip.h" |
| 9 | #include "../util/utils.h" |
| 10 | #include "diskutil.h" |
| 11 | #include <QDebug> |
| 12 | #include <QFileInfo> |
| 13 | |
| 14 | #include <XSys> |
| 15 | |
| 16 | #ifdef Q_OS_WIN32 |
| 17 | #include <windows.h> |
| 18 | #include <shellapi.h> |
| 19 | #pragma comment(lib, "shell32.lib") |
| 20 | #pragma comment(lib, "advapi32.lib") |
| 21 | #endif |
| 22 | |
| 23 | #ifdef Q_OS_LINUX |
| 24 | #include <unistd.h> |
| 25 | #include <sys/reboot.h> |
| 26 | #endif |
| 27 | |
| 28 | BootMaker::BootMaker(QObject *parent) : BMHandler(parent) |
| 29 | ,m_pInstaller(nullptr) |
| 30 | { |
| 31 | m_usbDeviceMonitor = new DeviceMonitor(); |
| 32 | |
| 33 | m_monitorWork = new QThread(); |
| 34 | m_usbDeviceMonitor->moveToThread(m_monitorWork); |
| 35 | // connect(monitorWork, &QThread::started, |
| 36 | // m_usbDeviceMonitor, &DeviceMonitor::startMonitor); |
| 37 | |
| 38 | connect(m_monitorWork, &QThread::finished, m_usbDeviceMonitor, &DeviceMonitor::deleteLater); |
| 39 | connect(m_usbDeviceMonitor, &DeviceMonitor::removablePartitionsChanged, |
| 40 | this, &BootMaker::removablePartitionsChanged); |
| 41 | |
| 42 | m_monitorWork->start(); |
| 43 | |
| 44 | connect(this, &BootMaker::finished, this, [ = ](int errcode, const QString & description) { |
| 45 | this->reportProgress(101, errcode, "install failed", description); |
| 46 | }); |
| 47 | } |
| 48 | |
| 49 | void BootMaker::reboot() |
| 50 | { |
| 51 | #ifdef Q_OS_WIN32 |
| 52 | HANDLE hToken; |
| 53 | TOKEN_PRIVILEGES tkp; |
| 54 | OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken); |
| 55 | LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid); |
| 56 | tkp.PrivilegeCount = 1; |
| 57 | tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; |
| 58 | AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES) NULL, 0); |
| 59 | // ExitWindowsEx(EWX_REBOOT, EWX_FORCE); |
| 60 | #endif |
| 61 | #ifdef Q_OS_LINUX |
| 62 | sync(); |
| 63 | ::reboot(RB_AUTOBOOT); |
| 64 | #endif |
| 65 | #ifdef Q_OS_MAC |
| 66 | XSys::SynExec("shutdown", "-r now &"); |
| 67 | #endif |
| 68 | } |
| 69 | |
| 70 | void BootMaker::start() |
| 71 | { |
| 72 | qInfo() << "BootMaker start"; |
| 73 | |
| 74 | if (m_pInstaller == nullptr) { |
| 75 | m_pInstaller = QtInstallerFactory::getInstance()->createInstaller(); |
| 76 | |
| 77 | if (m_pInstaller != nullptr) { |
| 78 | connect(m_pInstaller, &QtBaseInstaller::progressfinished, this, [=](ProgressStatus status, BMHandler::ErrorType error) { |
| 79 | Q_UNUSED(status); |
| 80 | emit finished(error, errorString(BMHandler::ErrorType(error))); |
| 81 | }); |
| 82 | |
| 83 | connect(m_pInstaller, &QtBaseInstaller::reportProgress, m_usbDeviceMonitor, [=](int current, const QString &title, const QString &description){ |
| 84 | emit this->reportProgress(current, ErrorType::NoError, title, description); |
| 85 | }); |
| 86 | |
| 87 | connect(m_pInstaller->m_sevenZipCheck.m_szpp, &SevenZipProcessParser::progressChanged, |
| 88 | m_usbDeviceMonitor, [ = ](int current, int /*total*/, const QString & fileName) { |
| 89 | emit this->reportProgress(current * 60 / 100 + 20, ErrorType::NoError, "extract", fileName); |
| 90 | }, Qt::QueuedConnection); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | if (m_pInstaller != nullptr) { |
| 95 | if (m_pInstaller->isRunning()) { |
| 96 | qInfo() << "Installer is running"; |
| 97 | m_pInstaller->stopInstall(); |
| 98 | } |
| 99 | else { |
| 100 | qInfo() << "Start flush disk"; |
| 101 | emit m_usbDeviceMonitor->startMonitor(); |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | void BootMaker::stop() |
| 107 | { |
| 108 | emit m_usbDeviceMonitor->pauseMonitor(); |
| 109 | } |
| 110 | |
| 111 | const QList<DeviceInfo> BootMaker::deviceList() const |
| 112 | { |
| 113 | qDebug() << "BootMaker deviceList"; |
| 114 | return m_usbDeviceMonitor->deviceList(); |
| 115 | } |
| 116 | bool BootMaker::checkfile(const QString &filepath) |
| 117 | { |
| 118 | qDebug() << "CheckFile:" << filepath; |
| 119 | // //check iso integrity |
| 120 | // SevenZip sevenZipCheck(filepath, ""); |
| 121 | |
| 122 | // if (!sevenZipCheck.check()) { |
| 123 | // qCritical() << "Error::file check error"; |
| 124 | // // emit checkFileResult(false); |
| 125 | // return false; |
| 126 | // } |
| 127 | // // emit checkFileResult(true); |
| 128 | return true; |
| 129 | } |
| 130 | |
| 131 | bool BootMaker::install(const QString &image, const QString &unused_device, const QString &partition, bool formatDevice) |
| 132 | { |
| 133 | qDebug() << "Begin install" << image << unused_device << partition << formatDevice; |
| 134 | |
| 135 | if (m_pInstaller != nullptr) { |
| 136 | emit m_usbDeviceMonitor->pauseMonitor(); |
| 137 | m_pInstaller->setImage(image); |
| 138 | m_pInstaller->setPartionName(partition); |
| 139 | m_pInstaller->setformat(formatDevice); |
| 140 | m_pInstaller->beginInstall(); |
| 141 | emit m_usbDeviceMonitor->startMonitor(); |
| 142 | } |
| 143 | |
| 144 | return true; |
| 145 | } |