diff --git a/VisualCard.ShowContacts/TestFiles/four.vcf b/VisualCard.ShowContacts/TestFiles/four.vcf index 4de60d5..22d732d 100644 --- a/VisualCard.ShowContacts/TestFiles/four.vcf +++ b/VisualCard.ShowContacts/TestFiles/four.vcf @@ -9,6 +9,15 @@ TEL;CELL:495-522-3560 EMAIL;HOME:john.s@acme.co ADR;HOME:;;Los Angeles, USA;;;; ORG:Acme Co. +AGENT: +BEGIN:VCARD +VERSION:2.1 +FN:Joe Friday +N:Friday;Joe;;; +TEL:+1-919-555-7878 +TITLE:Area Administrator, Assistant +EMAIL;TYPE=INTERNET:jfriday@host.com +END:VCARD TITLE:Product Manager NOTE:Note test for VisualCard X-AIM:john.s diff --git a/VisualCard.ShowContacts/TestFiles/fourVCard3.vcf b/VisualCard.ShowContacts/TestFiles/fourVCard3.vcf index 5a6d10c..a37c999 100644 --- a/VisualCard.ShowContacts/TestFiles/fourVCard3.vcf +++ b/VisualCard.ShowContacts/TestFiles/fourVCard3.vcf @@ -9,6 +9,9 @@ TEL;TYPE=cell:495-522-3560 EMAIL;TYPE=home:john.s@acme.co ADR;TYPE=home:;;Los Angeles, USA;;;; ORG:Acme Co. +AGENT:BEGIN:VCARD\nVERSION:5.0\nFN:Joe Friday\nTEL:+1-919-555-7878\nTITLE: + Area Administrator, Assistant\nEMAIL;TYPE=INTERNET:jfriday@host.com\nN:Fri + day;Joe;;;\nEND:VCARD\n TITLE:Product Manager NOTE:Note test for VisualCard X-AIM:john.s diff --git a/VisualCard/CardTools.cs b/VisualCard/CardTools.cs index 33384db..f8c559c 100644 --- a/VisualCard/CardTools.cs +++ b/VisualCard/CardTools.cs @@ -85,6 +85,7 @@ public static Card[] GetCards(StreamReader stream) List<(int, string)> lines = []; bool nested = false; bool versionDirect = false; + bool isAgent = false; int lineNumber = 0; while (!stream.EndOfStream) { @@ -114,11 +115,11 @@ public static Card[] GetCards(StreamReader stream) if (!stream.EndOfStream) continue; } - else if (!prefix.EqualsNoCase(VcardConstants._beginSpecifier) && + else if ((!prefix.EqualsNoCase(VcardConstants._beginSpecifier) && !prefix.EqualsNoCase(VcardConstants._versionSpecifier) && - !prefix.EqualsNoCase(VcardConstants._endSpecifier)) + !prefix.EqualsNoCase(VcardConstants._endSpecifier)) || isAgent) append = true; - else if (prefix.EqualsNoCase(VcardConstants._beginSpecifier) && nested) + else if (prefix.EqualsNoCase(VcardConstants._beginSpecifier) && nested && !isAgent) { // We have a nested card! StringBuilder nestedBuilder = new(); @@ -143,7 +144,18 @@ public static Card[] GetCards(StreamReader stream) continue; } if (append) - lines.Add((lineNumber, CardLine)); + lines.Add((lineNumber, isAgent ? (CardLine.StartsWith(" ") ? CardLine : $" {CardLine}\\n") : CardLine)); + + // Check for agent property + if (prefix == VcardConstants._agentSpecifier && string.IsNullOrEmpty(value)) + isAgent = true; + else if (prefix == VcardConstants._endSpecifier && isAgent) + { + isAgent = false; + continue; + } + else if (isAgent) + continue; // All VCards must begin with BEGIN:VCARD if (!prefix.EqualsNoCase(VcardConstants._beginSpecifier) && !value.EqualsNoCase(VcardConstants._objectVCardSpecifier) && !BeginSpotted) diff --git a/VisualCard/Parsers/VcardCommonTools.cs b/VisualCard/Parsers/VcardCommonTools.cs index ddb4721..d589401 100644 --- a/VisualCard/Parsers/VcardCommonTools.cs +++ b/VisualCard/Parsers/VcardCommonTools.cs @@ -503,7 +503,7 @@ argFromSpecifier is not null ? return argString; } - internal static string MakeStringBlock(string target, int firstLength = 0) + internal static string MakeStringBlock(string target, int firstLength = 0, bool writeSpace = true) { const int maxChars = 74; int maxCharsFirst = maxChars - firstLength; @@ -517,9 +517,11 @@ internal static string MakeStringBlock(string target, int firstLength = 0) if (processed >= selectedMax || target[currCharNum] == '\n') { // Append a new line because we reached the maximum limit - selectedMax = maxChars - 1; + selectedMax = writeSpace ? maxChars - 1 : maxChars; processed = 0; - block.Append("\n "); + block.Append("\n"); + if (writeSpace) + block.Append(" "); } if (target[currCharNum] != '\n' && target[currCharNum] != '\r') { diff --git a/VisualCard/Parts/Card.cs b/VisualCard/Parts/Card.cs index 29d6085..c25a660 100644 --- a/VisualCard/Parts/Card.cs +++ b/VisualCard/Parts/Card.cs @@ -251,11 +251,15 @@ public string SaveToString() string partArguments = CardBuilderTools.BuildArguments(part, defaultType, defaultValueType); string[] partArgumentsLines = partArguments.SplitNewLines(); string group = part.Group; + + // Special treatment for vCard 2.1's AGENT property: add the AGENT vcard line by line + if (partsArrayEnum == PartsArrayEnum.Agents && version.Major == 2) + partRepresentation = "\n" + string.Join("\n", partRepresentation.Split(["\\n", "\\N"], StringSplitOptions.None)); if (!string.IsNullOrEmpty(group)) cardBuilder.Append($"{group}."); partBuilder.Append($"{prefix}"); partBuilder.Append($"{partArguments}"); - partBuilder.Append($"{VcardCommonTools.MakeStringBlock(partRepresentation, partArgumentsLines[partArgumentsLines.Length - 1].Length + prefix.Length)}"); + partBuilder.Append($"{VcardCommonTools.MakeStringBlock(partRepresentation, partArgumentsLines[partArgumentsLines.Length - 1].Length + prefix.Length, !(partsArrayEnum == PartsArrayEnum.Agents && version.Major == 2))}"); cardBuilder.AppendLine($"{partBuilder}"); } }