Пример #1
0
        public void TestContactAndVCard()
        {
            string vcardText = File.ReadAllText(MockPath + "Contact of Outlook.vcf");
            VCard  v         = VCardReader.ParseText(vcardText);

            Application app = new Application();
            OutlookContactsWithVCard agent        = new OutlookContactsWithVCard(app);
            OutlookContacts          outlookAgent = new OutlookContacts(app);
            const string             fullName     = "Manager Contact Of Outlook Sr.";
            string entryId = outlookAgent.GetEntryIdByDisplayName(fullName);

            if (entryId != null)
            {
                bool deletionOK = outlookAgent.DeleteItem(entryId);
                Assert.IsTrue(deletionOK);
            }
            agent.AddItem(vcardText);
            entryId = outlookAgent.GetEntryIdByDisplayName(fullName);
            Assert.IsTrue(entryId != null);

            //test conversions
            ContactItem contactItem = outlookAgent.GetItemByEntryId(entryId);
            string      text        = agent.ReadItemToText(contactItem);
            VCard       v2          = VCardReader.ParseText(text);

            Assert.IsTrue(v.Surname == v2.Surname &&
                          v.GivenName == v2.GivenName &&
                          v.MiddleName == v2.MiddleName &&
                          v.Suffix == v2.Suffix && //full name will not be compared as Outlook treat fullname specially.
                          v.Title == v2.Title &&
                          v.Role == v2.Role &&
                          v.Birthday == v2.Birthday &&
                          v.Org == v2.Org &&
                          v.Department == v2.Department &&
                          v.Phones.Count == v2.Phones.Count &&
                          v.Emails.Count == v2.Emails.Count &&
                          v.Addresses.Count == v2.Addresses.Count &&
                          v.URLs.Count == v2.URLs.Count);
        }
