Skip to content

Commit

Permalink
add - doc - Added gender specifier
Browse files Browse the repository at this point in the history
---

We've added the gender specifier to the vCard parser.

---

Type: add
Breaking: False
Doc Required: True
Part: 1/1
  • Loading branch information
AptiviCEO committed Mar 29, 2024
1 parent c289fb9 commit 214e6d2
Show file tree
Hide file tree
Showing 9 changed files with 248 additions and 0 deletions.
3 changes: 3 additions & 0 deletions VisualCard.ShowContacts/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,13 @@ static void Main(string[] args)
// List remaining
var birth = Contact.GetPart(PartsEnum.Birthdate);
var wed = Contact.GetPart(PartsEnum.Anniversary);
var gnd = Contact.GetPart(PartsEnum.Gender);
if (birth is BirthDateInfo bday)
TextWriterColor.Write("Contact birthdate: {0}", bday.BirthDate);
if (wed is AnniversaryInfo adate)
TextWriterColor.Write("Contact wedding date: {0}", adate.Anniversary);
if (gnd is GenderInfo gender)
TextWriterColor.Write("Contact gender {0} [{1}]", gender.Gender.ToString(), gender.GenderDescription);
TextWriterColor.Write("Contact mailer: {0}", Contact.GetString(StringsEnum.Mailer));
TextWriterColor.Write("Contact URL: {0}", Contact.GetString(StringsEnum.Url));
TextWriterColor.Write("Contact Note: {0}", Contact.GetString(StringsEnum.Notes));
Expand Down
2 changes: 2 additions & 0 deletions VisualCard.ShowContacts/TestFiles/fourVCard4.vcf
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ TEL;TYPE=cell:1-234-567-890
TEL;TYPE=voice:078-494-6434
TEL;TYPE=home:348-404-8404
TITLE:Title
GENDER:M
X-ANDROID-CUSTOM:vnd.android.cursor.item/nickname;NVL.N;1;;;;;;;;;;;;;
END:VCARD

Expand All @@ -39,6 +40,7 @@ TITLE:Chief Executive Officer
URL:https://sso.org/
X-SIP-SIP:sip test
ANNIVERSARY:20230406
GENDER:F;grrrl
END:VCARD

BEGIN:VCARD
Expand Down
2 changes: 2 additions & 0 deletions VisualCard.ShowContacts/TestFiles/fourVCard5.vcf
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ TEL;TYPE=cell:1-234-567-890
TEL;TYPE=voice:078-494-6434
TEL;TYPE=home:348-404-8404
TITLE:Title
GENDER:M
X-ANDROID-CUSTOM:vnd.android.cursor.item/nickname;NVL.N;1;;;;;;;;;;;;;
END:VCARD

Expand All @@ -40,6 +41,7 @@ URL:https://sso.org/
X-SIP-SIP:sip test
SORT-STRING:sarahsantos
ANNIVERSARY:20230406
GENDER:F;grrrl
END:VCARD

BEGIN:VCARD
Expand Down
2 changes: 2 additions & 0 deletions VisualCard.ShowContacts/TestFiles/fourVCard5Agents.vcf
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ TEL;TYPE=cell:1-234-567-890
TEL;TYPE=voice:078-494-6434
TEL;TYPE=home:348-404-8404
TITLE:Title
GENDER:M
X-ANDROID-CUSTOM:vnd.android.cursor.item/nickname;NVL.N;1;;;;;;;;;;;;;
END:VCARD

Expand All @@ -40,6 +41,7 @@ URL:https://sso.org/
X-SIP-SIP:sip test
SORT-STRING:sarahsantos
ANNIVERSARY:20230406
GENDER:F;grrrl
END:VCARD

BEGIN:VCARD
Expand Down
12 changes: 12 additions & 0 deletions VisualCard/Parsers/BaseVcardParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,16 @@ bool StartsWithPrefix(string prefix) =>
var partInfo = AnniversaryInfo.FromStringVcardStatic(_value, altId, CardVersion, CardContentReader);
card.SetPart(PartsEnum.Anniversary, partInfo);
}

// IMPP information (IMPP;TYPE=home:sip:test)
// ALTID is supported.
if (StartsWithPrefix(VcardConstants._genderSpecifier) &&
EnumTypeSupported(PartsEnum.Gender, CardVersion))
{
// Get the name
var partInfo = GenderInfo.FromStringVcardStatic(_value, altId, CardVersion, CardContentReader);
card.SetPart(PartsEnum.Gender, partInfo);
}
}
catch (Exception ex)
{
Expand Down Expand Up @@ -755,6 +765,7 @@ internal static bool EnumTypeSupported(PartsEnum partsEnum, Version cardVersion)
PartsEnum.Revision => true,
PartsEnum.Birthdate => true,
PartsEnum.Anniversary => cardVersion.Major >= 4,
PartsEnum.Gender => cardVersion.Major >= 4,
_ =>
throw new InvalidOperationException("Invalid parts enumeration type to get supported value"),
};
Expand Down Expand Up @@ -790,6 +801,7 @@ internal static Type GetEnumType(PartsEnum partsEnum) =>
PartsEnum.Revision => typeof(RevisionInfo),
PartsEnum.Birthdate => typeof(BirthDateInfo),
PartsEnum.Anniversary => typeof(AnniversaryInfo),
PartsEnum.Gender => typeof(GenderInfo),
_ =>
throw new InvalidOperationException("Invalid parts enumeration type"),
};
Expand Down
1 change: 1 addition & 0 deletions VisualCard/Parsers/VcardConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ internal static class VcardConstants
// Available in vCard 4.0 and 5.0
internal const string _kindSpecifier = "KIND";
internal const string _anniversarySpecifier = "ANNIVERSARY";
internal const string _genderSpecifier = "GENDER";
internal const string _altIdArgumentSpecifier = "ALTID=";
}
}
4 changes: 4 additions & 0 deletions VisualCard/Parts/Enums/PartsEnum.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,9 @@ public enum PartsEnum
/// The contact's wedding anniversary date (that is, the day that this contact is married)
/// </summary>
Anniversary,
/// <summary>
/// The contact's gender
/// </summary>
Gender,
}
}
170 changes: 170 additions & 0 deletions VisualCard/Parts/Implementations/GenderInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
//
// 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 System.IO;
using System.Linq;
using VisualCard.Parsers;
using VisualCard.Parts.Implementations.Tools;

