コード例 #1
0
    public void ParseOutlook()
    {
        IList <VCard> vcard = VCard.LoadVcf(fileName: TestFiles.OutlookV2vcf);

        Assert.IsNotNull(vcard);
        Assert.IsNotNull(vcard.FirstOrDefault());

        //string s = vcard[0].ToString();

        DataProperty?photo = vcard[0].Photos?.FirstOrDefault();

        Assert.IsNotNull(photo);

        if (photo?.Value is DataUrl dataUrl)
        {
            Assert.AreEqual(dataUrl.MimeType.MediaType, "image/jpeg");
            //System.IO.File.WriteAllBytes(System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), $"Testbild{dataUrl.GetFileExtension()}"), dataUrl.GetEmbeddedBytes());
        }
        else
        {
            Assert.Fail();
        }

        VCard.SaveVcf(System.IO.Path.Combine(
                          Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), $"TestV2.1.vcf"),
                      vcard !, VCdVersion.V2_1, options: VcfOptions.Default.Set(VcfOptions.WriteNonStandardProperties));
    }
コード例 #2
0
ファイル: V3Tests.cs プロジェクト: FolkerKinzel/VCards
    public void ParseTest()
    {
        IList <VCard>?vcard = VCard.LoadVcf(TestFiles.V3vcf);

        Assert.IsNotNull(vcard);
        Assert.AreEqual(2, vcard.Count);
    }
コード例 #3
0
    public void Parse()
    {
        IList <VCard> vcard = VCard.LoadVcf(fileName: TestFiles.V2vcf);

        Assert.IsNotNull(vcard);
        Assert.AreNotEqual(0, vcard.Count);
    }
コード例 #4
0
ファイル: V3Tests.cs プロジェクト: FolkerKinzel/VCards
    public void ParseTest2()
    {
        IList <VCard>?vcard = VCard.LoadVcf(@"C:\Users\fkinz\OneDrive\Kontakte\Thunderbird\21-01-13.vcf");

        Assert.IsNotNull(vcard);
        Assert.AreNotEqual(0, vcard.Count);
    }
コード例 #5
0
ファイル: V3Tests.cs プロジェクト: FolkerKinzel/VCards
    public void ParseTest3()
    {
        IList <VCard>?vcard = VCard.LoadVcf(TestFiles.PhotoV3vcf);

        Assert.IsNotNull(vcard);
        Assert.AreNotEqual(0, vcard.Count);
    }