Пример #2
0
        public DataTable ImportDataToDataTable()
        {
            DataTable dt = BuildTableStructure();

            String Vcf = File.ReadAllText(FilePath);

            Fonlow.VCard.VCard vc = VCardReader.ParseText(Vcf);
            DataRow            dr = dt.NewRow();

            if (String.IsNullOrEmpty(vc.FormattedName) == false)
            {
                dr["Full Name"] = vc.FormattedName;
            }

            if (String.IsNullOrEmpty(vc.FormattedName) == false)
            {
                dr["Surname"] = vc.Surname;
            }

            if (String.IsNullOrEmpty(vc.GivenName) == false)
            {
                dr["GivenName"] = vc.GivenName;
            }

            if (String.IsNullOrEmpty(vc.MiddleName) == false)
            {
                dr["MiddleName"] = vc.MiddleName;
            }

            if (String.IsNullOrEmpty(vc.Title) == false)
            {
                dr["Title"] = vc.Title;
            }

            if (String.IsNullOrEmpty(vc.Prefix) == false)
            {
                dr["Prefix"] = vc.Prefix;
            }

            if (String.IsNullOrEmpty(vc.Suffix) == false)
            {
                dr["Suffix"] = vc.Suffix;
            }

            if (vc.Birthday > DateTime.MinValue)
            {
                dr["Birthday"] = vc.Birthday.ToLongDateString();
            }

            if (vc.Rev > DateTime.MinValue)
            {
                dr["Rev"] = vc.Rev.ToLongDateString();
            }

            if (String.IsNullOrEmpty(vc.Org) == false)
            {
                dr["Org"] = vc.Org;
            }

            for (int j = 0; j == vc.Phones.Count - 1; j++)
            {
                if (String.IsNullOrEmpty(dr["Phones"].ToString()) == false)
                {
                    dr["Phones"] = dr["Phones"] + " | " + ("Phone " + vc.Phones[j].PhoneTypes.ToString("G") + " " + vc.Phones[j].HomeWorkTypes.ToString() + " (Preferred = " +
                                                           (vc.Phones[j].Pref + ") =" + vc.Phones[j].Number));
                }
                else
                {
                    dr["Phones"] = ("Phone " + vc.Phones[j].PhoneTypes.ToString("G") + " " + vc.Phones[j].HomeWorkTypes.ToString() + " (Preferred = " +
                                    (vc.Phones[j].Pref + ") =" + vc.Phones[j].Number));
                }
            }

            for (int j = 0; j == vc.Emails.Count - 1; j++)
            {
                if (String.IsNullOrEmpty(dr["Emails"].ToString()) == false)
                {
                    dr["Emails"] = dr["Emails"] + " | " + ("Email " + " (Preferred = " + (vc.Emails[j].Pref.ToString()) + ") =" + vc.Emails[j].Address);
                }
                else
                {
                    dr["Emails"] = ("Email " + " (Preferred = " + (vc.Emails[j].Pref.ToString()) + ") =" + vc.Emails[j].Address);
                }
            }

            for (int j = 0; j == vc.URLs.Count - 1; j++)
            {
                if (String.IsNullOrEmpty(dr["URL"].ToString()) == false)
                {
                    dr["URL"] = dr["URL"] + " | " + ("URL " + vc.URLs[j].HomeWorkTypes.ToString() + " " + "=" + vc.URLs[j].Address);
                }
                else
                {
                    dr["URL"] = ("URL " + vc.URLs[j].HomeWorkTypes.ToString() + " " + "=" + vc.URLs[j].Address);
                }
            }

            for (int j = 0; j == vc.Addresses.Count - 1; j++)
            {
                if (String.IsNullOrEmpty(dr["Addresses"].ToString()) == false)
                {
                    dr["Addresses"] = dr["Addresses"] + " | " + ("Address " + vc.Addresses[j].HomeWorkType.ToString() + "=" + vc.Addresses[j].POBox + "," + vc.Addresses[j].Ext +
                                                                 ", " + vc.Addresses[j].Street + ", " + vc.Addresses[j].Locality + ", " + vc.Addresses[j].Region + ", " +
                                                                 vc.Addresses[j].Postcode + ", " + vc.Addresses[j].Country);
                }
                else
                {
                    dr["Addresses"] = ("Address " + vc.Addresses[j].HomeWorkType.ToString() + "=" + vc.Addresses[j].POBox + "," + vc.Addresses[j].Ext + ", " + vc.Addresses[j].Street + ", " +
                                       vc.Addresses[j].Locality + ", " + vc.Addresses[j].Region + ", " + vc.Addresses[j].Postcode + ", " + vc.Addresses[j].Country);
                }
            }


            if (String.IsNullOrEmpty(vc.Note) == false)
            {
                dr["Note"] = ("Note=" + vc.Note);
            }

            dt.Rows.Add(dr);
            Maindataset.Tables.Add(dt);
            return(dt);
        }
