예제 #1
0
 /// <summary>
 /// Writes a vcard object to a file
 /// </summary>
 /// <param name="vcard">The vcard object to be written</param>
 /// <param name="filePath">The path the vcard should be saved to</param>
 /// <param name="version">The version to be serialized into</param>
 /// <param name="options">State whether the card should be overwritten if it exists</param>
 /// <returns>A value stating if the serialization was successful or not</returns>
 /// <exception cref="InvalidOperationException">Thrown when the file path exists and the overwrite option is not invoked</exception>
 /// <exception cref="ArgumentNullException">Thrown when the vcard supplied is null</exception>
 public static bool Serialize(vCard vcard, string filePath, Version version, WriteOptions options = WriteOptions.ThrowError)
 {
     if (options == WriteOptions.ThrowError)
     {
         if (File.Exists(filePath))
         {
             throw new InvalidOperationException(
                       "A file with the given filePath exists."
                       + " If you want to overwrite the file,"
                       + " then call this method and pass the "
                       + "optional overwrite option"
                       );
         }
     }
     if (vcard == null)
     {
         throw new ArgumentNullException("The vcard cannot be null.");
     }
     if (version == Version.V2)
     {
         try
         {
             string vcfString = Serialize(vcard, Version.V2);
             File.WriteAllText(filePath, vcfString);
         }
         catch (Exception e)
         {
             Console.WriteLine(e);
             return(false);
         }
     }
     else if (version == Version.V3)
     {
         try
         {
             string vcfString = Serialize(vcard, Version.V3);
             File.WriteAllText(filePath, vcfString);
         }
         catch (Exception e)
         {
             Console.WriteLine(e);
             return(false);
         }
     }
     else if (version == Version.V4)
     {
         //TODO: once support has been implemented, enable writing capabilities
         /*string vcfString = */ Serialize(vcard, Version.V4);
         //File.WriteAllText(filePath, vcfString)
     }
     return(true);
 }
예제 #2
0
        /// <summary>
        /// Central point from which all deserializing starts
        /// </summary>
        /// <param name="contactDetails">A string array of the contact details</param>
        /// <param name="version">The version to be deserialized from</param>
        /// <returns>A <see cref="vCard"/> comtaining the contacts details</returns>
        private static vCard Deserialize(string[] contactDetails, Version version)
        {
            _contactDetails = contactDetails;
            vCard vcard = new vCard
            {
                Version           = version,
                BirthDay          = ParseBirthDay(),
                BirthPlace        = ParseBirthPlace(),
                DeathPlace        = ParseDeathPlace(),
                FamilyName        = ParseFamilyName(),
                Geo               = ParseGeo(),
                Gender            = ParseGender(),
                GivenName         = ParseGivenName(),
                Kind              = ParseKind(),
                Language          = ParseLanguage(),
                MiddleName        = ParseMiddleName(),
                NickName          = ParseNickname(),
                Organization      = ParseOrganization(),
                Prefix            = ParsePrefix(),
                Revision          = ParseRevision(),
                Suffix            = ParseSuffix(),
                TimeZone          = ParseTimeZone(),
                Title             = ParseTitle(),
                Url               = ParseUrl(),
                XSkypeDisplayName = ParseXSkypeDisplayName(),
                XSkypePstnNumber  = ParseXSkypePstnNumber()
            };

            switch (version)
            {
            case Version.V2:
                return(V2Deserializer.Parse(contactDetails, vcard));

            case Version.V3:
                return(V3Deserializer.Parse(contactDetails, vcard));

            default:
                return(V4Deserializer.Parse(contactDetails, vcard));
            }
        }
예제 #3
0
 /// <summary>
 /// Save a vcard object to a vcf file
 /// </summary>
 /// <param name="filePath">Path to file to save to</param>
 /// <param name="version">Set the save version</param>
 /// <param name="writeOption">Option to determine if the method would overwrite the file or throw an error</param>
 /// <returns>A boolean value stating whether the save option was successful or not</returns>
 public bool Save(string filePath, Version version, WriteOptions writeOption = WriteOptions.ThrowError)
 {
     return(Serializer.Serialize(this, filePath, version, writeOption));
 }
