/// <summary> /// Used to find duplicates. /// </summary> /// <param name="name"></param> /// <param name="email"></param> /// <returns></returns> public Collection <Outlook.ContactItem> OutlookContactByEmail(string email) { //TODO: optimise by using OutlookContacts.Find Collection <Outlook.ContactItem> col = new Collection <Outlook.ContactItem>(); Outlook.ContactItem item = null; try { item = OutlookContacts.Find("[Email1Address] = \"" + email + "\"") as Outlook.ContactItem; if (item != null) { col.Add(item); do { item = OutlookContacts.FindNext() as Outlook.ContactItem; if (item != null) { col.Add(item); } } while (item != null); } } catch (Exception) { //TODO: should not get here. } return(col); //Collection<Outlook.ContactItem> col = new Collection<Outlook.ContactItem>(); //foreach (Outlook.ContactItem outlookContact in OutlookContacts) //{ // try // { // if (!(outlookContact is Outlook.ContactItem)) // continue; // } // catch (Exception ex) // { // //this is needed because some contacts throw exceptions // continue; // } // if (outlookContact != null && ( // outlookContact.Email1Address != null && outlookContact.Email1Address == email)) // { // col.Add(outlookContact); // } //} //return col; }
public void TestReplaceContact() { Application app = new Application(); OutlookContactsWithSifC agent = new OutlookContactsWithSifC(app); OutlookContacts outlookAgent = new OutlookContacts(app); AddContactToOutlook(); string existingEntryId = outlookAgent.GetEntryIdByDisplayName("Ing John Patrick Doe Sn"); XElement x = XElement.Load(MockPath + "sifc.xml"); string entryId = agent.ReplaceItem(x.ToString(), existingEntryId); Assert.IsTrue(entryId == existingEntryId); }
public void AddContactToOutlook() { XElement x = XElement.Load(MockPath + "sifc.xml"); Application app = new Application(); OutlookContactsWithSifC agent = new OutlookContactsWithSifC(app); OutlookContacts outlookAgent = new OutlookContacts(app); string entryId = outlookAgent.GetEntryIdByDisplayName("Ing John Patrick Doe Sn");//Delete it if exists if (entryId != null) { bool deletionOK = outlookAgent.DeleteItem(entryId); Assert.IsTrue(deletionOK); } agent.AddItem(x.ToString());//Add to outlook entryId = outlookAgent.GetEntryIdByDisplayName("Ing John Patrick Doe Sn"); Assert.IsTrue(entryId != null); }
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); }
public void FullSync(Config.Direction dir) { MutexManager.StartingFullSync(); try { try { if (dir == Config.Direction.dirToOutlook) { SetLastGoogleFetch(DateTime.Now); } if (StartSynching != null) { StartSynching(this); } List <ContactItem> OutlookContacts; List <Contact> GoogleContacts; logger.Info("Obtaining Outlook contacts..."); OutlookContacts = outlookAdapter.Contacts; logger.Info("Obtaining Google Contacts..."); GoogleContacts = googleAdapter.Contacts; EmailComparer comparer = new EmailComparer(); int i = 0; int total = OutlookContacts.Count() + GoogleContacts.Count(); foreach (ContactItem item in OutlookContacts) { try { try { if (Thread.CurrentThread.ThreadState == ThreadState.AbortRequested) { break; } i++; var qryFindGoogleContact = GoogleContacts.Where(c => c.Title == item.FullName || c.Emails.Contains(new EMail(item.Email1Address), comparer) || c.Emails.Contains(new EMail(item.Email2Address), comparer) || c.Emails.Contains(new EMail(item.Email3Address), comparer)); if (qryFindGoogleContact.Count() > 0) { if (dir == Config.Direction.dirToOutlook) { logger.Info(String.Format("{0}/{1} Updating Outlook contact: " + OutlookAdapter.ContactItemDisplay(item), i, total)); Contact gContact = qryFindGoogleContact.First(); logger.Info("Updating outlook contact"); SyncContact(gContact, item, dir); } else { logger.Info(String.Format("{0}/{1} Processing...", i, total)); } } else { logger.Info(String.Format("{0}/{1} Creating Google contact for " + OutlookAdapter.ContactItemDisplay(item), i, total)); // MutexManager.AddToBlockedContacts(item); CreateGoogleContact(item); } } finally { if (OutlookSynched != null) { OutlookSynched(this, item, i, OutlookContacts.Count()); } } } catch (System.Exception ex) { if (ex is ThreadAbortException) { break; } } } foreach (Contact item in GoogleContacts) { try { try { if (Thread.CurrentThread.ThreadState == ThreadState.AbortRequested) { break; } i++; var qryFindOutlookContact = OutlookContacts.Where(c => c.FullName == item.Title || item.Emails.Contains(new EMail(c.Email1Address), comparer) || item.Emails.Contains(new EMail(c.Email2Address), comparer) || item.Emails.Contains(new EMail(c.Email3Address), comparer)); if (qryFindOutlookContact.Count() > 0) { if (dir == Config.Direction.dirToGoogle) { logger.Info(String.Format("{0}/{1} Updating Google contact: " + GoogleAdapter.ContactDisplay(item), i, total)); ContactItem oContact = qryFindOutlookContact.First(); SyncContact(item, oContact, dir); } else { logger.Info(String.Format("{0}/{1} Processing...", i, total)); } } else { logger.Info(String.Format("{0}/{1} Creating Outlook contact for " + GoogleAdapter.ContactDisplay(item), i, total)); CreateOutlookContact(item); } } finally { if (GoogleSynched != null) { GoogleSynched(this, item, i, GoogleContacts.Count()); } } } catch (System.Exception ex) { if (ex is ThreadAbortException) { break; } } } if (EndSynching != null) { EndSynching(this); } } catch (System.Exception ex) { if ((EndSynching != null)) { EndSynching(this); } else if (!(ex is ThreadAbortException) & (Error != null)) { Error(this, ex); } } } finally { if (dir == Config.Direction.dirToGoogle) { SetLastGoogleFetch(DateTime.Now); } MutexManager.EndingFullSync(); } }