PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/Secondary tiles sample C# C++/C#,C++/NotificationsExtensions/Common.cs

https://bitbucket.org/hphuong/eventbrite-win8
C# | 282 lines | 228 code | 33 blank | 21 comment | 33 complexity | e657e1cbce135c9bfb7989cc250f0478 MD5 | raw file
Possible License(s): CC-BY-SA-3.0
  1. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
  2. // ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
  3. // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  4. // PARTICULAR PURPOSE.
  5. //
  6. // Copyright (c) Microsoft Corporation. All rights reserved
  7. using System;
  8. using System.Collections.ObjectModel;
  9. using System.Runtime.InteropServices;
  10. using System.Text;
  11. #if !WINRT_NOT_PRESENT
  12. using Windows.Data.Xml.Dom;
  13. #endif
  14. namespace NotificationsExtensions
  15. {
  16. internal sealed class NotificationContentText : INotificationContentText
  17. {
  18. internal NotificationContentText() { }
  19. public string Text
  20. {
  21. get { return m_Text; }
  22. set { m_Text = value; }
  23. }
  24. public string Lang
  25. {
  26. get { return m_Lang; }
  27. set { m_Lang = value; }
  28. }
  29. private string m_Text;
  30. private string m_Lang;
  31. }
  32. internal sealed class NotificationContentImage : INotificationContentImage
  33. {
  34. internal NotificationContentImage() { }
  35. public string Src
  36. {
  37. get { return m_Src; }
  38. set { m_Src = value; }
  39. }
  40. public string Alt
  41. {
  42. get { return m_Alt; }
  43. set { m_Alt = value; }
  44. }
  45. public bool AddImageQuery
  46. {
  47. get
  48. {
  49. if (m_AddImageQueryNullable == null || m_AddImageQueryNullable.Value == false)
  50. {
  51. return false;
  52. }
  53. else
  54. {
  55. return true;
  56. }
  57. }
  58. set { m_AddImageQueryNullable = value; }
  59. }
  60. public bool? AddImageQueryNullable
  61. {
  62. get { return m_AddImageQueryNullable; }
  63. set { m_AddImageQueryNullable = value; }
  64. }
  65. private string m_Src;
  66. private string m_Alt;
  67. private bool? m_AddImageQueryNullable;
  68. }
  69. internal static class Util
  70. {
  71. public const int NOTIFICATION_CONTENT_VERSION = 1;
  72. public static string HttpEncode(string value)
  73. {
  74. return value.Replace("&", "&amp;").Replace("<", "&lt;").Replace(">", "&gt;").Replace("\"", "&quot;").Replace("'", "&apos;");
  75. }
  76. }
  77. /// <summary>
  78. /// Base class for the notification content creation helper classes.
  79. /// </summary>
  80. #if !WINRT_NOT_PRESENT
  81. internal abstract class NotificationBase
  82. #else
  83. abstract partial class NotificationBase
  84. #endif
  85. {
  86. protected NotificationBase(string templateName, int imageCount, int textCount)
  87. {
  88. m_TemplateName = templateName;
  89. m_Images = new NotificationContentImage[imageCount];
  90. for (int i = 0; i < m_Images.Length; i++)
  91. {
  92. m_Images[i] = new NotificationContentImage();
  93. }
  94. m_TextFields = new INotificationContentText[textCount];
  95. for (int i = 0; i < m_TextFields.Length; i++)
  96. {
  97. m_TextFields[i] = new NotificationContentText();
  98. }
  99. }
  100. public bool StrictValidation
  101. {
  102. get { return m_StrictValidation; }
  103. set { m_StrictValidation = value; }
  104. }
  105. public abstract string GetContent();
  106. public override string ToString()
  107. {
  108. return GetContent();
  109. }
  110. #if !WINRT_NOT_PRESENT
  111. public XmlDocument GetXml()
  112. {
  113. XmlDocument xml = new XmlDocument();
  114. xml.LoadXml(GetContent());
  115. return xml;
  116. }
  117. #endif
  118. /// <summary>
  119. /// Retrieves the list of images that can be manipulated on the notification content.
  120. /// </summary>
  121. public INotificationContentImage[] Images
  122. {
  123. get { return m_Images; }
  124. }
  125. /// <summary>
  126. /// Retrieves the list of text fields that can be manipulated on the notification content.
  127. /// </summary>
  128. public INotificationContentText[] TextFields
  129. {
  130. get { return m_TextFields; }
  131. }
  132. /// <summary>
  133. /// The base Uri path that should be used for all image references in the notification.
  134. /// </summary>
  135. public string BaseUri
  136. {
  137. get { return m_BaseUri; }
  138. set
  139. {
  140. bool goodPrefix = this.StrictValidation || value == null;
  141. goodPrefix = goodPrefix || value.StartsWith("http://", StringComparison.OrdinalIgnoreCase);
  142. goodPrefix = goodPrefix || value.StartsWith("https://", StringComparison.OrdinalIgnoreCase);
  143. goodPrefix = goodPrefix || value.StartsWith("ms-appx:///", StringComparison.OrdinalIgnoreCase);
  144. goodPrefix = goodPrefix || value.StartsWith("ms-appdata:///local/", StringComparison.OrdinalIgnoreCase);
  145. if (!goodPrefix)
  146. {
  147. throw new ArgumentException("The BaseUri must begin with http://, https://, ms-appx:///, or ms-appdata:///local/.", "value");
  148. }
  149. m_BaseUri = value;
  150. }
  151. }
  152. public string Lang
  153. {
  154. get { return m_Lang; }
  155. set { m_Lang = value; }
  156. }
  157. public bool AddImageQuery
  158. {
  159. get
  160. {
  161. if (m_AddImageQueryNullable == null || m_AddImageQueryNullable.Value == false)
  162. {
  163. return false;
  164. }
  165. else
  166. {
  167. return true;
  168. }
  169. }
  170. set { m_AddImageQueryNullable = value; }
  171. }
  172. public bool? AddImageQueryNullable
  173. {
  174. get { return m_AddImageQueryNullable; }
  175. set { m_AddImageQueryNullable = value; }
  176. }
  177. protected string SerializeProperties(string globalLang, string globalBaseUri, bool globalAddImageQuery)
  178. {
  179. globalLang = (globalLang != null) ? globalLang : String.Empty;
  180. globalBaseUri = String.IsNullOrWhiteSpace(globalBaseUri) ? null : globalBaseUri;
  181. StringBuilder builder = new StringBuilder(String.Empty);
  182. for (int i = 0; i < m_Images.Length; i++)
  183. {
  184. if (!String.IsNullOrEmpty(m_Images[i].Src))
  185. {
  186. string escapedSrc = Util.HttpEncode(m_Images[i].Src);
  187. if (!String.IsNullOrWhiteSpace(m_Images[i].Alt))
  188. {
  189. string escapedAlt = Util.HttpEncode(m_Images[i].Alt);
  190. if (m_Images[i].AddImageQueryNullable == null || m_Images[i].AddImageQueryNullable == globalAddImageQuery)
  191. {
  192. builder.AppendFormat("<image id='{0}' src='{1}' alt='{2}'/>", i + 1, escapedSrc, escapedAlt);
  193. }
  194. else
  195. {
  196. builder.AppendFormat("<image addImageQuery='{0}' id='{1}' src='{2}' alt='{3}'/>", m_Images[i].AddImageQuery.ToString().ToLowerInvariant(), i + 1, escapedSrc, escapedAlt);
  197. }
  198. }
  199. else
  200. {
  201. if (m_Images[i].AddImageQueryNullable == null || m_Images[i].AddImageQueryNullable == globalAddImageQuery)
  202. {
  203. builder.AppendFormat("<image id='{0}' src='{1}'/>", i + 1, escapedSrc);
  204. }
  205. else
  206. {
  207. builder.AppendFormat("<image addImageQuery='{0}' id='{1}' src='{2}'/>", m_Images[i].AddImageQuery.ToString().ToLowerInvariant(), i + 1, escapedSrc);
  208. }
  209. }
  210. }
  211. }
  212. for (int i = 0; i < m_TextFields.Length; i++)
  213. {
  214. if (!String.IsNullOrWhiteSpace(m_TextFields[i].Text))
  215. {
  216. string escapedValue = Util.HttpEncode(m_TextFields[i].Text);
  217. if (!String.IsNullOrWhiteSpace(m_TextFields[i].Lang) && !m_TextFields[i].Lang.Equals(globalLang))
  218. {
  219. string escapedLang = Util.HttpEncode(m_TextFields[i].Lang);
  220. builder.AppendFormat("<text id='{0}' lang='{1}'>{2}</text>", i + 1, escapedLang, escapedValue);
  221. }
  222. else
  223. {
  224. builder.AppendFormat("<text id='{0}'>{1}</text>", i + 1, escapedValue);
  225. }
  226. }
  227. }
  228. return builder.ToString();
  229. }
  230. public string TemplateName { get { return m_TemplateName; } }
  231. private bool m_StrictValidation = true;
  232. private NotificationContentImage[] m_Images;
  233. private INotificationContentText[] m_TextFields;
  234. private string m_Lang;
  235. private string m_BaseUri;
  236. private string m_TemplateName;
  237. private bool? m_AddImageQueryNullable;
  238. }
  239. /// <summary>
  240. /// Exception returned when invalid notification content is provided.
  241. /// </summary>
  242. internal sealed class NotificationContentValidationException : COMException
  243. {
  244. public NotificationContentValidationException(string message)
  245. : base(message, unchecked((int)0x80070057))
  246. {
  247. }
  248. }
  249. }