예제 #4
0
        /// <summary>
        /// Writes a vcard collection object to a file
        /// </summary>
        /// <param name="vcardCollection">The vcard collection object to be written</param>
        /// <param name="filePath">The path the collection should be saved to</param>
        /// <param name="version">The version to be serialized into</param>
        /// <param name="options">tate whether the card should be overwritten if it exists</param>
        /// <returns>A value stating if the serialization was successful or not</returns>
        /// <exception cref="InvalidOperationException">Thrown when the file path exists and the overwrite option is not invoked</exception>
        /// <exception cref="ArgumentNullException">Thrown when the vcard supplied is null</exception>
        public static bool Serialize(vCardCollection vcardCollection, string filePath, Version version,
                                     WriteOptions options = WriteOptions.ThrowError)
        {
            if (options == WriteOptions.ThrowError)
            {
                if (File.Exists(filePath))
                {
                    throw new InvalidOperationException(
                              "A file with the given filePath exists."
                              + " If you want to overwrite the file,"
                              + " then call this method and pass the "
                              + "optional overwrite option"
                              );
                }
            }
            if (vcardCollection == null)
            {
                throw new ArgumentNullException("The vcard collection cannot be null.");
            }
            var vcardCollectionString = "";

            if (version == Version.V2)
            {
                foreach (vCard vcard in vcardCollection)
                {
                    vcardCollectionString += Serialize(vcard, Version.V2);
                }
            }
            else if (version == Version.V3)
            {
                foreach (vCard vcard in vcardCollection)
                {
                    vcardCollectionString += Serialize(vcard, Version.V3);
                }
            }
            else
            {
                foreach (vCard vcard in vcardCollection)
                {
                    vcardCollectionString += Serialize(vcard, Version.V4);
                }
            }
            try
            {
                File.WriteAllText(filePath, vcardCollectionString);
                return(true);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                return(false);
            }
        }
예제 #5
0
        private static string Serialize(vCard vcard, Version version)
        {
            StringBuilder stringBuilder = new StringBuilder();

            stringBuilder.Append("BEGIN:VCARD" + Environment.NewLine);
            stringBuilder.Append("REV:" + DateTime.Now.ToString("yyyyMMddTHHmmssZ") + Environment.NewLine);
            stringBuilder.Append("N:" + vcard.FamilyName + ";" + vcard.GivenName + ";" + vcard.MiddleName + ";" + vcard.Prefix + ";" + vcard.Suffix + Environment.NewLine);
            stringBuilder.Append("FN:" + vcard.FormattedName + Environment.NewLine);
            if (!string.IsNullOrEmpty(vcard.Organization))
            {
                stringBuilder.Append("ORG:" + vcard.Organization + Environment.NewLine);
            }
            if (!string.IsNullOrEmpty(vcard.Title))
            {
                stringBuilder.Append("TITLE:" + vcard.Title + Environment.NewLine);
            }
            if (!string.IsNullOrEmpty(vcard.Url))
            {
                stringBuilder.Append("URL:" + vcard.Url + Environment.NewLine);
            }
            if (!string.IsNullOrEmpty(vcard.NickName))
            {
                stringBuilder.Append("NICKNAME:" + vcard.NickName + Environment.NewLine);
            }
            if (!string.IsNullOrEmpty(vcard.Language))
            {
                stringBuilder.Append("LANG:" + vcard.Language + Environment.NewLine);
            }
            if (!string.IsNullOrEmpty(vcard.BirthPlace))
            {
                stringBuilder.Append("BIRTHPLACE:" + vcard.BirthPlace + Environment.NewLine);
            }
            if (!string.IsNullOrEmpty(vcard.DeathPlace))
            {
                stringBuilder.Append("DEATHPLACE:" + vcard.DeathPlace + Environment.NewLine);
            }
            if (!string.IsNullOrEmpty(vcard.TimeZone))
            {
                stringBuilder.Append("TZ:" + vcard.TimeZone + Environment.NewLine);
            }
            if (!string.IsNullOrEmpty(vcard.Note))
            {
                stringBuilder.Append("NOTE:" + vcard.Note);
            }
            if (!string.IsNullOrEmpty(vcard.XSkypeDisplayName))
            {
                stringBuilder.Append("X-SKYPE-DISPLAYNAME:" + vcard.XSkypeDisplayName + Environment.NewLine);
            }
            if (!string.IsNullOrEmpty(vcard.XSkypePstnNumber))
            {
                stringBuilder.Append("X-SKYPE-PSTNNUMBER:" + vcard.XSkypePstnNumber + Environment.NewLine);
            }
            stringBuilder.Append("KIND:" + vcard.Kind.ToString().ToUpper() + Environment.NewLine);
            stringBuilder.Append("GENDER:" + vcard.Gender.ToString().ToUpper() + Environment.NewLine);

            if (vcard.Geo != null)
            {
                stringBuilder.Append("GEO:" + vcard.Geo.Longitude + ";" + vcard.Geo.Latitude);
            }
            if (vcard.BirthDay != null)
            {
                var birthDay = (DateTime)vcard.BirthDay;
                stringBuilder.Append("BDAY:" + birthDay.Year + birthDay.Month.ToString("00") + birthDay.Day.ToString("00"));
            }

            if (version == Version.V2)
            {
                stringBuilder.Append("VERSION:2.1" + Environment.NewLine);
                stringBuilder.Append(V2Serializer.Serialize(vcard));
            }
            else if (version == Version.V3)
            {
                stringBuilder.Append("VERSION:3.0" + Environment.NewLine);
                stringBuilder.Append(V3Serializer.Serialize(vcard));
            }
            else
            {
                stringBuilder.Append("VERSION:4.0" + Environment.NewLine);
                stringBuilder.Append(V4Serializer.Serialize(vcard));
            }
            stringBuilder.Append(Environment.NewLine);
            stringBuilder.Append("END:VCARD");
            return(stringBuilder.ToString());
        }