Esempio n. 1
0
        public override bool Encode(ref byte[] buffer)
        {
            /* First : determine the total number of characters	*/
            int NbBytes = count_nb_bytes();

            /* Second : create the whole byte array                         */
            int index = 0;
            int new_index;

            byte[] pl = new byte[NbBytes];

            new_index = AddLine(pl, pl.Length, index, "BEGIN:VCARD", "");
            if (new_index < 0)
            {
                Trace.WriteLine("Error generating 'VCard' object: after 'BEGIN'");
                return(false);
            }
            index = new_index;

            new_index = AddLine(pl, pl.Length, index, "VERSION:3.0", "");
            if (new_index < 0)
            {
                Trace.WriteLine("Error generating 'VCard' object: after 'VERSION'");
                return(false);
            }
            index = new_index;

            string name = _first_name + " " + _family_name;

            new_index = AddLine(pl, pl.Length, index, "FN:", name);
            if (new_index < 0)
            {
                Trace.WriteLine("Error generating 'VCard' object: after 'FN'");
                return(false);
            }
            index = new_index;


            if (!Nickname.Equals(""))
            {
                new_index = AddLine(pl, pl.Length, index, "NICKNAME:", Nickname);
                if (new_index < 0)
                {
                    Trace.WriteLine("Error generating 'VCard' object: after 'Nickname'");
                    return(false);
                }
                index = new_index;
            }

            DateTime dateBirthday;

            if ((!Birthday.Equals("")) && (DateTime.TryParse(Birthday, out dateBirthday)))
            {
                string Bday = convert_into_vcard_date(Birthday);
                new_index = AddLine(pl, pl.Length, index, "BDAY:", Bday);
                if (new_index < 0)
                {
                    Trace.WriteLine("Error generating 'VCard' object: after 'Birthday'");
                    return(false);
                }
                index = new_index;
            }

            if (!(Address1.Equals("")) ||
                !(Address2.Equals("")) ||
                !(Town.Equals("")) ||
                !(Region_State.Equals("")) ||
                !(Post_Code.Equals("")) ||
                !(Country.Equals(""))
                )
            {
                string addr_line = Address1 + ";" + Address2 + ";" + Town + ";"
                                   + Region_State + ";" + Post_Code + ";" + Country;
                new_index = AddLine(pl, pl.Length, index, "ADR:;", addr_line);
                if (new_index < 0)
                {
                    Trace.WriteLine("Error generating 'VCard' object: after 'Address'");
                    return(false);
                }
                index = new_index;
            }

            if (!(Pro_Address1.Equals("")) ||
                !(Pro_Address2.Equals("")) ||
                !(Pro_Town.Equals("")) ||
                !(Pro_Region_State.Equals("")) ||
                !(Pro_Post_Code.Equals("")) ||
                !(Pro_Country.Equals(""))
                )
            {
                string pro_addr_line = Pro_Address1 + ";" + Pro_Address2 + ";" + Pro_Town + ";"
                                       + Pro_Region_State + ";" + Pro_Post_Code + ";" + Pro_Country;
                new_index = AddLine(pl, pl.Length, index, "ADR;TYPE=work:;", pro_addr_line);
                if (new_index < 0)
                {
                    Trace.WriteLine("Error generating 'VCard' object: after 'Professional Address'");
                    return(false);
                }
                index = new_index;
            }

            if (!Home_phone.Equals(""))
            {
                new_index = AddLine(pl, pl.Length, index, "TEL;TYPE=home:", Home_phone);
                if (new_index < 0)
                {
                    Trace.WriteLine("Error generating 'VCard' object: after 'TEL;TYPE=home'");
                    return(false);
                }
                index = new_index;
            }


            if (!Business_phone.Equals(""))
            {
                new_index = AddLine(pl, pl.Length, index, "TEL;TYPE=work:", Business_phone);
                if (new_index < 0)
                {
                    Trace.WriteLine("Error generating 'VCard' object: after 'TEL;TYPE=work'");
                    return(false);
                }
                index = new_index;
            }

            if (!Cell_phone.Equals(""))
            {
                new_index = AddLine(pl, pl.Length, index, "TEL;TYPE=cell:", Cell_phone);
                if (new_index < 0)
                {
                    Trace.WriteLine("Error generating 'VCard' object: after 'TEL;TYPE=cell'");
                    return(false);
                }
                index = new_index;
            }

            if (!Pager.Equals(""))
            {
                new_index = AddLine(pl, pl.Length, index, "TEL;TYPE=pager:", Pager);
                if (new_index < 0)
                {
                    Trace.WriteLine("Error generating 'VCard' object: after 'TEL;TYPE=pager'");
                    return(false);
                }
                index = new_index;
            }

            if (!Fax.Equals(""))
            {
                new_index = AddLine(pl, pl.Length, index, "TEL;TYPE=fax:", Fax);
                if (new_index < 0)
                {
                    Trace.WriteLine("Error generating 'VCard' object: after 'TEL;TYPE=fax'");
                    return(false);
                }
                index = new_index;
            }

            if (!Email.Equals(""))
            {
                if (Email_alternative.Equals(""))
                {
                    new_index = AddLine(pl, pl.Length, index, "EMAIL:", Email);
                    if (new_index < 0)
                    {
                        Trace.WriteLine("Error generating 'VCard' object: after 'EMAIL'");
                        return(false);
                    }
                    index = new_index;
                }
                else
                {
                    /*	Two E-mails to add */
                    new_index = AddLine(pl, pl.Length, index, "EMAIL;PREF=1:", Email);
                    if (new_index < 0)
                    {
                        Trace.WriteLine("Error generating 'VCard' object: after 'EMAIL;PREF=1'");
                        return(false);
                    }
                    index = new_index;

                    new_index = AddLine(pl, pl.Length, index, "EMAIL;PREF=2:", Email_alternative);
                    if (new_index < 0)
                    {
                        Trace.WriteLine("Error generating 'VCard' object: after 'EMAIL;PREF=2'");
                        return(false);
                    }
                    index = new_index;
                }
            }

            if (!Title.Equals(""))
            {
                new_index = AddLine(pl, pl.Length, index, "TITLE:", Title);
                if (new_index < 0)
                {
                    Trace.WriteLine("Error generating 'VCard' object: after 'TITLE'");
                    return(false);
                }
                index = new_index;
            }

            if (!Role.Equals(""))
            {
                new_index = AddLine(pl, pl.Length, index, "ROLE:", Role);
                if (new_index < 0)
                {
                    Trace.WriteLine("Error generating 'VCard' object: after 'ROLE'");
                    return(false);
                }
                index = new_index;
            }

            if (!Company.Equals(""))
            {
                new_index = AddLine(pl, pl.Length, index, "ORG:", Company);
                if (new_index < 0)
                {
                    Trace.WriteLine("Error generating 'VCard' object: after 'ORG'");
                    return(false);
                }
                index = new_index;
            }

            if (!_photo.Equals(""))
            {
                new_index = AddLine(pl, pl.Length, index, "PHOTO;ENCODING=BASE64;TYPE=JPEG:", _photo);

                if (new_index < 0)
                {
                    Trace.WriteLine("Error generating 'VCard' object: after 'PHOTO'");
                    return(false);
                }

                index = new_index;
            }

            new_index = AddLine(pl, pl.Length, index, "END:VCARD", "");
            _payload  = pl;

            return(base.Encode(ref buffer));
        }
