From 21657306286ea8b1c88d114cf25a90c3f98f2805 Mon Sep 17 00:00:00 2001 From: SimplestStudio Date: Thu, 3 Jul 2025 13:53:10 +0300 Subject: [PATCH 1/2] [win] fix notifications --- win-linux/src/components/cnotification.cpp | 15 +++++++++++ win-linux/src/platform_win/wintoastlib.cpp | 30 +++++++++++++++++++--- win-linux/src/platform_win/wintoastlib.h | 4 +++ 3 files changed, 45 insertions(+), 4 deletions(-) diff --git a/win-linux/src/components/cnotification.cpp b/win-linux/src/components/cnotification.cpp index c0426da35..0fd80f982 100644 --- a/win-linux/src/components/cnotification.cpp +++ b/win-linux/src/components/cnotification.cpp @@ -32,7 +32,10 @@ #ifdef _WIN32 # include +# include # include "utils.h" +# define APP_LAUNCH_NAME "\\" REG_APP_NAME ".exe" +# define APP_SHORTCUT_NAME "\\" APP_REG_NAME ".lnk" #else # include # include @@ -266,6 +269,18 @@ bool CNotification::init() } WinToast::instance()->setAppName(TEXT(WINDOW_TITLE)); WinToast::instance()->setAppUserModelId(TEXT(APP_USER_MODEL_ID)); + WinToast::instance()->setShortcutPolicy(WinToastLib::WinToast::SHORTCUT_POLICY_REQUIRE_NO_CREATE); + PWSTR progPath = nullptr; + if (SUCCEEDED(SHGetKnownFolderPath(FOLDERID_CommonPrograms, 0, nullptr, &progPath))) { + std::wstring shortcutPath(progPath); + shortcutPath.append(L"\\"); + shortcutPath.append(TEXT(APP_REG_NAME)); + shortcutPath.append(TEXT(APP_SHORTCUT_NAME)); + WinToast::instance()->setShortcutPath(shortcutPath); + } + CoTaskMemFree(progPath); + const QString shortcutTarget = qApp->applicationDirPath() + APP_LAUNCH_NAME; + WinToast::instance()->setShortcutTarget(QDir::toNativeSeparators(shortcutTarget).toStdWString()); pimpl->isInit = WinToast::instance()->initialize(); #endif return pimpl->isInit; diff --git a/win-linux/src/platform_win/wintoastlib.cpp b/win-linux/src/platform_win/wintoastlib.cpp index 10bf3b99c..986e10f43 100644 --- a/win-linux/src/platform_win/wintoastlib.cpp +++ b/win-linux/src/platform_win/wintoastlib.cpp @@ -474,6 +474,16 @@ void WinToast::setAppUserModelId(_In_ std::wstring const& aumi) { DEBUG_MSG(L"Default App User Model Id: " << _aumi.c_str()); } +void WinToast::setShortcutPath(_In_ const std::wstring &shortcutPath) +{ + _shortcutPath = shortcutPath; +} + +void WinToast::setShortcutTarget(_In_ const std::wstring &shortcutTarget) +{ + _shortcutTarget = shortcutTarget; +} + void WinToast::setShortcutPolicy(_In_ ShortcutPolicy shortcutPolicy) { _shortcutPolicy = shortcutPolicy; } @@ -609,7 +619,11 @@ std::wstring const& WinToast::appUserModelId() const { HRESULT WinToast::validateShellLinkHelper(_Out_ bool& wasChanged) { WCHAR path[MAX_PATH] = {L'\0'}; - Util::defaultShellLinkPath(_appName, path); + if (!_shortcutPath.empty()) { + wcsncpy_s(path, _shortcutPath.c_str(), _TRUNCATE); + } else { + Util::defaultShellLinkPath(_appName, path); + } // Check if the file exist DWORD attr = GetFileAttributesW(path); if (attr >= 0xFFFFFFF) { @@ -629,7 +643,7 @@ HRESULT WinToast::validateShellLinkHelper(_Out_ bool& wasChanged) { ComPtr persistFile; hr = shellLink.As(&persistFile); if (SUCCEEDED(hr)) { - hr = persistFile->Load(path, STGM_READWRITE); + hr = persistFile->Load(path, _shortcutPolicy == SHORTCUT_POLICY_REQUIRE_CREATE ? STGM_READWRITE : STGM_READ); if (SUCCEEDED(hr)) { ComPtr propertyStore; hr = shellLink.As(&propertyStore); @@ -676,8 +690,16 @@ HRESULT WinToast::createShellLinkHelper() { WCHAR exePath[MAX_PATH]{L'\0'}; WCHAR slPath[MAX_PATH]{L'\0'}; - Util::defaultShellLinkPath(_appName, slPath); - Util::defaultExecutablePath(exePath); + if (!_shortcutPath.empty()) { + wcsncpy_s(slPath, _shortcutPath.c_str(), _TRUNCATE); + } else { + Util::defaultShellLinkPath(_appName, slPath); + } + if (!_shortcutTarget.empty()) { + wcsncpy_s(exePath, _shortcutTarget.c_str(), _TRUNCATE); + } else { + Util::defaultExecutablePath(exePath); + } std::wstring exeDir = Util::parentDirectory(exePath, sizeof(exePath) / sizeof(exePath[0])); ComPtr shellLink; HRESULT hr = CoCreateInstance(CLSID_ShellLink, nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&shellLink)); diff --git a/win-linux/src/platform_win/wintoastlib.h b/win-linux/src/platform_win/wintoastlib.h index 3f410672a..d54e10cf2 100644 --- a/win-linux/src/platform_win/wintoastlib.h +++ b/win-linux/src/platform_win/wintoastlib.h @@ -232,6 +232,8 @@ namespace WinToastLib { std::wstring const& appUserModelId() const; void setAppUserModelId(_In_ std::wstring const& aumi); void setAppName(_In_ std::wstring const& appName); + void setShortcutPath(_In_ const std::wstring &shortcutPath); + void setShortcutTarget(_In_ const std::wstring &shortcutTarget); void setShortcutPolicy(_In_ ShortcutPolicy policy); protected: @@ -290,6 +292,8 @@ namespace WinToastLib { ShortcutPolicy _shortcutPolicy{SHORTCUT_POLICY_REQUIRE_CREATE}; std::wstring _appName{}; std::wstring _aumi{}; + std::wstring _shortcutPath; + std::wstring _shortcutTarget; std::map _buffer{}; void markAsReadyForDeletion(_In_ INT64 id); From d49f9c45eb24abf260fc36a1ede5baef18d50015 Mon Sep 17 00:00:00 2001 From: SimplestStudio Date: Thu, 3 Jul 2025 14:21:40 +0300 Subject: [PATCH 2/2] [win] cnotification: adaptation for portable version --- win-linux/src/components/cnotification.cpp | 26 +++++++++++++--------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/win-linux/src/components/cnotification.cpp b/win-linux/src/components/cnotification.cpp index 0fd80f982..b4d3559bd 100644 --- a/win-linux/src/components/cnotification.cpp +++ b/win-linux/src/components/cnotification.cpp @@ -269,18 +269,22 @@ bool CNotification::init() } WinToast::instance()->setAppName(TEXT(WINDOW_TITLE)); WinToast::instance()->setAppUserModelId(TEXT(APP_USER_MODEL_ID)); - WinToast::instance()->setShortcutPolicy(WinToastLib::WinToast::SHORTCUT_POLICY_REQUIRE_NO_CREATE); - PWSTR progPath = nullptr; - if (SUCCEEDED(SHGetKnownFolderPath(FOLDERID_CommonPrograms, 0, nullptr, &progPath))) { - std::wstring shortcutPath(progPath); - shortcutPath.append(L"\\"); - shortcutPath.append(TEXT(APP_REG_NAME)); - shortcutPath.append(TEXT(APP_SHORTCUT_NAME)); - WinToast::instance()->setShortcutPath(shortcutPath); + if (IsPackage(Portable)) { + WinToast::instance()->setShortcutPolicy(WinToastLib::WinToast::SHORTCUT_POLICY_REQUIRE_CREATE); + const QString shortcutTarget = qApp->applicationDirPath() + APP_LAUNCH_NAME; + WinToast::instance()->setShortcutTarget(QDir::toNativeSeparators(shortcutTarget).toStdWString()); + } else { + WinToast::instance()->setShortcutPolicy(WinToastLib::WinToast::SHORTCUT_POLICY_REQUIRE_NO_CREATE); + PWSTR progPath = nullptr; + if (SUCCEEDED(SHGetKnownFolderPath(FOLDERID_CommonPrograms, 0, nullptr, &progPath))) { + std::wstring shortcutPath(progPath); + shortcutPath.append(L"\\"); + shortcutPath.append(TEXT(APP_REG_NAME)); + shortcutPath.append(TEXT(APP_SHORTCUT_NAME)); + WinToast::instance()->setShortcutPath(shortcutPath); + } + CoTaskMemFree(progPath); } - CoTaskMemFree(progPath); - const QString shortcutTarget = qApp->applicationDirPath() + APP_LAUNCH_NAME; - WinToast::instance()->setShortcutTarget(QDir::toNativeSeparators(shortcutTarget).toStdWString()); pimpl->isInit = WinToast::instance()->initialize(); #endif return pimpl->isInit;