private void InitializeFields() { // TODO: init all non null fields fields = new Collection <CPField>(); int index = 0; int height = Font.Height; if (outlookContact.FirstName != null) { fields.Add(new CPField("First name", outlookContact.FirstName, new PointF(0, index * height))); index++; } if (outlookContact.LastName != null) { fields.Add(new CPField("Last name", outlookContact.LastName, new PointF(0, index * height))); index++; } if (outlookContact.Email1Address != null) { fields.Add(new CPField("Email", ContactPropertiesUtils.GetOutlookEmailAddress1(outlookContact), new PointF(0, index * height))); index++; } // resize to fit this.Height = (index + 1) * height; }
internal void Update(ContactItem outlookContactItem, Syncronizer sync) { this.EntryID = outlookContactItem.EntryID; this.FileAs = outlookContactItem.FileAs; this.FullName = outlookContactItem.FullName; this.Email1Address = ContactPropertiesUtils.GetOutlookEmailAddress1(outlookContactItem); this.MobileTelephoneNumber = outlookContactItem.MobileTelephoneNumber; this.Categories = outlookContactItem.Categories; this.LastModificationTime = outlookContactItem.LastModificationTime; this.Company = outlookContactItem.CompanyName; this.TitleFirstLastAndSuffix = GetTitleFirstLastAndSuffix(outlookContactItem); UserProperties userProperties = outlookContactItem.UserProperties; UserProperty prop = userProperties[sync.OutlookPropertyNameId]; this.UserProperties.GoogleContactId = prop != null?string.Copy((string)prop.Value) : null; if (prop != null) { Marshal.ReleaseComObject(prop); } prop = userProperties[sync.OutlookPropertyNameSynced]; this.UserProperties.LastSync = prop != null ? (DateTime)prop.Value : (DateTime?)null; if (prop != null) { Marshal.ReleaseComObject(prop); } Marshal.ReleaseComObject(userProperties); }
public static void SetEmails(Outlook.ContactItem source, Contact destination) { destination.Emails.Clear(); string email = ContactPropertiesUtils.GetOutlookEmailAddress1(source); AddEmail(destination, email, source.Email1DisplayName, ContactsRelationships.IsWork); email = ContactPropertiesUtils.GetOutlookEmailAddress2(source); AddEmail(destination, email, source.Email2DisplayName, ContactsRelationships.IsHome); email = ContactPropertiesUtils.GetOutlookEmailAddress3(source); AddEmail(destination, email, source.Email3DisplayName, ContactsRelationships.IsOther); }
/// <summary> /// Updates Outlook contact from Google (but without groups/categories) /// </summary> public static void UpdateContact(Contact master, Outlook.ContactItem slave, bool useFileAs) { //// if no email or number, contact will be updated at each sync //if (master.Emails.Count == 0 && master.Phonenumbers.Count == 0) // return; #region Name slave.Title = master.Name.NamePrefix; slave.FirstName = master.Name.GivenName; slave.MiddleName = master.Name.AdditionalName; slave.LastName = master.Name.FamilyName; slave.Suffix = master.Name.NameSuffix; if (string.IsNullOrEmpty(slave.FullName)) //The Outlook fullName is automatically set, so don't assign it from Google, unless the structured properties were empty { slave.FullName = master.Name.FullName; } #endregion Name #region Title/FileAs if (string.IsNullOrEmpty(slave.FileAs) || useFileAs) { if (!string.IsNullOrEmpty(master.Name.FullName)) { slave.FileAs = master.Name.FullName.Replace("\r\n", "\n").Replace("\n", "\r\n"); //Replace twice to not replace a \r\n by \r\r\n. This is necessary because \r\n are saved as \n only to google and \r\n is saved on Outlook side to separate the single parts of the FullName } else if (!string.IsNullOrEmpty(master.Title)) { slave.FileAs = master.Title.Replace("\r\n", "\n").Replace("\n", "\r\n"); //Replace twice to not replace a \r\n by \r\r\n. This is necessary because \r\n are saved as \n only to google and \r\n is saved on Outlook side to separate the single parts of the FullName } else if (master.Organizations.Count > 0 && !string.IsNullOrEmpty(master.Organizations[0].Name)) { slave.FileAs = master.Organizations[0].Name; } else if (master.Emails.Count > 0 && !string.IsNullOrEmpty(master.Emails[0].Address)) { slave.FileAs = master.Emails[0].Address; } } if (string.IsNullOrEmpty(slave.FileAs)) { if (!String.IsNullOrEmpty(slave.Email1Address)) { string emailAddress = ContactPropertiesUtils.GetOutlookEmailAddress1(slave); Logger.Log("Google Contact '" + master.Summary + "' has neither name nor email address. Setting email address of Outlook contact: " + emailAddress, EventType.Warning); master.Emails.Add(new EMail(emailAddress)); slave.FileAs = master.Emails[0].Address; } else { Logger.Log("Google Contact '" + master.Summary + "' has neither name nor email address. Cannot merge with Outlook contact: " + slave.FileAs, EventType.Error); return; } } #endregion Title/FileAs #region birthday try { DateTime birthday; DateTime.TryParse(master.ContactEntry.Birthday, out birthday); if (birthday != DateTime.MinValue) { if (!birthday.Date.Equals(slave.Birthday.Date)) //Only update if not already equal to avoid recreating the calendar item again and again { slave.Birthday = birthday.Date; } } else { slave.Birthday = outlookDateNone; } } catch (Exception ex) { Logger.Log("Birthday (" + master.ContactEntry.Birthday + ") couldn't be updated from Google to Outlook for '" + slave.FileAs + "': " + ex.Message, EventType.Error); } #endregion birthday slave.NickName = master.ContactEntry.Nickname; slave.OfficeLocation = master.Location; //Categories are synced separately in Syncronizer.OverwriteContactGroups: slave.Categories = master.Categories; slave.Initials = master.ContactEntry.Initials; if (master.Languages != null) { slave.Language = string.Empty; foreach (Language language in master.Languages) { slave.Language = language.Label + ";"; } if (!string.IsNullOrEmpty(slave.Language)) { slave.Language = slave.Language.TrimEnd(';'); } } SetEmails(master, slave); #region phones //First delete the destination phone numbers slave.PrimaryTelephoneNumber = string.Empty; slave.HomeTelephoneNumber = string.Empty; slave.Home2TelephoneNumber = string.Empty; slave.BusinessTelephoneNumber = string.Empty; slave.Business2TelephoneNumber = string.Empty; slave.MobileTelephoneNumber = string.Empty; slave.BusinessFaxNumber = string.Empty; slave.HomeFaxNumber = string.Empty; slave.PagerNumber = string.Empty; //slave.RadioTelephoneNumber = string.Empty; //IsSatellite is not working with google (invalid rel value is returned) slave.OtherTelephoneNumber = string.Empty; slave.CarTelephoneNumber = string.Empty; slave.AssistantTelephoneNumber = string.Empty; foreach (PhoneNumber phone in master.Phonenumbers) { SetPhoneNumber(phone, slave); } //ToDo: Temporary cleanup algorithm to get rid of duplicate primary phone numbers //Can be removed once the contacts are clean for all users: //if (!String.IsNullOrEmpty(slave.PrimaryTelephoneNumber)) //{ // if (slave.PrimaryTelephoneNumber.Equals(slave.MobileTelephoneNumber)) // { // slave.PrimaryTelephoneNumber = String.Empty; // if (slave.MobileTelephoneNumber.Equals(slave.HomeTelephoneNumber) || // slave.MobileTelephoneNumber.Equals(slave.Home2TelephoneNumber) || // slave.MobileTelephoneNumber.Equals(slave.BusinessTelephoneNumber) || // slave.MobileTelephoneNumber.Equals(slave.Business2TelephoneNumber) || // slave.MobileTelephoneNumber.Equals(slave.HomeFaxNumber) || // slave.MobileTelephoneNumber.Equals(slave.BusinessFaxNumber) || // slave.MobileTelephoneNumber.Equals(slave.OtherTelephoneNumber) || // slave.MobileTelephoneNumber.Equals(slave.PagerNumber) || // slave.MobileTelephoneNumber.Equals(slave.CarTelephoneNumber)) // { // slave.MobileTelephoneNumber = String.Empty; // } // } // else if (slave.PrimaryTelephoneNumber.Equals(slave.HomeTelephoneNumber) || // slave.PrimaryTelephoneNumber.Equals(slave.Home2TelephoneNumber) || // slave.PrimaryTelephoneNumber.Equals(slave.BusinessTelephoneNumber) || // slave.PrimaryTelephoneNumber.Equals(slave.Business2TelephoneNumber) || // slave.PrimaryTelephoneNumber.Equals(slave.HomeFaxNumber) || // slave.PrimaryTelephoneNumber.Equals(slave.BusinessFaxNumber) || // slave.PrimaryTelephoneNumber.Equals(slave.OtherTelephoneNumber) || // slave.PrimaryTelephoneNumber.Equals(slave.PagerNumber) || // slave.PrimaryTelephoneNumber.Equals(slave.CarTelephoneNumber)) // { // //Reset primary TelephoneNumber because it is duplicate // slave.PrimaryTelephoneNumber = string.Empty; // } //} #endregion phones #region addresses slave.HomeAddress = string.Empty; slave.HomeAddressStreet = string.Empty; slave.HomeAddressCity = string.Empty; slave.HomeAddressPostalCode = string.Empty; slave.HomeAddressCountry = string.Empty; slave.HomeAddressState = string.Empty; slave.HomeAddressPostOfficeBox = string.Empty; slave.BusinessAddress = string.Empty; slave.BusinessAddressStreet = string.Empty; slave.BusinessAddressCity = string.Empty; slave.BusinessAddressPostalCode = string.Empty; slave.BusinessAddressCountry = string.Empty; slave.BusinessAddressState = string.Empty; slave.BusinessAddressPostOfficeBox = string.Empty; slave.OtherAddress = string.Empty; slave.OtherAddressStreet = string.Empty; slave.OtherAddressCity = string.Empty; slave.OtherAddressPostalCode = string.Empty; slave.OtherAddressCountry = string.Empty; slave.OtherAddressState = string.Empty; slave.OtherAddressPostOfficeBox = string.Empty; slave.SelectedMailingAddress = Microsoft.Office.Interop.Outlook.OlMailingAddress.olNone; foreach (StructuredPostalAddress address in master.PostalAddresses) { SetPostalAddress(address, slave); } #endregion addresses #region companies slave.Companies = string.Empty; slave.CompanyName = string.Empty; slave.JobTitle = string.Empty; slave.Department = string.Empty; foreach (Organization company in master.Organizations) { if (string.IsNullOrEmpty(company.Name) && string.IsNullOrEmpty(company.Title) && string.IsNullOrEmpty(company.Department)) { continue; } if (company.Primary || company.Equals(master.Organizations[0])) {//Per default copy the first company, but if there is a primary existing, use the primary slave.CompanyName = company.Name; slave.JobTitle = company.Title; slave.Department = company.Department; } if (!string.IsNullOrEmpty(slave.Companies)) { slave.Companies += "; "; } slave.Companies += company.Name; } #endregion companies #region IM slave.IMAddress = string.Empty; foreach (IMAddress im in master.IMs) { if (!string.IsNullOrEmpty(slave.IMAddress)) { slave.IMAddress += "; "; } if (!string.IsNullOrEmpty(im.Protocol) && !im.Protocol.Equals("None", StringComparison.InvariantCultureIgnoreCase)) { slave.IMAddress += im.Protocol + ": " + im.Address; } else { slave.IMAddress += im.Address; } } #endregion IM #region anniversary bool found = false; try { foreach (Event ev in master.ContactEntry.Events) { if (ev.Relation != null && ev.Relation.Equals(relAnniversary)) { if (!ev.When.StartTime.Date.Equals(slave.Anniversary.Date)) //Only update if not already equal to avoid recreating the calendar item again and again { slave.Anniversary = ev.When.StartTime.Date; } found = true; break; } } if (!found) { slave.Anniversary = outlookDateNone; //set to empty in the end } } catch (Exception ex) { Logger.Log("Anniversary couldn't be updated from Google to Outlook for '" + slave.FileAs + "': " + ex.Message, EventType.Error); } #endregion anniversary #region relations (spouse, child, manager, assistant) slave.Children = string.Empty; slave.Spouse = string.Empty; slave.ManagerName = string.Empty; slave.AssistantName = string.Empty; foreach (Relation rel in master.ContactEntry.Relations) { if (rel.Rel != null && rel.Rel.Equals(relChild)) { slave.Children = rel.Value; } else if (rel.Rel != null && rel.Rel.Equals(relSpouse)) { slave.Spouse = rel.Value; } else if (rel.Rel != null && rel.Rel.Equals(relManager)) { slave.ManagerName = rel.Value; } else if (rel.Rel != null && rel.Rel.Equals(relAssistant)) { slave.AssistantName = rel.Value; } } #endregion relations (spouse, child, manager, assistant) slave.WebPage = string.Empty; foreach (Website website in master.ContactEntry.Websites) { if (website.Primary || website.Equals(master.ContactEntry.Websites[0])) {//Per default copy the first website, but if there is a primary existing, use the primary slave.WebPage = master.ContactEntry.Websites[0].Href; } } try { string rtf; if (slave.Body == null) { rtf = string.Empty; } else { rtf = Utilities.ConvertToText(slave.RTFBody as byte[]); } if (string.IsNullOrEmpty(rtf) || rtf.Equals(slave.Body) && !rtf.Equals(master.Content)) //only update, if RTF text is same as plain text and is different between master and slave { slave.Body = master.Content; } else if (!rtf.Equals(master.Content)) { Logger.Log("Outlook contact notes body not updated, because it is RTF, otherwise it will overwrite it by plain text: " + slave.FileAs, EventType.Warning); } } catch (Exception e) { Logger.Log("Error when converting RTF to plain text, updating Google Contact '" + slave.FileAs + "' notes to Outlook without RTF check: " + e.Message, EventType.Debug); slave.Body = master.Content; } }