Skip to content

Commit

Permalink
add - prt - Added calendar components (pt. 2)
Browse files Browse the repository at this point in the history
---

We've finally added some required properties along with the type restriction.

Now, we need to add most of the properties found in all the calendar components.

---

Type: add
Breaking: False
Doc Required: False
Backport Required: False
Part: 2/2
  • Loading branch information
AptiviCEO committed Sep 10, 2024
1 parent 9cdb198 commit 0404bc8
Show file tree
Hide file tree
Showing 10 changed files with 317 additions and 41 deletions.
5 changes: 5 additions & 0 deletions VisualCard.Calendar/Parsers/VCalendarConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ internal static class VCalendarConstants
internal const string _transparencySpecifier = "TRANSP";
internal const string _createdSpecifier = "CREATED";
internal const string _created1Specifier = "DCREATED";
internal const string _actionSpecifier = "ACTION";
internal const string _triggerSpecifier = "TRIGGER";
internal const string _tzidSpecifier = "TZID";
internal const string _tzOffsetFromSpecifier = "TZOFFSETFROM";
internal const string _tzOffsetToSpecifier = "TZOFFSETTO";
internal const string _xSpecifier = "X-";
internal const string _typeArgumentSpecifier = "TYPE=";
internal const string _valueArgumentSpecifier = "VALUE=";
Expand Down
9 changes: 8 additions & 1 deletion VisualCard.Calendar/Parsers/VCalendarParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -168,13 +168,17 @@ public Parts.Calendar Parse()
}

