| 1 | // SPDX-FileCopyrightText: 2017 - 2022 UnionTech Software Technology Co., Ltd. |
| 2 | // |
| 3 | // SPDX-License-Identifier: GPL-3.0-only |
| 4 | |
| 5 | #include <QDebug> |
| 6 | #include <QIcon> |
| 7 | #include <QProcess> |
| 8 | |
| 9 | #include "bmwindow.h" |
| 10 | #include <bminterface.h> |
| 11 | #include <util/utils.h> |
| 12 | |
| 13 | #include <DLog> |
| 14 | #include <DApplication> |
| 15 | #include <DWidgetUtil> |
| 16 | #include <DApplicationSettings> |
| 17 | #include <DGuiApplicationHelper> |
| 18 | |
| 19 | DCORE_USE_NAMESPACE |
| 20 | DWIDGET_USE_NAMESPACE |
| 21 | |
| 22 | #ifdef Q_OS_MAC |
| 23 | static bool switchToRoot(QApplication &app) |
| 24 | { |
| 25 | QStringList allappargs = app.arguments(); |
| 26 | QProcess whoamip; |
| 27 | whoamip.start("whoami"); |
| 28 | whoamip.waitForFinished(); |
| 29 | |
| 30 | if (QString(whoamip.readAll()).remove("\r").remove("\n") != "root") { |
| 31 | QString argsconc = ""; |
| 32 | QString argsconcSingleQuote = ""; |
| 33 | |
| 34 | for (int i = 1; i < allappargs.size(); ++i) { |
| 35 | argsconc += QString("\"%1\" ").arg(allappargs.at(i)); |
| 36 | argsconcSingleQuote += QString("'%1' ").arg(allappargs.at(i)); |
| 37 | } |
| 38 | |
| 39 | argsconc += "\"rootcheck=no\""; |
| 40 | argsconcSingleQuote += "'rootcheck=no'"; |
| 41 | |
| 42 | QProcess::startDetached("osascript", QStringList() << "-e" << QString("do shell script \"'%1' %2\" with administrator privileges").arg(app.applicationFilePath()).arg(argsconcSingleQuote)); |
| 43 | return true; |
| 44 | |
| 45 | } |
| 46 | |
| 47 | return false; |
| 48 | } |
| 49 | |
| 50 | #endif |
| 51 | |
| 52 | int main(int argc, char **argv) |
| 53 | { |
| 54 | Utils::initResource(); |
| 55 | #ifndef Q_OS_WIN |
| 56 | #if defined(DTK_STATIC_LIB) |
| 57 | DWIDGET_INIT_RESOURCE(); |
| 58 | #endif |
| 59 | #endif |
| 60 | |
| 61 | if (!QString(qgetenv("XDG_CURRENT_DESKTOP")).toLower().startsWith("deepin")) { |
| 62 | |
| 63 | setenv("XDG_CURRENT_DESKTOP", "Deepin", 1); |
| 64 | } |
| 65 | |
| 66 | // DApplication::loadDXcbPlugin(); //去除为兼容wayland环境 |
| 67 | DApplication app(argc, argv); |
| 68 | app.setAttribute(Qt::AA_UseHighDpiPixmaps); |
| 69 | app.setAttribute(Qt::AA_EnableHighDpiScaling); |
| 70 | app.setOrganizationName("deepin"); |
| 71 | app.setApplicationName("deepin-boot-maker"); |
| 72 | app.setApplicationVersion(DApplication::buildVersion("20191031")); |
| 73 | // app.setApplicationVersion(DApplication::buildVersion(VERSION)); |
| 74 | // app.setTheme("light"); |
| 75 | |
| 76 | #ifdef Q_OS_MAC |
| 77 | if (switchToRoot(app)) { |
| 78 | exit(0); |
| 79 | } |
| 80 | #endif |
| 81 | const QString m_format = "%{time}{yyyyMMdd.HH:mm:ss.zzz}[%{type:1}][%{function:-40} %{line:-4} %{threadid:-8} ] %{message}\n"; |
| 82 | DLogManager::setLogFormat(m_format); |
| 83 | DLogManager::registerConsoleAppender(); |
| 84 | DLogManager::registerFileAppender(); |
| 85 | |
| 86 | #ifndef Q_OS_MAC |
| 87 | qputenv("DTK_USE_SEMAPHORE_SINGLEINSTANCE", "1"); |
| 88 | if (!DGuiApplicationHelper::instance()->setSingleInstance(app.applicationName(), DGuiApplicationHelper::UserScope)) { |
| 89 | exit(0); |
| 90 | } |
| 91 | #endif |
| 92 | |
| 93 | #ifdef Q_OS_WIN |
| 94 | Utils::loadFonts(); |
| 95 | app.setWindowIcon(QIcon(":/theme/light/image/deepin-boot-maker.svg")); |
| 96 | #endif |
| 97 | app.loadTranslator(); |
| 98 | Utils::loadTranslate(); |
| 99 | app.setApplicationDisplayName(QObject::tr("Boot Maker")); |
| 100 | |
| 101 | qDebug() << "Boot Maker UI started."; |
| 102 | |
| 103 | DApplicationSettings savetheme; |
| 104 | BMWindow w; |
| 105 | Dtk::Widget::moveToCenter(&w); |
| 106 | w.show(); |
| 107 | // w.waitAuth(); |
| 108 | |
| 109 | auto ret = app.exec(); |
| 110 | BMInterface::instance()->stop(); |
| 111 | return ret; |
| 112 | } |
| 113 | |
| 114 | #ifdef Q_OS_WIN32 |
| 115 | namespace Utils { |
| 116 | void loadFonts() |
| 117 | { |
| 118 | QFontDatabase database; |
| 119 | QStringList fontlist = database.families(); |
| 120 | |
| 121 | QStringList preferList; |
| 122 | preferList.append("Microsoft YaHei"); |
| 123 | preferList.append("微软雅黑"); |
| 124 | preferList.append("SimHei"); |
| 125 | preferList.append("黑体"); |
| 126 | |
| 127 | foreach (QString font, preferList) { |
| 128 | if (fontlist.contains(font)) { |
| 129 | QFont newFont = QFont(font); |
| 130 | qApp->setFont(newFont); |
| 131 | return; |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | #endif |
| 137 |