Menu

[r25]: / trunk / GoogleContactsSync / ContactPropertiesUtils.cs  Maximize  Restore  History

Download this file

129 lines (119 with data), 5.4 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
using System;
using System.Collections.Generic;
using System.Text;
using Google.GData.Contacts;
using Outlook = Microsoft.Office.Interop.Outlook;
using Google.GData.Extensions;
using System.Collections;
namespace WebGear.GoogleContactsSync
{
internal static class ContactPropertiesUtils
{
public static string GetOutlookId(Outlook.ContactItem outlookContact)
{
return outlookContact.EntryID;
}
public static string GetGoogleId(ContactEntry googleContact)
{
string id = googleContact.Id.ToString();
if (id == null)
throw new Exception();
return id;
}
public static void SetGoogleOutlookContactId(string syncProfile, ContactEntry googleContact, Outlook.ContactItem outlookContact)
{
if (outlookContact.EntryID == null)
throw new Exception("Must save outlook contact before getting id");
SetGoogleOutlookContactId(syncProfile, googleContact, GetOutlookId(outlookContact));
}
public static void SetGoogleOutlookContactId(string syncProfile, ContactEntry googleContact, string outlookContactId)
{
// check if exists
bool found = false;
foreach (Google.GData.Extensions.ExtendedProperty p in googleContact.ExtendedProperties)
{
if (p.Name == "gos:oid:" + syncProfile + "")
{
p.Value = outlookContactId;
found = true;
break;
}
}
if (!found)
{
Google.GData.Extensions.ExtendedProperty prop = new ExtendedProperty(outlookContactId, "gos:oid:" + syncProfile + "");
prop.Value = outlookContactId;
googleContact.ExtendedProperties.Add(prop);
}
}
public static string GetGoogleOutlookContactId(string syncProfile, ContactEntry googleContact)
{
// get extended prop
foreach (Google.GData.Extensions.ExtendedProperty p in googleContact.ExtendedProperties)
{
if (p.Name == "gos:oid:" + syncProfile + "")
return (string)p.Value;
}
return null;
}
public static void ResetGoogleOutlookContactId(string syncProfile, ContactEntry googleContact)
{
// get extended prop
foreach (Google.GData.Extensions.ExtendedProperty p in googleContact.ExtendedProperties)
{
if (p.Name == "gos:oid:" + syncProfile + "")
{
// remove
googleContact.ExtendedProperties.Remove(p);
return;
}
}
}
public static void SetOutlookGoogleContactId(Syncronizer sync, Outlook.ContactItem outlookContact, ContactEntry googleContact)
{
if (googleContact.Id.Uri == null)
throw new NullReferenceException("GoogleContact must have a valid Id");
//check if outlook contact aready has google id property.
Outlook.UserProperty prop = outlookContact.UserProperties[sync.OutlookPropertyNameId];
if (prop == null)
prop = outlookContact.UserProperties.Add(sync.OutlookPropertyNameId, Outlook.OlUserPropertyType.olText, null, null);
prop.Value = googleContact.Id.Uri.Content;
//save last google's updated date as property
/*prop = outlookContact.UserProperties[OutlookPropertyNameUpdated];
if (prop == null)
prop = outlookContact.UserProperties.Add(OutlookPropertyNameUpdated, Outlook.OlUserPropertyType.olDateTime, null, null);
prop.Value = googleContact.Updated;*/
//save sync datetime
prop = outlookContact.UserProperties[sync.OutlookPropertyNameSynced];
if (prop == null)
prop = outlookContact.UserProperties.Add(sync.OutlookPropertyNameSynced, Outlook.OlUserPropertyType.olDateTime, null, null);
prop.Value = DateTime.Now;
}
public static string GetOutlookGoogleContactId(Syncronizer sync, Outlook.ContactItem outlookContact)
{
Outlook.UserProperty idProp = outlookContact.UserProperties[sync.OutlookPropertyNameId];
string id = (string)idProp.Value;
if (id == null)
throw new Exception();
return id;
}
public static void ResetOutlookGoogleContactId(Syncronizer sync, Outlook.ContactItem outlookContact)
{
Outlook.UserProperty idProp = outlookContact.UserProperties[sync.OutlookPropertyNameId];
if (idProp == null)
return;
IEnumerator en = outlookContact.UserProperties.GetEnumerator();
int index = 1; // 1 based collection
while (en.MoveNext())
{
if (en.Current as Outlook.UserProperty == idProp)
{
outlookContact.UserProperties.Remove(index);
return;
}
index++;
}
throw new Exception("Did not find prop.");
}
}
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.