From 90a6c5d5c12451a5a9d6e193865983cf4a55b537 Mon Sep 17 00:00:00 2001 From: Nodir Temirkhodjaev Date: Thu, 22 Apr 2021 15:02:43 +0300 Subject: [PATCH] UI: StartupUtil: Add default language to auto-run command. --- src/ui/form/opt/pages/optionspage.cpp | 2 +- src/ui/util/startuputil.cpp | 46 +++++++++++++++------------ src/ui/util/startuputil.h | 5 +-- 3 files changed, 27 insertions(+), 26 deletions(-) diff --git a/src/ui/form/opt/pages/optionspage.cpp b/src/ui/form/opt/pages/optionspage.cpp index fd66035f..390e280b 100644 --- a/src/ui/form/opt/pages/optionspage.cpp +++ b/src/ui/form/opt/pages/optionspage.cpp @@ -53,7 +53,7 @@ void OptionsPage::onSaved() const bool wasServiceMode = StartupUtil::isServiceMode(m_currentStartMode); m_currentStartMode = m_comboStartMode->currentIndex(); - StartupUtil::setStartupMode(m_currentStartMode); + StartupUtil::setStartupMode(m_currentStartMode, settings()->defaultLanguage()); const bool isServiceMode = StartupUtil::isServiceMode(m_currentStartMode); if (isServiceMode != wasServiceMode) { diff --git a/src/ui/util/startuputil.cpp b/src/ui/util/startuputil.cpp index e01bb896..a1b47798 100644 --- a/src/ui/util/startuputil.cpp +++ b/src/ui/util/startuputil.cpp @@ -46,20 +46,20 @@ bool isAutorunForAllUsers() return isAutorunForUser(regAllUsersRun); } -void setAutorunForUser(const char *key) +void setAutorunForUser(const char *key, const QString &command) { QSettings reg(key, QSettings::Registry64Format); - reg.setValue(APP_NAME, wrappedAppFilePath()); + reg.setValue(APP_NAME, command); } -void setAutorunForCurrentUser() +void setAutorunForCurrentUser(const QString &command) { - setAutorunForUser(regCurUserRun); + setAutorunForUser(regCurUserRun, command); } -void setAutorunForAllUsers() +void setAutorunForAllUsers(const QString &command) { - setAutorunForUser(regAllUsersRun); + setAutorunForUser(regAllUsersRun, command); } void removeAutorunForUser(const char *key) @@ -78,17 +78,12 @@ void removeAutorunForAllUsers() removeAutorunForUser(regAllUsersRun); } -} - -const wchar_t *const StartupUtil::serviceName = L"" APP_BASE "Svc"; -const wchar_t *const StartupUtil::serviceDisplay = L"" APP_NAME; - -bool StartupUtil::installService() +bool installService( + const wchar_t *serviceName, const wchar_t *serviceDisplay, const QString &command) { bool res = false; const SC_HANDLE mngr = OpenSCManager(NULL, NULL, SC_MANAGER_CREATE_SERVICE); if (mngr) { - const auto command = wrappedAppFilePath() + " --service"; const SC_HANDLE svc = CreateServiceW(mngr, serviceName, serviceDisplay, SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS, SERVICE_AUTO_START, SERVICE_ERROR_NORMAL, (LPCWSTR) command.utf16(), 0, 0, 0, 0, 0); @@ -101,7 +96,7 @@ bool StartupUtil::installService() return res; } -bool StartupUtil::uninstallService() +bool uninstallService(const wchar_t *serviceName) { bool res = false; const SC_HANDLE mngr = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); @@ -124,6 +119,11 @@ bool StartupUtil::uninstallService() return res; } +} + +const wchar_t *const StartupUtil::serviceName = L"" APP_BASE "Svc"; +const wchar_t *const StartupUtil::serviceDisplay = L"" APP_NAME; + bool StartupUtil::isServiceInstalled() { bool res = false; @@ -161,7 +161,7 @@ StartupUtil::StartupMode StartupUtil::getStartupMode() : (isAutorunForCurrentUser() ? StartupCurrentUser : StartupDisabled); } -void StartupUtil::setStartupMode(int mode) +void StartupUtil::setStartupMode(int mode, const QString &defaultLanguage) { // COMPAT: Remove link from Programs -> Startup // TODO: Remove after v4.1.0 (via v4.0.0) @@ -169,19 +169,23 @@ void StartupUtil::setStartupMode(int mode) removeAutorunForCurrentUser(); removeAutorunForAllUsers(); - uninstallService(); + uninstallService(serviceName); + + if (mode == StartupDisabled) + return; + + const QString command = wrappedAppFilePath() + + (defaultLanguage.isEmpty() ? QString() : " --lang " + defaultLanguage); switch (mode) { - case StartupDisabled: - break; case StartupCurrentUser: - setAutorunForCurrentUser(); + setAutorunForCurrentUser(command); break; case StartupAllUsers: - setAutorunForAllUsers(); + setAutorunForAllUsers(command); Q_FALLTHROUGH(); case StartupAllUsersBackground: - installService(); + installService(serviceName, serviceDisplay, command + " --service"); break; } } diff --git a/src/ui/util/startuputil.h b/src/ui/util/startuputil.h index ae340bc6..89759dcc 100644 --- a/src/ui/util/startuputil.h +++ b/src/ui/util/startuputil.h @@ -16,14 +16,11 @@ public: static const wchar_t *const serviceName; static const wchar_t *const serviceDisplay; - static bool installService(); - static bool uninstallService(); - static bool isServiceInstalled(); static bool startService(); static StartupMode getStartupMode(); - static void setStartupMode(int mode); + static void setStartupMode(int mode, const QString &defaultLanguage = QString()); static bool isServiceMode(int mode); };