Esempio n. 2
0
        private int count_nb_bytes()
        {
            /* All the ASCII strings in the VCard must be separated by 0x0D0A, which represent 2 characters */
            int NbBytes = 0;

            NbBytes  = "BEGIN:VCARD".Length + 2 + "VERSION:3.0".Length + 2;
            NbBytes += "FN:".Length + First_name.Length + 1 + Family_name.Length + 2;                           /* 1 is for "space"	*/

            if (!Nickname.Equals(""))
            {
                NbBytes += "NICKNAME:".Length + Nickname.Length + 2;
            }

            /* Birthday with 8 characters: yyyymmdd	*/
            DateTime dateBirthday;

            if ((!Birthday.Equals("")) && (DateTime.TryParse(Birthday, out dateBirthday)))
            {
                NbBytes += "BDAY:".Length + 8 + 2;
            }

            /* Home address in 6 fields, separated by a ';'						*/
            /* The PO Box is discarded, which explains the first ';'	*/
            if (!(Address1.Equals("")) ||
                !(Address2.Equals("")) ||
                !(Town.Equals("")) ||
                !(Region_State.Equals("")) ||
                !(Post_Code.Equals("")) ||
                !(Country.Equals(""))
                )
            {
                NbBytes += "ADR:;".Length
                           + Address1.Length + 1
                           + Address2.Length + 1
                           + Town.Length + 1
                           + Region_State.Length + 1
                           + Post_Code.Length + 1
                           + Country.Length + 2;
            }

            /* Work address in 6 fields, separated by a ';'						*/
            /* The PO Box is discarded, which explains the first ';'	*/
            if (!(Pro_Address1.Equals("")) ||
                !(Pro_Address2.Equals("")) ||
                !(Pro_Town.Equals("")) ||
                !(Pro_Region_State.Equals("")) ||
                !(Pro_Post_Code.Equals("")) ||
                !(Pro_Country.Equals(""))
                )
            {
                NbBytes += "ADR;TYPE=work:;".Length
                           + Pro_Address1.Length + 1
                           + Pro_Address2.Length + 1
                           + Pro_Town.Length + 1
                           + Pro_Region_State.Length + 1
                           + Pro_Post_Code.Length + 1
                           + Pro_Country.Length + 2;
            }

            if (!Home_phone.Equals(""))
            {
                NbBytes += "TEL;TYPE=home:".Length + Home_phone.Length + 2;
            }

            if (!Business_phone.Equals(""))
            {
                NbBytes += "TEL;TYPE=work:".Length + Business_phone.Length + 2;
            }

            if (!Cell_phone.Equals(""))
            {
                NbBytes += "TEL;TYPE=cell:".Length + Cell_phone.Length + 2;
            }

            if (!Fax.Equals(""))
            {
                NbBytes += "TEL;TYPE=fax:".Length + Fax.Length + 2;
            }

            if (!Pager.Equals(""))
            {
                NbBytes += "TEL;TYPE=pager:".Length + Pager.Length + 2;
            }

            if (!Email.Equals(""))
            {
                if (Email_alternative.Equals(""))
                {
                    NbBytes += "EMAIL:".Length + Email.Length + 2;
                }
                else
                {
                    /* TWO EMAILS : PREF=1 and PREF=2	*/
                    NbBytes += "EMAIL;PREF=1:".Length + Email.Length + 2 + "EMAIL;PREF=2:".Length + Email_alternative.Length + 2;
                }
            }

            if (!Title.Equals(""))
            {
                NbBytes += "TITLE:".Length + Title.Length + 2;
            }

            if (!Role.Equals(""))
            {
                NbBytes += "ROLE:".Length + Role.Length + 2;
            }

            if (!Company.Equals(""))
            {
                NbBytes += "ORG:".Length + Company.Length + 2;
            }

            if (!_photo.Equals(""))
            {
                NbBytes += "PHOTO;ENCODING=BASE64;TYPE=JPEG:".Length + _photo.Length + 2;
            }

            NbBytes += "END:VCARD".Length + 2;

            return(NbBytes);
        }