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"
|
2021-02-25 15:12:51 +00:00
|
|
|
#include "window/window_session_controller.h"
|
2021-10-05 08:09:15 +00:00
|
|
|
#include "base/platform/win/base_windows_co_task_mem.h"
|
|
|
|
#include "base/platform/win/base_windows_winrt.h"
|
2021-01-16 12:33:09 +00:00
|
|
|
#include "base/platform/base_platform_info.h"
|
2021-10-05 18:37:34 +00:00
|
|
|
#include "base/platform/win/wrl/wrl_module_h.h"
|
|
|
|
#include "base/qthelp_url.h"
|
2016-10-02 16:32:46 +00:00
|
|
|
#include "platform/win/windows_app_user_model_id.h"
|
2021-10-05 18:37:34 +00:00
|
|
|
#include "platform/win/windows_toast_activator.h"
|
2016-10-02 16:32:46 +00:00
|
|
|
#include "platform/win/windows_dlls.h"
|
2021-10-08 09:46:52 +00:00
|
|
|
#include "platform/win/specific_win.h"
|
2022-10-12 20:23:14 +00:00
|
|
|
#include "data/data_forum_topic.h"
|
2018-01-13 12:45:11 +00:00
|
|
|
#include "history/history.h"
|
2021-10-15 13:57:27 +00:00
|
|
|
#include "history/history_item.h"
|
2020-06-18 18:04:16 +00:00
|
|
|
#include "core/application.h"
|
|
|
|
#include "core/core_settings.h"
|
2020-06-19 12:59:31 +00:00
|
|
|
#include "main/main_session.h"
|
2016-10-02 16:32:46 +00:00
|
|
|
#include "mainwindow.h"
|
2021-04-23 04:00:17 +00:00
|
|
|
#include "windows_quiethours_h.h"
|
2021-10-05 12:52:46 +00:00
|
|
|
#include "styles/style_chat.h"
|
2016-10-02 16:32:46 +00:00
|
|
|
|
2021-09-01 15:33:53 +00:00
|
|
|
#include <QtCore/QOperatingSystemVersion>
|
|
|
|
|
2016-10-02 16:32:46 +00:00
|
|
|
#include <Shobjidl.h>
|
|
|
|
#include <shellapi.h>
|
2021-10-05 08:09:15 +00:00
|
|
|
#include <strsafe.h>
|
2016-10-02 16:32:46 +00:00
|
|
|
|
2020-05-09 18:07:18 +00:00
|
|
|
#ifndef __MINGW32__
|
2021-10-05 08:09:15 +00:00
|
|
|
#include <winrt/Windows.Foundation.h>
|
|
|
|
#include <winrt/Windows.Data.Xml.Dom.h>
|
|
|
|
#include <winrt/Windows.UI.Notifications.h>
|
2016-10-02 16:32:46 +00:00
|
|
|
|
|
|
|
HICON qt_pixmapToWinHICON(const QPixmap &);
|
|
|
|
|
2021-10-05 08:09:15 +00:00
|
|
|
using namespace winrt::Windows::UI::Notifications;
|
|
|
|
using namespace winrt::Windows::Data::Xml::Dom;
|
|
|
|
using namespace winrt::Windows::Foundation;
|
|
|
|
using winrt::com_ptr;
|
2020-05-09 18:07:18 +00:00
|
|
|
#endif // !__MINGW32__
|
2016-10-02 09:30:28 +00:00
|
|
|
|
|
|
|
namespace Platform {
|
|
|
|
namespace Notifications {
|
2020-05-09 18:07:18 +00:00
|
|
|
|
|
|
|
#ifndef __MINGW32__
|
2016-10-02 16:32:46 +00:00
|
|
|
namespace {
|
|
|
|
|
2021-10-06 15:24:30 +00:00
|
|
|
[[nodiscard]] std::wstring NotificationTemplate(
|
|
|
|
QString id,
|
|
|
|
Window::Notifications::Manager::DisplayOptions options) {
|
2021-10-05 18:37:34 +00:00
|
|
|
const auto wid = id.replace('&', "&").toStdWString();
|
2021-10-06 15:24:30 +00:00
|
|
|
const auto fastReply = LR"(
|
2021-10-05 12:52:46 +00:00
|
|
|
<input id="fastReply" type="text" placeHolderContent=""/>
|
|
|
|
<action
|
|
|
|
content="Send"
|
2021-10-05 18:37:34 +00:00
|
|
|
arguments="action=reply&)" + wid + LR"("
|
2021-10-05 12:52:46 +00:00
|
|
|
activationType="background"
|
|
|
|
imageUri=""
|
|
|
|
hint-inputId="fastReply"/>
|
2021-10-06 15:24:30 +00:00
|
|
|
)";
|
|
|
|
const auto markAsRead = LR"(
|
2021-10-05 18:37:34 +00:00
|
|
|
<action
|
|
|
|
content=""
|
|
|
|
arguments="action=mark&)" + wid + LR"("
|
|
|
|
activationType="background"/>
|
2021-10-05 12:52:46 +00:00
|
|
|
)";
|
2021-10-06 15:24:30 +00:00
|
|
|
const auto actions = (options.hideReplyButton ? L"" : fastReply)
|
|
|
|
+ (options.hideMarkAsRead ? L"" : markAsRead);
|
2021-10-05 18:37:34 +00:00
|
|
|
return LR"(
|
|
|
|
<toast launch="action=open&)" + wid + LR"(">
|
2021-10-05 12:52:46 +00:00
|
|
|
<visual>
|
|
|
|
<binding template="ToastGeneric">
|
|
|
|
<image placement="appLogoOverride" hint-crop="circle" src=""/>
|
|
|
|
<text hint-maxLines="1"></text>
|
|
|
|
<text></text>
|
|
|
|
<text></text>
|
|
|
|
</binding>
|
|
|
|
</visual>
|
2021-10-06 15:24:30 +00:00
|
|
|
)" + (actions.empty()
|
|
|
|
? L""
|
|
|
|
: (L"<actions>" + actions + L"</actions>")) + LR"(
|
2021-10-05 12:52:46 +00:00
|
|
|
<audio silent="true"/>
|
|
|
|
</toast>
|
|
|
|
)";
|
2021-10-05 18:37:34 +00:00
|
|
|
}
|
2021-10-05 12:52:46 +00:00
|
|
|
|
2016-10-02 16:32:46 +00:00
|
|
|
bool init() {
|
2021-01-16 12:33:09 +00:00
|
|
|
if (!IsWindows8OrGreater()) {
|
2016-10-02 16:32:46 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if ((Dlls::SetCurrentProcessExplicitAppUserModelID == nullptr)
|
2021-10-05 08:09:15 +00:00
|
|
|
|| !base::WinRT::Supported()) {
|
2016-10-02 16:32:46 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-10-05 18:37:34 +00:00
|
|
|
{
|
|
|
|
using namespace Microsoft::WRL;
|
2021-10-06 13:36:30 +00:00
|
|
|
const auto hr = Module<OutOfProc>::GetModule().RegisterObjects();
|
|
|
|
if (!SUCCEEDED(hr)) {
|
|
|
|
LOG(("App Error: Object registration failed."));
|
|
|
|
}
|
2021-10-05 18:37:34 +00:00
|
|
|
}
|
2016-10-02 16:32:46 +00:00
|
|
|
if (!AppUserModelId::validateShortcut()) {
|
2021-10-06 13:36:30 +00:00
|
|
|
LOG(("App Error: Shortcut validation failed."));
|
2016-10-02 16:32:46 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto appUserModelId = AppUserModelId::getId();
|
|
|
|
if (!SUCCEEDED(Dlls::SetCurrentProcessExplicitAppUserModelID(appUserModelId))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2021-10-05 08:09:15 +00:00
|
|
|
// Throws.
|
|
|
|
void SetNodeValueString(
|
|
|
|
const XmlDocument &xml,
|
|
|
|
const IXmlNode &node,
|
|
|
|
const std::wstring &text) {
|
|
|
|
node.AppendChild(xml.CreateTextNode(text).as<IXmlNode>());
|
2016-10-02 16:32:46 +00:00
|
|
|
}
|
|
|
|
|
2021-10-05 08:09:15 +00:00
|
|
|
// Throws.
|
|
|
|
void SetAudioSilent(const XmlDocument &toastXml) {
|
|
|
|
const auto nodeList = toastXml.GetElementsByTagName(L"audio");
|
|
|
|
if (const auto audioNode = nodeList.Item(0)) {
|
|
|
|
audioNode.as<IXmlElement>().SetAttribute(L"silent", L"true");
|
2016-10-02 16:32:46 +00:00
|
|
|
} else {
|
2021-10-05 08:09:15 +00:00
|
|
|
auto audioElement = toastXml.CreateElement(L"audio");
|
|
|
|
audioElement.SetAttribute(L"silent", L"true");
|
|
|
|
auto nodeList = toastXml.GetElementsByTagName(L"toast");
|
|
|
|
nodeList.Item(0).AppendChild(audioElement.as<IXmlNode>());
|
2016-10-02 16:32:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-05 08:09:15 +00:00
|
|
|
// Throws.
|
|
|
|
void SetImageSrc(const XmlDocument &toastXml, const std::wstring &path) {
|
|
|
|
const auto nodeList = toastXml.GetElementsByTagName(L"image");
|
|
|
|
const auto attributes = nodeList.Item(0).Attributes();
|
|
|
|
return SetNodeValueString(
|
|
|
|
toastXml,
|
|
|
|
attributes.GetNamedItem(L"src"),
|
|
|
|
L"file:///" + path);
|
2016-10-02 16:32:46 +00:00
|
|
|
}
|
|
|
|
|
2021-10-05 12:52:46 +00:00
|
|
|
// Throws.
|
|
|
|
void SetReplyIconSrc(const XmlDocument &toastXml, const std::wstring &path) {
|
|
|
|
const auto nodeList = toastXml.GetElementsByTagName(L"action");
|
2021-10-06 15:24:30 +00:00
|
|
|
const auto length = int(nodeList.Length());
|
|
|
|
for (auto i = 0; i != length; ++i) {
|
|
|
|
const auto attributes = nodeList.Item(i).Attributes();
|
|
|
|
if (const auto uri = attributes.GetNamedItem(L"imageUri")) {
|
|
|
|
return SetNodeValueString(toastXml, uri, L"file:///" + path);
|
|
|
|
}
|
|
|
|
}
|
2021-10-05 12:52:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Throws.
|
|
|
|
void SetReplyPlaceholder(
|
|
|
|
const XmlDocument &toastXml,
|
|
|
|
const std::wstring &placeholder) {
|
|
|
|
const auto nodeList = toastXml.GetElementsByTagName(L"input");
|
|
|
|
const auto attributes = nodeList.Item(0).Attributes();
|
|
|
|
return SetNodeValueString(
|
|
|
|
toastXml,
|
|
|
|
attributes.GetNamedItem(L"placeHolderContent"),
|
|
|
|
placeholder);
|
|
|
|
}
|
|
|
|
|
2021-10-05 18:37:34 +00:00
|
|
|
// Throws.
|
|
|
|
void SetAction(const XmlDocument &toastXml, const QString &id) {
|
|
|
|
auto nodeList = toastXml.GetElementsByTagName(L"toast");
|
|
|
|
if (const auto toast = nodeList.Item(0).try_as<XmlElement>()) {
|
|
|
|
toast.SetAttribute(L"launch", L"action=open&" + id.toStdWString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Throws.
|
|
|
|
void SetMarkAsReadText(
|
|
|
|
const XmlDocument &toastXml,
|
|
|
|
const std::wstring &text) {
|
|
|
|
const auto nodeList = toastXml.GetElementsByTagName(L"action");
|
2021-10-06 15:24:30 +00:00
|
|
|
const auto length = int(nodeList.Length());
|
|
|
|
for (auto i = 0; i != length; ++i) {
|
|
|
|
const auto attributes = nodeList.Item(i).Attributes();
|
|
|
|
if (!attributes.GetNamedItem(L"imageUri")) {
|
|
|
|
return SetNodeValueString(
|
|
|
|
toastXml,
|
|
|
|
attributes.GetNamedItem(L"content"),
|
|
|
|
text);
|
|
|
|
}
|
|
|
|
}
|
2021-10-05 18:37:34 +00:00
|
|
|
}
|
|
|
|
|
2017-03-04 19:36:59 +00:00
|
|
|
auto Checked = false;
|
|
|
|
auto InitSucceeded = false;
|
|
|
|
|
|
|
|
void Check() {
|
|
|
|
InitSucceeded = init();
|
|
|
|
}
|
|
|
|
|
2021-04-28 07:20:39 +00:00
|
|
|
bool QuietHoursEnabled = false;
|
|
|
|
DWORD QuietHoursValue = 0;
|
|
|
|
|
|
|
|
[[nodiscard]] bool UseQuietHoursRegistryEntry() {
|
|
|
|
static const bool result = [] {
|
2021-09-01 15:33:53 +00:00
|
|
|
const auto version = QOperatingSystemVersion::current();
|
|
|
|
|
2021-04-28 07:20:39 +00:00
|
|
|
// At build 17134 (Redstone 4) the "Quiet hours" was replaced
|
|
|
|
// by "Focus assist" and it looks like it doesn't use registry.
|
2021-09-01 15:33:53 +00:00
|
|
|
return (version.majorVersion() == 10)
|
|
|
|
&& (version.minorVersion() == 0)
|
|
|
|
&& (version.microVersion() < 17134);
|
2021-04-28 07:20:39 +00:00
|
|
|
}();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Thanks https://stackoverflow.com/questions/35600128/get-windows-quiet-hours-from-win32-or-c-sharp-api
|
|
|
|
void QueryQuietHours() {
|
|
|
|
if (!UseQuietHoursRegistryEntry()) {
|
|
|
|
// 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.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
LPCWSTR lpKeyName = L"Software\\Microsoft\\Windows\\CurrentVersion\\Notifications\\Settings";
|
|
|
|
LPCWSTR lpValueName = L"NOC_GLOBAL_SETTING_TOASTS_ENABLED";
|
|
|
|
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);
|
|
|
|
|
|
|
|
auto quietHoursEnabled = (result == ERROR_SUCCESS) && (value == 0);
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool FocusAssistBlocks = false;
|
|
|
|
|
|
|
|
// Thanks https://www.withinrafael.com/2019/09/19/determine-if-your-app-is-in-a-focus-assist-profiles-priority-list/
|
|
|
|
void QueryFocusAssist() {
|
2021-10-05 08:09:15 +00:00
|
|
|
const auto quietHoursSettings = base::WinRT::TryCreateInstance<
|
|
|
|
IQuietHoursSettings
|
|
|
|
>(CLSID_QuietHoursSettings, CLSCTX_LOCAL_SERVER);
|
|
|
|
if (!quietHoursSettings) {
|
2021-04-28 07:20:39 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-10-05 08:09:15 +00:00
|
|
|
auto profileId = base::CoTaskMemString();
|
|
|
|
auto hr = quietHoursSettings->get_UserSelectedProfile(profileId.put());
|
|
|
|
if (FAILED(hr) || !profileId) {
|
2021-04-28 07:20:39 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-10-05 08:09:15 +00:00
|
|
|
const auto profileName = QString::fromWCharArray(profileId.data());
|
2021-04-28 07:20:39 +00:00
|
|
|
if (profileName.endsWith(".alarmsonly", Qt::CaseInsensitive)) {
|
|
|
|
if (!FocusAssistBlocks) {
|
|
|
|
LOG(("Focus Assist: Alarms Only."));
|
|
|
|
FocusAssistBlocks = true;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
} else if (!profileName.endsWith(".priorityonly", Qt::CaseInsensitive)) {
|
|
|
|
if (!profileName.endsWith(".unrestricted", Qt::CaseInsensitive)) {
|
|
|
|
LOG(("Focus Assist Warning: Unknown profile '%1'"
|
|
|
|
).arg(profileName));
|
|
|
|
}
|
|
|
|
if (FocusAssistBlocks) {
|
|
|
|
LOG(("Focus Assist: Unrestricted."));
|
|
|
|
FocusAssistBlocks = false;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto appUserModelId = std::wstring(AppUserModelId::getId());
|
|
|
|
auto blocked = true;
|
|
|
|
const auto guard = gsl::finally([&] {
|
|
|
|
if (FocusAssistBlocks != blocked) {
|
|
|
|
LOG(("Focus Assist: %1, AppUserModelId: %2, Blocks: %3"
|
|
|
|
).arg(profileName
|
|
|
|
).arg(QString::fromStdWString(appUserModelId)
|
|
|
|
).arg(Logs::b(blocked)));
|
|
|
|
FocusAssistBlocks = blocked;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-10-05 08:09:15 +00:00
|
|
|
com_ptr<IQuietHoursProfile> profile;
|
|
|
|
hr = quietHoursSettings->GetProfile(profileId.data(), profile.put());
|
|
|
|
if (FAILED(hr) || !profile) {
|
2021-04-28 07:20:39 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-10-05 08:09:15 +00:00
|
|
|
auto apps = base::CoTaskMemStringArray();
|
|
|
|
hr = profile->GetAllowedApps(apps.put_size(), apps.put());
|
|
|
|
if (FAILED(hr) || !apps) {
|
2021-04-28 07:20:39 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-10-05 08:09:15 +00:00
|
|
|
for (const auto &app : apps) {
|
|
|
|
if (app && app.data() == appUserModelId) {
|
2021-04-28 07:20:39 +00:00
|
|
|
blocked = false;
|
2021-10-05 08:09:15 +00:00
|
|
|
break;
|
2021-04-28 07:20:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-05 08:09:15 +00:00
|
|
|
QUERY_USER_NOTIFICATION_STATE UserNotificationState
|
|
|
|
= QUNS_ACCEPTS_NOTIFICATIONS;
|
2021-04-28 07:20:39 +00:00
|
|
|
|
|
|
|
void QueryUserNotificationState() {
|
|
|
|
if (Dlls::SHQueryUserNotificationState != nullptr) {
|
|
|
|
QUERY_USER_NOTIFICATION_STATE state;
|
|
|
|
if (SUCCEEDED(Dlls::SHQueryUserNotificationState(&state))) {
|
|
|
|
UserNotificationState = state;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static constexpr auto kQuerySettingsEachMs = 1000;
|
|
|
|
crl::time LastSettingsQueryMs = 0;
|
|
|
|
|
|
|
|
void QuerySystemNotificationSettings() {
|
|
|
|
auto ms = crl::now();
|
|
|
|
if (LastSettingsQueryMs > 0 && ms <= LastSettingsQueryMs + kQuerySettingsEachMs) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
LastSettingsQueryMs = ms;
|
|
|
|
QueryQuietHours();
|
|
|
|
QueryFocusAssist();
|
|
|
|
QueryUserNotificationState();
|
|
|
|
}
|
|
|
|
|
2016-10-02 16:32:46 +00:00
|
|
|
} // namespace
|
2020-05-09 18:07:18 +00:00
|
|
|
#endif // !__MINGW32__
|
2016-10-02 09:30:28 +00:00
|
|
|
|
2021-04-28 07:20:39 +00:00
|
|
|
bool SkipAudioForCustom() {
|
|
|
|
QuerySystemNotificationSettings();
|
|
|
|
|
|
|
|
return (UserNotificationState == QUNS_NOT_PRESENT)
|
|
|
|
|| (UserNotificationState == QUNS_PRESENTATION_MODE)
|
2021-05-27 14:47:51 +00:00
|
|
|
|| Core::App().screenIsLocked();
|
2021-04-28 07:20:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool SkipToastForCustom() {
|
|
|
|
QuerySystemNotificationSettings();
|
|
|
|
|
|
|
|
return (UserNotificationState == QUNS_PRESENTATION_MODE)
|
|
|
|
|| (UserNotificationState == QUNS_RUNNING_D3D_FULL_SCREEN);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool SkipFlashBounceForCustom() {
|
|
|
|
return SkipToastForCustom();
|
|
|
|
}
|
|
|
|
|
2017-03-04 19:36:59 +00:00
|
|
|
bool Supported() {
|
2020-05-09 18:07:18 +00:00
|
|
|
#ifndef __MINGW32__
|
2017-03-04 19:36:59 +00:00
|
|
|
if (!Checked) {
|
|
|
|
Checked = true;
|
|
|
|
Check();
|
2016-10-02 16:32:46 +00:00
|
|
|
}
|
2017-03-04 19:36:59 +00:00
|
|
|
return InitSucceeded;
|
2020-05-09 18:07:18 +00:00
|
|
|
#endif // !__MINGW32__
|
|
|
|
|
|
|
|
return false;
|
2016-10-02 09:30:28 +00:00
|
|
|
}
|
|
|
|
|
2021-01-13 09:56:49 +00:00
|
|
|
bool Enforced() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-01-24 23:16:32 +00:00
|
|
|
bool ByDefault() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-01-21 10:18:40 +00:00
|
|
|
void Create(Window::Notifications::System *system) {
|
2020-05-09 18:07:18 +00:00
|
|
|
#ifndef __MINGW32__
|
2020-06-18 18:04:16 +00:00
|
|
|
if (Core::App().settings().nativeNotifications() && Supported()) {
|
2017-03-04 19:36:59 +00:00
|
|
|
auto result = std::make_unique<Manager>(system);
|
|
|
|
if (result->init()) {
|
2021-01-21 10:18:40 +00:00
|
|
|
system->setManager(std::move(result));
|
|
|
|
return;
|
2017-03-04 19:36:59 +00:00
|
|
|
}
|
2016-10-02 09:30:28 +00:00
|
|
|
}
|
2020-05-09 18:07:18 +00:00
|
|
|
#endif // !__MINGW32__
|
2021-01-21 10:18:40 +00:00
|
|
|
system->setManager(nullptr);
|
2016-10-02 09:30:28 +00:00
|
|
|
}
|
|
|
|
|
2020-05-09 18:07:18 +00:00
|
|
|
#ifndef __MINGW32__
|
2017-03-04 19:36:59 +00:00
|
|
|
class Manager::Private {
|
2016-10-02 16:32:46 +00:00
|
|
|
public:
|
2022-12-05 12:18:10 +00:00
|
|
|
explicit Private(Manager *instance);
|
2017-03-04 19:36:59 +00:00
|
|
|
bool init();
|
2017-01-07 11:55:05 +00:00
|
|
|
|
2019-08-28 14:24:12 +00:00
|
|
|
bool showNotification(
|
|
|
|
not_null<PeerData*> peer,
|
2022-10-12 20:23:14 +00:00
|
|
|
MsgId topicRootId,
|
2022-12-05 12:18:10 +00:00
|
|
|
Ui::PeerUserpicView &userpicView,
|
2019-08-28 14:24:12 +00:00
|
|
|
MsgId msgId,
|
|
|
|
const QString &title,
|
|
|
|
const QString &subtitle,
|
|
|
|
const QString &msg,
|
2021-10-06 15:24:30 +00:00
|
|
|
DisplayOptions options);
|
2016-10-02 16:32:46 +00:00
|
|
|
void clearAll();
|
2021-10-15 13:57:27 +00:00
|
|
|
void clearFromItem(not_null<HistoryItem*> item);
|
2022-10-12 20:23:14 +00:00
|
|
|
void clearFromTopic(not_null<Data::ForumTopic*> topic);
|
2019-08-30 14:06:21 +00:00
|
|
|
void clearFromHistory(not_null<History*> history);
|
2020-06-19 12:59:31 +00:00
|
|
|
void clearFromSession(not_null<Main::Session*> session);
|
|
|
|
void beforeNotificationActivated(NotificationId id);
|
2021-02-25 15:12:51 +00:00
|
|
|
void afterNotificationActivated(
|
|
|
|
NotificationId id,
|
|
|
|
not_null<Window::SessionController*> window);
|
2020-06-19 12:59:31 +00:00
|
|
|
void clearNotification(NotificationId id);
|
2016-10-02 16:32:46 +00:00
|
|
|
|
2021-10-05 18:37:34 +00:00
|
|
|
void handleActivation(const ToastActivation &activation);
|
|
|
|
|
2017-03-04 19:36:59 +00:00
|
|
|
~Private();
|
2016-10-02 16:32:46 +00:00
|
|
|
|
|
|
|
private:
|
2021-10-05 08:09:15 +00:00
|
|
|
bool showNotificationInTryCatch(
|
|
|
|
not_null<PeerData*> peer,
|
2022-10-12 20:23:14 +00:00
|
|
|
MsgId topicRootId,
|
2022-12-05 12:18:10 +00:00
|
|
|
Ui::PeerUserpicView &userpicView,
|
2021-10-05 08:09:15 +00:00
|
|
|
MsgId msgId,
|
|
|
|
const QString &title,
|
|
|
|
const QString &subtitle,
|
|
|
|
const QString &msg,
|
2021-10-06 15:24:30 +00:00
|
|
|
DisplayOptions options);
|
2022-06-22 16:07:17 +00:00
|
|
|
void tryHide(const ToastNotification ¬ification);
|
2021-10-05 12:52:46 +00:00
|
|
|
[[nodiscard]] std::wstring ensureSendButtonIcon();
|
2021-10-05 08:09:15 +00:00
|
|
|
|
2016-10-03 08:56:03 +00:00
|
|
|
Window::Notifications::CachedUserpics _cachedUserpics;
|
2021-10-05 12:52:46 +00:00
|
|
|
std::wstring _sendButtonIconPath;
|
2016-10-02 16:32:46 +00:00
|
|
|
|
2017-03-04 19:36:59 +00:00
|
|
|
std::shared_ptr<Manager*> _guarded;
|
2021-10-05 08:09:15 +00:00
|
|
|
ToastNotifier _notifier = nullptr;
|
2017-03-04 19:36:59 +00:00
|
|
|
|
2021-10-05 08:09:15 +00:00
|
|
|
base::flat_map<
|
2022-10-12 20:23:14 +00:00
|
|
|
ContextId,
|
2021-10-05 08:09:15 +00:00
|
|
|
base::flat_map<MsgId, ToastNotification>> _notifications;
|
2021-10-05 18:37:34 +00:00
|
|
|
rpl::lifetime _lifetime;
|
2017-03-04 19:36:59 +00:00
|
|
|
|
2016-10-02 16:32:46 +00:00
|
|
|
};
|
|
|
|
|
2022-12-05 12:18:10 +00:00
|
|
|
Manager::Private::Private(Manager *instance)
|
|
|
|
: _guarded(std::make_shared<Manager*>(instance)) {
|
2021-10-05 18:37:34 +00:00
|
|
|
ToastActivations(
|
|
|
|
) | rpl::start_with_next([=](const ToastActivation &activation) {
|
|
|
|
handleActivation(activation);
|
|
|
|
}, _lifetime);
|
2017-03-04 19:36:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Manager::Private::init() {
|
2021-10-05 08:09:15 +00:00
|
|
|
return base::WinRT::Try([&] {
|
|
|
|
_notifier = ToastNotificationManager::CreateToastNotifier(
|
|
|
|
AppUserModelId::getId());
|
|
|
|
});
|
2017-03-04 19:36:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Manager::Private::~Private() {
|
|
|
|
clearAll();
|
|
|
|
|
2016-10-02 16:32:46 +00:00
|
|
|
_notifications.clear();
|
2021-10-05 08:09:15 +00:00
|
|
|
_notifier = nullptr;
|
2016-10-02 16:32:46 +00:00
|
|
|
}
|
|
|
|
|
2017-03-04 19:36:59 +00:00
|
|
|
void Manager::Private::clearAll() {
|
2020-06-30 07:52:59 +00:00
|
|
|
if (!_notifier) {
|
|
|
|
return;
|
|
|
|
}
|
2016-10-02 16:32:46 +00:00
|
|
|
|
2020-06-19 12:59:31 +00:00
|
|
|
for (const auto &[key, notifications] : base::take(_notifications)) {
|
|
|
|
for (const auto &[msgId, notification] : notifications) {
|
2022-06-22 16:07:17 +00:00
|
|
|
tryHide(notification);
|
2016-10-02 16:32:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-15 13:57:27 +00:00
|
|
|
void Manager::Private::clearFromItem(not_null<HistoryItem*> item) {
|
|
|
|
if (!_notifier) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-10-12 20:23:14 +00:00
|
|
|
auto i = _notifications.find(ContextId{
|
2021-10-15 13:57:27 +00:00
|
|
|
.sessionId = item->history()->session().uniqueId(),
|
2022-10-12 20:23:14 +00:00
|
|
|
.peerId = item->history()->peer->id,
|
|
|
|
.topicRootId = item->topicRootId(),
|
2021-10-15 13:57:27 +00:00
|
|
|
});
|
|
|
|
if (i == _notifications.cend()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto j = i->second.find(item->id);
|
|
|
|
if (j == end(i->second)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto taken = std::exchange(j->second, nullptr);
|
|
|
|
i->second.erase(j);
|
|
|
|
if (i->second.empty()) {
|
|
|
|
_notifications.erase(i);
|
|
|
|
}
|
2022-06-22 16:07:17 +00:00
|
|
|
tryHide(taken);
|
2021-10-15 13:57:27 +00:00
|
|
|
}
|
|
|
|
|
2022-10-12 20:23:14 +00:00
|
|
|
void Manager::Private::clearFromTopic(not_null<Data::ForumTopic*> topic) {
|
2020-06-30 07:52:59 +00:00
|
|
|
if (!_notifier) {
|
|
|
|
return;
|
|
|
|
}
|
2016-10-02 16:32:46 +00:00
|
|
|
|
2022-10-12 20:23:14 +00:00
|
|
|
const auto i = _notifications.find(ContextId{
|
|
|
|
.sessionId = topic->session().uniqueId(),
|
|
|
|
.peerId = topic->history()->peer->id,
|
|
|
|
.topicRootId = topic->rootId(),
|
2020-06-19 12:59:31 +00:00
|
|
|
});
|
2016-10-02 16:32:46 +00:00
|
|
|
if (i != _notifications.cend()) {
|
2021-10-15 13:57:27 +00:00
|
|
|
const auto temp = base::take(i->second);
|
2016-10-02 16:32:46 +00:00
|
|
|
_notifications.erase(i);
|
|
|
|
|
2020-06-19 12:59:31 +00:00
|
|
|
for (const auto &[msgId, notification] : temp) {
|
2022-06-22 16:07:17 +00:00
|
|
|
tryHide(notification);
|
2016-10-02 16:32:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-12 20:23:14 +00:00
|
|
|
void Manager::Private::clearFromHistory(not_null<History*> history) {
|
|
|
|
if (!_notifier) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto sessionId = history->session().uniqueId();
|
|
|
|
const auto peerId = history->peer->id;
|
|
|
|
auto i = _notifications.lower_bound(ContextId{
|
|
|
|
.sessionId = sessionId,
|
|
|
|
.peerId = peerId,
|
|
|
|
});
|
|
|
|
while (i != _notifications.cend()
|
|
|
|
&& i->first.sessionId == sessionId
|
|
|
|
&& i->first.peerId == peerId) {
|
|
|
|
const auto temp = base::take(i->second);
|
|
|
|
i = _notifications.erase(i);
|
|
|
|
|
|
|
|
for (const auto &[msgId, notification] : temp) {
|
|
|
|
tryHide(notification);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-19 12:59:31 +00:00
|
|
|
void Manager::Private::clearFromSession(not_null<Main::Session*> session) {
|
2020-06-30 07:52:59 +00:00
|
|
|
if (!_notifier) {
|
|
|
|
return;
|
|
|
|
}
|
2020-06-19 12:59:31 +00:00
|
|
|
|
2020-06-24 09:05:56 +00:00
|
|
|
const auto sessionId = session->uniqueId();
|
2022-10-12 20:23:14 +00:00
|
|
|
auto i = _notifications.lower_bound(ContextId{
|
|
|
|
.sessionId = sessionId,
|
|
|
|
});
|
|
|
|
while (i != _notifications.cend() && i->first.sessionId == sessionId) {
|
2020-06-30 07:52:59 +00:00
|
|
|
const auto temp = base::take(i->second);
|
2022-10-12 20:23:14 +00:00
|
|
|
i = _notifications.erase(i);
|
2020-06-19 12:59:31 +00:00
|
|
|
|
2020-06-30 07:52:59 +00:00
|
|
|
for (const auto &[msgId, notification] : temp) {
|
2022-06-22 16:07:17 +00:00
|
|
|
tryHide(notification);
|
2020-06-19 12:59:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Manager::Private::beforeNotificationActivated(NotificationId id) {
|
|
|
|
clearNotification(id);
|
2016-10-02 16:32:46 +00:00
|
|
|
}
|
|
|
|
|
2021-02-25 15:12:51 +00:00
|
|
|
void Manager::Private::afterNotificationActivated(
|
|
|
|
NotificationId id,
|
|
|
|
not_null<Window::SessionController*> window) {
|
|
|
|
SetForegroundWindow(window->widget()->psHwnd());
|
2016-10-02 16:32:46 +00:00
|
|
|
}
|
|
|
|
|
2020-06-19 12:59:31 +00:00
|
|
|
void Manager::Private::clearNotification(NotificationId id) {
|
2022-10-12 20:23:14 +00:00
|
|
|
auto i = _notifications.find(id.contextId);
|
2016-10-02 16:32:46 +00:00
|
|
|
if (i != _notifications.cend()) {
|
2020-06-19 12:59:31 +00:00
|
|
|
i->second.remove(id.msgId);
|
|
|
|
if (i->second.empty()) {
|
2016-10-02 16:32:46 +00:00
|
|
|
_notifications.erase(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-05 18:37:34 +00:00
|
|
|
void Manager::Private::handleActivation(const ToastActivation &activation) {
|
|
|
|
const auto parsed = qthelp::url_parse_params(activation.args);
|
2021-10-08 09:46:52 +00:00
|
|
|
const auto pid = parsed.value("pid").toULong();
|
|
|
|
const auto my = GetCurrentProcessId();
|
|
|
|
if (pid != my) {
|
|
|
|
DEBUG_LOG(("Toast Info: "
|
|
|
|
"Got activation \"%1\", my %2, activating %3."
|
|
|
|
).arg(activation.args
|
|
|
|
).arg(my
|
|
|
|
).arg(pid));
|
|
|
|
psActivateProcess(pid);
|
|
|
|
return;
|
|
|
|
}
|
2021-10-05 18:37:34 +00:00
|
|
|
const auto action = parsed.value("action");
|
|
|
|
const auto id = NotificationId{
|
2022-10-12 20:23:14 +00:00
|
|
|
.contextId = ContextId{
|
2021-10-08 09:46:52 +00:00
|
|
|
.sessionId = parsed.value("session").toULongLong(),
|
|
|
|
.peerId = PeerId(parsed.value("peer").toULongLong()),
|
2022-10-12 20:23:14 +00:00
|
|
|
.topicRootId = MsgId(parsed.value("topic").toLongLong())
|
2021-10-05 18:37:34 +00:00
|
|
|
},
|
2021-10-08 09:46:52 +00:00
|
|
|
.msgId = MsgId(parsed.value("msg").toLongLong()),
|
2021-10-05 18:37:34 +00:00
|
|
|
};
|
2022-10-12 20:23:14 +00:00
|
|
|
if (!id.contextId.sessionId || !id.contextId.peerId || !id.msgId) {
|
2021-10-08 09:46:52 +00:00
|
|
|
DEBUG_LOG(("Toast Info: Got activation \"%1\", my %1, skipping."
|
|
|
|
).arg(activation.args
|
|
|
|
).arg(pid));
|
2021-10-05 18:37:34 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-10-08 09:46:52 +00:00
|
|
|
DEBUG_LOG(("Toast Info: Got activation \"%1\", my %1, handling."
|
|
|
|
).arg(activation.args
|
|
|
|
).arg(pid));
|
2021-10-05 18:37:34 +00:00
|
|
|
auto text = TextWithTags();
|
|
|
|
for (const auto &entry : activation.input) {
|
|
|
|
if (entry.key == "fastReply") {
|
|
|
|
text.text = entry.value;
|
|
|
|
}
|
|
|
|
}
|
2022-10-12 20:23:14 +00:00
|
|
|
const auto i = _notifications.find(id.contextId);
|
2021-10-05 18:37:34 +00:00
|
|
|
if (i == _notifications.cend() || !i->second.contains(id.msgId)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const auto manager = *_guarded;
|
|
|
|
if (action == "reply") {
|
|
|
|
manager->notificationReplied(id, text);
|
|
|
|
} else if (action == "mark") {
|
|
|
|
manager->notificationReplied(id, TextWithTags());
|
|
|
|
} else {
|
|
|
|
manager->notificationActivated(id, text);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-28 14:24:12 +00:00
|
|
|
bool Manager::Private::showNotification(
|
|
|
|
not_null<PeerData*> peer,
|
2022-10-12 20:23:14 +00:00
|
|
|
MsgId topicRootId,
|
2022-12-05 12:18:10 +00:00
|
|
|
Ui::PeerUserpicView &userpicView,
|
2019-08-28 14:24:12 +00:00
|
|
|
MsgId msgId,
|
|
|
|
const QString &title,
|
|
|
|
const QString &subtitle,
|
|
|
|
const QString &msg,
|
2021-10-06 15:24:30 +00:00
|
|
|
DisplayOptions options) {
|
2021-10-05 08:09:15 +00:00
|
|
|
if (!_notifier) {
|
2020-05-28 14:32:10 +00:00
|
|
|
return false;
|
|
|
|
}
|
2016-10-02 16:32:46 +00:00
|
|
|
|
2021-10-05 08:09:15 +00:00
|
|
|
return base::WinRT::Try([&] {
|
|
|
|
return showNotificationInTryCatch(
|
|
|
|
peer,
|
2022-10-12 20:23:14 +00:00
|
|
|
topicRootId,
|
2021-10-05 08:09:15 +00:00
|
|
|
userpicView,
|
|
|
|
msgId,
|
|
|
|
title,
|
|
|
|
subtitle,
|
|
|
|
msg,
|
2021-10-06 15:24:30 +00:00
|
|
|
options);
|
2021-10-05 08:09:15 +00:00
|
|
|
}).value_or(false);
|
|
|
|
}
|
2016-10-02 16:32:46 +00:00
|
|
|
|
2021-10-05 12:52:46 +00:00
|
|
|
std::wstring Manager::Private::ensureSendButtonIcon() {
|
|
|
|
if (_sendButtonIconPath.empty()) {
|
|
|
|
const auto path = cWorkingDir() + u"tdata/temp/fast_reply.png"_q;
|
|
|
|
st::historySendIcon.instance(Qt::white, 300).save(path, "PNG");
|
|
|
|
_sendButtonIconPath = path.toStdWString();
|
|
|
|
}
|
|
|
|
return _sendButtonIconPath;
|
|
|
|
}
|
|
|
|
|
2021-10-05 08:09:15 +00:00
|
|
|
bool Manager::Private::showNotificationInTryCatch(
|
|
|
|
not_null<PeerData*> peer,
|
2022-10-12 20:23:14 +00:00
|
|
|
MsgId topicRootId,
|
2022-12-05 12:18:10 +00:00
|
|
|
Ui::PeerUserpicView &userpicView,
|
2021-10-05 08:09:15 +00:00
|
|
|
MsgId msgId,
|
|
|
|
const QString &title,
|
|
|
|
const QString &subtitle,
|
|
|
|
const QString &msg,
|
2021-10-06 15:24:30 +00:00
|
|
|
DisplayOptions options) {
|
2021-10-05 08:09:15 +00:00
|
|
|
const auto withSubtitle = !subtitle.isEmpty();
|
2021-10-05 12:52:46 +00:00
|
|
|
auto toastXml = XmlDocument();
|
2021-10-05 18:37:34 +00:00
|
|
|
|
2022-10-12 20:23:14 +00:00
|
|
|
const auto key = ContextId{
|
2021-10-05 18:37:34 +00:00
|
|
|
.sessionId = peer->session().uniqueId(),
|
|
|
|
.peerId = peer->id,
|
2022-10-12 20:23:14 +00:00
|
|
|
.topicRootId = topicRootId,
|
2021-10-05 18:37:34 +00:00
|
|
|
};
|
|
|
|
const auto notificationId = NotificationId{
|
2022-10-12 20:23:14 +00:00
|
|
|
.contextId = key,
|
2021-10-05 18:37:34 +00:00
|
|
|
.msgId = msgId
|
|
|
|
};
|
2022-10-12 20:23:14 +00:00
|
|
|
const auto idString = u"pid=%1&session=%2&peer=%3&topic=%4&msg=%5"_q
|
2021-10-08 09:46:52 +00:00
|
|
|
.arg(GetCurrentProcessId())
|
2021-10-05 18:37:34 +00:00
|
|
|
.arg(key.sessionId)
|
|
|
|
.arg(key.peerId.value)
|
2022-10-12 20:23:14 +00:00
|
|
|
.arg(topicRootId.bare)
|
2021-10-05 18:37:34 +00:00
|
|
|
.arg(msgId.bare);
|
|
|
|
|
2021-10-05 12:52:46 +00:00
|
|
|
const auto modern = Platform::IsWindows10OrGreater();
|
|
|
|
if (modern) {
|
2021-10-06 15:24:30 +00:00
|
|
|
toastXml.LoadXml(NotificationTemplate(idString, options));
|
2021-10-05 12:52:46 +00:00
|
|
|
} else {
|
|
|
|
toastXml = ToastNotificationManager::GetTemplateContent(
|
|
|
|
(withSubtitle
|
|
|
|
? ToastTemplateType::ToastImageAndText04
|
|
|
|
: ToastTemplateType::ToastImageAndText02));
|
|
|
|
SetAudioSilent(toastXml);
|
2021-10-05 18:37:34 +00:00
|
|
|
SetAction(toastXml, idString);
|
2021-10-05 12:52:46 +00:00
|
|
|
}
|
2016-10-02 16:32:46 +00:00
|
|
|
|
2021-10-06 15:24:30 +00:00
|
|
|
const auto userpicKey = options.hideNameAndPhoto
|
2019-03-22 13:43:34 +00:00
|
|
|
? InMemoryKey()
|
2020-05-29 16:55:01 +00:00
|
|
|
: peer->userpicUniqueKey(userpicView);
|
2021-10-05 12:52:46 +00:00
|
|
|
const auto userpicPath = _cachedUserpics.get(
|
|
|
|
userpicKey,
|
|
|
|
peer,
|
|
|
|
userpicView);
|
|
|
|
const auto userpicPathWide = QDir::toNativeSeparators(
|
|
|
|
userpicPath).toStdWString();
|
2021-10-06 15:24:30 +00:00
|
|
|
if (modern && !options.hideReplyButton) {
|
2021-10-05 12:52:46 +00:00
|
|
|
SetReplyIconSrc(toastXml, ensureSendButtonIcon());
|
|
|
|
SetReplyPlaceholder(
|
|
|
|
toastXml,
|
|
|
|
tr::lng_message_ph(tr::now).toStdWString());
|
2021-10-06 15:24:30 +00:00
|
|
|
}
|
|
|
|
if (modern && !options.hideMarkAsRead) {
|
2021-10-05 18:37:34 +00:00
|
|
|
SetMarkAsReadText(
|
|
|
|
toastXml,
|
|
|
|
tr::lng_context_mark_read(tr::now).toStdWString());
|
2021-10-05 12:52:46 +00:00
|
|
|
}
|
2016-10-02 16:32:46 +00:00
|
|
|
|
2021-10-05 08:09:15 +00:00
|
|
|
SetImageSrc(toastXml, userpicPathWide);
|
2016-10-02 16:32:46 +00:00
|
|
|
|
2021-10-05 08:09:15 +00:00
|
|
|
const auto nodeList = toastXml.GetElementsByTagName(L"text");
|
|
|
|
if (nodeList.Length() < (withSubtitle ? 3U : 2U)) {
|
|
|
|
return false;
|
2016-10-02 16:32:46 +00:00
|
|
|
}
|
|
|
|
|
2021-10-05 08:09:15 +00:00
|
|
|
SetNodeValueString(toastXml, nodeList.Item(0), title.toStdWString());
|
|
|
|
if (withSubtitle) {
|
|
|
|
SetNodeValueString(
|
|
|
|
toastXml,
|
|
|
|
nodeList.Item(1),
|
|
|
|
subtitle.toStdWString());
|
|
|
|
}
|
|
|
|
SetNodeValueString(
|
|
|
|
toastXml,
|
|
|
|
nodeList.Item(withSubtitle ? 2 : 1),
|
|
|
|
msg.toStdWString());
|
|
|
|
|
|
|
|
const auto weak = std::weak_ptr(_guarded);
|
|
|
|
const auto performOnMainQueue = [=](FnMut<void(Manager *manager)> task) {
|
|
|
|
crl::on_main(weak, [=, task = std::move(task)]() mutable {
|
|
|
|
task(*weak.lock());
|
|
|
|
});
|
|
|
|
};
|
2016-10-02 16:32:46 +00:00
|
|
|
|
2021-10-05 08:09:15 +00:00
|
|
|
auto toast = ToastNotification(toastXml);
|
|
|
|
const auto token1 = toast.Activated([=](
|
|
|
|
const ToastNotification &sender,
|
2021-10-05 12:52:46 +00:00
|
|
|
const winrt::Windows::Foundation::IInspectable &object) {
|
2021-10-05 18:37:34 +00:00
|
|
|
auto activation = ToastActivation();
|
|
|
|
const auto string = &ToastActivation::String;
|
2021-10-05 12:52:46 +00:00
|
|
|
if (const auto args = object.try_as<ToastActivatedEventArgs>()) {
|
2021-10-05 18:37:34 +00:00
|
|
|
activation.args = string(args.Arguments().c_str());
|
2021-10-06 13:36:30 +00:00
|
|
|
const auto args2 = args.try_as<IToastActivatedEventArgs2>();
|
|
|
|
if (!args2 && activation.args.startsWith("action=reply&")) {
|
|
|
|
LOG(("WinRT Error: "
|
|
|
|
"FastReply without IToastActivatedEventArgs2 support."));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto input = args2 ? args2.UserInput() : nullptr;
|
|
|
|
const auto reply = input
|
|
|
|
? input.TryLookup(L"fastReply")
|
|
|
|
: nullptr;
|
|
|
|
const auto data = reply
|
|
|
|
? reply.try_as<IReference<winrt::hstring>>()
|
|
|
|
: nullptr;
|
2021-10-05 18:37:34 +00:00
|
|
|
if (data) {
|
|
|
|
activation.input.push_back({
|
|
|
|
.key = u"fastReply"_q,
|
|
|
|
.value = string(data.GetString().c_str()),
|
2021-10-05 12:52:46 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
} else {
|
2021-10-05 18:37:34 +00:00
|
|
|
activation.args = "action=open&" + idString;
|
2021-10-05 12:52:46 +00:00
|
|
|
}
|
2021-10-05 18:37:34 +00:00
|
|
|
crl::on_main([=, activation = std::move(activation)]() mutable {
|
|
|
|
if (const auto strong = weak.lock()) {
|
|
|
|
(*strong)->handleActivation(activation);
|
|
|
|
}
|
|
|
|
});
|
2021-10-05 08:09:15 +00:00
|
|
|
});
|
|
|
|
const auto token2 = toast.Dismissed([=](
|
|
|
|
const ToastNotification &sender,
|
|
|
|
const ToastDismissedEventArgs &args) {
|
2021-10-05 12:52:46 +00:00
|
|
|
const auto reason = args.Reason();
|
|
|
|
switch (reason) {
|
|
|
|
case ToastDismissalReason::ApplicationHidden:
|
|
|
|
case ToastDismissalReason::TimedOut: // Went to Action Center.
|
|
|
|
break;
|
|
|
|
case ToastDismissalReason::UserCanceled:
|
|
|
|
default:
|
|
|
|
performOnMainQueue([notificationId](Manager *manager) {
|
|
|
|
manager->clearNotification(notificationId);
|
|
|
|
});
|
|
|
|
break;
|
|
|
|
}
|
2021-10-05 08:09:15 +00:00
|
|
|
});
|
|
|
|
const auto token3 = toast.Failed([=](
|
2021-10-06 13:36:30 +00:00
|
|
|
const ToastNotification &sender,
|
2021-10-05 08:09:15 +00:00
|
|
|
const ToastFailedEventArgs &args) {
|
|
|
|
performOnMainQueue([notificationId](Manager *manager) {
|
|
|
|
manager->clearNotification(notificationId);
|
|
|
|
});
|
|
|
|
});
|
2016-10-02 16:32:46 +00:00
|
|
|
|
2020-06-19 12:59:31 +00:00
|
|
|
auto i = _notifications.find(key);
|
2016-10-02 16:32:46 +00:00
|
|
|
if (i != _notifications.cend()) {
|
2020-06-19 12:59:31 +00:00
|
|
|
auto j = i->second.find(msgId);
|
|
|
|
if (j != i->second.end()) {
|
2021-10-05 08:09:15 +00:00
|
|
|
const auto existing = j->second;
|
2020-06-19 12:59:31 +00:00
|
|
|
i->second.erase(j);
|
2022-06-22 16:07:17 +00:00
|
|
|
tryHide(existing);
|
2020-06-19 12:59:31 +00:00
|
|
|
i = _notifications.find(key);
|
2016-10-02 16:32:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (i == _notifications.cend()) {
|
2020-06-19 12:59:31 +00:00
|
|
|
i = _notifications.emplace(
|
|
|
|
key,
|
2021-10-05 08:09:15 +00:00
|
|
|
base::flat_map<MsgId, ToastNotification>()).first;
|
2016-10-02 16:32:46 +00:00
|
|
|
}
|
2021-10-05 08:09:15 +00:00
|
|
|
if (!base::WinRT::Try([&] { _notifier.Show(toast); })) {
|
2020-06-19 12:59:31 +00:00
|
|
|
i = _notifications.find(key);
|
|
|
|
if (i != _notifications.cend() && i->second.empty()) {
|
|
|
|
_notifications.erase(i);
|
|
|
|
}
|
2016-10-02 16:32:46 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-06-19 12:59:31 +00:00
|
|
|
i->second.emplace(msgId, toast);
|
2016-10-02 16:32:46 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-06-22 16:07:17 +00:00
|
|
|
void Manager::Private::tryHide(const ToastNotification ¬ification) {
|
|
|
|
base::WinRT::Try([&] {
|
|
|
|
_notifier.Hide(notification);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Manager::Manager(Window::Notifications::System *system)
|
|
|
|
: NativeManager(system)
|
2022-12-05 12:18:10 +00:00
|
|
|
, _private(std::make_unique<Private>(this)) {
|
2017-03-04 19:36:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Manager::init() {
|
|
|
|
return _private->init();
|
2016-10-02 16:32:46 +00:00
|
|
|
}
|
|
|
|
|
2020-06-19 12:59:31 +00:00
|
|
|
void Manager::clearNotification(NotificationId id) {
|
|
|
|
_private->clearNotification(id);
|
2016-10-02 16:32:46 +00:00
|
|
|
}
|
|
|
|
|
2021-10-05 18:37:34 +00:00
|
|
|
void Manager::handleActivation(const ToastActivation &activation) {
|
|
|
|
_private->handleActivation(activation);
|
|
|
|
}
|
|
|
|
|
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,
|
2022-10-12 20:23:14 +00:00
|
|
|
MsgId topicRootId,
|
2022-12-05 12:18:10 +00:00
|
|
|
Ui::PeerUserpicView &userpicView,
|
2019-08-28 14:24:12 +00:00
|
|
|
MsgId msgId,
|
|
|
|
const QString &title,
|
|
|
|
const QString &subtitle,
|
|
|
|
const QString &msg,
|
2021-10-06 15:24:30 +00:00
|
|
|
DisplayOptions options) {
|
2019-08-28 14:24:12 +00:00
|
|
|
_private->showNotification(
|
|
|
|
peer,
|
2022-10-12 20:23:14 +00:00
|
|
|
topicRootId,
|
2020-05-29 16:55:01 +00:00
|
|
|
userpicView,
|
2019-08-28 14:24:12 +00:00
|
|
|
msgId,
|
|
|
|
title,
|
|
|
|
subtitle,
|
|
|
|
msg,
|
2021-10-06 15:24:30 +00:00
|
|
|
options);
|
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
|
|
|
}
|
|
|
|
|
2021-10-15 13:57:27 +00:00
|
|
|
void Manager::doClearFromItem(not_null<HistoryItem*> item) {
|
|
|
|
_private->clearFromItem(item);
|
|
|
|
}
|
|
|
|
|
2022-10-12 20:23:14 +00:00
|
|
|
void Manager::doClearFromTopic(not_null<Data::ForumTopic*> topic) {
|
|
|
|
_private->clearFromTopic(topic);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-06-19 12:59:31 +00:00
|
|
|
void Manager::doClearFromSession(not_null<Main::Session*> session) {
|
|
|
|
_private->clearFromSession(session);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Manager::onBeforeNotificationActivated(NotificationId id) {
|
|
|
|
_private->beforeNotificationActivated(id);
|
2016-10-02 16:32:46 +00:00
|
|
|
}
|
|
|
|
|
2021-02-25 15:12:51 +00:00
|
|
|
void Manager::onAfterNotificationActivated(
|
|
|
|
NotificationId id,
|
|
|
|
not_null<Window::SessionController*> window) {
|
|
|
|
_private->afterNotificationActivated(id, window);
|
2016-10-02 09:30:28 +00:00
|
|
|
}
|
2016-10-17 10:55:17 +00:00
|
|
|
|
2021-04-28 07:20:39 +00:00
|
|
|
bool Manager::doSkipAudio() const {
|
|
|
|
return SkipAudioForCustom()
|
2021-04-23 04:00:17 +00:00
|
|
|
|| QuietHoursEnabled
|
2021-04-28 07:20:39 +00:00
|
|
|
|| FocusAssistBlocks;
|
2016-10-17 10:55:17 +00:00
|
|
|
}
|
|
|
|
|
2021-04-28 07:20:39 +00:00
|
|
|
bool Manager::doSkipToast() const {
|
2016-10-17 10:55:17 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-04-28 07:20:39 +00:00
|
|
|
bool Manager::doSkipFlashBounce() const {
|
|
|
|
return SkipFlashBounceForCustom()
|
|
|
|
|| QuietHoursEnabled
|
|
|
|
|| FocusAssistBlocks;
|
2020-05-12 10:04:53 +00:00
|
|
|
}
|
2021-04-28 07:20:39 +00:00
|
|
|
#endif // !__MINGW32__
|
2020-05-12 10:04:53 +00:00
|
|
|
|
2016-10-02 09:30:28 +00:00
|
|
|
} // namespace Notifications
|
|
|
|
} // namespace Platform
|