Пример #3
0
        protected override void WriteMetaToItem(string meta, ContactItem item)
        {
            Fonlow.VCard.VCard vcard = VCardReader.ParseText(meta);
            item.LastName   = vcard.Surname;
            item.FirstName  = vcard.GivenName;
            item.MiddleName = vcard.MiddleName;
            item.Title      = vcard.Title;
            item.Suffix     = vcard.Suffix;
            //   item.FullName = vcard.FormattedName;//This must be after lastName to Suffix, otherwise, item.FullName will be recomposed by Outlook
            item.Body = vcard.Note;

            item.CompanyName = vcard.Org;
            item.Department  = vcard.Department;
            item.JobTitle    = vcard.Role;

            if (vcard.Birthday > DateTime.MinValue)
            {
                item.Birthday = vcard.Birthday;
            }

            PhoneNumber phoneNumber = vcard.Phones.GetItemsOfTypes(PhoneTypes.CELL).FirstOrDefault();

            if (phoneNumber != null)
            {
                item.MobileTelephoneNumber = phoneNumber.Number;
            }

            //Home phones
            IEnumerable <PhoneNumber> numbers = vcard.Phones.GetItemsOfTypes(PhoneTypes.VOICE, HomeWorkTypes.HOME);

            phoneNumber = numbers.FirstOrDefault();
            if (phoneNumber != null)
            {
                item.HomeTelephoneNumber = phoneNumber.Number;
                if (numbers.Count() > 1)
                {
                    item.Home2TelephoneNumber = numbers.ElementAt(1).Number;
                }
            }

            //Home fax
            numbers     = vcard.Phones.GetItemsOfTypes(PhoneTypes.FAX, HomeWorkTypes.HOME);
            phoneNumber = numbers.FirstOrDefault();
            if (phoneNumber != null)
            {
                item.HomeFaxNumber = phoneNumber.Number;
            }

            //Work phones
            numbers     = vcard.Phones.GetItemsOfTypes(PhoneTypes.VOICE, HomeWorkTypes.WORK);
            phoneNumber = numbers.FirstOrDefault();
            if (phoneNumber != null)
            {
                item.BusinessTelephoneNumber = phoneNumber.Number;
                if (numbers.Count() > 1)
                {
                    item.Business2TelephoneNumber = numbers.ElementAt(1).Number;
                }
            }

            //Work fax
            numbers     = vcard.Phones.GetItemsOfTypes(PhoneTypes.FAX, HomeWorkTypes.WORK);
            phoneNumber = numbers.FirstOrDefault();
            if (phoneNumber != null)
            {
                item.BusinessFaxNumber = phoneNumber.Number;
            }

            //Other phone
            numbers     = vcard.Phones.GetItemsOfTypesExactly(PhoneTypes.VOICE, HomeWorkTypes.None);
            phoneNumber = numbers.FirstOrDefault();
            if (phoneNumber != null)
            {
                item.OtherTelephoneNumber = phoneNumber.Number;
            }

            //Other fax
            numbers     = vcard.Phones.GetItemsOfTypesExactly(PhoneTypes.FAX, HomeWorkTypes.None);
            phoneNumber = numbers.FirstOrDefault();
            if (phoneNumber != null)
            {
                item.OtherFaxNumber = phoneNumber.Number;
            }

            phoneNumber = vcard.Phones.GetPreferredItems().FirstOrDefault(n => (n.HomeWorkTypes == HomeWorkTypes.HOME) || (n.HomeWorkTypes == HomeWorkTypes.None));
            if (phoneNumber != null)
            {
                item.PrimaryTelephoneNumber = phoneNumber.Number;
            }

            phoneNumber = vcard.Phones.GetPreferredItems().FirstOrDefault(n => n.HomeWorkTypes == HomeWorkTypes.WORK);
            if (phoneNumber != null)
            {
                item.CompanyMainTelephoneNumber = phoneNumber.Number;
            }

            phoneNumber = vcard.Phones.GetItemsOfTypes(PhoneTypes.PAGER).FirstOrDefault();
            if (phoneNumber != null)
            {
                item.PagerNumber = phoneNumber.Number;
            }

            EmailAddress email = vcard.Emails.FirstOrDefault();

            if (email != null)
            {
                item.Email1Address = email.Address;
                if (vcard.Emails.Count() > 1)
                {
                    item.Email2Address = vcard.Emails.ElementAt(1).Address;
                }

                if (vcard.Emails.Count() > 2)
                {
                    item.Email3Address = vcard.Emails.ElementAt(2).Address;
                }
            }

            Address address = vcard.Addresses.FirstOrDefault(n => n.HomeWorkType == HomeWorkTypes.HOME);

            if (address != null)
            {
                item.HomeAddressCity          = address.Locality;
                item.HomeAddressCountry       = address.Country;
                item.HomeAddressPostalCode    = address.Postcode;
                item.HomeAddressPostOfficeBox = address.POBox;
                item.HomeAddressState         = address.Region;
                item.HomeAddressStreet        = address.Street;
            }

            address = vcard.Addresses.FirstOrDefault(n => n.HomeWorkType == HomeWorkTypes.WORK);
            if (address != null)
            {
                item.BusinessAddressCity          = address.Locality;
                item.BusinessAddressCountry       = address.Country;
                item.BusinessAddressPostalCode    = address.Postcode;
                item.BusinessAddressPostOfficeBox = address.POBox;
                item.BusinessAddressState         = address.Region;
                item.BusinessAddressStreet        = address.Street;
            }

            URL url = vcard.URLs.FirstOrDefault(n => n.HomeWorkTypes == HomeWorkTypes.HOME);

            if (url != null)
            {
                item.PersonalHomePage = url.Address;
            }

            url = vcard.URLs.FirstOrDefault(n => n.HomeWorkTypes == HomeWorkTypes.WORK);
            if (url != null)
            {
                item.BusinessHomePage = url.Address;
            }
        }
        }  // using xmlWriter

        /// <summary>
        ///
        /// </summary>
        /// <param name="topElement">the 'Changes' element, to be manipulated</param>
        /// <param name="command"></param>
        private static void WriteAddOrReplaceCommandToXml(XElement topElement, SyncMLUpdateBase command)
        {
            if (command == null)
            {
                return;
            }

            bool isBase64 = false;
            ContactExchangeType contentType = ContactExchangeType.Unknown;
            bool isAddCommand = false;

            string commandTypeStr;

            SyncMLMeta commandMeta = command.Meta;
            SyncMLItem commandItem = command.ItemCollection[0]; //assuming there is always one item.

            if (String.IsNullOrEmpty(commandMeta.Content) && (!commandMeta.Xml.HasElements))
            {
                commandMeta = commandItem.Meta;
            }

            MetaParser metaParser = new MetaParser(commandMeta.Xml);
            MetaFormat metaFormat = metaParser.GetMetaFormat();

            if (metaFormat != null)
            {
                isBase64 = metaFormat.Content == "b64";
            }

            MetaType metaType = metaParser.GetMetaType();

            if (metaType == null)
            {
                return; //the meta element may be empty, so no need to proceed.
            }

            if (metaType.Content == "text/x-s4j-sifc")
            {
                contentType = ContactExchangeType.Sifc;
            }
            else if ((metaType.Content == "text/x-vcard") || (metaType.Content == "text/vcard"))
            {
                contentType = ContactExchangeType.Vcard21;
            }

            if (contentType == ContactExchangeType.Unknown)
            {
                throw new LocalDataSourceException("expected data is Base64 encoded SIF-C or vCard");
            }

            isAddCommand   = command is SyncMLAdd;
            commandTypeStr = isAddCommand ? "New" : "Update";

            //Assuming there will be only one item.
            string id;

            if (isAddCommand)
            {
                id = commandItem.Source.LocURI.Content;
            }
            else
            {
                id = commandItem.Target.LocURI.Content;
            }

            XElement cItem     = new XElement("C", new XAttribute("ID", id));
            XElement changItem = new XElement(commandTypeStr, cItem);

            topElement.Add(changItem);

            switch (contentType)
            {
            case ContactExchangeType.Sifc:     // do not need to check isBase64, because it must be
                cItem.Add(XElement.Parse(Utility.ConvertFromBase64(commandItem.Data.Content)));
                break;

            case ContactExchangeType.Vcard21:
                XElement sifcXml;
                if (isBase64)
                {
                    sifcXml = VCardSIFC.ConvertVCardToSifCXml(VCardReader.ParseText(
                                                                  Utility.ConvertFromBase64(commandItem.Data.Content)));
                }
                else
                {
                    sifcXml = VCardSIFC.ConvertVCardToSifCXml(VCardReader.ParseText(commandItem.Data.Content));
                }

                cItem.Add(sifcXml);
                break;

            default:
                throw new LocalDataSourceException("Can not create stream from command Data.");
            }
        }