|
| 1 | +using System; |
| 2 | + |
| 3 | +namespace QRCoder |
| 4 | +{ |
| 5 | + public static partial class PayloadGenerator |
| 6 | + { |
| 7 | + public class CalendarEvent : Payload |
| 8 | + { |
| 9 | + private readonly string subject, description, location, start, end; |
| 10 | + private readonly EventEncoding encoding; |
| 11 | + |
| 12 | + /// <summary> |
| 13 | + /// Generates a calender entry/event payload. |
| 14 | + /// </summary> |
| 15 | + /// <param name="subject">Subject/title of the calender event</param> |
| 16 | + /// <param name="description">Description of the event</param> |
| 17 | + /// <param name="location">Location (lat:long or address) of the event</param> |
| 18 | + /// <param name="start">Start time (incl. UTC offset) of the event</param> |
| 19 | + /// <param name="end">End time (incl. UTC offset) of the event</param> |
| 20 | + /// <param name="allDayEvent">Is it a full day event?</param> |
| 21 | + /// <param name="encoding">Type of encoding (universal or iCal)</param> |
| 22 | + public CalendarEvent(string subject, string description, string location, DateTimeOffset start, DateTimeOffset end, bool allDayEvent, EventEncoding encoding = EventEncoding.Universal) : this(subject, description, location, start.UtcDateTime, end.UtcDateTime, allDayEvent, encoding) |
| 23 | + { |
| 24 | + } |
| 25 | + |
| 26 | + /// <summary> |
| 27 | + /// Generates a calender entry/event payload. |
| 28 | + /// </summary> |
| 29 | + /// <param name="subject">Subject/title of the calender event</param> |
| 30 | + /// <param name="description">Description of the event</param> |
| 31 | + /// <param name="location">Location (lat:long or address) of the event</param> |
| 32 | + /// <param name="start">Start time of the event</param> |
| 33 | + /// <param name="end">End time of the event</param> |
| 34 | + /// <param name="allDayEvent">Is it a full day event?</param> |
| 35 | + /// <param name="encoding">Type of encoding (universal or iCal)</param> |
| 36 | + public CalendarEvent(string subject, string description, string location, DateTime start, DateTime end, bool allDayEvent, EventEncoding encoding = EventEncoding.Universal) |
| 37 | + { |
| 38 | + this.subject = subject; |
| 39 | + this.description = description; |
| 40 | + this.location = location; |
| 41 | + this.encoding = encoding; |
| 42 | + string dtFormatStart = "yyyyMMdd", dtFormatEnd = "yyyyMMdd"; |
| 43 | + if (!allDayEvent) |
| 44 | + { |
| 45 | + dtFormatStart = dtFormatEnd = "yyyyMMddTHHmmss"; |
| 46 | + if (start.Kind == DateTimeKind.Utc) |
| 47 | + dtFormatStart = "yyyyMMddTHHmmssZ"; |
| 48 | + if (end.Kind == DateTimeKind.Utc) |
| 49 | + dtFormatEnd = "yyyyMMddTHHmmssZ"; |
| 50 | + } |
| 51 | + this.start = start.ToString(dtFormatStart); |
| 52 | + this.end = end.ToString(dtFormatEnd); |
| 53 | + } |
| 54 | + |
| 55 | + public override string ToString() |
| 56 | + { |
| 57 | + var vEvent = $"BEGIN:VEVENT{Environment.NewLine}"; |
| 58 | + vEvent += $"SUMMARY:{this.subject}{Environment.NewLine}"; |
| 59 | + vEvent += !string.IsNullOrEmpty(this.description) ? $"DESCRIPTION:{this.description}{Environment.NewLine}" : ""; |
| 60 | + vEvent += !string.IsNullOrEmpty(this.location) ? $"LOCATION:{this.location}{Environment.NewLine}" : ""; |
| 61 | + vEvent += $"DTSTART:{this.start}{Environment.NewLine}"; |
| 62 | + vEvent += $"DTEND:{this.end}{Environment.NewLine}"; |
| 63 | + vEvent += "END:VEVENT"; |
| 64 | + |
| 65 | + if (this.encoding == EventEncoding.iCalComplete) |
| 66 | + vEvent = $@"BEGIN:VCALENDAR{Environment.NewLine}VERSION:2.0{Environment.NewLine}{vEvent}{Environment.NewLine}END:VCALENDAR"; |
| 67 | + |
| 68 | + return vEvent; |
| 69 | + } |
| 70 | + |
| 71 | + public enum EventEncoding |
| 72 | + { |
| 73 | + iCalComplete, |
| 74 | + Universal |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | +} |
0 commit comments