예제 #1
0
        ///////////////////////////////////////////////////////////////

        static VCard InitializeTheVCardAndFillItWithData(string directoryPath, string photoFileName)
        {
            var vcard = new VCard
            {
                // Although NameViews is of Type IEnumerable<NameProperty?>
                // you can assign a single NameProperty instance because NameProperty
                // (like almost all classes derived from VCardProperty) has an explicit
                // implementation of IEnumerable<T>
                NameViews = new VC::NameProperty
                            (
                    lastName: new string[] { "Müller-Risinowsky" },
                    firstName: new string[] { "Käthe" },
                    middleName: new string[] { "Alexandra", "Caroline" },
                    prefix: new string[] { "Prof.", "Dr." }
                            ),
                DisplayNames  = new VC.TextProperty("Käthe Müller-Risinowsky"),
                Organizations = new VC::OrganizationProperty
                                (
                    "Millers Company",
                    new string[] { "C#", "Webdesign" }
                                ),
                Titles    = new VC::TextProperty("CEO"),
                TimeStamp = new VC::TimeStampProperty(DateTimeOffset.UtcNow)
            };


            // Creates a small "Photo" file for demonstration purposes:
            string photoFilePath = Path.Combine(directoryPath, photoFileName);

            CreatePhoto(photoFilePath);

            vcard.Photos = new VC::DataProperty(VC::DataUrl.FromFile(photoFilePath));

            // This shows how to create a PropertyIDMapping, which helps to identify vCard-Properties
            // even if they are located in vCards that come from different sources (vCard 4.0 only):
            var pidMap     = new VC::PropertyIDMapping(1, new Uri("http://folkerKinzel.de/file1.htm"));
            var pidMapProp = new VC::PropertyIDMappingProperty(pidMap);

            vcard.PropertyIDMappings = pidMapProp;

            const string groupName = "gr1";
            var          telHome   = new VC::TextProperty("tel:+49-123-9876543");

            telHome.Parameters.DataType      = VC::Enums.VCdDataType.Uri;
            telHome.Parameters.PropertyClass = VC::Enums.PropertyClassTypes.Home;
            telHome.Parameters.TelephoneType = VC::Enums.TelTypes.Voice | VC::Enums.TelTypes.BBS;
            telHome.Parameters.PropertyIDs   = new VC::PropertyID(1, pidMap);

            var telWork = new VC::TextProperty("tel:+49-321-1234567");

            telWork.Parameters.DataType      = VC::Enums.VCdDataType.Uri;
            telWork.Parameters.PropertyClass = VC::Enums.PropertyClassTypes.Work;
            telWork.Parameters.TelephoneType = VC::Enums.TelTypes.Cell
                                               | VC::Enums.TelTypes.Text
                                               | VC::Enums.TelTypes.Msg
                                               | VC::Enums.TelTypes.BBS
                                               | VC::Enums.TelTypes.Voice;
            telWork.Parameters.PropertyIDs = new VC::PropertyID(2, pidMap);


            var adrWorkProp = new VC::AddressProperty
                                  ("Friedrichstraße 22", "Berlin", "10117", country: "Germany", propertyGroup: groupName);

            adrWorkProp.Parameters.PropertyClass = VC::Enums.PropertyClassTypes.Work;
            adrWorkProp.Parameters.AddressType   = VC::Enums.AddressTypes.Dom | VC::Enums.AddressTypes.Intl | VC::Enums.AddressTypes.Postal | VC::Enums.AddressTypes.Parcel;
            vcard.Addresses = adrWorkProp;

            var tz  = new VC::TimeZoneID("Europe/Berlin");
            var geo = new VC::GeoCoordinate(52.51182050685474, 13.389581454284256);

            // vCard 4.0 allows to specify the time zone and geographical
            // position in the Parameters of an AddressProperty.
            // These entries are ignored when writing older vCard versions:
            adrWorkProp.Parameters.TimeZone    = tz;
            adrWorkProp.Parameters.GeoPosition = geo;

            // Make separate TZ and GEO Properties to preserve the time zone
            // and geographical Position in older vCard versions:
            vcard.TimeZones      = new VC::TimeZoneProperty(tz, groupName);
            vcard.GeoCoordinates = new VC::GeoProperty(geo, groupName);

            vcard.PhoneNumbers = new VC::TextProperty[]
            {
                telHome, telWork
            };

            var prefMail = new VC::TextProperty("*****@*****.**");

            prefMail.Parameters.PropertyClass = VC::Enums.PropertyClassTypes.Work;
            prefMail.Parameters.Preference    = 1;

            vcard.EmailAddresses = prefMail;

            // System.DateTime has an implicit conversion to
            // System.DateTimeOffset:
            vcard.BirthDayViews = new VC::DateTimeOffsetProperty(new DateTime(1984, 3, 28));

            vcard.Relations = new VC::RelationTextProperty
                              (
                "Paul Müller-Risinowsky",
                VC::Enums.RelationTypes.Spouse
                | VC::Enums.RelationTypes.CoResident
                | VC::Enums.RelationTypes.Colleague
                              );

            vcard.AnniversaryViews = new VC::DateTimeOffsetProperty(new DateTime(2006, 7, 14));
            return(vcard);
        }
