From 1ea6224e60c0265c8800e5f0ee41ae93f6de4dbb Mon Sep 17 00:00:00 2001 From: John Preston Date: Fri, 5 Nov 2021 09:52:02 +0400 Subject: [PATCH] Fix autostart toggling. --- .../platform/linux/specific_linux.cpp | 4 +- .../SourceFiles/platform/mac/specific_mac.mm | 4 +- .../SourceFiles/platform/win/specific_win.cpp | 107 +++++++++--------- 3 files changed, 61 insertions(+), 54 deletions(-) diff --git a/Telegram/SourceFiles/platform/linux/specific_linux.cpp b/Telegram/SourceFiles/platform/linux/specific_linux.cpp index dff7bc434..e81b14422 100644 --- a/Telegram/SourceFiles/platform/linux/specific_linux.cpp +++ b/Telegram/SourceFiles/platform/linux/specific_linux.cpp @@ -562,7 +562,9 @@ bool AutostartSupported() { void AutostartToggle(bool enabled, Fn done) { const auto guard = gsl::finally([&] { - done(enabled); + if (done) { + done(enabled); + } }); #ifdef __HAIKU__ diff --git a/Telegram/SourceFiles/platform/mac/specific_mac.mm b/Telegram/SourceFiles/platform/mac/specific_mac.mm index 47bef98ba..1d68457bb 100644 --- a/Telegram/SourceFiles/platform/mac/specific_mac.mm +++ b/Telegram/SourceFiles/platform/mac/specific_mac.mm @@ -182,7 +182,9 @@ void IgnoreApplicationActivationRightNow() { } void AutostartToggle(bool enabled, Fn done) { - done(false); + if (done) { + done(false); + } } bool AutostartSkip() { diff --git a/Telegram/SourceFiles/platform/win/specific_win.cpp b/Telegram/SourceFiles/platform/win/specific_win.cpp index 806d3f542..f036ff077 100644 --- a/Telegram/SourceFiles/platform/win/specific_win.cpp +++ b/Telegram/SourceFiles/platform/win/specific_win.cpp @@ -78,7 +78,7 @@ bool finished = true; QMargins simpleMargins, margins; HICON bigIcon = 0, smallIcon = 0, overlayIcon = 0; -BOOL CALLBACK _ActivateProcess(HWND hWnd, LPARAM lParam) { +BOOL CALLBACK ActivateProcessByPid(HWND hWnd, LPARAM lParam) { uint64 &processId(*(uint64*)lParam); DWORD dwProcessId; @@ -143,11 +143,58 @@ void DeleteMyModules() { RemoveDirectory(modules.c_str()); } +void ManageAppLink(bool create, bool silent, int path_csidl, const wchar_t *args, const wchar_t *description) { + if (cExeName().isEmpty()) { + return; + } + WCHAR startupFolder[MAX_PATH]; + HRESULT hr = SHGetFolderPath(0, path_csidl, 0, SHGFP_TYPE_CURRENT, startupFolder); + if (SUCCEEDED(hr)) { + QString lnk = QString::fromWCharArray(startupFolder) + '\\' + AppFile.utf16() + qsl(".lnk"); + if (create) { + const auto shellLink = base::WinRT::TryCreateInstance( + CLSID_ShellLink, + CLSCTX_INPROC_SERVER); + if (shellLink) { + QString exe = QDir::toNativeSeparators(cExeDir() + cExeName()), dir = QDir::toNativeSeparators(QDir(cWorkingDir()).absolutePath()); + shellLink->SetArguments(args); + shellLink->SetPath(exe.toStdWString().c_str()); + shellLink->SetWorkingDirectory(dir.toStdWString().c_str()); + shellLink->SetDescription(description); + + if (const auto propertyStore = shellLink.try_as()) { + PROPVARIANT appIdPropVar; + hr = InitPropVariantFromString(AppUserModelId::getId(), &appIdPropVar); + if (SUCCEEDED(hr)) { + hr = propertyStore->SetValue(AppUserModelId::getKey(), appIdPropVar); + PropVariantClear(&appIdPropVar); + if (SUCCEEDED(hr)) { + hr = propertyStore->Commit(); + } + } + } + + if (const auto persistFile = shellLink.try_as()) { + hr = persistFile->Save(lnk.toStdWString().c_str(), TRUE); + } else { + if (!silent) LOG(("App Error: could not create interface IID_IPersistFile %1").arg(hr)); + } + } else { + if (!silent) LOG(("App Error: could not create instance of IID_IShellLink %1").arg(hr)); + } + } else { + QFile::remove(lnk); + } + } else { + if (!silent) LOG(("App Error: could not get CSIDL %1 folder %2").arg(path_csidl).arg(hr)); + } +} + } // namespace void psActivateProcess(uint64 pid) { if (pid) { - ::EnumWindows((WNDENUMPROC)_ActivateProcess, (LPARAM)&pid); + ::EnumWindows((WNDENUMPROC)ActivateProcessByPid, (LPARAM)&pid); } } @@ -364,8 +411,11 @@ void AutostartToggle(bool enabled, Fn done) { enabled, done ? Fn(callback) : nullptr); #else // OS_WIN_STORE - _manageAppLnk(start, silent, CSIDL_STARTUP, L"-autostart", L"Telegram autorun link.\nYou can disable autorun in Telegram settings."); - done(enabled); + const auto silent = !done; + ManageAppLink(enabled, silent, CSIDL_STARTUP, L"-autostart", L"Telegram autorun link.\nYou can disable autorun in Telegram settings."); + if (done) { + done(enabled); + } #endif // OS_WIN_STORE } @@ -533,55 +583,8 @@ void psNewVersion() { } } -void _manageAppLnk(bool create, bool silent, int path_csidl, const wchar_t *args, const wchar_t *description) { - if (cExeName().isEmpty()) { - return; - } - WCHAR startupFolder[MAX_PATH]; - HRESULT hr = SHGetFolderPath(0, path_csidl, 0, SHGFP_TYPE_CURRENT, startupFolder); - if (SUCCEEDED(hr)) { - QString lnk = QString::fromWCharArray(startupFolder) + '\\' + AppFile.utf16() + qsl(".lnk"); - if (create) { - const auto shellLink = base::WinRT::TryCreateInstance( - CLSID_ShellLink, - CLSCTX_INPROC_SERVER); - if (shellLink) { - QString exe = QDir::toNativeSeparators(cExeDir() + cExeName()), dir = QDir::toNativeSeparators(QDir(cWorkingDir()).absolutePath()); - shellLink->SetArguments(args); - shellLink->SetPath(exe.toStdWString().c_str()); - shellLink->SetWorkingDirectory(dir.toStdWString().c_str()); - shellLink->SetDescription(description); - - if (const auto propertyStore = shellLink.try_as()) { - PROPVARIANT appIdPropVar; - hr = InitPropVariantFromString(AppUserModelId::getId(), &appIdPropVar); - if (SUCCEEDED(hr)) { - hr = propertyStore->SetValue(AppUserModelId::getKey(), appIdPropVar); - PropVariantClear(&appIdPropVar); - if (SUCCEEDED(hr)) { - hr = propertyStore->Commit(); - } - } - } - - if (const auto persistFile = shellLink.try_as()) { - hr = persistFile->Save(lnk.toStdWString().c_str(), TRUE); - } else { - if (!silent) LOG(("App Error: could not create interface IID_IPersistFile %1").arg(hr)); - } - } else { - if (!silent) LOG(("App Error: could not create instance of IID_IShellLink %1").arg(hr)); - } - } else { - QFile::remove(lnk); - } - } else { - if (!silent) LOG(("App Error: could not get CSIDL %1 folder %2").arg(path_csidl).arg(hr)); - } -} - void psSendToMenu(bool send, bool silent) { - _manageAppLnk(send, silent, CSIDL_SENDTO, L"-sendpath", L"Telegram send to link.\nYou can disable send to menu item in Telegram settings."); + ManageAppLink(send, silent, CSIDL_SENDTO, L"-sendpath", L"Telegram send to link.\nYou can disable send to menu item in Telegram settings."); } bool psLaunchMaps(const Data::LocationPoint &point) {