-
Notifications
You must be signed in to change notification settings - Fork 349
/
Copy pathAppNotificationBuilderUtility.h
167 lines (152 loc) · 6.46 KB
/
AppNotificationBuilderUtility.h
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See LICENSE in the project root for license information.
#include "pch.h"
#include "winrt/Microsoft.Windows.AppNotifications.Builder.h"
#include <algorithm>
#include <regex>
#include <map>
#include <iostream>
constexpr size_t c_maxAppNotificationPayload{ 5120 };
constexpr uint8_t c_maxTextElements{ 3 };
constexpr uint8_t c_maxButtonElements{ 5 };
constexpr size_t c_maxEncodingSize{ 3 };
constexpr uint8_t c_maxTextInputElements{ 5 };
constexpr uint8_t c_maxSelectionElements{ 5 };
constexpr uint8_t c_offsetIndexValue{ 2 };
namespace AppNotificationBuilder
{
using namespace winrt::Microsoft::Windows::AppNotifications::Builder;
}
inline std::unordered_map<wchar_t, std::wstring> GetXmlEscapeEncodings()
{
static std::unordered_map<wchar_t, std::wstring> encodings = { { L'&', L"&"}, { L'\"', L"""}, {L'<', L"<"}, {L'>', L">"}, {L'\'', L"'"}};
return encodings;
}
inline std::unordered_map<wchar_t, std::wstring> GetPercentEncodings()
{
static std::unordered_map<wchar_t, std::wstring> encodings = {{ L'%', L"%25"}, {L';', L"%3B"}, {L'=', L"%3D"} };
return encodings;
}
inline std::unordered_map<std::wstring, wchar_t> GetPercentEncodingsReverse()
{
static std::unordered_map<std::wstring, wchar_t> encodings = { { L"%25", L'%' }, {L"%3B", L';' }, { L"%3D", L'=' } };
return encodings;
}
inline PCWSTR GetWinSoundEventString(AppNotificationBuilder::AppNotificationSoundEvent soundEvent)
{
switch (soundEvent)
{
case AppNotificationBuilder::AppNotificationSoundEvent::IM:
return L"ms-winsoundevent:Notification.IM";
case AppNotificationBuilder::AppNotificationSoundEvent::Mail:
return L"ms-winsoundevent:Notification.Mail";
case AppNotificationBuilder::AppNotificationSoundEvent::Reminder:
return L"ms-winsoundevent:Notification.Reminder";
case AppNotificationBuilder::AppNotificationSoundEvent::SMS:
return L"ms-winsoundevent:Notification.SMS";
case AppNotificationBuilder::AppNotificationSoundEvent::Alarm:
return L"ms-winsoundevent:Notification.Looping.Alarm";
case AppNotificationBuilder::AppNotificationSoundEvent::Alarm2:
return L"ms-winsoundevent:Notification.Looping.Alarm2";
case AppNotificationBuilder::AppNotificationSoundEvent::Alarm3:
return L"ms-winsoundevent:Notification.Looping.Alarm3";
case AppNotificationBuilder::AppNotificationSoundEvent::Alarm4:
return L"ms-winsoundevent:Notification.Looping.Alarm4";
case AppNotificationBuilder::AppNotificationSoundEvent::Alarm5:
return L"ms-winsoundevent:Notification.Looping.Alarm5";
case AppNotificationBuilder::AppNotificationSoundEvent::Alarm6:
return L"ms-winsoundevent:Notification.Looping.Alarm6";
case AppNotificationBuilder::AppNotificationSoundEvent::Alarm7:
return L"ms-winsoundevent:Notification.Looping.Alarm7";
case AppNotificationBuilder::AppNotificationSoundEvent::Alarm8:
return L"ms-winsoundevent:Notification.Looping.Alarm8";
case AppNotificationBuilder::AppNotificationSoundEvent::Alarm9:
return L"ms-winsoundevent:Notification.Looping.Alarm9";
case AppNotificationBuilder::AppNotificationSoundEvent::Alarm10:
return L"ms-winsoundevent:Notification.Looping.Alarm10";
case AppNotificationBuilder::AppNotificationSoundEvent::Call:
return L"ms-winsoundevent:Notification.Looping.Call";
case AppNotificationBuilder::AppNotificationSoundEvent::Call2:
return L"ms-winsoundevent:Notification.Looping.Call2";
case AppNotificationBuilder::AppNotificationSoundEvent::Call3:
return L"ms-winsoundevent:Notification.Looping.Call3";
case AppNotificationBuilder::AppNotificationSoundEvent::Call4:
return L"ms-winsoundevent:Notification.Looping.Call4";
case AppNotificationBuilder::AppNotificationSoundEvent::Call5:
return L"ms-winsoundevent:Notification.Looping.Call5";
case AppNotificationBuilder::AppNotificationSoundEvent::Call6:
return L"ms-winsoundevent:Notification.Looping.Call6";
case AppNotificationBuilder::AppNotificationSoundEvent::Call7:
return L"ms-winsoundevent:Notification.Looping.Call7";
case AppNotificationBuilder::AppNotificationSoundEvent::Call8:
return L"ms-winsoundevent:Notification.Looping.Call8";
case AppNotificationBuilder::AppNotificationSoundEvent::Call9:
return L"ms-winsoundevent:Notification.Looping.Call9";
case AppNotificationBuilder::AppNotificationSoundEvent::Call10:
return L"ms-winsoundevent:Notification.Looping.Call10";
default:
return L"ms-winsoundevent:Notification.Default";
}
}
inline std::wstring EncodeArgument(std::wstring const& value)
{
std::wstring encodedValue{};
auto percentEncodings{ GetPercentEncodings() };
auto xmlEncodings{ GetXmlEscapeEncodings() };
for (auto ch : value)
{
if (percentEncodings.find(ch) != percentEncodings.end())
{
encodedValue.append(percentEncodings[ch]);
}
else if (xmlEncodings.find(ch) != xmlEncodings.end())
{
encodedValue.append(xmlEncodings[ch]);
}
else
{
encodedValue.push_back(ch);
}
}
return encodedValue;
}
inline std::wstring EncodeXml(winrt::hstring const& value)
{
std::wstring encodedValue{};
auto xmlEncodings{ GetXmlEscapeEncodings() };
for (auto ch : value)
{
if (xmlEncodings.find(ch) != xmlEncodings.end())
{
encodedValue.append(xmlEncodings[ch]);
}
else
{
encodedValue.push_back(ch);
}
}
return encodedValue;
}
// Decoding process based off the Windows Community Toolkit:
// https://github.com/CommunityToolkit/WindowsCommunityToolkit/blob/rel/7.1.0/Microsoft.Toolkit.Uwp.Notifications/Toasts/ToastArguments.cs#L389inline
inline std::wstring Decode(std::wstring const& value)
{
std::wstring result{};
auto percentEncodings{ GetPercentEncodingsReverse() };
// Need to unescape special characters
for (size_t index = 0; index < value.size();)
{
std::wstring curr{ value.substr(index, c_maxEncodingSize) };
if (percentEncodings.find(curr) != percentEncodings.end())
{
result.push_back(percentEncodings[curr]);
index += c_maxEncodingSize;
}
else
{
result.push_back(value.at(index));
index++;
}
}
return result;
}