// Handle the part type
Type calendarType = subPart is not null ? subPart.GetType() : calendar.GetType();
string values = VcardParserTools.GetValuesString(splitArgs, defaultValue, VCalendarConstants._valueArgumentSpecifier);
switch (type)
{
case PartType.Strings:
{
CalendarStringsEnum stringType = (CalendarStringsEnum)enumeration;
string finalValue = value;
bool supported = VCalendarParserTools.StringSupported(stringType, CalendarVersion, calendarType);
if (!supported)
continue;

// Now, handle each type individually
switch (stringType)
Expand Down Expand Up @@ -226,6 +230,9 @@ public Parts.Calendar Parse()
CalendarIntegersEnum integerType = (CalendarIntegersEnum)enumeration;
int primaryValue = int.Parse(value);
int finalValue = 0;
bool supported = VCalendarParserTools.IntegerSupported(integerType, CalendarVersion, calendarType);
if (!supported)
continue;

// Now, handle each type individually
switch (integerType)
Expand Down Expand Up @@ -257,7 +264,7 @@ public Parts.Calendar Parse()
{
CalendarPartsArrayEnum partsArrayType = (CalendarPartsArrayEnum)enumeration;
Type? partsArrayClass = classType;
bool supported = VCalendarParserTools.EnumArrayTypeSupported(partsArrayType, CalendarVersion);
bool supported = VCalendarParserTools.EnumArrayTypeSupported(partsArrayType, CalendarVersion, calendarType);
if (!supported)
continue;
if (fromString is null)
Expand Down
67 changes: 43 additions & 24 deletions VisualCard.Calendar/Parsers/VCalendarParserTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,53 +23,59 @@
using VisualCard.Calendar.Parts.Enums;
using VisualCard.Calendar.Parts.Implementations.Event;
using VisualCard.Parts.Enums;
using System.Linq;

namespace VisualCard.Calendar.Parsers
{
internal class VCalendarParserTools
{
internal static bool StringSupported(CalendarStringsEnum stringsEnum, Version calendarVersion) =>
internal static bool StringSupported(CalendarStringsEnum stringsEnum, Version calendarVersion, Type componentType) =>
stringsEnum switch
{
CalendarStringsEnum.ProductId => true,
CalendarStringsEnum.CalScale => calendarVersion.Major == 2,
CalendarStringsEnum.Method => calendarVersion.Major == 2,
CalendarStringsEnum.Class => true,
CalendarStringsEnum.Uid => true,
CalendarStringsEnum.Organizer => true,
CalendarStringsEnum.Status => true,
CalendarStringsEnum.Summary => true,
CalendarStringsEnum.Description => true,
CalendarStringsEnum.Transparency => true,
CalendarStringsEnum.Class => TypeMatch(componentType, typeof(CalendarEvent), typeof(CalendarTodo), typeof(CalendarJournal)),
CalendarStringsEnum.Uid => TypeMatch(componentType, typeof(CalendarEvent), typeof(CalendarTodo), typeof(CalendarJournal), typeof(CalendarFreeBusy)),
CalendarStringsEnum.Organizer => TypeMatch(componentType, typeof(CalendarEvent), typeof(CalendarTodo), typeof(CalendarJournal), typeof(CalendarFreeBusy)),
CalendarStringsEnum.Status => TypeMatch(componentType, typeof(CalendarEvent), typeof(CalendarTodo), typeof(CalendarJournal)),
CalendarStringsEnum.Summary => TypeMatch(componentType, typeof(CalendarEvent), typeof(CalendarTodo), typeof(CalendarJournal), typeof(CalendarAlarm)),
CalendarStringsEnum.Description => TypeMatch(componentType, typeof(CalendarEvent), typeof(CalendarTodo), typeof(CalendarJournal), typeof(CalendarAlarm)),
CalendarStringsEnum.Transparency => TypeMatch(componentType, typeof(CalendarEvent)),
CalendarStringsEnum.Action => calendarVersion.Major == 2 && TypeMatch(componentType, typeof(CalendarAlarm)),
CalendarStringsEnum.Trigger => calendarVersion.Major == 2 && TypeMatch(componentType, typeof(CalendarAlarm)),
CalendarStringsEnum.TimeZoneId => calendarVersion.Major == 2 && TypeMatch(componentType, typeof(TimeZoneInfo)),
_ =>
throw new InvalidOperationException("Invalid string enumeration type to get supported value"),
};

internal static bool IntegerSupported(CalendarIntegersEnum integersEnum, Version calendarVersion) =>
internal static bool IntegerSupported(CalendarIntegersEnum integersEnum, Version calendarVersion, Type componentType) =>
integersEnum switch
{
CalendarIntegersEnum.Priority => calendarVersion.Major == 2,
CalendarIntegersEnum.Sequence => true,
CalendarIntegersEnum.Priority => calendarVersion.Major == 2 && TypeMatch(componentType, typeof(CalendarEvent), typeof(CalendarTodo)),
CalendarIntegersEnum.Sequence => TypeMatch(componentType, typeof(CalendarEvent), typeof(CalendarTodo), typeof(CalendarJournal)),
CalendarIntegersEnum.TimeZoneOffsetFrom => TypeMatch(componentType, typeof(CalendarStandard), typeof(CalendarDaylight)),
CalendarIntegersEnum.TimeZoneOffsetTo => TypeMatch(componentType, typeof(CalendarStandard), typeof(CalendarDaylight)),
_ =>
throw new InvalidOperationException("Invalid integer enumeration type to get supported value"),
};

internal static bool EnumArrayTypeSupported(CalendarPartsArrayEnum partsArrayEnum, Version calendarVersion) =>
internal static bool EnumArrayTypeSupported(CalendarPartsArrayEnum partsArrayEnum, Version calendarVersion, Type componentType) =>
partsArrayEnum switch
{
CalendarPartsArrayEnum.Attach => true,
CalendarPartsArrayEnum.Categories => true,
CalendarPartsArrayEnum.Comment => calendarVersion.Major == 2,
CalendarPartsArrayEnum.Geography => true,
CalendarPartsArrayEnum.Location => calendarVersion.Major == 2,
CalendarPartsArrayEnum.Resources => true,
CalendarPartsArrayEnum.Attendee => true,
CalendarPartsArrayEnum.DateCreated => true,
CalendarPartsArrayEnum.DateCreatedAlt => true,
CalendarPartsArrayEnum.DateStart => true,
CalendarPartsArrayEnum.DateEnd => true,
CalendarPartsArrayEnum.Attach => TypeMatch(componentType, typeof(CalendarEvent), typeof(CalendarTodo), typeof(CalendarJournal), typeof(CalendarAlarm)),
CalendarPartsArrayEnum.Categories => TypeMatch(componentType, typeof(CalendarEvent), typeof(CalendarTodo), typeof(CalendarJournal)),
CalendarPartsArrayEnum.Comment => calendarVersion.Major == 2 && TypeMatch(componentType, typeof(CalendarEvent), typeof(CalendarTodo), typeof(CalendarJournal), typeof(CalendarFreeBusy), typeof(CalendarStandard), typeof(CalendarDaylight)),
CalendarPartsArrayEnum.Geography => TypeMatch(componentType, typeof(CalendarEvent), typeof(CalendarTodo)),
CalendarPartsArrayEnum.Location => calendarVersion.Major == 2 && TypeMatch(componentType, typeof(CalendarEvent), typeof(CalendarTodo)),
CalendarPartsArrayEnum.Resources => TypeMatch(componentType, typeof(CalendarEvent), typeof(CalendarTodo)),
CalendarPartsArrayEnum.Attendee => TypeMatch(componentType, typeof(CalendarEvent), typeof(CalendarTodo), typeof(CalendarJournal), typeof(CalendarFreeBusy), typeof(CalendarAlarm)),
CalendarPartsArrayEnum.DateCreated => TypeMatch(componentType, typeof(CalendarEvent), typeof(CalendarTodo), typeof(CalendarJournal)),
CalendarPartsArrayEnum.DateCreatedAlt => TypeMatch(componentType, typeof(CalendarEvent), typeof(CalendarTodo)),
CalendarPartsArrayEnum.DateStart => TypeMatch(componentType, typeof(CalendarEvent)),
CalendarPartsArrayEnum.DateEnd => TypeMatch(componentType, typeof(CalendarEvent)),
CalendarPartsArrayEnum.NonstandardNames => true,
CalendarPartsArrayEnum.DateStamp => calendarVersion.Major == 2,
CalendarPartsArrayEnum.DateStamp => calendarVersion.Major == 2 && TypeMatch(componentType, typeof(CalendarEvent), typeof(CalendarTodo), typeof(CalendarJournal), typeof(CalendarFreeBusy)),
_ =>
throw new InvalidOperationException("Invalid parts array enumeration type to get supported value"),
};
Expand All @@ -87,6 +93,9 @@ internal static string GetPrefixFromStringsEnum(CalendarStringsEnum stringsEnum)
CalendarStringsEnum.Summary => VCalendarConstants._summarySpecifier,
CalendarStringsEnum.Description => VCalendarConstants._descriptionSpecifier,
CalendarStringsEnum.Transparency => VCalendarConstants._transparencySpecifier,
CalendarStringsEnum.Action => VCalendarConstants._actionSpecifier,
CalendarStringsEnum.Trigger => VCalendarConstants._triggerSpecifier,
CalendarStringsEnum.TimeZoneId => VCalendarConstants._tzidSpecifier,
_ =>
throw new NotImplementedException($"String enumeration {stringsEnum} is not implemented.")
};
Expand All @@ -96,6 +105,8 @@ internal static string GetPrefixFromIntegersEnum(CalendarIntegersEnum integersEn
{
CalendarIntegersEnum.Priority => VCalendarConstants._prioritySpecifier,
CalendarIntegersEnum.Sequence => VCalendarConstants._sequenceSpecifier,
CalendarIntegersEnum.TimeZoneOffsetFrom => VCalendarConstants._tzOffsetFromSpecifier,
CalendarIntegersEnum.TimeZoneOffsetTo => VCalendarConstants._tzOffsetToSpecifier,
_ =>
throw new NotImplementedException($"Integer enumeration {integersEnum} is not implemented.")
};
Expand Down Expand Up @@ -179,10 +190,18 @@ internal static (PartType type, object enumeration, Type? enumType, Func<string,
VCalendarConstants._summarySpecifier => (PartType.Strings, CalendarStringsEnum.Summary, null, null, "", "", []),
VCalendarConstants._descriptionSpecifier => (PartType.Strings, CalendarStringsEnum.Description, null, null, "", "", []),
VCalendarConstants._transparencySpecifier => (PartType.Strings, CalendarStringsEnum.Transparency, null, null, "", "", []),
VCalendarConstants._actionSpecifier => (PartType.Strings, CalendarStringsEnum.Action, null, null, "", "", []),
VCalendarConstants._triggerSpecifier => (PartType.Strings, CalendarStringsEnum.Trigger, null, null, "", "", []),
VCalendarConstants._tzidSpecifier => (PartType.Strings, CalendarStringsEnum.TimeZoneId, null, null, "", "", []),
VCalendarConstants._prioritySpecifier => (PartType.Integers, CalendarIntegersEnum.Priority, null, null, "", "", []),
VCalendarConstants._sequenceSpecifier => (PartType.Integers, CalendarIntegersEnum.Sequence, null, null, "", "", []),
VCalendarConstants._tzOffsetFromSpecifier => (PartType.Integers, CalendarIntegersEnum.TimeZoneOffsetFrom, null, null, "", "", []),
VCalendarConstants._tzOffsetToSpecifier => (PartType.Integers, CalendarIntegersEnum.TimeZoneOffsetTo, null, null, "", "", []),
_ =>
throw new InvalidOperationException($"Unknown prefix {prefix}"),
};

private static bool TypeMatch(Type componentType, params Type[] expectedTypes) =>
expectedTypes.Contains(componentType);
}
}
6 changes: 3 additions & 3 deletions VisualCard.Calendar/Parts/Calendar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ internal BaseCalendarPartInfo[] GetPartsArray(Type partType, Version version, Di
internal BaseCalendarPartInfo[] GetPartsArray(Type partType, CalendarPartsArrayEnum key, Version version, Dictionary<CalendarPartsArrayEnum, List<BaseCalendarPartInfo>> partsArray)
{
// Check for version support
if (!VCalendarParserTools.EnumArrayTypeSupported(key, version))
if (!VCalendarParserTools.EnumArrayTypeSupported(key, version, GetType()))
return [];

// Get the parts enumeration according to the type
Expand Down Expand Up @@ -209,7 +209,7 @@ public string GetString(CalendarStringsEnum key) =>
internal string GetString(CalendarStringsEnum key, Version version, Dictionary<CalendarStringsEnum, string> strings)
{
// Check for version support
if (!VCalendarParserTools.StringSupported(key, version))
if (!VCalendarParserTools.StringSupported(key, version, GetType()))
return "";

// Get the fallback value
Expand All @@ -236,7 +236,7 @@ public int GetInteger(CalendarIntegersEnum key) =>
internal int GetInteger(CalendarIntegersEnum key, Version version, Dictionary<CalendarIntegersEnum, int> integers)
{
// Check for version support
if (!VCalendarParserTools.IntegerSupported(key, version))
if (!VCalendarParserTools.IntegerSupported(key, version, GetType()))
return -1;

// Get the fallback value
Expand Down
8 changes: 8 additions & 0 deletions VisualCard.Calendar/Parts/Enums/CalendarIntegersEnum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,13 @@ public enum CalendarIntegersEnum
/// The calendar's sequence (event, to-do, or journal)
/// </summary>
Sequence,
/// <summary>
/// Time zone offset from
/// </summary>
TimeZoneOffsetFrom,
/// <summary>
/// Time zone offset to
/// </summary>
TimeZoneOffsetTo,
}
}
4 changes: 2 additions & 2 deletions VisualCard.Calendar/Parts/Enums/CalendarPartsArrayEnum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public enum CalendarPartsArrayEnum
/// </summary>
DateCreated,
/// <summary>
/// Calendar date of creation (vCalendar 1.0) (event, todo, or journal)
/// Calendar date of creation (vCalendar 1.0) (event or todo)
/// </summary>
DateCreatedAlt,
/// <summary>
Expand All @@ -69,7 +69,7 @@ public enum CalendarPartsArrayEnum
/// </summary>
DateEnd,
/// <summary>
/// Event date stamp
/// Date stamp (event, todo, journal, or free/busy)
/// </summary>
DateStamp,
/// <summary>
Expand Down
12 changes: 12 additions & 0 deletions VisualCard.Calendar/Parts/Enums/CalendarStringsEnum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,17 @@ public enum CalendarStringsEnum
/// Event transparency
/// </summary>
Transparency,
/// <summary>
/// Alarm action
/// </summary>
Action,
/// <summary>
/// Alarm trigger
/// </summary>
Trigger,
/// <summary>
/// Time zone ID
/// </summary>
TimeZoneId,
}
}
4 changes: 4 additions & 0 deletions VisualCard.ShowCalendars/Properties/launchSettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
"vCalendar 2.0 test - with X-nonstandard properties": {
"commandName": "Project",
"commandLineArgs": "TestFiles/veventXnonstandard.ics"
},
"vCalendar 2.0 test - event with time zone": {
"commandName": "Project",
"commandLineArgs": "TestFiles/vcalendarEventTz.ics"
}
}
}
Loading

0 comments on commit 0404bc8

Please sign in to comment.