示例#1
0
        /// <summary>
        /// Updates Google contact from Outlook (but without groups/categories)
        /// </summary>
        public static void UpdateContact(Outlook.ContactItem master, Contact slave, bool useFileAs)
        {
            //// if no email or number, contact will be updated at each sync
            //if (string.IsNullOrEmpty(master.Email1Address) && string.IsNullOrEmpty(master.PrimaryTelephoneNumber))
            //{
            //    if (slave.Emails.Count > 0)
            //    {
            //        Logger.Log("Outlook Contact '" + master.FullNameAndCompany + "' has neither email address nor phone number. Setting email address of Google contact: " + slave.Emails[0].Address, EventType.Warning);
            //        master.Email1Address = slave.Emails[0].Address;
            //    }
            //    else
            //    {
            //        Logger.Log("Outlook Contact '" + master.FullNameAndCompany + "' has neither email address nor phone number. Cannot merge with Google contact: " + slave.Summary, EventType.Error);
            //        return;
            //    }
            //}

            //TODO: convert to merge as opposed to replace

            #region Title/FileAs

            slave.Title = null;
            if (useFileAs)
            {
                if (!string.IsNullOrEmpty(master.FileAs))
                    slave.Title = master.FileAs;
                else if (!string.IsNullOrEmpty(master.FullName))
                    slave.Title = master.FullName;
                else if (!string.IsNullOrEmpty(master.CompanyName))
                    slave.Title = master.CompanyName;
                else if (!string.IsNullOrEmpty(master.Email1Address))
                    slave.Title = master.Email1Address;
            }

            #endregion Title/FileAs

            #region Name
            Name name = new Name();

            name.NamePrefix = master.Title;
            name.GivenName = master.FirstName;
            name.AdditionalName = master.MiddleName;
            name.FamilyName = master.LastName;
            name.NameSuffix = master.Suffix;

            //Use the Google's full name to save a unique identifier. When saving the FullName, it always overwrites the Google Title
            if (!string.IsNullOrEmpty(master.FullName)) //Only if master.FullName has a value, i.e. not only a company or email contact
            {
                if (useFileAs)
                    name.FullName = master.FileAs;
                else
                {
                    name.FullName = OutlookContactInfo.GetTitleFirstLastAndSuffix(master);
                    if (!string.IsNullOrEmpty(name.FullName))
                        name.FullName = name.FullName.Trim().Replace("  ", " ");
                }
            }

            slave.Name = name;
            #endregion Name

            #region Birthday
            try
            {
                if (master.Birthday.Equals(outlookDateNone)) //earlier also || master.Birthday.Year < 1900
                    slave.ContactEntry.Birthday = null;
                else
                    slave.ContactEntry.Birthday = master.Birthday.ToString("yyyy-MM-dd");
            }
            catch (Exception ex)
            {
                Logger.Log("Birthday couldn't be updated from Outlook to Google for '" + master.FileAs + "': " + ex.Message, EventType.Error);
            }
            #endregion Birthday

            slave.ContactEntry.Nickname = master.NickName;
            slave.Location = master.OfficeLocation;
            //Categories are synced separately in Syncronizer.OverwriteContactGroups: slave.Categories = master.Categories;
            slave.ContactEntry.Initials = master.Initials;
            slave.Languages.Clear();
            if (!string.IsNullOrEmpty(master.Language))
            {
                foreach (string language in master.Language.Split(';'))
                {
                    Language googleLanguage = new Language();
                    googleLanguage.Label = language;
                    slave.Languages.Add(googleLanguage);
                }
            }

            SetEmails(master, slave);

            SetAddresses(master, slave);

            SetPhoneNumbers(master, slave);

            SetCompanies(master, slave);

            SetIMs(master, slave);

            #region anniversary
            //First remove anniversary
            foreach (Event ev in slave.ContactEntry.Events)
            {
                if (ev.Relation != null && ev.Relation.Equals(relAnniversary))
                {
                    slave.ContactEntry.Events.Remove(ev);
                    break;
                }
            }
            try
            {
                //Then add it again if existing
                if (!master.Anniversary.Equals(outlookDateNone)) //earlier also || master.Birthday.Year < 1900
                {
                    Event ev = new Event();
                    ev.Relation = relAnniversary;
                    ev.When = new When();
                    ev.When.AllDay = true;
                    ev.When.StartTime = master.Anniversary.Date;
                    slave.ContactEntry.Events.Add(ev);
                }
            }
            catch (Exception ex)
            {
                Logger.Log("Anniversary couldn't be updated from Outlook to Google for '" + master.FileAs + "': " + ex.Message, EventType.Error);
            }
            #endregion anniversary

            #region relations (spouse, child, manager and assistant)
            //First remove spouse, child, manager and assistant
            for (int i=slave.ContactEntry.Relations.Count-1; i>=0;i--)
            {
                Relation rel = slave.ContactEntry.Relations[i];
                if (rel.Rel != null && (rel.Rel.Equals(relSpouse) || rel.Rel.Equals(relChild) || rel.Rel.Equals(relManager) || rel.Rel.Equals(relAssistant)))
                    slave.ContactEntry.Relations.RemoveAt(i);
            }
            //Then add spouse again if existing
            if (!string.IsNullOrEmpty(master.Spouse))
            {
                Relation rel = new Relation();
                rel.Rel = relSpouse;
                rel.Value = master.Spouse;
                slave.ContactEntry.Relations.Add(rel);
            }
            //Then add children again if existing
            if (!string.IsNullOrEmpty(master.Children))
            {
                Relation rel = new Relation();
                rel.Rel = relChild;
                rel.Value = master.Children;
                slave.ContactEntry.Relations.Add(rel);
            }
            //Then add manager again if existing
            if (!string.IsNullOrEmpty(master.ManagerName))
            {
                Relation rel = new Relation();
                rel.Rel = relManager;
                rel.Value = master.ManagerName;
                slave.ContactEntry.Relations.Add(rel);
            }
            //Then add assistant again if existing
            if (!string.IsNullOrEmpty(master.AssistantName))
            {
                Relation rel = new Relation();
                rel.Rel = relAssistant;
                rel.Value = master.AssistantName;
                slave.ContactEntry.Relations.Add(rel);
            }
            #endregion relations (spouse, child, manager and assistant)

            #region HomePage
            slave.ContactEntry.Websites.Clear();
            //Just copy the first URL, because Outlook only has 1
            if (!string.IsNullOrEmpty(master.WebPage))
            {
                Website url = new Website();
                url.Href = master.WebPage;
                url.Rel = relHomePage;
                url.Primary = true;
                slave.ContactEntry.Websites.Add(url);
            }
            #endregion HomePage

            //CH - Fixed error with invalid xml being sent to google... This may need to be added to everything
            //slave.Content = String.Format("<![CDATA[{0}]]>", master.Body);
            //floriwan: Maybe better to just escape the XML instead of putting it in CDATA, because this causes a CDATA added to all my contacts
            if (!string.IsNullOrEmpty(master.Body))
                slave.Content = System.Security.SecurityElement.Escape(master.Body);
            else
                slave.Content = null;
        }
        /// <summary>
        /// Merges the specified websites.
        /// </summary>
        /// <param name="websites">The websites.</param>
        /// <param name="website">The website.</param>
        /// <returns>True if Changed.</returns>
        public static bool Merge(this ExtensionCollection<Website> websites, Website website)
        {
            if (!string.IsNullOrWhiteSpace(website.Href) && !websites.Any(e => e.Href == website.Href))
            {
                websites.Add(website);

                if (websites.Any() && !websites.Any(e => e.Primary))
                {
                    websites.First().Primary = true;
                }

                return true;
            }

            return false;
        }