namespace VisualCard.Parts.Implementations
{
/// <summary>
/// Contact gender info
/// </summary>
[DebuggerDisplay("Gender = {Gender}")]
public class GenderInfo : BaseCardPartInfo, IEquatable<GenderInfo>
{
/// <summary>
/// The contact's gender
/// </summary>
public Gender Gender { get; }
/// <summary>
/// The contact's gender description
/// </summary>
public string GenderDescription { get; }

internal static BaseCardPartInfo FromStringVcardStatic(string value, int altId, Version cardVersion, StreamReader cardContentReader) =>
new GenderInfo().FromStringVcardInternal(value, altId, cardVersion, cardContentReader);

internal static BaseCardPartInfo FromStringVcardWithTypeStatic(string value, string[] finalArgs, int altId, Version cardVersion, StreamReader cardContentReader) =>
new GenderInfo().FromStringVcardWithTypeInternal(value, finalArgs, altId, cardVersion, cardContentReader);

internal override string ToStringVcardInternal(Version cardVersion) =>
$"{VcardConstants._genderSpecifier}{VcardConstants._argumentDelimiter}" +
(Gender != Gender.Unspecified ? Gender.ToString()[0] : "") +
(!string.IsNullOrEmpty(GenderDescription) ? $"{VcardConstants._fieldDelimiter}{GenderDescription}" : "");

internal override BaseCardPartInfo FromStringVcardInternal(string value, int altId, Version cardVersion, StreamReader cardContentReader)
{
// Get the value
string genderValue = value.Substring(VcardConstants._genderSpecifier.Length + 1);

// Populate the fields
return InstallInfo(genderValue);
}

internal override BaseCardPartInfo FromStringVcardWithTypeInternal(string value, string[] finalArgs, int altId, Version cardVersion, StreamReader cardContentReader)
{
// Get the value
string genderValue = value.Substring(value.IndexOf(VcardConstants._argumentDelimiter) + 1);

// Populate the fields
return InstallInfo(genderValue);
}

private GenderInfo InstallInfo(string value)
{
// Populate field
string genderString = value;
string genderDescription = "";

// Check to see if we have the description or not (as in GENDER:F;grrrl or GENDER:F)
bool hasGenderDescription = value.Contains(VcardConstants._fieldDelimiter);
if (hasGenderDescription)
{
// We have the description!
genderString = value.Substring(0, value.IndexOf(VcardConstants._fieldDelimiter));
genderDescription = value.Substring(value.IndexOf(VcardConstants._fieldDelimiter) + 1);
}

// Now, translate the gender string to its enum equivalent
Gender gender = genderString switch
{
"M" => Gender.Male,
"F" => Gender.Female,
"O" => Gender.Other,
"N" => Gender.NotApplicable,
"U" => Gender.Unknown,
"" => Gender.Unspecified,
_ =>
throw new InvalidDataException($"Invalid gender string {genderString}")
};

// Add the fetched information
GenderInfo _gender = new(0, [], gender, genderDescription);
return _gender;
}

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

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

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

// Check all the properties
return
source.AltArguments.SequenceEqual(target.AltArguments) &&
source.AltId == target.AltId &&
source.Gender == target.Gender &&
source.GenderDescription == target.GenderDescription
;
}

/// <inheritdoc/>
public override int GetHashCode()
{
int hashCode = -446160983;
hashCode = hashCode * -1521134295 + base.GetHashCode();
hashCode = hashCode * -1521134295 + EqualityComparer<string[]>.Default.GetHashCode(AltArguments);
hashCode = hashCode * -1521134295 + AltId.GetHashCode();
hashCode = hashCode * -1521134295 + Gender.GetHashCode();
hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(GenderDescription);
return hashCode;
}

/// <inheritdoc/>
public static bool operator ==(GenderInfo left, GenderInfo right) =>
EqualityComparer<GenderInfo>.Default.Equals(left, right);

/// <inheritdoc/>
public static bool operator !=(GenderInfo left, GenderInfo right) =>
!(left == right);

internal GenderInfo() { }

internal GenderInfo(int altId, string[] altArguments, Gender gender, string genderDescription)
{
AltId = altId;
AltArguments = altArguments;
Gender = gender;
GenderDescription = genderDescription;
}
}
}
52 changes: 52 additions & 0 deletions VisualCard/Parts/Implementations/Tools/Gender.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/>.
//

namespace VisualCard.Parts.Implementations.Tools
{
/// <summary>
/// Enumeration of gender
/// </summary>
public enum Gender
{
/// <summary>
/// Unspecified gender
/// </summary>
Unspecified,
/// <summary>
/// Male gender
/// </summary>
Male,
/// <summary>
/// Female gender
/// </summary>
Female,
/// <summary>
/// Other gender
/// </summary>
Other,
/// <summary>
/// Gender is not applicable
/// </summary>
NotApplicable,
/// <summary>
/// Unknown gender
/// </summary>
Unknown,
}
}

0 comments on commit 214e6d2

Please sign in to comment.