Skip to content

Commit 80dd046

Browse files
author
Oleg Kozhukharenko
committed
Merge pull request 'fix/notifications' (#362) from fix/notifications into develop
Reviewed-on: https://git.onlyoffice.com/ONLYOFFICE/desktop-apps/pulls/362
2 parents 8208563 + d49f9c4 commit 80dd046

File tree

3 files changed

+49
-4
lines changed

3 files changed

+49
-4
lines changed

win-linux/src/components/cnotification.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,10 @@
3232

3333
#ifdef _WIN32
3434
# include <QCoreApplication>
35+
# include <QDir>
3536
# include "utils.h"
37+
# define APP_LAUNCH_NAME "\\" REG_APP_NAME ".exe"
38+
# define APP_SHORTCUT_NAME "\\" APP_REG_NAME ".lnk"
3639
#else
3740
# include <libnotify/notify.h>
3841
# include <gio/gio.h>
@@ -266,6 +269,22 @@ bool CNotification::init()
266269
}
267270
WinToast::instance()->setAppName(TEXT(WINDOW_TITLE));
268271
WinToast::instance()->setAppUserModelId(TEXT(APP_USER_MODEL_ID));
272+
if (IsPackage(Portable)) {
273+
WinToast::instance()->setShortcutPolicy(WinToastLib::WinToast::SHORTCUT_POLICY_REQUIRE_CREATE);
274+
const QString shortcutTarget = qApp->applicationDirPath() + APP_LAUNCH_NAME;
275+
WinToast::instance()->setShortcutTarget(QDir::toNativeSeparators(shortcutTarget).toStdWString());
276+
} else {
277+
WinToast::instance()->setShortcutPolicy(WinToastLib::WinToast::SHORTCUT_POLICY_REQUIRE_NO_CREATE);
278+
PWSTR progPath = nullptr;
279+
if (SUCCEEDED(SHGetKnownFolderPath(FOLDERID_CommonPrograms, 0, nullptr, &progPath))) {
280+
std::wstring shortcutPath(progPath);
281+
shortcutPath.append(L"\\");
282+
shortcutPath.append(TEXT(APP_REG_NAME));
283+
shortcutPath.append(TEXT(APP_SHORTCUT_NAME));
284+
WinToast::instance()->setShortcutPath(shortcutPath);
285+
}
286+
CoTaskMemFree(progPath);
287+
}
269288
pimpl->isInit = WinToast::instance()->initialize();
270289
#endif
271290
return pimpl->isInit;

win-linux/src/platform_win/wintoastlib.cpp

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,16 @@ void WinToast::setAppUserModelId(_In_ std::wstring const& aumi) {
474474
DEBUG_MSG(L"Default App User Model Id: " << _aumi.c_str());
475475
}
476476

477+
void WinToast::setShortcutPath(_In_ const std::wstring &shortcutPath)
478+
{
479+
_shortcutPath = shortcutPath;
480+
}
481+
482+
void WinToast::setShortcutTarget(_In_ const std::wstring &shortcutTarget)
483+
{
484+
_shortcutTarget = shortcutTarget;
485+
}
486+
477487
void WinToast::setShortcutPolicy(_In_ ShortcutPolicy shortcutPolicy) {
478488
_shortcutPolicy = shortcutPolicy;
479489
}
@@ -609,7 +619,11 @@ std::wstring const& WinToast::appUserModelId() const {
609619

610620
HRESULT WinToast::validateShellLinkHelper(_Out_ bool& wasChanged) {
611621
WCHAR path[MAX_PATH] = {L'\0'};
612-
Util::defaultShellLinkPath(_appName, path);
622+
if (!_shortcutPath.empty()) {
623+
wcsncpy_s(path, _shortcutPath.c_str(), _TRUNCATE);
624+
} else {
625+
Util::defaultShellLinkPath(_appName, path);
626+
}
613627
// Check if the file exist
614628
DWORD attr = GetFileAttributesW(path);
615629
if (attr >= 0xFFFFFFF) {
@@ -629,7 +643,7 @@ HRESULT WinToast::validateShellLinkHelper(_Out_ bool& wasChanged) {
629643
ComPtr<IPersistFile> persistFile;
630644
hr = shellLink.As(&persistFile);
631645
if (SUCCEEDED(hr)) {
632-
hr = persistFile->Load(path, STGM_READWRITE);
646+
hr = persistFile->Load(path, _shortcutPolicy == SHORTCUT_POLICY_REQUIRE_CREATE ? STGM_READWRITE : STGM_READ);
633647
if (SUCCEEDED(hr)) {
634648
ComPtr<IPropertyStore> propertyStore;
635649
hr = shellLink.As(&propertyStore);
@@ -676,8 +690,16 @@ HRESULT WinToast::createShellLinkHelper() {
676690

677691
WCHAR exePath[MAX_PATH]{L'\0'};
678692
WCHAR slPath[MAX_PATH]{L'\0'};
679-
Util::defaultShellLinkPath(_appName, slPath);
680-
Util::defaultExecutablePath(exePath);
693+
if (!_shortcutPath.empty()) {
694+
wcsncpy_s(slPath, _shortcutPath.c_str(), _TRUNCATE);
695+
} else {
696+
Util::defaultShellLinkPath(_appName, slPath);
697+
}
698+
if (!_shortcutTarget.empty()) {
699+
wcsncpy_s(exePath, _shortcutTarget.c_str(), _TRUNCATE);
700+
} else {
701+
Util::defaultExecutablePath(exePath);
702+
}
681703
std::wstring exeDir = Util::parentDirectory(exePath, sizeof(exePath) / sizeof(exePath[0]));
682704
ComPtr<IShellLinkW> shellLink;
683705
HRESULT hr = CoCreateInstance(CLSID_ShellLink, nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&shellLink));

win-linux/src/platform_win/wintoastlib.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,8 @@ namespace WinToastLib {
232232
std::wstring const& appUserModelId() const;
233233
void setAppUserModelId(_In_ std::wstring const& aumi);
234234
void setAppName(_In_ std::wstring const& appName);
235+
void setShortcutPath(_In_ const std::wstring &shortcutPath);
236+
void setShortcutTarget(_In_ const std::wstring &shortcutTarget);
235237
void setShortcutPolicy(_In_ ShortcutPolicy policy);
236238

237239
protected:
@@ -290,6 +292,8 @@ namespace WinToastLib {
290292
ShortcutPolicy _shortcutPolicy{SHORTCUT_POLICY_REQUIRE_CREATE};
291293
std::wstring _appName{};
292294
std::wstring _aumi{};
295+
std::wstring _shortcutPath;
296+
std::wstring _shortcutTarget;
293297
std::map<INT64, NotifyData> _buffer{};
294298

295299
void markAsReadyForDeletion(_In_ INT64 id);

0 commit comments

Comments
 (0)