예제 #2
0
    private static VCard?ToVCard(Contact?contact)
    {
        if (contact is null)
        {
            return(null);
        }

        contact.Clean();

        if (contact.IsEmpty)
        {
            return(null);
        }

        var  vcard        = new VCard();
        bool writeAdrWork = true;
        Work?work         = contact.Work;

        Address?adrHome = contact.AddressHome;

        if (adrHome != null)
        {
            var homeAdr = new VC::AddressProperty(street: adrHome.Street,
                                                  locality: adrHome.City,
                                                  postalCode: adrHome.PostalCode,
                                                  region: adrHome.State,
                                                  country: adrHome.Country);

            vcard.Addresses = homeAdr;

            homeAdr.Parameters.AddressType = VC::Enums.AddressTypes.Dom | VC::Enums.AddressTypes.Intl | VC::Enums.AddressTypes.Parcel | VC::Enums.AddressTypes.Postal;

            if (adrHome == work?.AddressWork)
            {
                homeAdr.Parameters.PropertyClass = VC::Enums.PropertyClassTypes.Home | VC::Enums.PropertyClassTypes.Work;
                writeAdrWork = false;
            }
            else
            {
                homeAdr.Parameters.PropertyClass = VC::Enums.PropertyClassTypes.Home;
            }

            homeAdr.Parameters.Label = BuildAddressLabel(adrHome);
        }



        if (work != null)
        {
            Address?adrWork = work.AddressWork;

            if (writeAdrWork && adrWork != null)
            {
                var workAdr = new VC::AddressProperty(street: adrWork.Street,
                                                      locality: adrWork.City,
                                                      postalCode: adrWork.PostalCode,
                                                      region: adrWork.State,
                                                      country: adrWork.Country);

                IEnumerable <VC::AddressProperty?>?addresses = vcard.Addresses;

                if (addresses is null)
                {
                    vcard.Addresses = workAdr;
                }
                else if (addresses is VC::AddressProperty homeAdr)
                {
                    vcard.Addresses = new VC::AddressProperty[] { homeAdr, workAdr };
                }

                workAdr.Parameters.AddressType   = VC::Enums.AddressTypes.Dom | VC::Enums.AddressTypes.Intl | VC::Enums.AddressTypes.Parcel | VC::Enums.AddressTypes.Postal;
                workAdr.Parameters.PropertyClass = VC::Enums.PropertyClassTypes.Work;

                workAdr.Parameters.Label = BuildAddressLabel(adrWork);
            }

            if (work.Company != null || work.Department != null || work.Office != null)
            {
                vcard.Organizations =
                    new VC::OrganizationProperty(work.Company,
                                                 new string?[] { work.Department, work.Office });
            }

            if (work.JobTitle != null)
            {
                vcard.Titles = new VC::TextProperty(work.JobTitle);
            }
        }

        var comment = contact.Comment;

        if (comment != null)
        {
            vcard.Notes = new VC::TextProperty(comment);
        }

        var displayName = contact.DisplayName;

        if (displayName != null)
        {
            vcard.DisplayNames = new VC::TextProperty(displayName);
        }

        IEnumerable <string?>?emails = contact.EmailAddresses;

        if (emails != null)
        {
            var emailProps = new List <VC::TextProperty>();
            vcard.EmailAddresses = emailProps;

            int counter = 1;

            foreach (var mailAddress in emails)
            {
                Debug.Assert(mailAddress != null);

                var mailProp = new VC::TextProperty(mailAddress);
                emailProps.Add(mailProp);

                mailProp.Parameters.EmailType  = VC::Enums.EmailType.SMTP;
                mailProp.Parameters.Preference = counter++;
            }
        }

        var  webPersonal  = contact.WebPagePersonal;
        var  webWork      = contact.WebPageWork;
        bool writeWebWork = true;

        if (webPersonal != null)
        {
            var urlHomeProp = new VC::TextProperty(webPersonal);
            vcard.URLs = urlHomeProp;

            if (webPersonal == webWork)
            {
                urlHomeProp.Parameters.PropertyClass = VC::Enums.PropertyClassTypes.Home | VC::Enums.PropertyClassTypes.Work;
                writeWebWork = false;
            }
            else
            {
                urlHomeProp.Parameters.PropertyClass = VC::Enums.PropertyClassTypes.Home;
            }
        }

        if (writeWebWork && webWork != null)
        {
            var urlWork = new VC::TextProperty(webWork);
            urlWork.Parameters.PropertyClass = VC::Enums.PropertyClassTypes.Work;

            IEnumerable <VC::TextProperty?>?urls = vcard.URLs;

            if (urls is null)
            {
                vcard.URLs = urlWork;
            }
            else if (urls is VC::TextProperty urlHome)
            {
                vcard.URLs = new VC::TextProperty[] { urlHome, urlWork };
            }
        }

        IEnumerable <string?>?impps = contact.InstantMessengerHandles;

        if (impps != null)
        {
            var imppProps = new List <VC::TextProperty>();
            vcard.InstantMessengerHandles = imppProps;

            int counter = 1;

            foreach (var imppAddress in impps)
            {
                Debug.Assert(imppAddress != null);

                var imppProp = new VC::TextProperty(imppAddress);
                imppProps.Add(imppProp);

                imppProp.Parameters.Preference = counter++;
            }
        }

        Person?person = contact.Person;

        if (person != null)
        {
            DateTime?bday = person.BirthDay;
            if (bday.HasValue)
            {
                vcard.BirthDayViews = new VC::DateTimeOffsetProperty(bday.Value);
            }

            Name?name = person.Name;
            if (name != null)
            {
                vcard.NameViews = new VC::NameProperty(name.LastName,
                                                       name.FirstName,
                                                       name.MiddleName,
                                                       name.Prefix,
                                                       name.Suffix);
            }

            Sex gender = person.Gender;
            if (gender != Sex.Unspecified)
            {
                vcard.GenderViews = new VC::GenderProperty(
                    gender == Sex.Female ? VC::Enums.VCdSex.Female : VC::Enums.VCdSex.Male);
            }

            var nickName = person.NickName;
            if (nickName != null)
            {
                vcard.NickNames = new VC::StringCollectionProperty(nickName);
            }

            var spouseName = person.Spouse;
            if (spouseName != null)
            {
                vcard.Relations = new VC::RelationTextProperty(
                    spouseName, VC::Enums.RelationTypes.Spouse);
            }

            DateTime?anniversary = person.Anniversary;
            if (anniversary.HasValue)
            {
                vcard.AnniversaryViews = new VC::DateTimeOffsetProperty(anniversary.Value);
            }
        }

        IEnumerable <PhoneNumber?>?phones = contact.PhoneNumbers;

        if (phones != null)
        {
            var phoneProps = new List <VC::TextProperty>();
            vcard.PhoneNumbers = phoneProps;

            foreach (PhoneNumber?number in phones)
            {
                Debug.Assert(number != null);

                var phoneProp = new VC::TextProperty(number !.Value);
                phoneProps.Add(phoneProp);

                VC::Enums.TelTypes?telType = null;

                if (number.IsMobile)
                {
                    telType = VC::Enums.TelTypes.Cell;
                }

                if (number.IsFax)
                {
                    telType = telType.Set(VC.Enums.TelTypes.Fax);
                }

                phoneProp.Parameters.TelephoneType = telType;

                if (number.IsWork)
                {
                    phoneProp.Parameters.PropertyClass = VC::Enums.PropertyClassTypes.Work;
                }
            }
        }

        vcard.TimeStamp = contact.TimeStamp == default ? null : new VC::TimeStampProperty(contact.TimeStamp);

        return(vcard);
    }