/// <summary> /// Creates Google Contacts one by one /// </summary> /// <param name="auth">a valid Google token or refresh token</param> /// <param name="contacts">contacts to be created</param> private void CreateContacts(UserCredential auth, Dictionary <string, VPContact> contacts) { try { foreach (KeyValuePair <string, VPContact> item in contacts) { Contact newContact = item.Value.GetGoogleContact(); newContact = GConnect.CreateNewContact(newContact, auth); //HACK! - dotnet lib does not support to set "nickname" while creating object ;-( newContact.ContactEntry.Nickname = item.Value.Initials; GConnect.UpdateContact(auth, newContact); //update photo GConnect.UpdatePhoto(auth, newContact, item.Value.Picture); //Console.WriteLine("Created Contact: " + item.Key); _status.ToBeCreated--; _status.GoogleContacts++; ProgressUpdate(_status); //System.Threading.Thread.Sleep(3000); } } catch (Exception) { throw; } }
/// <summary> /// Updates Google Contacts one by one. /// </summary> /// <param name="auth">a valid Google token or refresh token</param> /// <param name="toBeUpdated"></param> private void UpdateContacts(UserCredential auth, Dictionary <string, VPContact> toBeUpdated) { try { foreach (var item in toBeUpdated) { Contact gContact = item.Value.GetGoogleContact(); GConnect.UpdateContact(auth, gContact); GConnect.UpdatePhoto(auth, gContact, item.Value.Picture); //System.Threading.Thread.Sleep(3000); _status.ToBeUpdated--; ProgressUpdate(_status); } } catch (Exception) { throw; } }
/// <summary> /// Deletes Google Contacts one by one. /// </summary> /// <param name="auth">a valid Google token or refresh token</param> /// <param name="gContacts">contacts to be deleted.</param> private void DeleteContacts(UserCredential auth, List <Contact> gContacts) { try { foreach (var c in gContacts) { if (c != null && !string.IsNullOrEmpty(c.ETag)) { GConnect.DeleteContact(c, auth); //System.Threading.Thread.Sleep(3000); } _status.ToBeDeleted--; _status.GoogleContacts--; ProgressUpdate(_status); } } catch (Exception) { throw; } }
/// <summary> /// Start the Sync of contacts. /// </summary> public void DoSync() { _status = new ProgressUpdater(); _status.IsRunning = true; _status.CurrentTask = "Getting VP Contacts"; ProgressUpdate(_status); Dictionary <string, VPContact> vpContacts = VPConnect.GetContacts(Environment.UserName); if (vpContacts == null) { _status.HasError = true; _status.IsRunning = false; _status.Error = "Could not get contacts from VP, stopping sync"; ProgressUpdate(_status); return; } _status.VPContacts = vpContacts.Count; ProgressUpdate(_status); _status.CurrentTask = "Logging into Google"; ProgressUpdate(_status); var auth = GConnect.Authenticate(); _status.CurrentTask = "Getting Google Contacts"; ProgressUpdate(_status); var gContacts = GConnect.GetGroupContacts(auth); _status.GoogleContacts = gContacts.TotalResults; List <Contact> toBeDeleted; _status.CurrentTask = "Analyzing data......"; ProgressUpdate(_status); vpContacts = CheckGoogleContacts(vpContacts, gContacts, out toBeDeleted); Dictionary <string, VPContact> toBeCreated = vpContacts.Where(item => item.Value.ActionNeeded == VPContact.SyncAction.Create) .ToDictionary(key => key.Key, value => value.Value); Dictionary <string, VPContact> toBeUpdated = vpContacts.Where(item => item.Value.ActionNeeded == VPContact.SyncAction.Update) .ToDictionary(key => key.Key, value => value.Value); _status.ToBeCreated = toBeCreated.Count; _status.ToBeUpdated = toBeUpdated.Count; _status.ToBeDeleted = toBeDeleted.Count; _status.CurrentTask = "Creating Google Contacts"; ProgressUpdate(_status); CreateContacts(auth, toBeCreated); _status.CurrentTask = "Updating existing Google Contacts"; ProgressUpdate(_status); UpdateContacts(auth, toBeUpdated); _status.CurrentTask = "Deleting unwanted Google Contacts"; ProgressUpdate(_status); DeleteContacts(auth, toBeDeleted); _status.CurrentTask = "Done...."; _status.IsRunning = false; ProgressUpdate(_status); }