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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
|
#include "stdafx.h"
ToastNotification::ToastNotification(_In_ wchar_t* text, _In_ wchar_t* caption, _In_ wchar_t* imagePath)
: _text(text), _caption(caption), _imagePath(imagePath)
{
}
ToastNotification::~ToastNotification()
{
}
HRESULT ToastNotification::GetNodeByTag(_In_ HSTRING tagName, _Outptr_ ABI::Windows::Data::Xml::Dom::IXmlNode **node, _In_ ABI::Windows::Data::Xml::Dom::IXmlDocument* xml)
{
Microsoft::WRL::ComPtr<ABI::Windows::Data::Xml::Dom::IXmlNodeList> nodeList;
HRESULT hr = xml->GetElementsByTagName(tagName, &nodeList);
if (FAILED(hr))
return hr;
UINT32 length;
hr = nodeList->get_Length(&length);
if (FAILED(hr))
return hr;
if (length == 0)
return E_INVALIDARG;
return nodeList->Item(0, node);
}
HRESULT ToastNotification::AddNode(_In_ HSTRING name, _Outptr_ ABI::Windows::Data::Xml::Dom::IXmlNode **node, _In_ ABI::Windows::Data::Xml::Dom::IXmlNode *rootNode, _In_ ABI::Windows::Data::Xml::Dom::IXmlDocument* xml)
{
Microsoft::WRL::ComPtr<ABI::Windows::Data::Xml::Dom::IXmlElement> element;
HRESULT hr = xml->CreateElement(name, &element);
if (FAILED(hr))
return hr;
Microsoft::WRL::ComPtr<ABI::Windows::Data::Xml::Dom::IXmlNode> tempNode;
hr = element.As(&tempNode);
if (FAILED(hr))
return hr;
return rootNode->AppendChild(tempNode.Get(), node);
}
HRESULT ToastNotification::SetNodeValueString(_In_ HSTRING inputString, _In_ ABI::Windows::Data::Xml::Dom::IXmlNode* node, _In_ ABI::Windows::Data::Xml::Dom::IXmlDocument* xml)
{
Microsoft::WRL::ComPtr<ABI::Windows::Data::Xml::Dom::IXmlText> inputText;
HRESULT hr = xml->CreateTextNode(inputString, &inputText);
if (FAILED(hr))
return hr;
Microsoft::WRL::ComPtr<ABI::Windows::Data::Xml::Dom::IXmlNode> inputTextNode;
hr = inputText.As(&inputTextNode);
if (FAILED(hr))
return hr;
Microsoft::WRL::ComPtr<ABI::Windows::Data::Xml::Dom::IXmlNode> pAppendedChild;
return hr = node->AppendChild(inputTextNode.Get(), &pAppendedChild);
}
HRESULT ToastNotification::SetTextValues(_In_reads_(textValuesCount) wchar_t** textValues, _In_ UINT32 textValuesCount, _In_ ABI::Windows::Data::Xml::Dom::IXmlDocument* xml)
{
HRESULT hr = textValues != nullptr && textValuesCount > 0 ? S_OK : E_INVALIDARG;
if (FAILED(hr))
return hr;
Microsoft::WRL::ComPtr<ABI::Windows::Data::Xml::Dom::IXmlNodeList> nodeList;
hr = xml->GetElementsByTagName(StringReferenceWrapper(L"text").Get(), &nodeList);
if (FAILED(hr))
return hr;
UINT32 nodeListLength;
hr = nodeList->get_Length(&nodeListLength);
if (FAILED(hr))
return hr;
hr = textValuesCount <= nodeListLength ? S_OK : E_INVALIDARG;
if (FAILED(hr))
return hr;
for (UINT32 i = 0; i < textValuesCount; i++)
{
Microsoft::WRL::ComPtr<ABI::Windows::Data::Xml::Dom::IXmlNode> textNode;
hr = nodeList->Item(i, &textNode);
if (FAILED(hr))
return hr;
hr = SetNodeValueString(StringReferenceWrapper(textValues[i], (UINT32)wcslen(textValues[i])).Get(), textNode.Get(), xml);
if (FAILED(hr))
return hr;
}
return S_OK;
}
HRESULT ToastNotification::SetImageSrc(_In_z_ wchar_t* imagePath, _In_ ABI::Windows::Data::Xml::Dom::IXmlDocument* xml)
{
wchar_t imageSrc[MAX_PATH] = L"file:///";
HRESULT hr = StringCchCat(imageSrc, ARRAYSIZE(imageSrc), imagePath);
if (FAILED(hr))
return hr;
Microsoft::WRL::ComPtr<ABI::Windows::Data::Xml::Dom::IXmlNode> imageNode;
hr = GetNodeByTag(StringReferenceWrapper(L"image").Get(), &imageNode, xml);
Microsoft::WRL::ComPtr<ABI::Windows::Data::Xml::Dom::IXmlNamedNodeMap> attributes;
hr = imageNode->get_Attributes(&attributes);
if (FAILED(hr))
return hr;
Microsoft::WRL::ComPtr<ABI::Windows::Data::Xml::Dom::IXmlNode> srcAttribute;
hr = attributes->GetNamedItem(StringReferenceWrapper(L"src").Get(), &srcAttribute);
if (FAILED(hr))
return hr;
return hr = SetNodeValueString(StringReferenceWrapper(imageSrc).Get(), srcAttribute.Get(), xml);
}
HRESULT ToastNotification::Setup(_In_ ABI::Windows::Data::Xml::Dom::IXmlDocument* xml)
{
Microsoft::WRL::ComPtr<ABI::Windows::Data::Xml::Dom::IXmlNode> toastNode;
HRESULT hr = GetNodeByTag(StringReferenceWrapper(L"toast").Get(), &toastNode, xml);
if (FAILED(hr))
return hr;
Microsoft::WRL::ComPtr<ABI::Windows::Data::Xml::Dom::IXmlNode> audioNode;
hr = AddNode(StringReferenceWrapper(L"audio").Get(), &audioNode, toastNode.Get(), xml);
if (FAILED(hr))
return hr;
Microsoft::WRL::ComPtr<ABI::Windows::Data::Xml::Dom::IXmlNamedNodeMap> attributes;
hr = audioNode->get_Attributes(&attributes);
if (FAILED(hr))
return hr;
Microsoft::WRL::ComPtr<ABI::Windows::Data::Xml::Dom::IXmlAttribute> silentAttribute;
hr = xml->CreateAttribute(StringReferenceWrapper(L"silent").Get(), &silentAttribute);
if (FAILED(hr))
return hr;
Microsoft::WRL::ComPtr<ABI::Windows::Data::Xml::Dom::IXmlNode> silentAttributeNode;
hr = silentAttribute.As(&silentAttributeNode);
if (FAILED(hr))
return hr;
Microsoft::WRL::ComPtr<ABI::Windows::Data::Xml::Dom::IXmlNode> tempNode;
hr = attributes->SetNamedItem(silentAttributeNode.Get(), &tempNode);
if (FAILED(hr))
return hr;
return SetNodeValueString(StringReferenceWrapper(L"true").Get(), silentAttributeNode.Get(), xml);
}
HRESULT ToastNotification::CreateXml(_Outptr_ ABI::Windows::Data::Xml::Dom::IXmlDocument** xml)
{
Microsoft::WRL::ComPtr<ABI::Windows::UI::Notifications::IToastNotificationManagerStatics> notificationManager;
HRESULT hr = Windows::Foundation::GetActivationFactory(StringReferenceWrapper(RuntimeClass_Windows_UI_Notifications_ToastNotificationManager).Get(), ¬ificationManager);
if (FAILED(hr))
RaiseException(static_cast<DWORD>(STATUS_INVALID_PARAMETER), EXCEPTION_NONCONTINUABLE, 0, nullptr);
ABI::Windows::UI::Notifications::ToastTemplateType templateId = _imagePath == nullptr
? ABI::Windows::UI::Notifications::ToastTemplateType_ToastText02
: ABI::Windows::UI::Notifications::ToastTemplateType_ToastImageAndText02;
hr = notificationManager->GetTemplateContent(templateId, xml);
if (FAILED(hr))
return hr;
hr = Setup(*xml);
if (FAILED(hr))
return hr;
if (_imagePath)
{
hr = SetImageSrc(_imagePath, *xml);
if (FAILED(hr))
return hr;
}
wchar_t* textValues[] =
{
_caption == nullptr
? L"Miranda NG"
: _caption,
_text
};
return SetTextValues(textValues, _countof(textValues), *xml);
}
HRESULT ToastNotification::Create(_Outptr_ ABI::Windows::UI::Notifications::IToastNotification** notification)
{
Microsoft::WRL::ComPtr<ABI::Windows::Data::Xml::Dom::IXmlDocument> xml;
HRESULT hr = CreateXml(&xml);
if (FAILED(hr))
return hr;
Microsoft::WRL::ComPtr<ABI::Windows::UI::Notifications::IToastNotificationFactory> factory;
hr = Windows::Foundation::GetActivationFactory(StringReferenceWrapper(RuntimeClass_Windows_UI_Notifications_ToastNotification).Get(), &factory);
if (FAILED(hr))
return hr;
return hr = factory->CreateToastNotification(xml.Get(), notification);
}
HRESULT ToastNotification::Show()
{
return Show(new ToastEventHandler(nullptr));
}
HRESULT ToastNotification::Show(_In_ ToastEventHandler* handler)
{
Microsoft::WRL::ComPtr<ABI::Windows::UI::Notifications::IToastNotificationManagerStatics> notificationManager;
HRESULT hr = Windows::Foundation::GetActivationFactory(StringReferenceWrapper(RuntimeClass_Windows_UI_Notifications_ToastNotificationManager).Get(), ¬ificationManager);
if (FAILED(hr))
return hr;
//RaiseException(static_cast<DWORD>(STATUS_INVALID_PARAMETER), EXCEPTION_NONCONTINUABLE, 0, nullptr);
hr = notificationManager->CreateToastNotifierWithId(StringReferenceWrapper(::AppUserModelID).Get(), ¬ifier);
if (FAILED(hr))
return hr;
hr = Create(¬ification);
if (FAILED(hr))
return hr;
EventRegistrationToken activatedToken, dismissedToken, failedToken;
Microsoft::WRL::ComPtr<ToastEventHandler> eventHandler(handler);
hr = notification->add_Activated(eventHandler.Get(), &activatedToken);
if (FAILED(hr))
return hr;
notification->add_Activated(eventHandler.Get(), &activatedToken);
notification->add_Dismissed(eventHandler.Get(), &dismissedToken);
notification->add_Failed(eventHandler.Get(), &failedToken);
return notifier->Show(notification.Get());
}
HRESULT ToastNotification::Hide()
{
return notifier->Hide(notification.Get());
}
|