Exemplo n.º 1
0
        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();
            }
        }
Exemplo n.º 2
0
 private void SyncContact(Contact gContact, ContactItem item, Config.Direction dir)
 {
     if (dir == Config.Direction.dirToGoogle)
     {
         googleAdapter.UpdateContactFromOutlook(item, gContact);
     }
     else
     {
         outlookAdapter.UpdateContactFromGoogle(gContact, item);
     }
 }