public void SaveGoogleContact(ContactMatch match) { //check if this contact was not yet inserted on google. if (match.GoogleContact.Id.Uri == null) { //insert contact. Uri feedUri = new Uri(ContactsQuery.CreateContactsUri("default")); try { ContactPropertiesUtils.SetGoogleOutlookContactId(SyncProfile, match.GoogleContact, match.OutlookContact); ContactEntry createdEntry = (ContactEntry)_googleService.Insert(feedUri, match.GoogleContact); match.GoogleContact = createdEntry; ContactPropertiesUtils.SetOutlookGoogleContactId(this, match.OutlookContact, match.GoogleContact); match.OutlookContact.Save(); } catch (Exception ex) { MemoryStream ms = new MemoryStream(); match.GoogleContact.SaveToXml(ms); StreamReader sr = new StreamReader(ms); ms.Seek(0, SeekOrigin.Begin); Debug.WriteLine(String.Format("Error saving google contact: {0}", ex.Message)); Debug.WriteLine(sr.ReadToEnd()); //TODO: save google contact xml for diagnistics throw; } } else { try { //contact already present in google. just update ContactPropertiesUtils.SetGoogleOutlookContactId(SyncProfile, match.GoogleContact, match.OutlookContact); //TODO: this will fail if original contact had an empty name or rpimary email address. ContactEntry updatedEntry = match.GoogleContact.Update() as ContactEntry; match.GoogleContact = updatedEntry; ContactPropertiesUtils.SetOutlookGoogleContactId(this, match.OutlookContact, match.GoogleContact); match.OutlookContact.Save(); } catch (Exception ex) { //TODO: save google contact xml for diagnistics throw; } } }
public void SaveContact(ContactMatch match) { if (match.GoogleContact != null && match.OutlookContact != null) { //bool googleChanged, outlookChanged; //SaveContactGroups(match, out googleChanged, out outlookChanged); if (match.GoogleContact.IsDirty() || !match.OutlookContact.Saved) { _syncedCount++; } if (match.GoogleContact.IsDirty())// || googleChanged) { //google contact was modified. save. SaveGoogleContact(match); if (Logger != null) { Logger.Log("Saved Google contact: \"" + match.GoogleContact.Title.Text + "\"", EventType.Information); } } if (!match.OutlookContact.Saved)// || outlookChanged) { match.OutlookContact.Save(); ContactPropertiesUtils.SetGoogleOutlookContactId(SyncProfile, match.GoogleContact, match.OutlookContact); ContactEntry updatedEntry = match.GoogleContact.Update() as ContactEntry; match.GoogleContact = updatedEntry; ContactPropertiesUtils.SetOutlookGoogleContactId(this, match.OutlookContact, match.GoogleContact); match.OutlookContact.Save(); if (Logger != null) { Logger.Log("Saved Outlook contact: \"" + match.OutlookContact.FileAs + "\"", EventType.Information); } //TODO: this will cause the google contact to be updated on next run because Outlook's contact will be marked as saved later that Google's contact. } // save photos SaveContactPhotos(match); } else if (match.GoogleContact == null && match.OutlookContact != null) { if (ContactPropertiesUtils.GetOutlookGoogleContactId(this, match.OutlookContact) != null && _syncDelete) { _deletedCount++; string name = match.OutlookContact.FileAs; // peer google contact was deleted, delete outlook contact match.OutlookContact.Delete(); if (Logger != null) { Logger.Log("Deleted Outlook contact: \"" + name + "\"", EventType.Information); } } } else if (match.GoogleContact != null && match.OutlookContact == null) { if (ContactPropertiesUtils.GetGoogleOutlookContactId(SyncProfile, match.GoogleContact) != null && _syncDelete) { _deletedCount++; // peer outlook contact was deleted, delete google contact match.GoogleContact.Delete(); if (Logger != null) { Logger.Log("Deleted Google contact: \"" + match.GoogleContact.Title.Text + "\"", EventType.Information); } } } else { //TODO: ignore for now: throw new ArgumentNullException("To save contacts both ContactMatch peers must be present."); if (Logger != null) { Logger.Log("Both Google and Outlook contact: \"" + match.GoogleContact.Title.Text + "\" have been changed! Not implemented yet.", EventType.Warning); } } }