Skip to content

Commit

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

We've added all calendar components as classes. Now, we need to implement their properties, but we need to make sure that unsupported ones are not parsed.

---

Type: add
Breaking: False
Doc Required: False
Backport Required: False
Part: 1/2
  • Loading branch information
AptiviCEO committed Sep 10, 2024
1 parent 1089cbd commit 9cdb198
Show file tree
Hide file tree
Showing 11 changed files with 1,287 additions and 8 deletions.
13 changes: 11 additions & 2 deletions VisualCard.Calendar/Parsers/VCalendarConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,24 @@ namespace VisualCard.Calendar.Parsers
internal static class VCalendarConstants
{
// Mandatory for each vCalendar
internal const string _beginText = _beginSpecifier + ":VCALENDAR";
internal const string _endText = _endSpecifier + ":VCALENDAR";
internal const string _beginText = _beginSpecifier + ":" + _objectVCalendarSpecifier;
internal const string _endText = _endSpecifier + ":" + _objectVCalendarSpecifier;
internal const string _beginSpecifier = "BEGIN";
internal const string _endSpecifier = "END";
internal const string _versionSpecifier = "VERSION";

// Object specifiers
internal const string _objectVCalendarSpecifier = "VCALENDAR";
internal const string _objectVEventSpecifier = "VEVENT";
internal const string _objectVTodoSpecifier = "VTODO";

// Object specifiers (vCalendar 2.0)
internal const string _objectVJournalSpecifier = "VJOURNAL";
internal const string _objectVFreeBusySpecifier = "VFREEBUSY";
internal const string _objectVTimeZoneSpecifier = "VTIMEZONE";
internal const string _objectVStandardSpecifier = "VSTANDARD";
internal const string _objectVDaylightSpecifier = "VDAYLIGHT";
internal const string _objectVAlarmSpecifier = "VALARM";

// Misc vCalendar constants
internal const string _spaceBreak = " ";
Expand Down
265 changes: 260 additions & 5 deletions VisualCard.Calendar/Parsers/VCalendarParser.cs

Large diffs are not rendered by default.

30 changes: 29 additions & 1 deletion VisualCard.Calendar/Parts/Calendar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ namespace VisualCard.Calendar.Parts
public class Calendar : IEquatable<Calendar>
{
internal readonly List<CalendarEvent> events = [];
internal readonly List<CalendarTodo> todos = [];
internal readonly List<CalendarJournal> journals = [];
internal readonly List<CalendarFreeBusy> freeBusyList = [];
internal readonly List<CalendarTimeZone> timeZones = [];
private readonly Version version;
private readonly Dictionary<CalendarPartsArrayEnum, List<BaseCalendarPartInfo>> partsArray = [];
private readonly Dictionary<CalendarStringsEnum, string> strings = [];
Expand All @@ -57,11 +61,35 @@ public class Calendar : IEquatable<Calendar>
GetString(CalendarStringsEnum.Uid);

/// <summary>
/// Unique ID for this card
/// Event list
/// </summary>
public CalendarEvent[] Events =>
[.. events];

/// <summary>
/// To-do list
/// </summary>
public CalendarTodo[] Todos =>
[.. todos];

/// <summary>
/// Journal list
/// </summary>
public CalendarJournal[] Journals =>
[.. journals];

/// <summary>
/// Free/busy list
/// </summary>
public CalendarFreeBusy[] FreeBusyList =>
[.. freeBusyList];

/// <summary>
/// Time zone list
/// </summary>
public CalendarTimeZone[] TimeZones =>
[.. timeZones];

/// <summary>
/// Gets a part array from a specified key
/// </summary>
Expand Down
137 changes: 137 additions & 0 deletions VisualCard.Calendar/Parts/CalendarAlarm.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
//
// VisualCard Copyright (C) 2021-2024 Aptivi
//
// This file is part of VisualCard
//
// VisualCard is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// VisualCard is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY, without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//

using System;
using System.Collections.Generic;
using System.Diagnostics;
using VisualCard.Calendar.Parsers;
using VisualCard.Calendar.Parts.Comparers;
using VisualCard.Calendar.Parts.Enums;

namespace VisualCard.Calendar.Parts
{
/// <summary>
/// A vCalendar card instance
/// </summary>
[DebuggerDisplay("vCalendar alarm version {CalendarVersion.ToString()}, parts: (A [{partsArray.Count}] | S [{strings.Count}])")]
public class CalendarAlarm : Calendar, IEquatable<CalendarAlarm>
{
private readonly Dictionary<CalendarPartsArrayEnum, List<BaseCalendarPartInfo>> partsArray = [];
private readonly Dictionary<CalendarStringsEnum, string> strings = [];

/// <summary>
/// Gets a part array from a specified key
/// </summary>
/// <returns>An array of values or an empty part array []</returns>
public new TPart[] GetPartsArray<TPart>() where TPart : BaseCalendarPartInfo
{
// Get the parts enumeration according to the type
var key = VCalendarParserTools.GetPartsArrayEnumFromType(typeof(TPart), CalendarVersion);

// Now, return the value
return GetPartsArray<TPart>(key.Item1, CalendarVersion, partsArray);
}

/// <summary>
/// Gets a part array from a specified key
/// </summary>
/// <param name="key">A key to use</param>
/// <returns>An array of values or an empty part array []</returns>
public new TPart[] GetPartsArray<TPart>(CalendarPartsArrayEnum key) where TPart : BaseCalendarPartInfo =>
GetPartsArray<TPart>(key, CalendarVersion, partsArray);

/// <summary>
/// Gets a string from a specified key
/// </summary>
/// <param name="key">A key to use</param>
/// <returns>A value, or "individual" if the kind doesn't exist, or an empty string ("") if any other type either doesn't exist or the type is not supported by the card version</returns>
public new string GetString(CalendarStringsEnum key) =>
GetString(key, CalendarVersion, strings);

/// <summary>
/// Saves this parsed card to the string
/// </summary>
public new string SaveToString() =>
SaveToString(CalendarVersion, partsArray, strings, VCalendarConstants._objectVAlarmSpecifier);

/// <summary>
/// Saves the contact to the returned string
/// </summary>
public override string ToString() =>
SaveToString();

/// <inheritdoc/>
public override bool Equals(object obj) =>
Equals((CalendarAlarm)obj);

/// <summary>
/// Checks to see if both the cards are equal
/// </summary>
/// <param name="other">The target <see cref="Calendar"/> instance to check to see if they equal</param>
/// <returns>True if all the card elements are equal. Otherwise, false.</returns>
public bool Equals(CalendarAlarm other) =>
Equals(this, other);

/// <summary>
/// Checks to see if both the cards are equal
/// </summary>
/// <param name="source">The source <see cref="Calendar"/> instance to check to see if they equal</param>
/// <param name="target">The target <see cref="Calendar"/> instance to check to see if they equal</param>
/// <returns>True if all the card elements are equal. Otherwise, false.</returns>
public bool Equals(CalendarAlarm source, CalendarAlarm target)
{
// We can't perform this operation on null.
if (source is null || target is null)
return false;

// Check all the properties
return
PartComparison.PartsArrayEnumEqual(source.partsArray, target.partsArray) &&
PartComparison.StringsEqual(source.strings, target.strings)
;
}

/// <inheritdoc/>
public override int GetHashCode()
{
int hashCode = 1047895655;
hashCode = hashCode * -1521134295 + EqualityComparer<Dictionary<CalendarPartsArrayEnum, List<BaseCalendarPartInfo>>>.Default.GetHashCode(partsArray);
hashCode = hashCode * -1521134295 + EqualityComparer<Dictionary<CalendarStringsEnum, string>>.Default.GetHashCode(strings);
return hashCode;
}

/// <inheritdoc/>
public static bool operator ==(CalendarAlarm a, CalendarAlarm b)
=> a.Equals(b);

/// <inheritdoc/>
public static bool operator !=(CalendarAlarm a, CalendarAlarm b)
=> !a.Equals(b);

internal new void AddPartToArray(CalendarPartsArrayEnum key, BaseCalendarPartInfo value) =>
AddPartToArray(key, value, CalendarVersion, partsArray);

internal new void SetString(CalendarStringsEnum key, string value) =>
SetString(key, value, strings);

internal CalendarAlarm(Version version) :
base(version)
{ }
}
}
137 changes: 137 additions & 0 deletions VisualCard.Calendar/Parts/CalendarDaylight.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
//
// VisualCard Copyright (C) 2021-2024 Aptivi
//
// This file is part of VisualCard
//
// VisualCard is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// VisualCard is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY, without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//

using System;
using System.Collections.Generic;
using System.Diagnostics;
using VisualCard.Calendar.Parsers;
using VisualCard.Calendar.Parts.Comparers;
using VisualCard.Calendar.Parts.Enums;

namespace VisualCard.Calendar.Parts
{
/// <summary>
/// A vCalendar card instance
/// </summary>
[DebuggerDisplay("vCalendar daylight time version {CalendarVersion.ToString()}, parts: (A [{partsArray.Count}] | S [{strings.Count}])")]
public class CalendarDaylight : Calendar, IEquatable<CalendarDaylight>
{
private readonly Dictionary<CalendarPartsArrayEnum, List<BaseCalendarPartInfo>> partsArray = [];
private readonly Dictionary<CalendarStringsEnum, string> strings = [];

/// <summary>
/// Gets a part array from a specified key
/// </summary>
/// <returns>An array of values or an empty part array []</returns>
public new TPart[] GetPartsArray<TPart>() where TPart : BaseCalendarPartInfo
{
// Get the parts enumeration according to the type
var key = VCalendarParserTools.GetPartsArrayEnumFromType(typeof(TPart), CalendarVersion);

// Now, return the value
return GetPartsArray<TPart>(key.Item1, CalendarVersion, partsArray);
}

/// <summary>
/// Gets a part array from a specified key
/// </summary>
/// <param name="key">A key to use</param>
/// <returns>An array of values or an empty part array []</returns>
public new TPart[] GetPartsArray<TPart>(CalendarPartsArrayEnum key) where TPart : BaseCalendarPartInfo =>
GetPartsArray<TPart>(key, CalendarVersion, partsArray);

/// <summary>
/// Gets a string from a specified key
/// </summary>
/// <param name="key">A key to use</param>
/// <returns>A value, or "individual" if the kind doesn't exist, or an empty string ("") if any other type either doesn't exist or the type is not supported by the card version</returns>
public new string GetString(CalendarStringsEnum key) =>
GetString(key, CalendarVersion, strings);

/// <summary>
/// Saves this parsed card to the string
/// </summary>
public new string SaveToString() =>
SaveToString(CalendarVersion, partsArray, strings, VCalendarConstants._objectVDaylightSpecifier);

/// <summary>
/// Saves the contact to the returned string
/// </summary>
public override string ToString() =>
SaveToString();

/// <inheritdoc/>
public override bool Equals(object obj) =>
Equals((CalendarDaylight)obj);

/// <summary>
/// Checks to see if both the cards are equal
/// </summary>
/// <param name="other">The target <see cref="Calendar"/> instance to check to see if they equal</param>
/// <returns>True if all the card elements are equal. Otherwise, false.</returns>
public bool Equals(CalendarDaylight other) =>
Equals(this, other);

/// <summary>
/// Checks to see if both the cards are equal
/// </summary>
/// <param name="source">The source <see cref="Calendar"/> instance to check to see if they equal</param>
/// <param name="target">The target <see cref="Calendar"/> instance to check to see if they equal</param>
/// <returns>True if all the card elements are equal. Otherwise, false.</returns>
public bool Equals(CalendarDaylight source, CalendarDaylight target)
{
// We can't perform this operation on null.
if (source is null || target is null)
return false;

// Check all the properties
return
PartComparison.PartsArrayEnumEqual(source.partsArray, target.partsArray) &&
PartComparison.StringsEqual(source.strings, target.strings)
;
}

/// <inheritdoc/>
public override int GetHashCode()
{
int hashCode = 1047895655;
hashCode = hashCode * -1521134295 + EqualityComparer<Dictionary<CalendarPartsArrayEnum, List<BaseCalendarPartInfo>>>.Default.GetHashCode(partsArray);
hashCode = hashCode * -1521134295 + EqualityComparer<Dictionary<CalendarStringsEnum, string>>.Default.GetHashCode(strings);
return hashCode;
}

/// <inheritdoc/>
public static bool operator ==(CalendarDaylight a, CalendarDaylight b)
=> a.Equals(b);

/// <inheritdoc/>
public static bool operator !=(CalendarDaylight a, CalendarDaylight b)
=> !a.Equals(b);

internal new void AddPartToArray(CalendarPartsArrayEnum key, BaseCalendarPartInfo value) =>
AddPartToArray(key, value, CalendarVersion, partsArray);

internal new void SetString(CalendarStringsEnum key, string value) =>
SetString(key, value, strings);

internal CalendarDaylight(Version version) :
base(version)
{ }
}
}
7 changes: 7 additions & 0 deletions VisualCard.Calendar/Parts/CalendarEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,16 @@ namespace VisualCard.Calendar.Parts
[DebuggerDisplay("vCalendar event version {CalendarVersion.ToString()}, parts: (A [{partsArray.Count}] | S [{strings.Count}])")]
public class CalendarEvent : Calendar, IEquatable<CalendarEvent>
{
internal readonly List<CalendarAlarm> alarms = [];
private readonly Dictionary<CalendarPartsArrayEnum, List<BaseCalendarPartInfo>> partsArray = [];
private readonly Dictionary<CalendarStringsEnum, string> strings = [];

/// <summary>
/// Alarm list
/// </summary>
public CalendarAlarm[] Alarms =>
[.. alarms];

/// <summary>
/// Gets a part array from a specified key
/// </summary>
Expand Down
Loading

0 comments on commit 9cdb198

Please sign in to comment.