summaryrefslogtreecommitdiff
path: root/plugins/Toaster/src/toast_notification.cpp
blob: d9ecb3a5643f9910781ba1dfd5c7a1802f2afdb0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include "stdafx.h"
#include <wrl.h>
using namespace Microsoft::WRL;

static mir_cs csNotifications;
static LIST<ToastNotification> lstNotifications(10, PtrKeySortT);

void HideAllToasts()
{
	mir_cslock lck(csNotifications);
	for (auto &it : lstNotifications.rev_iter())
		if (it->IsValid())
			delete it;

	lstNotifications.destroy();
}

/////////////////////////////////////////////////////////////////////////////////////////

ToastNotification::ToastNotification(
	_In_ wchar_t *text,
	_In_ wchar_t *caption,
	_In_ wchar_t *imagePath,
	MCONTACT hContact,
	WNDPROC pWndProc,
	void *pData)
	: _text(text), _caption(caption), _imagePath(imagePath), _hContact(hContact), _pfnPopupProc(pWndProc), _pvPopupData(pData)
{
	{	mir_cslock lck(csNotifications);
		lstNotifications.insert(this);
	}

	if (FAILED(Windows::Foundation::GetActivationFactory(StringReferenceWrapper(RuntimeClass_Windows_UI_Notifications_ToastNotificationManager).Get(), &notificationManager)))
		return;

	if (FAILED(notificationManager->CreateToastNotifierWithId(StringReferenceWrapper(::AppUserModelID).Get(), &notifier)))
		return;

	if (FAILED(Create(&notification)))
		return;

	notification->add_Activated(Callback<ToastActivationHandler>(this, &ToastNotification::OnActivate).Get(), &_ertActivated);
	notification->add_Dismissed(Callback<ToastDismissHandler>(this, &ToastNotification::OnDismiss).Get(), &_ertDismissed);
	notification->add_Failed(Callback<ToastFailHandler>(this, &ToastNotification::OnFail).Get(), &_ertFailed);
	notifier->Show(notification.Get());
	CallPopupProc(UM_INITPOPUP);
}

ToastNotification::~ToastNotification()
{
	_signature = 0;
	if (_pvPopupData != nullptr)
		CallPopupProc(UM_FREEPLUGINDATA);

	if (notification)
		notifier->Hide(notification.Get());
}

HRESULT ToastNotification::CreateXml(_Outptr_ ABI::Windows::Data::Xml::Dom::IXmlDocument **xml)
{
	ComPtr<ABI::Windows::Data::Xml::Dom::IXmlDocumentIO> xmlDocument;
	CHECKHR(Windows::Foundation::ActivateInstance(StringReferenceWrapper(RuntimeClass_Windows_Data_Xml_Dom_XmlDocument).Get(), &xmlDocument));

	TiXmlDocument doc;
	TiXmlElement *xmlToast = doc.NewElement("toast"); doc.InsertEndChild(xmlToast);

	TiXmlElement *xmlAudioNode = doc.NewElement("audio"); xmlToast->InsertEndChild(xmlAudioNode);
	xmlAudioNode->SetAttribute("silent", "true");

	TiXmlElement *xmlVisualNode = doc.NewElement("visual"); xmlToast->InsertEndChild(xmlVisualNode);

	TiXmlElement *xmlBindingNode = doc.NewElement("binding"); xmlVisualNode->InsertEndChild(xmlBindingNode);
	xmlBindingNode->SetAttribute("template", "ToastImageAndText02");

	if (_imagePath) {
		TiXmlElement *xmlImageNode = doc.NewElement("image"); xmlBindingNode->InsertEndChild(xmlImageNode);
		xmlImageNode->SetAttribute("id", 1);
		xmlImageNode->SetAttribute("src", CMStringW(FORMAT, L"file:///%s", _imagePath));
	}

	TiXmlElement *xmlTitleNode = doc.NewElement("text"); xmlBindingNode->InsertEndChild(xmlTitleNode);
	xmlTitleNode->SetAttribute("id", 1); xmlTitleNode->SetText(_caption != nullptr ? _caption : L"Miranda NG");

	if (_text) {
		TiXmlElement *xmlTextNode = doc.NewElement("text"); xmlBindingNode->InsertEndChild(xmlTextNode);
		xmlTextNode->SetAttribute("id", 2); xmlTextNode->SetText(_text);
	}

	tinyxml2::XMLPrinter printer(0, true);
	doc.Print(&printer);
	Utf2T xtmp(printer.CStr());
	CHECKHR(xmlDocument->LoadXml(StringReferenceWrapper(xtmp, (UINT32)mir_wstrlen(xtmp)).Get()));

	return xmlDocument.CopyTo(xml);
}

HRESULT ToastNotification::Create(_Outptr_ ABI::Windows::UI::Notifications::IToastNotification **_notification)
{
	ComPtr<ABI::Windows::Data::Xml::Dom::IXmlDocument> xml;
	CHECKHR(CreateXml(&xml));

	ComPtr<ABI::Windows::UI::Notifications::IToastNotificationFactory> factory;
	CHECKHR(Windows::Foundation::GetActivationFactory(StringReferenceWrapper(RuntimeClass_Windows_UI_Notifications_ToastNotification).Get(), &factory));

	return factory->CreateToastNotification(xml.Get(), _notification);
}

HRESULT ToastNotification::OnActivate(_In_ ABI::Windows::UI::Notifications::IToastNotification *, IInspectable *)
{
	if (_signature == TOAST_SIGNATURE) {
		CallPopupProc(WM_COMMAND);
		Destroy();
	}
	return S_OK;
}

HRESULT ToastNotification::OnDismiss(_In_ ABI::Windows::UI::Notifications::IToastNotification *, _In_ ABI::Windows::UI::Notifications::IToastDismissedEventArgs *e)
{
	if (_signature == TOAST_SIGNATURE) {
		ABI::Windows::UI::Notifications::ToastDismissalReason tdr;
		CHECKHR(e->get_Reason(&tdr));
		if (tdr == ABI::Windows::UI::Notifications::ToastDismissalReason_UserCanceled)
			CallPopupProc(WM_CONTEXTMENU);
		Destroy();
	}
	return S_OK;
}

HRESULT ToastNotification::OnFail(_In_ ABI::Windows::UI::Notifications::IToastNotification *, _In_ ABI::Windows::UI::Notifications::IToastFailedEventArgs *)
{
	if (_signature == TOAST_SIGNATURE)
		Destroy();
	return S_OK;
}

void ToastNotification::Destroy()
{
	if (_signature != TOAST_SIGNATURE) return;
	{
		mir_cslock lck(csNotifications);
		lstNotifications.remove(this);
	}
	delete this;
}