summaryrefslogtreecommitdiff
path: root/plugins/Toaster/src/toast_notification.cpp
blob: 0275920c641de4bed62fdf42631633ce8400a7f7 (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
#include "stdafx.h"

using namespace Microsoft::WRL;

ToastNotification::ToastNotification(_In_ wchar_t* text, _In_ wchar_t* caption, _In_ wchar_t* imagePath)
	: _text(text), _caption(caption), _imagePath(imagePath)
{}

ToastNotification::~ToastNotification()
{
	notification->remove_Activated(_ertActivated);
	notification->remove_Dismissed(_ertDismissed);
	notification->remove_Failed(_ertFailed);
}

HRESULT ToastNotification::Initialize()
{
	CHECKHR(Windows::Foundation::GetActivationFactory(StringReferenceWrapper(RuntimeClass_Windows_UI_Notifications_ToastNotificationManager).Get(), &notificationManager));
	CHECKHR(notificationManager->CreateToastNotifierWithId(StringReferenceWrapper(::AppUserModelID).Get(), &notifier))
	return Create(&notification);
}

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));

	HXML xmlToast = xmlCreateNode(L"toast", NULL, 0);

	HXML xmlAudioNode = xmlAddChild(xmlToast, L"audio", NULL);
	xmlAddAttr(xmlAudioNode, L"silent", L"true");

	HXML xmlVisualNode = xmlAddChild(xmlToast, L"visual", NULL);

	HXML xmlBindingNode = xmlAddChild(xmlVisualNode, L"binding", NULL);
	xmlAddAttr(xmlBindingNode, L"template", IsWinVer10Plus() ? L"ToastGeneric" : L"ToastImageAndText02");
	if (_imagePath)
	{
		HXML xmlImageNode = xmlAddChild(xmlBindingNode, L"image", NULL);

		if (IsWinVer10Plus())
			xmlAddAttr(xmlImageNode, L"placement", L"appLogoOverride");

		xmlAddAttr(xmlImageNode, L"id", L"1");
		xmlAddAttr(xmlImageNode, L"src", CMString(FORMAT, L"file:///%s", _imagePath));
	}

	HXML xmlTitleNode = xmlAddChild(xmlBindingNode, L"text", _caption != NULL ? _caption : L"Miranda NG");
	xmlAddAttr(xmlTitleNode, L"id", L"1");
	if (_text)
	{
		HXML xmlTextNode = xmlAddChild(xmlBindingNode, L"text", _text);
		xmlAddAttr(xmlTextNode, L"id", L"2");
	}

	int nLength;
	TCHAR *xtmp = xmlToString(xmlToast, &nLength);
	xmlDestroyNode(xmlToast);

	CHECKHR(xmlDocument->LoadXml(StringReferenceWrapper(xtmp, nLength).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::Show(_In_ ToastEventHandler* handler)
{
	ComPtr<ToastEventHandler> eventHandler(handler);

	CHECKHR(notification->add_Activated(eventHandler.Get(), &_ertActivated));
	CHECKHR(notification->add_Dismissed(eventHandler.Get(), &_ertDismissed));
	CHECKHR(notification->add_Failed(eventHandler.Get(), &_ertFailed));

	return notifier->Show(notification.Get());
}

HRESULT ToastNotification::Hide()
{
	return notifier->Hide(notification.Get());
}