コード例 #6
0
ファイル: VCardExample.cs プロジェクト: FolkerKinzel/VCards
    public static void ReadingAndWritingVCard(string directoryPath)
    {
        string v2FilePath = Path.Combine(directoryPath, v2FileName);
        string v3FilePath = Path.Combine(directoryPath, v3FileName);
        string v4FilePath = Path.Combine(directoryPath, v4FileName);

        VCard vcard = InitializeTheVCardAndFillItWithData(directoryPath, photoFileName);

        // Implements ITimeZoneIDConverter to convert IANA time zone names to UTC-Offsets.
        // (See the implementation in the example below.)
        VC::ITimeZoneIDConverter tzConverter = new TimeZoneIDConverter();

        // Save vcard as vCard 2.1:
        vcard.SaveVcf(v2FilePath, VC::Enums.VCdVersion.V2_1, tzConverter);

        // Save vcard as vCard 3.0:
        // You don't need to specify the version: Version 3.0 is the default.
        vcard.SaveVcf(v3FilePath, tzConverter: tzConverter);

        // Save vcard as vCard 4.0. (A time zone converter is not needed):
        vcard.SaveVcf(v4FilePath, VC::Enums.VCdVersion.V4_0);

        // Load vCard:
        vcard = VCard.LoadVcf(v3FilePath)[0];

        WriteResultsToConsole(vcard);
コード例 #7
0
        public void SaveVcfTest_vCard4_0()
        {
            List <VCard?> list = GenerateVCardList();

            string path = Path.Combine(TestContext !.TestRunResultsDirectory, "SaveVcfTest_v4.0.vcf");

            list.SaveVcf(path, VCdVersion.V4_0);

            IList <VCard> list2 = VCard.LoadVcf(path);

            Assert.AreEqual(2, list2.Count);
            Assert.IsInstanceOfType(list2.FirstOrDefault()?.Relations?.FirstOrDefault()?.Value, typeof(VCard));
        }
コード例 #8
0
ファイル: VcfReader.cs プロジェクト: FolkerKinzel/Contacts.IO
    /// <summary>
    /// Liest eine vCard-Datei und gibt ihre Daten als <see cref="Contact"/>-Array zurück. (Eine vCard-Datei kann
    /// mehrere aneinandergehängte vCards enthalten.) Enthält die Datei keinen Text, wird ein leeres Array zurückgegeben.
    /// </summary>
    /// <param name="fileName">Der vollständige Pfad der vCard-Datei.</param>
    /// <returns>Die Daten der vCard als <see cref="Contact"/>-Array.</returns>
    /// <exception cref="ArgumentNullException"><paramref name="fileName"/> ist <c>null</c>.</exception>
    /// <exception cref="ArgumentException"><paramref name="fileName"/> ist kein gültiger Dateipfad.</exception>
    /// <exception cref="IOException">Die Datei konnte nicht geladen werden.</exception>
    internal static List <Contact> Read(string fileName)
    {
        IList <VCard> vcards = VCard.LoadVcf(fileName);


        var wabContacts = new List <Contact>(vcards.Count);

        for (int i = 0; i < vcards.Count; i++)
        {
            wabContacts.Add(ConvertToContact(vcards[i]));
        }

        return(wabContacts);
    }
コード例 #9
0
    public void WhatsAppIssueTest1()
    {
        IList <VCard> list = VCard.LoadVcf(TestFiles.WhatsAppIssueVcf);

        Assert.AreNotEqual(0, list.Count);

        IEnumerable <Models.TextProperty?>?phoneNumbers = list[0].PhoneNumbers;

        Assert.IsNotNull(phoneNumbers);

        Models.TextProperty?whatsAppNumber = phoneNumbers !.ElementAtOrDefault(1);
        Assert.IsNotNull(whatsAppNumber);

        KeyValuePair <string, string>?parameter = whatsAppNumber !.Parameters.NonStandardParameters?.FirstOrDefault();

        Assert.IsTrue(parameter.HasValue);
        Assert.AreEqual("TYPE", parameter !.Value.Key);
        Assert.AreEqual("WhatsApp", parameter !.Value.Value);
    }
コード例 #10
0
    public static void SaveSingleVCardAsVcf(string directoryPath)
    {
        const string vcfExtension = ".vcf";

        // Note that argument validation and exception handling is completely omitted in this
        // example. The following "if" statement only ensures, that the method doesn't destroy
        // valueable data.
        if (Directory.GetFiles(directoryPath).Any(x => x.EndsWith(vcfExtension, StringComparison.OrdinalIgnoreCase)))
        {
            Console.WriteLine("The method \"SaveSingleVCardAsVcf(string)\" could not be executed");
            Console.WriteLine("because the destination directory contains .VCF files, that might");
            Console.WriteLine("be overwritten.");

            return;
        }

        // Initialize a group vCard with composers names and live dates:
        var members = new VC::RelationVCardProperty[]
        {
            new VC::RelationVCardProperty(InitializeComposerVCard(
                                              "Sergei Rachmaninoff", new DateTime(1873, 4, 1), new DateTime(1943, 3, 28))),
            new VC::RelationVCardProperty(InitializeComposerVCard(
                                              "Ludwig van Beethoven", new DateTime(1770, 12, 17), new DateTime(1827, 3, 26))),
            new VC::RelationVCardProperty(InitializeComposerVCard(
                                              "Frédéric Chopin", new DateTime(1810, 3, 1), new DateTime(1849, 10, 17)))
        };

        var composersVCard = new VCard
        {
            DisplayNames = new VC::TextProperty("Composers"),
            Kind         = new VC::KindProperty(VC::Enums.VCdKind.Group),
            Members      = members
        };


        // Replace the embedded VCards in composersVCard.Members with Guid references in order
        // to save them as separate vCard 4.0 .VCF files.
        // IMPORTANT: Never call ReferenceVCards() if you intend to serialize a vCard 2.1 or vCard 3.0 !
        IEnumerable <VCard> referenced = composersVCard.ReferenceVCards();

        // (The extension method can be called on a single VCard because VCard implements IEnumerable<VCard>.)

        Console.WriteLine();
        Console.WriteLine($"After ReferenceVCards() vCardList contains {referenced.Count()} VCard objects.");
        Console.WriteLine();
        Console.WriteLine("composersVCard:");
        Console.WriteLine();
        Console.WriteLine(
            referenced
            .Where(x => x.DisplayNames?.Any(x => StringComparer.Ordinal.Equals(x?.Value, "Composers")) ?? false)
            .First()
            .ToVcfString(VC::Enums.VCdVersion.V4_0));

        // Make sure to save ALL VCard objects in referenced - otherwise the information
        // originally stored in composersVCard will be irrevocably lost.
        foreach (VCard vcard in referenced)
        {
            string fileName = Path.Combine(
                directoryPath,
                $"{vcard.DisplayNames!.First()!.Value}{vcfExtension}");

            vcard.SaveVcf(fileName, VC::Enums.VCdVersion.V4_0);
        }

        // Reload the .VCF files:
        var vCardList = new List <VCard>();

        foreach (string fileName in Directory.EnumerateFiles(directoryPath, $"*{vcfExtension}"))
        {
            vCardList.AddRange(VCard.LoadVcf(fileName));
        }

        // Make the reloaded VCard objects searchable:
        IEnumerable <VCard> dereferenced = vCardList.DereferenceVCards();

        // Find the parsed result from "Composers.vcf":
        composersVCard = dereferenced.FirstOrDefault(x => x.DisplayNames?.Any(x => x?.Value == "Composers") ?? false);

        if (composersVCard is null)
        {
            Console.WriteLine("Composers.vcf not found!");
        }
        else
        {
            //Retrieve Beethovens birth year from the members of the "Composers.vcf" group:
            Console.Write("What year was Beethoven born?: ");

            DateTimeOffset?birthDay = composersVCard.Members?
                                      .Select(x => x as VC::RelationVCardProperty)
                                      .Where(x => x?.Value != null)
                                      .Select(x => x !.Value)
                                      .FirstOrDefault(x => x !.DisplayNames?.Any(x => x?.Value == "Ludwig van Beethoven") ?? false)?
                                      .BirthDayViews?
                                      .Select(x => x as VC::DateTimeOffsetProperty)
                                      .Where(x => x != null && !x.IsEmpty)
                                      .FirstOrDefault()?
                                      .Value;

            Console.WriteLine(birthDay.HasValue ? birthDay.Value.Year.ToString() : "Don't know.");
        }
    }