UI: StartupUtil: Add default language to auto-run command.

This commit is contained in:
Nodir Temirkhodjaev 2021-04-22 15:02:43 +03:00
parent c37e7d61ed
commit 90a6c5d5c1
3 changed files with 27 additions and 26 deletions

View File

@ -53,7 +53,7 @@ void OptionsPage::onSaved()
const bool wasServiceMode = StartupUtil::isServiceMode(m_currentStartMode); const bool wasServiceMode = StartupUtil::isServiceMode(m_currentStartMode);
m_currentStartMode = m_comboStartMode->currentIndex(); m_currentStartMode = m_comboStartMode->currentIndex();
StartupUtil::setStartupMode(m_currentStartMode); StartupUtil::setStartupMode(m_currentStartMode, settings()->defaultLanguage());
const bool isServiceMode = StartupUtil::isServiceMode(m_currentStartMode); const bool isServiceMode = StartupUtil::isServiceMode(m_currentStartMode);
if (isServiceMode != wasServiceMode) { if (isServiceMode != wasServiceMode) {

View File

@ -46,20 +46,20 @@ bool isAutorunForAllUsers()
return isAutorunForUser(regAllUsersRun); return isAutorunForUser(regAllUsersRun);
} }
void setAutorunForUser(const char *key) void setAutorunForUser(const char *key, const QString &command)
{ {
QSettings reg(key, QSettings::Registry64Format); 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) void removeAutorunForUser(const char *key)
@ -78,17 +78,12 @@ void removeAutorunForAllUsers()
removeAutorunForUser(regAllUsersRun); removeAutorunForUser(regAllUsersRun);
} }
} bool installService(
const wchar_t *serviceName, const wchar_t *serviceDisplay, const QString &command)
const wchar_t *const StartupUtil::serviceName = L"" APP_BASE "Svc";
const wchar_t *const StartupUtil::serviceDisplay = L"" APP_NAME;
bool StartupUtil::installService()
{ {
bool res = false; bool res = false;
const SC_HANDLE mngr = OpenSCManager(NULL, NULL, SC_MANAGER_CREATE_SERVICE); const SC_HANDLE mngr = OpenSCManager(NULL, NULL, SC_MANAGER_CREATE_SERVICE);
if (mngr) { if (mngr) {
const auto command = wrappedAppFilePath() + " --service";
const SC_HANDLE svc = CreateServiceW(mngr, serviceName, serviceDisplay, SERVICE_ALL_ACCESS, const SC_HANDLE svc = CreateServiceW(mngr, serviceName, serviceDisplay, SERVICE_ALL_ACCESS,
SERVICE_WIN32_OWN_PROCESS, SERVICE_AUTO_START, SERVICE_ERROR_NORMAL, SERVICE_WIN32_OWN_PROCESS, SERVICE_AUTO_START, SERVICE_ERROR_NORMAL,
(LPCWSTR) command.utf16(), 0, 0, 0, 0, 0); (LPCWSTR) command.utf16(), 0, 0, 0, 0, 0);
@ -101,7 +96,7 @@ bool StartupUtil::installService()
return res; return res;
} }
bool StartupUtil::uninstallService() bool uninstallService(const wchar_t *serviceName)
{ {
bool res = false; bool res = false;
const SC_HANDLE mngr = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS); const SC_HANDLE mngr = OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
@ -124,6 +119,11 @@ bool StartupUtil::uninstallService()
return res; 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 StartupUtil::isServiceInstalled()
{ {
bool res = false; bool res = false;
@ -161,7 +161,7 @@ StartupUtil::StartupMode StartupUtil::getStartupMode()
: (isAutorunForCurrentUser() ? StartupCurrentUser : StartupDisabled); : (isAutorunForCurrentUser() ? StartupCurrentUser : StartupDisabled);
} }
void StartupUtil::setStartupMode(int mode) void StartupUtil::setStartupMode(int mode, const QString &defaultLanguage)
{ {
// COMPAT: Remove link from Programs -> Startup // COMPAT: Remove link from Programs -> Startup
// TODO: Remove after v4.1.0 (via v4.0.0) // TODO: Remove after v4.1.0 (via v4.0.0)
@ -169,19 +169,23 @@ void StartupUtil::setStartupMode(int mode)
removeAutorunForCurrentUser(); removeAutorunForCurrentUser();
removeAutorunForAllUsers(); removeAutorunForAllUsers();
uninstallService(); uninstallService(serviceName);
if (mode == StartupDisabled)
return;
const QString command = wrappedAppFilePath()
+ (defaultLanguage.isEmpty() ? QString() : " --lang " + defaultLanguage);
switch (mode) { switch (mode) {
case StartupDisabled:
break;
case StartupCurrentUser: case StartupCurrentUser:
setAutorunForCurrentUser(); setAutorunForCurrentUser(command);
break; break;
case StartupAllUsers: case StartupAllUsers:
setAutorunForAllUsers(); setAutorunForAllUsers(command);
Q_FALLTHROUGH(); Q_FALLTHROUGH();
case StartupAllUsersBackground: case StartupAllUsersBackground:
installService(); installService(serviceName, serviceDisplay, command + " --service");
break; break;
} }
} }

View File

@ -16,14 +16,11 @@ public:
static const wchar_t *const serviceName; static const wchar_t *const serviceName;
static const wchar_t *const serviceDisplay; static const wchar_t *const serviceDisplay;
static bool installService();
static bool uninstallService();
static bool isServiceInstalled(); static bool isServiceInstalled();
static bool startService(); static bool startService();
static StartupMode getStartupMode(); static StartupMode getStartupMode();
static void setStartupMode(int mode); static void setStartupMode(int mode, const QString &defaultLanguage = QString());
static bool isServiceMode(int mode); static bool isServiceMode(int mode);
}; };