Skip to content

Commit

Permalink
add - doc - Added CardKind enumeration
Browse files Browse the repository at this point in the history
---

We've added the CardKind enumeration that gives you a fuzzy representation of what would the vCard kind resemble.

---

Type: add
Breaking: False
Doc Required: True
Backport Required: False
Part: 1/1
  • Loading branch information
AptiviCEO committed Oct 2, 2024
1 parent 96e1138 commit 02bd877
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 5 deletions.
2 changes: 1 addition & 1 deletion VisualCard.ShowContacts/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ static void Main(string[] args)
TextWriterColor.Write("Contact URL: {0} [G: {1}]", url[0].Value, url[0].Group);
if (note.Length > 0)
TextWriterColor.Write("Contact Note: {0} [G: {1}]", note[0].Value, note[0].Group);
TextWriterColor.Write("Card kind: {0}", Contact.CardKind);
TextWriterColor.Write("Card kind: {0} [{1}]", Contact.CardKind, Contact.CardKindStr);

// Print VCard
string raw = Contact.SaveToString();
Expand Down
11 changes: 11 additions & 0 deletions VisualCard/Parsers/VcardCommonTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
using System.Text.RegularExpressions;
using VisualCard.Parsers.Arguments;
using VisualCard.Parsers.Recurrence;
using VisualCard.Parts.Enums;

namespace VisualCard.Parsers
{
Expand Down Expand Up @@ -668,5 +669,15 @@ internal static DateTimeOffset ParsePosixRepresentation(string posixDateRepresen
// Return the result
return finalValue;
}

internal static CardKind GetKindEnum(string kind) =>
kind.ToLower() switch
{
"individual" => CardKind.Individual,
"group" => CardKind.Group,
"organization" => CardKind.Organization,
"location" => CardKind.Location,
_ => CardKind.Others,
};
}
}
10 changes: 9 additions & 1 deletion VisualCard/Parsers/VcardParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ internal class VcardParser
{
internal Card[] nestedCards = [];
private readonly Version cardVersion = new();
private readonly string[] cardContent = [];
private string[] cardContent = [];

/// <summary>
/// VCard card content
Expand All @@ -66,6 +66,14 @@ public Card Parse()
// Make a new vCard
var card = new Card(CardVersion);

// Move kind to the top
if (CardVersion.Major >= 4)
{
string kindLine = CardContent.SingleOrDefault((line) => line.ToUpper().StartsWith(VcardConstants._kindSpecifier));
if (!string.IsNullOrEmpty(kindLine))
cardContent = [kindLine, .. cardContent.Where((line) => line != kindLine).ToArray()];
}

// Iterate through all the lines
bool constructing = false;
StringBuilder valueBuilder = new();
Expand Down
5 changes: 4 additions & 1 deletion VisualCard/Parsers/VcardParserTools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ namespace VisualCard.Parsers
internal class VcardParserTools
{
internal static bool StringSupported(StringsEnum stringsEnum, Version cardVersion, string kind) =>
StringSupported(stringsEnum, cardVersion, VcardCommonTools.GetKindEnum(kind));

internal static bool StringSupported(StringsEnum stringsEnum, Version cardVersion, CardKind kind) =>
stringsEnum switch
{
StringsEnum.Kind => cardVersion.Major >= 4,
Expand Down Expand Up @@ -56,7 +59,7 @@ internal static bool StringSupported(StringsEnum stringsEnum, Version cardVersio
StringsEnum.CalendarUrl => cardVersion.Major >= 4,
StringsEnum.CalendarSchedulingRequestUrl => cardVersion.Major >= 4,
StringsEnum.ContactUri => cardVersion.Major >= 4,
StringsEnum.Member => kind == "group" && cardVersion.Major == 4,
StringsEnum.Member => kind == CardKind.Group && cardVersion.Major == 4,
StringsEnum.Related => cardVersion.Major == 4,
StringsEnum.Expertise => cardVersion.Major >= 4,
StringsEnum.Hobby => cardVersion.Major >= 4,
Expand Down
10 changes: 8 additions & 2 deletions VisualCard/Parts/Card.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,17 @@ public class Card : IEquatable<Card>
GetString(StringsEnum.Uid).Length > 0 ? GetString(StringsEnum.Uid)[0].Value : "";

/// <summary>
/// Card kind
/// Card kind string
/// </summary>
public string CardKind =>
public string CardKindStr =>
strings.ContainsKey(StringsEnum.Kind) && strings[StringsEnum.Kind].Count > 0 ? strings[StringsEnum.Kind][0].Value : "individual";

/// <summary>
/// Card kind
/// </summary>
public CardKind CardKind =>
VcardCommonTools.GetKindEnum(CardKindStr);

/// <summary>
/// Gets a part array from a specified key
/// </summary>
Expand Down
52 changes: 52 additions & 0 deletions VisualCard/Parts/Enums/CardKind.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//
// 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.Text;

namespace VisualCard.Parts.Enums
{
/// <summary>
/// vCard kind
/// </summary>
public enum CardKind
{
/// <summary>
/// Card that contains information about a person
/// </summary>
Individual,
/// <summary>
/// Card that contains information about an organization
/// </summary>
Organization,
/// <summary>
/// Card that contains information about a group
/// </summary>
Group,
/// <summary>
/// Card that contains information about a location
/// </summary>
Location,
/// <summary>
/// Card that is of any other type
/// </summary>
Others,
}
}

0 comments on commit 02bd877

Please sign in to comment.