PageRenderTime 50ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/Passbook.Generator/PassGeneratorRequest.cs

https://github.com/Comezon/dotnet-passbook
C# | 419 lines | 272 code | 62 blank | 85 comment | 22 complexity | f685e5c0502cdcbb48d64616bee10600 MD5 | raw file
  1. using System;
  2. using System.Linq;
  3. using Newtonsoft.Json;
  4. using Passbook.Generator.Fields;
  5. using System.Collections.Generic;
  6. using System.Security.Cryptography.X509Certificates;
  7. using System.Drawing;
  8. namespace Passbook.Generator
  9. {
  10. public class PassGeneratorRequest
  11. {
  12. public PassGeneratorRequest()
  13. {
  14. this.HeaderFields = new List<Field>();
  15. this.PrimaryFields = new List<Field>();
  16. this.SecondaryFields = new List<Field>();
  17. this.AuxiliaryFields = new List<Field>();
  18. this.BackFields = new List<Field>();
  19. this.Images = new Dictionary<PassbookImage, byte[]>();
  20. this.Locations = new List<Location>();
  21. }
  22. #region Standard Keys
  23. /// <summary>
  24. /// Required. Pass type identifier, as issued by Apple. The value must correspond with your signing certificate.
  25. /// </summary>
  26. public string Identifier { get; set; }
  27. /// <summary>
  28. /// Required. Version of the file format. The value must be 1.
  29. /// </summary>
  30. public int FormatVersion { get { return 1; } }
  31. /// <summary>
  32. /// Required. Serial number that uniquely identifies the pass. No two passes with the same pass type identifier may have the same serial number.
  33. /// </summary>
  34. public string SerialNumber { get; set; }
  35. /// <summary>
  36. /// Required. A simple description of the pass
  37. /// </summary>
  38. public string Description { get; set; }
  39. /// <summary>
  40. /// Required. Team identifier of the organization that originated and signed the pass, as issued by Apple.
  41. /// </summary>
  42. public string TeamIdentifier { get; set; }
  43. /// <summary>
  44. /// Required. Display name of the organization that originated and signed the pass.
  45. /// </summary>
  46. public string OrganizationName { get; set; }
  47. #endregion
  48. #region Images Files
  49. /// <summary>
  50. /// When using in memory, the binary of each image is put here.
  51. /// </summary>
  52. public Dictionary<PassbookImage, byte[]> Images { get; set; }
  53. #endregion
  54. #region Visual Appearance Keys
  55. /// <summary>
  56. /// Optional. Foreground color of the pass, specified as a CSS-style RGB triple. For example, rgb(100, 10, 110).
  57. /// </summary>
  58. public string ForegroundColor { get; set; }
  59. /// <summary>
  60. /// Optional. Background color of the pass, specified as an CSS-style RGB triple. For example, rgb(23, 187, 82).
  61. /// </summary>
  62. public string BackgroundColor { get; set; }
  63. /// <summary>
  64. /// Optional. Color of the label text, specified as a CSS-style RGB triple. For example, rgb(255, 255, 255).
  65. /// If omitted, the label color is determined automatically.
  66. /// </summary>
  67. public string LabelColor { get; set; }
  68. /// <summary>
  69. /// Optional. Text displayed next to the logo on the pass.
  70. /// </summary>
  71. public string LogoText { get; set; }
  72. /// <summary>
  73. /// Optional. If true, the strip image is displayed without a shine effect. The default value is false.
  74. /// </summary>
  75. public bool SuppressStripShine { get; set; }
  76. /// <summary>
  77. /// Optional. Fields to be displayed prominently on the front of the pass.
  78. /// </summary>
  79. public List<Field> HeaderFields { get; private set; }
  80. /// <summary>
  81. /// Optional. Fields to be displayed prominently on the front of the pass.
  82. /// </summary>
  83. public List<Field> PrimaryFields { get; private set; }
  84. /// <summary>
  85. /// Optional. Fields to be displayed on the front of the pass.
  86. /// </summary>
  87. public List<Field> SecondaryFields { get; private set; }
  88. /// <summary>
  89. /// Optional. Additional fields to be displayed on the front of the pass.
  90. /// </summary>
  91. public List<Field> AuxiliaryFields { get; private set; }
  92. /// <summary>
  93. /// Optional. Information about fields that are displayed on the back of the pass.
  94. /// </summary>
  95. public List<Field> BackFields { get; private set; }
  96. /// <summary>
  97. /// Optional. Information specific to barcodes.
  98. /// </summary>
  99. public BarCode Barcode { get; private set; }
  100. /// <summary>
  101. /// Required. Pass type.
  102. /// </summary>
  103. public PassStyle Style { get; set; }
  104. /// <summary>
  105. /// Required for boarding passes; otherwise not allowed. Type of transit.
  106. /// </summary>
  107. public TransitType TransitType { get; set; }
  108. #endregion
  109. #region Location Keys
  110. public List<Location> Locations { get; private set; }
  111. #endregion
  112. #region Certificate
  113. /// <summary>
  114. /// A byte array containing the X509 certificate
  115. /// </summary>
  116. public byte[] Certificate { get; set; }
  117. /// <summary>
  118. /// A byte array containing the Apple WWDRCA X509 certificate
  119. /// </summary>
  120. public byte[] AppleWWDRCACertificate { get; set; }
  121. /// <summary>
  122. /// The private key password for the certificate.
  123. /// </summary>
  124. public string CertificatePassword { get; set; }
  125. /// <summary>
  126. /// Certificate Thumbprint value
  127. /// </summary>
  128. public string CertThumbprint { get; set; }
  129. /// <summary>
  130. /// Certificate Store Location
  131. /// </summary>
  132. public StoreLocation CertLocation { get; set; }
  133. #endregion
  134. #region Web Service Keys
  135. /// <summary>
  136. /// The authentication token to use with the web service.
  137. /// </summary>
  138. public string AuthenticationToken { get; set; }
  139. /// <summary>
  140. /// The URL of a web service that conforms to the API described in Pass Kit Web Service Reference.
  141. /// The web service must use the HTTPS protocol and includes the leading https://.
  142. /// On devices configured for development, there is UI in Settings to allow HTTP web services.
  143. /// </summary>
  144. public string WebServiceUrl { get; set; }
  145. #endregion
  146. #region Helpers
  147. public void AddHeaderField(Field field)
  148. {
  149. this.HeaderFields.Add(field);
  150. }
  151. public void AddPrimaryField(Field field)
  152. {
  153. this.PrimaryFields.Add(field);
  154. }
  155. public void AddSecondaryField(Field field)
  156. {
  157. this.SecondaryFields.Add(field);
  158. }
  159. public void AddAuxiliaryField(Field field)
  160. {
  161. this.AuxiliaryFields.Add(field);
  162. }
  163. public void AddBackField(Field field)
  164. {
  165. this.BackFields.Add(field);
  166. }
  167. public void AddBarCode(string message, BarcodeType type, string encoding, string altText)
  168. {
  169. Barcode = new BarCode();
  170. Barcode.Type = type;
  171. Barcode.Message = message;
  172. Barcode.Encoding = encoding;
  173. Barcode.AlternateText = altText;
  174. }
  175. public void AddBarCode(string message, BarcodeType type, string encoding)
  176. {
  177. Barcode = new BarCode();
  178. Barcode.Type = type;
  179. Barcode.Message = message;
  180. Barcode.Encoding = encoding;
  181. Barcode.AlternateText = null;
  182. }
  183. public void AddLocation(double latitude, double longitude)
  184. {
  185. AddLocation(latitude, longitude, null);
  186. }
  187. public void AddLocation(double latitude, double longitude, string relevantText)
  188. {
  189. this.Locations.Add(new Location() { Latitude = latitude, Longitude = longitude, RelevantText = relevantText });
  190. }
  191. public virtual void PopulateFields()
  192. {
  193. // NO OP.
  194. }
  195. internal void Write(JsonWriter writer)
  196. {
  197. PopulateFields();
  198. writer.WriteStartObject();
  199. WriteStandardKeys(writer, this);
  200. WriteLocationKeys(writer, this);
  201. WriteAppearanceKeys(writer, this);
  202. OpenStyleSpecificKey(writer, this);
  203. WriteSection(writer, "headerFields", this.HeaderFields);
  204. WriteSection(writer, "primaryFields", this.PrimaryFields);
  205. WriteSection(writer, "secondaryFields", this.SecondaryFields);
  206. WriteSection(writer, "auxiliaryFields", this.AuxiliaryFields);
  207. WriteSection(writer, "backFields", this.BackFields);
  208. if (this.Style == PassStyle.BoardingPass)
  209. {
  210. writer.WritePropertyName("transitType");
  211. writer.WriteValue(this.TransitType.ToString());
  212. }
  213. CloseStyleSpecificKey(writer);
  214. WriteBarcode(writer, this);
  215. WriteUrls(writer, this);
  216. writer.WriteEndObject();
  217. }
  218. private void WriteLocationKeys(JsonWriter writer, PassGeneratorRequest passGeneratorRequest)
  219. {
  220. writer.WritePropertyName("locations");
  221. writer.WriteStartArray();
  222. foreach (var location in Locations)
  223. {
  224. location.Write(writer);
  225. }
  226. writer.WriteEndArray();
  227. }
  228. private void WriteUrls(JsonWriter writer, PassGeneratorRequest request)
  229. {
  230. if (!string.IsNullOrEmpty(request.AuthenticationToken))
  231. {
  232. writer.WritePropertyName("authenticationToken");
  233. writer.WriteValue(request.AuthenticationToken);
  234. writer.WritePropertyName("webServiceURL");
  235. writer.WriteValue(request.WebServiceUrl);
  236. }
  237. }
  238. private void WriteBarcode(JsonWriter writer, PassGeneratorRequest request)
  239. {
  240. if (Barcode != null)
  241. {
  242. writer.WritePropertyName("barcode");
  243. writer.WriteStartObject();
  244. writer.WritePropertyName("format");
  245. writer.WriteValue(request.Barcode.Type.ToString());
  246. writer.WritePropertyName("message");
  247. writer.WriteValue(request.Barcode.Message);
  248. writer.WritePropertyName("messageEncoding");
  249. writer.WriteValue(request.Barcode.Encoding);
  250. if (request.Barcode.AlternateText != null)
  251. {
  252. writer.WritePropertyName("altText");
  253. writer.WriteValue(request.Barcode.AlternateText);
  254. }
  255. writer.WriteEndObject();
  256. }
  257. }
  258. private void WriteStandardKeys(JsonWriter writer, PassGeneratorRequest request)
  259. {
  260. writer.WritePropertyName("passTypeIdentifier");
  261. writer.WriteValue(request.Identifier);
  262. writer.WritePropertyName("formatVersion");
  263. writer.WriteValue(request.FormatVersion);
  264. writer.WritePropertyName("serialNumber");
  265. writer.WriteValue(request.SerialNumber);
  266. writer.WritePropertyName("description");
  267. writer.WriteValue(request.Description);
  268. writer.WritePropertyName("organizationName");
  269. writer.WriteValue(request.OrganizationName);
  270. writer.WritePropertyName("teamIdentifier");
  271. writer.WriteValue(request.TeamIdentifier);
  272. if (request.LogoText != null)
  273. {
  274. writer.WritePropertyName("logoText");
  275. writer.WriteValue(request.LogoText);
  276. }
  277. }
  278. private void WriteAppearanceKeys(JsonWriter writer, PassGeneratorRequest request)
  279. {
  280. if (request.ForegroundColor != null)
  281. {
  282. writer.WritePropertyName("foregroundColor");
  283. writer.WriteValue(ConvertColor(request.ForegroundColor));
  284. }
  285. if (request.BackgroundColor != null)
  286. {
  287. writer.WritePropertyName("backgroundColor");
  288. writer.WriteValue(ConvertColor(request.BackgroundColor));
  289. }
  290. if (request.LabelColor != null)
  291. {
  292. writer.WritePropertyName("labelColor");
  293. writer.WriteValue(ConvertColor(request.LabelColor));
  294. }
  295. if (request.SuppressStripShine)
  296. {
  297. writer.WritePropertyName("suppressStripShine");
  298. writer.WriteValue(true);
  299. }
  300. }
  301. private void OpenStyleSpecificKey(JsonWriter writer, PassGeneratorRequest request)
  302. {
  303. switch (request.Style)
  304. {
  305. case PassStyle.EventTicket:
  306. writer.WritePropertyName("eventTicket");
  307. writer.WriteStartObject();
  308. break;
  309. case PassStyle.StoreCard:
  310. writer.WritePropertyName("storeCard");
  311. writer.WriteStartObject();
  312. break;
  313. case PassStyle.BoardingPass:
  314. writer.WritePropertyName("boardingPass");
  315. writer.WriteStartObject();
  316. break;
  317. case PassStyle.Generic:
  318. writer.WritePropertyName("generic");
  319. writer.WriteStartObject();
  320. break;
  321. case PassStyle.Coupon:
  322. writer.WritePropertyName("coupon");
  323. writer.WriteStartObject();
  324. break;
  325. default:
  326. throw new InvalidOperationException("Unsupported pass style specified");
  327. }
  328. }
  329. private void CloseStyleSpecificKey(JsonWriter writer)
  330. {
  331. writer.WriteEndObject();
  332. }
  333. private void WriteSection(JsonWriter writer, string sectionName, List<Field> fields)
  334. {
  335. writer.WritePropertyName(sectionName);
  336. writer.WriteStartArray();
  337. foreach (var field in fields)
  338. {
  339. field.Write(writer);
  340. }
  341. writer.WriteEndArray();
  342. }
  343. private string ConvertColor(string colour)
  344. {
  345. if (colour != null && colour.Length > 0 && colour.Substring(0, 1) == "#")
  346. {
  347. Color c = ColorTranslator.FromHtml(colour);
  348. return string.Format("rgb({0},{1},{2})", c.R, c.G, c.B);
  349. }
  350. else
  351. {
  352. return colour;
  353. }
  354. }
  355. #endregion
  356. }
  357. }