2016-10-02 09:30:28 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2018-01-03 10:23:14 +00:00
|
|
|
the official desktop application for the Telegram messaging service.
|
2016-10-02 09:30:28 +00:00
|
|
|
|
2018-01-03 10:23:14 +00:00
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
2016-10-02 09:30:28 +00:00
|
|
|
*/
|
|
|
|
#include "platform/win/notifications_manager_win.h"
|
|
|
|
|
2016-10-03 08:56:03 +00:00
|
|
|
#include "window/notifications_utilities.h"
|
2016-10-02 16:32:46 +00:00
|
|
|
#include "platform/win/windows_app_user_model_id.h"
|
2016-10-17 10:55:17 +00:00
|
|
|
#include "platform/win/windows_event_filter.h"
|
2016-10-02 16:32:46 +00:00
|
|
|
#include "platform/win/windows_dlls.h"
|
2018-01-13 12:45:11 +00:00
|
|
|
#include "history/history.h"
|
2016-10-02 16:32:46 +00:00
|
|
|
#include "mainwindow.h"
|
|
|
|
|
|
|
|
#include <Shobjidl.h>
|
|
|
|
#include <shellapi.h>
|
|
|
|
|
|
|
|
#include <roapi.h>
|
2019-02-15 11:24:58 +00:00
|
|
|
#include <wrl/client.h>
|
|
|
|
#include "platform/win/wrapper_wrl_implements_h.h"
|
2016-10-02 16:32:46 +00:00
|
|
|
#include <windows.ui.notifications.h>
|
|
|
|
|
|
|
|
#include <strsafe.h>
|
|
|
|
#include <intsafe.h>
|
|
|
|
|
|
|
|
HICON qt_pixmapToWinHICON(const QPixmap &);
|
|
|
|
|
|
|
|
using namespace Microsoft::WRL;
|
|
|
|
using namespace ABI::Windows::UI::Notifications;
|
|
|
|
using namespace ABI::Windows::Data::Xml::Dom;
|
|
|
|
using namespace Windows::Foundation;
|
2016-10-02 09:30:28 +00:00
|
|
|
|
|
|
|
namespace Platform {
|
|
|
|
namespace Notifications {
|
2016-10-02 16:32:46 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
class StringReferenceWrapper {
|
|
|
|
public:
|
|
|
|
StringReferenceWrapper(_In_reads_(length) PCWSTR stringRef, _In_ UINT32 length) throw() {
|
|
|
|
HRESULT hr = Dlls::WindowsCreateStringReference(stringRef, length, &_header, &_hstring);
|
|
|
|
if (!SUCCEEDED(hr)) {
|
|
|
|
RaiseException(static_cast<DWORD>(STATUS_INVALID_PARAMETER), EXCEPTION_NONCONTINUABLE, 0, nullptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
~StringReferenceWrapper() {
|
|
|
|
Dlls::WindowsDeleteString(_hstring);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <size_t N>
|
|
|
|
StringReferenceWrapper(_In_reads_(N) wchar_t const (&stringRef)[N]) throw() {
|
|
|
|
UINT32 length = N - 1;
|
|
|
|
HRESULT hr = Dlls::WindowsCreateStringReference(stringRef, length, &_header, &_hstring);
|
|
|
|
if (!SUCCEEDED(hr)) {
|
|
|
|
RaiseException(static_cast<DWORD>(STATUS_INVALID_PARAMETER), EXCEPTION_NONCONTINUABLE, 0, nullptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <size_t _>
|
|
|
|
StringReferenceWrapper(_In_reads_(_) wchar_t(&stringRef)[_]) throw() {
|
|
|
|
UINT32 length;
|
|
|
|
HRESULT hr = SizeTToUInt32(wcslen(stringRef), &length);
|
|
|
|
if (!SUCCEEDED(hr)) {
|
|
|
|
RaiseException(static_cast<DWORD>(STATUS_INVALID_PARAMETER), EXCEPTION_NONCONTINUABLE, 0, nullptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
Dlls::WindowsCreateStringReference(stringRef, length, &_header, &_hstring);
|
|
|
|
}
|
|
|
|
|
|
|
|
HSTRING Get() const throw() {
|
|
|
|
return _hstring;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
HSTRING _hstring;
|
|
|
|
HSTRING_HEADER _header;
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
_Check_return_ __inline HRESULT _1_GetActivationFactory(_In_ HSTRING activatableClassId, _COM_Outptr_ T** factory) {
|
|
|
|
return Dlls::RoGetActivationFactory(activatableClassId, IID_INS_ARGS(factory));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
inline HRESULT wrap_GetActivationFactory(_In_ HSTRING activatableClassId, _Inout_ Details::ComPtrRef<T> factory) throw() {
|
|
|
|
return _1_GetActivationFactory(activatableClassId, factory.ReleaseAndGetAddressOf());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool init() {
|
|
|
|
if (QSysInfo::windowsVersion() < QSysInfo::WV_WINDOWS8) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if ((Dlls::SetCurrentProcessExplicitAppUserModelID == nullptr)
|
|
|
|
|| (Dlls::PropVariantToString == nullptr)
|
|
|
|
|| (Dlls::RoGetActivationFactory == nullptr)
|
|
|
|
|| (Dlls::WindowsCreateStringReference == nullptr)
|
|
|
|
|| (Dlls::WindowsDeleteString == nullptr)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!AppUserModelId::validateShortcut()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto appUserModelId = AppUserModelId::getId();
|
|
|
|
if (!SUCCEEDED(Dlls::SetCurrentProcessExplicitAppUserModelID(appUserModelId))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT SetNodeValueString(_In_ HSTRING inputString, _In_ IXmlNode *node, _In_ IXmlDocument *xml) {
|
|
|
|
ComPtr<IXmlText> inputText;
|
|
|
|
|
|
|
|
HRESULT hr = xml->CreateTextNode(inputString, &inputText);
|
|
|
|
if (!SUCCEEDED(hr)) return hr;
|
|
|
|
ComPtr<IXmlNode> inputTextNode;
|
|
|
|
|
|
|
|
hr = inputText.As(&inputTextNode);
|
|
|
|
if (!SUCCEEDED(hr)) return hr;
|
|
|
|
|
|
|
|
ComPtr<IXmlNode> pAppendedChild;
|
|
|
|
return node->AppendChild(inputTextNode.Get(), &pAppendedChild);
|
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT SetAudioSilent(_In_ IXmlDocument *toastXml) {
|
|
|
|
ComPtr<IXmlNodeList> nodeList;
|
|
|
|
HRESULT hr = toastXml->GetElementsByTagName(StringReferenceWrapper(L"audio").Get(), &nodeList);
|
|
|
|
if (!SUCCEEDED(hr)) return hr;
|
|
|
|
|
|
|
|
ComPtr<IXmlNode> audioNode;
|
|
|
|
hr = nodeList->Item(0, &audioNode);
|
|
|
|
if (!SUCCEEDED(hr)) return hr;
|
|
|
|
|
|
|
|
if (audioNode) {
|
|
|
|
ComPtr<IXmlElement> audioElement;
|
|
|
|
hr = audioNode.As(&audioElement);
|
|
|
|
if (!SUCCEEDED(hr)) return hr;
|
|
|
|
|
|
|
|
hr = audioElement->SetAttribute(StringReferenceWrapper(L"silent").Get(), StringReferenceWrapper(L"true").Get());
|
|
|
|
if (!SUCCEEDED(hr)) return hr;
|
|
|
|
} else {
|
|
|
|
ComPtr<IXmlElement> audioElement;
|
|
|
|
hr = toastXml->CreateElement(StringReferenceWrapper(L"audio").Get(), &audioElement);
|
|
|
|
if (!SUCCEEDED(hr)) return hr;
|
|
|
|
|
|
|
|
hr = audioElement->SetAttribute(StringReferenceWrapper(L"silent").Get(), StringReferenceWrapper(L"true").Get());
|
|
|
|
if (!SUCCEEDED(hr)) return hr;
|
|
|
|
|
|
|
|
ComPtr<IXmlNode> audioNode;
|
|
|
|
hr = audioElement.As(&audioNode);
|
|
|
|
if (!SUCCEEDED(hr)) return hr;
|
|
|
|
|
|
|
|
ComPtr<IXmlNodeList> nodeList;
|
|
|
|
hr = toastXml->GetElementsByTagName(StringReferenceWrapper(L"toast").Get(), &nodeList);
|
|
|
|
if (!SUCCEEDED(hr)) return hr;
|
|
|
|
|
|
|
|
ComPtr<IXmlNode> toastNode;
|
|
|
|
hr = nodeList->Item(0, &toastNode);
|
|
|
|
if (!SUCCEEDED(hr)) return hr;
|
|
|
|
|
|
|
|
ComPtr<IXmlNode> appendedNode;
|
|
|
|
hr = toastNode->AppendChild(audioNode.Get(), &appendedNode);
|
|
|
|
}
|
|
|
|
return hr;
|
|
|
|
}
|
|
|
|
|
|
|
|
HRESULT SetImageSrc(_In_z_ const wchar_t *imagePath, _In_ IXmlDocument *toastXml) {
|
|
|
|
wchar_t imageSrc[MAX_PATH] = L"file:///";
|
|
|
|
HRESULT hr = StringCchCat(imageSrc, ARRAYSIZE(imageSrc), imagePath);
|
|
|
|
if (!SUCCEEDED(hr)) return hr;
|
|
|
|
|
|
|
|
ComPtr<IXmlNodeList> nodeList;
|
|
|
|
hr = toastXml->GetElementsByTagName(StringReferenceWrapper(L"image").Get(), &nodeList);
|
|
|
|
if (!SUCCEEDED(hr)) return hr;
|
|
|
|
|
|
|
|
ComPtr<IXmlNode> imageNode;
|
|
|
|
hr = nodeList->Item(0, &imageNode);
|
|
|
|
if (!SUCCEEDED(hr)) return hr;
|
|
|
|
|
|
|
|
ComPtr<IXmlNamedNodeMap> attributes;
|
|
|
|
hr = imageNode->get_Attributes(&attributes);
|
|
|
|
if (!SUCCEEDED(hr)) return hr;
|
|
|
|
|
|
|
|
ComPtr<IXmlNode> srcAttribute;
|
|
|
|
hr = attributes->GetNamedItem(StringReferenceWrapper(L"src").Get(), &srcAttribute);
|
|
|
|
if (!SUCCEEDED(hr)) return hr;
|
|
|
|
|
|
|
|
return SetNodeValueString(StringReferenceWrapper(imageSrc).Get(), srcAttribute.Get(), toastXml);
|
|
|
|
}
|
|
|
|
|
|
|
|
typedef ABI::Windows::Foundation::ITypedEventHandler<ToastNotification*, ::IInspectable *> DesktopToastActivatedEventHandler;
|
|
|
|
typedef ABI::Windows::Foundation::ITypedEventHandler<ToastNotification*, ToastDismissedEventArgs*> DesktopToastDismissedEventHandler;
|
|
|
|
typedef ABI::Windows::Foundation::ITypedEventHandler<ToastNotification*, ToastFailedEventArgs*> DesktopToastFailedEventHandler;
|
|
|
|
|
2019-02-15 11:24:58 +00:00
|
|
|
class ToastEventHandler final : public Implements<
|
2017-11-11 17:53:23 +00:00
|
|
|
DesktopToastActivatedEventHandler,
|
|
|
|
DesktopToastDismissedEventHandler,
|
|
|
|
DesktopToastFailedEventHandler> {
|
2016-10-02 16:32:46 +00:00
|
|
|
public:
|
2017-03-04 19:36:59 +00:00
|
|
|
// We keep a weak pointer to a member field of native notifications manager.
|
2017-11-11 17:53:23 +00:00
|
|
|
ToastEventHandler(
|
|
|
|
const std::shared_ptr<Manager*> &guarded,
|
|
|
|
const PeerId &peer,
|
|
|
|
MsgId msg)
|
2017-03-04 19:36:59 +00:00
|
|
|
: _peerId(peer)
|
|
|
|
, _msgId(msg)
|
|
|
|
, _weak(guarded) {
|
|
|
|
}
|
|
|
|
|
2018-06-04 15:35:11 +00:00
|
|
|
void performOnMainQueue(FnMut<void(Manager *manager)> task) {
|
2017-12-30 21:28:38 +00:00
|
|
|
const auto weak = _weak;
|
|
|
|
crl::on_main(weak, [=, task = std::move(task)]() mutable {
|
|
|
|
task(*weak.lock());
|
2017-03-04 19:36:59 +00:00
|
|
|
});
|
2016-10-02 16:32:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DesktopToastActivatedEventHandler
|
|
|
|
IFACEMETHODIMP Invoke(_In_ IToastNotification *sender, _In_ IInspectable* args) {
|
2017-03-04 19:36:59 +00:00
|
|
|
performOnMainQueue([peerId = _peerId, msgId = _msgId](Manager *manager) {
|
|
|
|
manager->notificationActivated(peerId, msgId);
|
|
|
|
});
|
2016-10-02 16:32:46 +00:00
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// DesktopToastDismissedEventHandler
|
|
|
|
IFACEMETHODIMP Invoke(_In_ IToastNotification *sender, _In_ IToastDismissedEventArgs *e) {
|
|
|
|
ToastDismissalReason tdr;
|
|
|
|
if (SUCCEEDED(e->get_Reason(&tdr))) {
|
|
|
|
switch (tdr) {
|
|
|
|
case ToastDismissalReason_ApplicationHidden:
|
|
|
|
break;
|
|
|
|
case ToastDismissalReason_UserCanceled:
|
|
|
|
case ToastDismissalReason_TimedOut:
|
|
|
|
default:
|
2017-03-04 19:36:59 +00:00
|
|
|
performOnMainQueue([peerId = _peerId, msgId = _msgId](Manager *manager) {
|
|
|
|
manager->clearNotification(peerId, msgId);
|
|
|
|
});
|
2016-10-02 16:32:46 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// DesktopToastFailedEventHandler
|
|
|
|
IFACEMETHODIMP Invoke(_In_ IToastNotification *sender, _In_ IToastFailedEventArgs *e) {
|
2017-03-04 19:36:59 +00:00
|
|
|
performOnMainQueue([peerId = _peerId, msgId = _msgId](Manager *manager) {
|
|
|
|
manager->clearNotification(peerId, msgId);
|
|
|
|
});
|
2016-10-02 16:32:46 +00:00
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// IUnknown
|
|
|
|
IFACEMETHODIMP_(ULONG) AddRef() {
|
2017-03-04 19:36:59 +00:00
|
|
|
return InterlockedIncrement(&_refCount);
|
2016-10-02 16:32:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
IFACEMETHODIMP_(ULONG) Release() {
|
2017-03-04 19:36:59 +00:00
|
|
|
auto refCount = InterlockedDecrement(&_refCount);
|
|
|
|
if (refCount == 0) {
|
|
|
|
delete this;
|
|
|
|
}
|
|
|
|
return refCount;
|
2016-10-02 16:32:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
IFACEMETHODIMP QueryInterface(_In_ REFIID riid, _COM_Outptr_ void **ppv) {
|
|
|
|
if (IsEqualIID(riid, IID_IUnknown))
|
|
|
|
*ppv = static_cast<IUnknown*>(static_cast<DesktopToastActivatedEventHandler*>(this));
|
|
|
|
else if (IsEqualIID(riid, __uuidof(DesktopToastActivatedEventHandler)))
|
|
|
|
*ppv = static_cast<DesktopToastActivatedEventHandler*>(this);
|
|
|
|
else if (IsEqualIID(riid, __uuidof(DesktopToastDismissedEventHandler)))
|
|
|
|
*ppv = static_cast<DesktopToastDismissedEventHandler*>(this);
|
|
|
|
else if (IsEqualIID(riid, __uuidof(DesktopToastFailedEventHandler)))
|
|
|
|
*ppv = static_cast<DesktopToastFailedEventHandler*>(this);
|
|
|
|
else *ppv = nullptr;
|
|
|
|
|
|
|
|
if (*ppv) {
|
|
|
|
reinterpret_cast<IUnknown*>(*ppv)->AddRef();
|
|
|
|
return S_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return E_NOINTERFACE;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2017-03-04 19:36:59 +00:00
|
|
|
ULONG _refCount = 0;
|
|
|
|
PeerId _peerId = 0;
|
|
|
|
MsgId _msgId = 0;
|
|
|
|
std::weak_ptr<Manager*> _weak;
|
2016-10-02 16:32:46 +00:00
|
|
|
|
|
|
|
};
|
|
|
|
|
2017-03-04 19:36:59 +00:00
|
|
|
auto Checked = false;
|
|
|
|
auto InitSucceeded = false;
|
|
|
|
|
|
|
|
void Check() {
|
|
|
|
InitSucceeded = init();
|
|
|
|
}
|
|
|
|
|
2016-10-02 16:32:46 +00:00
|
|
|
} // namespace
|
2016-10-02 09:30:28 +00:00
|
|
|
|
2017-03-04 19:36:59 +00:00
|
|
|
bool Supported() {
|
|
|
|
if (!Checked) {
|
|
|
|
Checked = true;
|
|
|
|
Check();
|
2016-10-02 16:32:46 +00:00
|
|
|
}
|
2017-03-04 19:36:59 +00:00
|
|
|
return InitSucceeded;
|
2016-10-02 09:30:28 +00:00
|
|
|
}
|
|
|
|
|
2017-03-04 19:36:59 +00:00
|
|
|
std::unique_ptr<Window::Notifications::Manager> Create(Window::Notifications::System *system) {
|
|
|
|
if (Global::NativeNotifications() && Supported()) {
|
|
|
|
auto result = std::make_unique<Manager>(system);
|
|
|
|
if (result->init()) {
|
|
|
|
return std::move(result);
|
|
|
|
}
|
2016-10-02 09:30:28 +00:00
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2017-03-04 19:36:59 +00:00
|
|
|
void FlashBounce() {
|
|
|
|
auto window = App::wnd();
|
|
|
|
if (!window || GetForegroundWindow() == window->psHwnd()) {
|
|
|
|
return;
|
|
|
|
}
|
2016-10-02 16:32:46 +00:00
|
|
|
|
2017-03-04 19:36:59 +00:00
|
|
|
FLASHWINFO info;
|
|
|
|
info.cbSize = sizeof(info);
|
|
|
|
info.hwnd = window->psHwnd();
|
|
|
|
info.dwFlags = FLASHW_ALL;
|
|
|
|
info.dwTimeout = 0;
|
|
|
|
info.uCount = 1;
|
|
|
|
FlashWindowEx(&info);
|
2016-10-02 16:32:46 +00:00
|
|
|
}
|
|
|
|
|
2017-03-04 19:36:59 +00:00
|
|
|
class Manager::Private {
|
2016-10-02 16:32:46 +00:00
|
|
|
public:
|
2017-01-07 11:55:05 +00:00
|
|
|
using Type = Window::Notifications::CachedUserpics::Type;
|
|
|
|
|
2017-03-04 19:36:59 +00:00
|
|
|
explicit Private(Manager *instance, Type type);
|
|
|
|
bool init();
|
2017-01-07 11:55:05 +00:00
|
|
|
|
2019-08-28 14:24:12 +00:00
|
|
|
bool showNotification(
|
|
|
|
not_null<PeerData*> peer,
|
|
|
|
MsgId msgId,
|
|
|
|
const QString &title,
|
|
|
|
const QString &subtitle,
|
|
|
|
const QString &msg,
|
|
|
|
bool hideNameAndPhoto,
|
|
|
|
bool hideReplyButton);
|
2016-10-02 16:32:46 +00:00
|
|
|
void clearAll();
|
2019-08-30 14:06:21 +00:00
|
|
|
void clearFromHistory(not_null<History*> history);
|
2016-10-02 16:32:46 +00:00
|
|
|
void beforeNotificationActivated(PeerId peerId, MsgId msgId);
|
|
|
|
void afterNotificationActivated(PeerId peerId, MsgId msgId);
|
|
|
|
void clearNotification(PeerId peerId, MsgId msgId);
|
|
|
|
|
2017-03-04 19:36:59 +00:00
|
|
|
~Private();
|
2016-10-02 16:32:46 +00:00
|
|
|
|
|
|
|
private:
|
2016-10-03 08:56:03 +00:00
|
|
|
Window::Notifications::CachedUserpics _cachedUserpics;
|
2016-10-02 16:32:46 +00:00
|
|
|
|
2017-03-04 19:36:59 +00:00
|
|
|
std::shared_ptr<Manager*> _guarded;
|
|
|
|
|
|
|
|
ComPtr<IToastNotificationManagerStatics> _notificationManager;
|
|
|
|
ComPtr<IToastNotifier> _notifier;
|
|
|
|
ComPtr<IToastNotificationFactory> _notificationFactory;
|
|
|
|
|
|
|
|
struct NotificationPtr {
|
|
|
|
NotificationPtr() {
|
|
|
|
}
|
|
|
|
NotificationPtr(const ComPtr<IToastNotification> &ptr) : p(ptr) {
|
|
|
|
}
|
|
|
|
|
|
|
|
ComPtr<IToastNotification> p;
|
|
|
|
};
|
|
|
|
QMap<PeerId, QMap<MsgId, NotificationPtr>> _notifications;
|
|
|
|
|
2016-10-02 16:32:46 +00:00
|
|
|
};
|
|
|
|
|
2017-03-04 19:36:59 +00:00
|
|
|
Manager::Private::Private(Manager *instance, Type type)
|
2019-01-18 11:26:43 +00:00
|
|
|
: _cachedUserpics(type)
|
|
|
|
, _guarded(std::make_shared<Manager*>(instance)) {
|
2017-03-04 19:36:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Manager::Private::init() {
|
|
|
|
if (!SUCCEEDED(wrap_GetActivationFactory(StringReferenceWrapper(RuntimeClass_Windows_UI_Notifications_ToastNotificationManager).Get(), &_notificationManager))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto appUserModelId = AppUserModelId::getId();
|
|
|
|
if (!SUCCEEDED(_notificationManager->CreateToastNotifierWithId(StringReferenceWrapper(appUserModelId, wcslen(appUserModelId)).Get(), &_notifier))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!SUCCEEDED(wrap_GetActivationFactory(StringReferenceWrapper(RuntimeClass_Windows_UI_Notifications_ToastNotification).Get(), &_notificationFactory))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Manager::Private::~Private() {
|
|
|
|
clearAll();
|
|
|
|
|
2016-10-02 16:32:46 +00:00
|
|
|
_notifications.clear();
|
|
|
|
if (_notificationManager) _notificationManager.Reset();
|
|
|
|
if (_notifier) _notifier.Reset();
|
|
|
|
if (_notificationFactory) _notificationFactory.Reset();
|
|
|
|
}
|
|
|
|
|
2017-03-04 19:36:59 +00:00
|
|
|
void Manager::Private::clearAll() {
|
2016-10-02 16:32:46 +00:00
|
|
|
if (!_notifier) return;
|
|
|
|
|
2016-10-07 16:45:45 +00:00
|
|
|
auto temp = base::take(_notifications);
|
2016-10-02 16:32:46 +00:00
|
|
|
for_const (auto ¬ifications, temp) {
|
|
|
|
for_const (auto ¬ification, notifications) {
|
|
|
|
_notifier->Hide(notification.p.Get());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-30 14:06:21 +00:00
|
|
|
void Manager::Private::clearFromHistory(not_null<History*> history) {
|
2016-10-02 16:32:46 +00:00
|
|
|
if (!_notifier) return;
|
|
|
|
|
|
|
|
auto i = _notifications.find(history->peer->id);
|
|
|
|
if (i != _notifications.cend()) {
|
2016-10-07 16:45:45 +00:00
|
|
|
auto temp = base::take(i.value());
|
2016-10-02 16:32:46 +00:00
|
|
|
_notifications.erase(i);
|
|
|
|
|
2016-10-03 08:56:03 +00:00
|
|
|
for_const (auto ¬ification, temp) {
|
|
|
|
_notifier->Hide(notification.p.Get());
|
2016-10-02 16:32:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-04 19:36:59 +00:00
|
|
|
void Manager::Private::beforeNotificationActivated(PeerId peerId, MsgId msgId) {
|
2016-10-02 16:32:46 +00:00
|
|
|
clearNotification(peerId, msgId);
|
|
|
|
}
|
|
|
|
|
2017-03-04 19:36:59 +00:00
|
|
|
void Manager::Private::afterNotificationActivated(PeerId peerId, MsgId msgId) {
|
2016-10-02 16:32:46 +00:00
|
|
|
if (auto window = App::wnd()) {
|
|
|
|
SetForegroundWindow(window->psHwnd());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-04 19:36:59 +00:00
|
|
|
void Manager::Private::clearNotification(PeerId peerId, MsgId msgId) {
|
2016-10-02 16:32:46 +00:00
|
|
|
auto i = _notifications.find(peerId);
|
|
|
|
if (i != _notifications.cend()) {
|
|
|
|
i.value().remove(msgId);
|
|
|
|
if (i.value().isEmpty()) {
|
|
|
|
_notifications.erase(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-28 14:24:12 +00:00
|
|
|
bool Manager::Private::showNotification(
|
|
|
|
not_null<PeerData*> peer,
|
|
|
|
MsgId msgId,
|
|
|
|
const QString &title,
|
|
|
|
const QString &subtitle,
|
|
|
|
const QString &msg,
|
|
|
|
bool hideNameAndPhoto,
|
|
|
|
bool hideReplyButton) {
|
2016-10-02 16:32:46 +00:00
|
|
|
if (!_notificationManager || !_notifier || !_notificationFactory) return false;
|
|
|
|
|
|
|
|
ComPtr<IXmlDocument> toastXml;
|
|
|
|
bool withSubtitle = !subtitle.isEmpty();
|
|
|
|
|
2019-08-28 14:24:12 +00:00
|
|
|
HRESULT hr = _notificationManager->GetTemplateContent(
|
|
|
|
(withSubtitle
|
|
|
|
? ToastTemplateType_ToastImageAndText04
|
|
|
|
: ToastTemplateType_ToastImageAndText02),
|
|
|
|
&toastXml);
|
2016-10-02 16:32:46 +00:00
|
|
|
if (!SUCCEEDED(hr)) return false;
|
|
|
|
|
|
|
|
hr = SetAudioSilent(toastXml.Get());
|
|
|
|
if (!SUCCEEDED(hr)) return false;
|
|
|
|
|
2019-03-22 13:43:34 +00:00
|
|
|
const auto key = hideNameAndPhoto
|
|
|
|
? InMemoryKey()
|
|
|
|
: peer->userpicUniqueKey();
|
|
|
|
const auto userpicPath = _cachedUserpics.get(key, peer);
|
|
|
|
const auto userpicPathWide = QDir::toNativeSeparators(userpicPath).toStdWString();
|
2016-10-02 16:32:46 +00:00
|
|
|
|
2016-10-03 08:56:03 +00:00
|
|
|
hr = SetImageSrc(userpicPathWide.c_str(), toastXml.Get());
|
2016-10-02 16:32:46 +00:00
|
|
|
if (!SUCCEEDED(hr)) return false;
|
|
|
|
|
|
|
|
ComPtr<IXmlNodeList> nodeList;
|
|
|
|
hr = toastXml->GetElementsByTagName(StringReferenceWrapper(L"text").Get(), &nodeList);
|
|
|
|
if (!SUCCEEDED(hr)) return false;
|
|
|
|
|
|
|
|
UINT32 nodeListLength;
|
|
|
|
hr = nodeList->get_Length(&nodeListLength);
|
|
|
|
if (!SUCCEEDED(hr)) return false;
|
|
|
|
|
|
|
|
if (nodeListLength < (withSubtitle ? 3U : 2U)) return false;
|
|
|
|
|
|
|
|
{
|
|
|
|
ComPtr<IXmlNode> textNode;
|
|
|
|
hr = nodeList->Item(0, &textNode);
|
|
|
|
if (!SUCCEEDED(hr)) return false;
|
|
|
|
|
|
|
|
std::wstring wtitle = title.toStdWString();
|
|
|
|
hr = SetNodeValueString(StringReferenceWrapper(wtitle.data(), wtitle.size()).Get(), textNode.Get(), toastXml.Get());
|
|
|
|
if (!SUCCEEDED(hr)) return false;
|
|
|
|
}
|
|
|
|
if (withSubtitle) {
|
|
|
|
ComPtr<IXmlNode> textNode;
|
|
|
|
hr = nodeList->Item(1, &textNode);
|
|
|
|
if (!SUCCEEDED(hr)) return false;
|
|
|
|
|
|
|
|
std::wstring wsubtitle = subtitle.toStdWString();
|
|
|
|
hr = SetNodeValueString(StringReferenceWrapper(wsubtitle.data(), wsubtitle.size()).Get(), textNode.Get(), toastXml.Get());
|
|
|
|
if (!SUCCEEDED(hr)) return false;
|
|
|
|
}
|
|
|
|
{
|
|
|
|
ComPtr<IXmlNode> textNode;
|
|
|
|
hr = nodeList->Item(withSubtitle ? 2 : 1, &textNode);
|
|
|
|
if (!SUCCEEDED(hr)) return false;
|
|
|
|
|
|
|
|
std::wstring wmsg = msg.toStdWString();
|
|
|
|
hr = SetNodeValueString(StringReferenceWrapper(wmsg.data(), wmsg.size()).Get(), textNode.Get(), toastXml.Get());
|
|
|
|
if (!SUCCEEDED(hr)) return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ComPtr<IToastNotification> toast;
|
|
|
|
hr = _notificationFactory->CreateToastNotification(toastXml.Get(), &toast);
|
|
|
|
if (!SUCCEEDED(hr)) return false;
|
|
|
|
|
|
|
|
EventRegistrationToken activatedToken, dismissedToken, failedToken;
|
2017-03-04 19:36:59 +00:00
|
|
|
ComPtr<ToastEventHandler> eventHandler(new ToastEventHandler(_guarded, peer->id, msgId));
|
2016-10-02 16:32:46 +00:00
|
|
|
|
|
|
|
hr = toast->add_Activated(eventHandler.Get(), &activatedToken);
|
|
|
|
if (!SUCCEEDED(hr)) return false;
|
|
|
|
|
|
|
|
hr = toast->add_Dismissed(eventHandler.Get(), &dismissedToken);
|
|
|
|
if (!SUCCEEDED(hr)) return false;
|
|
|
|
|
|
|
|
hr = toast->add_Failed(eventHandler.Get(), &failedToken);
|
|
|
|
if (!SUCCEEDED(hr)) return false;
|
|
|
|
|
|
|
|
auto i = _notifications.find(peer->id);
|
|
|
|
if (i != _notifications.cend()) {
|
|
|
|
auto j = i->find(msgId);
|
|
|
|
if (j != i->cend()) {
|
|
|
|
ComPtr<IToastNotification> notify = j->p;
|
|
|
|
i->erase(j);
|
|
|
|
_notifier->Hide(notify.Get());
|
|
|
|
i = _notifications.find(peer->id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (i == _notifications.cend()) {
|
|
|
|
i = _notifications.insert(peer->id, QMap<MsgId, NotificationPtr>());
|
|
|
|
}
|
|
|
|
hr = _notifier->Show(toast.Get());
|
|
|
|
if (!SUCCEEDED(hr)) {
|
|
|
|
i = _notifications.find(peer->id);
|
|
|
|
if (i != _notifications.cend() && i->isEmpty()) _notifications.erase(i);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
_notifications[peer->id].insert(msgId, toast);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-03-04 19:36:59 +00:00
|
|
|
Manager::Manager(Window::Notifications::System *system) : NativeManager(system)
|
|
|
|
, _private(std::make_unique<Private>(this, Private::Type::Rounded)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Manager::init() {
|
|
|
|
return _private->init();
|
2016-10-02 16:32:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Manager::clearNotification(PeerId peerId, MsgId msgId) {
|
2017-03-04 19:36:59 +00:00
|
|
|
_private->clearNotification(peerId, msgId);
|
2016-10-02 16:32:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Manager::~Manager() = default;
|
|
|
|
|
2019-08-28 14:24:12 +00:00
|
|
|
void Manager::doShowNativeNotification(
|
|
|
|
not_null<PeerData*> peer,
|
|
|
|
MsgId msgId,
|
|
|
|
const QString &title,
|
|
|
|
const QString &subtitle,
|
|
|
|
const QString &msg,
|
|
|
|
bool hideNameAndPhoto,
|
|
|
|
bool hideReplyButton) {
|
|
|
|
_private->showNotification(
|
|
|
|
peer,
|
|
|
|
msgId,
|
|
|
|
title,
|
|
|
|
subtitle,
|
|
|
|
msg,
|
|
|
|
hideNameAndPhoto,
|
|
|
|
hideReplyButton);
|
2016-10-02 16:32:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Manager::doClearAllFast() {
|
2017-03-04 19:36:59 +00:00
|
|
|
_private->clearAll();
|
2016-10-02 16:32:46 +00:00
|
|
|
}
|
|
|
|
|
2019-08-30 14:06:21 +00:00
|
|
|
void Manager::doClearFromHistory(not_null<History*> history) {
|
2017-03-04 19:36:59 +00:00
|
|
|
_private->clearFromHistory(history);
|
2016-10-02 16:32:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Manager::onBeforeNotificationActivated(PeerId peerId, MsgId msgId) {
|
2017-03-04 19:36:59 +00:00
|
|
|
_private->beforeNotificationActivated(peerId, msgId);
|
2016-10-02 16:32:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Manager::onAfterNotificationActivated(PeerId peerId, MsgId msgId) {
|
2017-03-04 19:36:59 +00:00
|
|
|
_private->afterNotificationActivated(peerId, msgId);
|
2016-10-02 09:30:28 +00:00
|
|
|
}
|
|
|
|
|
2016-10-17 10:55:17 +00:00
|
|
|
namespace {
|
|
|
|
|
2016-10-18 15:19:13 +00:00
|
|
|
bool QuietHoursEnabled = false;
|
|
|
|
DWORD QuietHoursValue = 0;
|
2016-10-17 10:55:17 +00:00
|
|
|
|
2018-07-17 12:44:58 +00:00
|
|
|
bool useQuietHoursRegistryEntry() {
|
|
|
|
// Taken from QSysInfo.
|
|
|
|
OSVERSIONINFO result = { sizeof(OSVERSIONINFO), 0, 0, 0, 0,{ '\0' } };
|
|
|
|
if (const auto library = GetModuleHandle(L"ntdll.dll")) {
|
|
|
|
using RtlGetVersionFunction = NTSTATUS(NTAPI*)(LPOSVERSIONINFO);
|
|
|
|
const auto RtlGetVersion = reinterpret_cast<RtlGetVersionFunction>(
|
|
|
|
GetProcAddress(library, "RtlGetVersion"));
|
|
|
|
if (RtlGetVersion) {
|
|
|
|
RtlGetVersion(&result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// At build 17134 (Redstone 4) the "Quiet hours" was replaced
|
|
|
|
// by "Focus assist" and it looks like it doesn't use registry.
|
|
|
|
return (result.dwMajorVersion == 10
|
|
|
|
&& result.dwMinorVersion == 0
|
|
|
|
&& result.dwBuildNumber < 17134);
|
|
|
|
}
|
|
|
|
|
2017-05-26 14:40:46 +00:00
|
|
|
// Thanks https://stackoverflow.com/questions/35600128/get-windows-quiet-hours-from-win32-or-c-sharp-api
|
2016-10-18 15:19:13 +00:00
|
|
|
void queryQuietHours() {
|
2018-07-17 12:44:58 +00:00
|
|
|
if (!useQuietHoursRegistryEntry()) {
|
2017-05-26 14:40:46 +00:00
|
|
|
// There are quiet hours in Windows starting from Windows 8.1
|
|
|
|
// But there were several reports about the notifications being shut
|
|
|
|
// down according to the registry while no quiet hours were enabled.
|
|
|
|
// So we try this method only starting with Windows 10.
|
2017-02-18 13:06:03 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-11 17:53:23 +00:00
|
|
|
LPCWSTR lpKeyName = L"Software\\Microsoft\\Windows\\CurrentVersion\\Notifications\\Settings";
|
|
|
|
LPCWSTR lpValueName = L"NOC_GLOBAL_SETTING_TOASTS_ENABLED";
|
2016-10-17 10:55:17 +00:00
|
|
|
HKEY key;
|
|
|
|
auto result = RegOpenKeyEx(HKEY_CURRENT_USER, lpKeyName, 0, KEY_READ, &key);
|
|
|
|
if (result != ERROR_SUCCESS) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
DWORD value = 0, type = 0, size = sizeof(value);
|
|
|
|
result = RegQueryValueEx(key, lpValueName, 0, &type, (LPBYTE)&value, &size);
|
|
|
|
RegCloseKey(key);
|
|
|
|
|
2017-02-18 13:06:03 +00:00
|
|
|
auto quietHoursEnabled = (result == ERROR_SUCCESS) && (value == 0);
|
2016-10-18 15:19:13 +00:00
|
|
|
if (QuietHoursEnabled != quietHoursEnabled) {
|
|
|
|
QuietHoursEnabled = quietHoursEnabled;
|
|
|
|
QuietHoursValue = value;
|
|
|
|
LOG(("Quiet hours changed, entry value: %1").arg(value));
|
|
|
|
} else if (QuietHoursValue != value) {
|
|
|
|
QuietHoursValue = value;
|
|
|
|
LOG(("Quiet hours value changed, was value: %1, entry value: %2").arg(QuietHoursValue).arg(value));
|
2016-10-17 10:55:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
QUERY_USER_NOTIFICATION_STATE UserNotificationState = QUNS_ACCEPTS_NOTIFICATIONS;
|
|
|
|
|
|
|
|
void queryUserNotificationState() {
|
|
|
|
if (Dlls::SHQueryUserNotificationState != nullptr) {
|
|
|
|
QUERY_USER_NOTIFICATION_STATE state;
|
|
|
|
if (SUCCEEDED(Dlls::SHQueryUserNotificationState(&state))) {
|
|
|
|
UserNotificationState = state;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-08 07:31:29 +00:00
|
|
|
static constexpr auto kQuerySettingsEachMs = 1000;
|
2019-02-19 06:57:53 +00:00
|
|
|
crl::time LastSettingsQueryMs = 0;
|
2016-10-17 10:55:17 +00:00
|
|
|
|
|
|
|
void querySystemNotificationSettings() {
|
2019-02-19 06:57:53 +00:00
|
|
|
auto ms = crl::now();
|
2017-03-08 07:31:29 +00:00
|
|
|
if (LastSettingsQueryMs > 0 && ms <= LastSettingsQueryMs + kQuerySettingsEachMs) {
|
2016-10-17 10:55:17 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
LastSettingsQueryMs = ms;
|
2016-10-18 15:19:13 +00:00
|
|
|
queryQuietHours();
|
2016-10-17 10:55:17 +00:00
|
|
|
queryUserNotificationState();
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2017-02-28 10:51:00 +00:00
|
|
|
bool SkipAudio() {
|
2016-10-17 10:55:17 +00:00
|
|
|
querySystemNotificationSettings();
|
|
|
|
|
2019-06-07 11:30:30 +00:00
|
|
|
if (UserNotificationState == QUNS_NOT_PRESENT
|
|
|
|
|| UserNotificationState == QUNS_PRESENTATION_MODE
|
|
|
|
|| QuietHoursEnabled) {
|
2016-10-17 10:55:17 +00:00
|
|
|
return true;
|
|
|
|
}
|
2019-06-07 11:30:30 +00:00
|
|
|
if (const auto filter = EventFilter::GetInstance()) {
|
|
|
|
if (filter->sessionLoggedOff()) {
|
|
|
|
return true;
|
|
|
|
}
|
2016-10-17 10:55:17 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-02-28 10:51:00 +00:00
|
|
|
bool SkipToast() {
|
2016-10-17 10:55:17 +00:00
|
|
|
querySystemNotificationSettings();
|
|
|
|
|
2019-06-07 11:30:30 +00:00
|
|
|
if (UserNotificationState == QUNS_PRESENTATION_MODE
|
|
|
|
|| UserNotificationState == QUNS_RUNNING_D3D_FULL_SCREEN
|
|
|
|
//|| UserNotificationState == QUNS_BUSY
|
|
|
|
|| QuietHoursEnabled) {
|
2016-10-17 10:55:17 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-10-02 09:30:28 +00:00
|
|
|
} // namespace Notifications
|
|
|
|
} // namespace Platform
|