[win] fix notifications

This commit is contained in:
SimplestStudio
2025-07-03 13:53:10 +03:00
parent 82085639a0
commit 2165730628
3 changed files with 45 additions and 4 deletions

View File

@ -32,7 +32,10 @@
#ifdef _WIN32
# include <QCoreApplication>
# include <QDir>
# include "utils.h"
# define APP_LAUNCH_NAME "\\" REG_APP_NAME ".exe"
# define APP_SHORTCUT_NAME "\\" APP_REG_NAME ".lnk"
#else
# include <libnotify/notify.h>
# include <gio/gio.h>
@ -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;

View File

@ -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<IPersistFile> 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<IPropertyStore> 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<IShellLinkW> shellLink;
HRESULT hr = CoCreateInstance(CLSID_ShellLink, nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&shellLink));

View File

@ -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<INT64, NotifyData> _buffer{};
void markAsReadyForDeletion(_In_ INT64 id);