Menu

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

Download this file

310 lines (281 with data), 13.0 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
using System;
using Outlook = Microsoft.Office.Interop.Outlook;
using Google.GData.Extensions;
using Google.Contacts;
using System.Runtime.InteropServices;
namespace GoContactSyncMod
{
internal static class ContactPropertiesUtils
{
public static string GetOutlookId(Outlook.ContactItem outlookContact)
{
return outlookContact.EntryID;
}
public static string GetGoogleId(Contact googleContact)
{
string id = googleContact.Id.ToString();
if (id == null)
throw new Exception();
return id;
}
public static void SetGoogleOutlookContactId(string syncProfile, Contact 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, Contact googleContact, string outlookContactId)
{
// check if exists
bool found = false;
foreach (var p in googleContact.ExtendedProperties)
{
if (p.Name == "gos:oid:" + syncProfile + "")
{
p.Value = outlookContactId;
found = true;
break;
}
}
if (!found)
{
var prop = new ExtendedProperty(outlookContactId, "gos:oid:" + syncProfile + "");
prop.Value = outlookContactId;
googleContact.ExtendedProperties.Add(prop);
}
}
public static string GetGoogleOutlookContactId(string syncProfile, Contact googleContact)
{
// get extended prop
foreach (var p in googleContact.ExtendedProperties)
{
if (p.Name == "gos:oid:" + syncProfile + "")
return p.Value;
}
return null;
}
public static void ResetGoogleOutlookContactId(string syncProfile, Contact googleContact)
{
// get extended prop
foreach (var p in googleContact.ExtendedProperties)
{
if (p.Name == "gos:oid:" + syncProfile + "")
{
// remove
googleContact.ExtendedProperties.Remove(p);
return;
}
}
}
/// <summary>
/// Sets the syncId of the Outlook contact and the last sync date.
/// Please assure to always call this function when saving OutlookItem
/// </summary>
/// <param name="sync"></param>
/// <param name="outlookContact"></param>
/// <param name="googleContact"></param>
public static void SetOutlookGoogleContactId(Synchronizer sync, Outlook.ContactItem outlookContact, Contact googleContact)
{
if (googleContact.ContactEntry.Id.Uri == null)
throw new NullReferenceException("GoogleContact must have a valid Id");
Outlook.UserProperties userProperties = null;
Outlook.UserProperty prop = null;
try
{
userProperties = outlookContact.UserProperties;
prop = userProperties[sync.OutlookPropertyNameId];
//check if outlook contact aready has google id property.
if (prop == null)
prop = userProperties.Add(sync.OutlookPropertyNameId, Outlook.OlUserPropertyType.olText, false);
prop.Value = googleContact.ContactEntry.Id.Uri.Content;
}
catch (Exception ex)
{
Logger.Log(ex, EventType.Debug);
Logger.Log("Name: " + sync.OutlookPropertyNameId, EventType.Debug);
Logger.Log("Value: " + googleContact.ContactEntry.Id.Uri.Content, EventType.Debug);
throw;
}
finally
{
if (prop != null)
Marshal.ReleaseComObject(prop);
if (userProperties != null)
Marshal.ReleaseComObject(userProperties);
}
SetOutlookLastSync(sync, outlookContact);
}
public static void SetOutlookLastSync(Synchronizer sync, Outlook.ContactItem outlookContact)
{
//save sync datetime
Outlook.UserProperties userProperties = null;
Outlook.UserProperty prop = null;
try
{
userProperties = outlookContact.UserProperties;
prop = userProperties[sync.OutlookPropertyNameSynced];
if (prop == null)
prop = userProperties.Add(sync.OutlookPropertyNameSynced, Outlook.OlUserPropertyType.olDateTime, false);
prop.Value = DateTime.Now;
}
finally
{
if (prop != null)
Marshal.ReleaseComObject(prop);
if (userProperties != null)
Marshal.ReleaseComObject(userProperties);
}
}
public static DateTime? GetOutlookLastSync(Synchronizer sync, Outlook.ContactItem outlookContact)
{
DateTime? result = null;
Outlook.UserProperties userProperties = null;
Outlook.UserProperty prop = null;
try
{
userProperties = outlookContact.UserProperties;
prop = userProperties[sync.OutlookPropertyNameSynced];
if (prop != null)
result = (DateTime)prop.Value;
}
finally
{
if (prop != null)
Marshal.ReleaseComObject(prop);
if (userProperties != null)
Marshal.ReleaseComObject(userProperties);
}
return result;
}
public static string GetOutlookGoogleContactId(Synchronizer sync, Outlook.ContactItem outlookContact)
{
string id = null;
Outlook.UserProperties userProperties = null;
Outlook.UserProperty idProp = null;
try
{
userProperties = outlookContact.UserProperties;
idProp = userProperties[sync.OutlookPropertyNameId];
if (idProp != null)
{
id = (string)idProp.Value;
if (id == null)
throw new Exception();
}
}
finally
{
if (idProp != null)
Marshal.ReleaseComObject(idProp);
if (userProperties != null)
Marshal.ReleaseComObject(userProperties);
}
return id;
}
public static void ResetOutlookGoogleContactId(Synchronizer sync, Outlook.ContactItem outlookContact)
{
Outlook.UserProperties userProperties = null;
try
{
userProperties = outlookContact.UserProperties;
for (var i = userProperties.Count; i > 0; i--)
{
Outlook.UserProperty p = null;
try
{
p = userProperties[i];
if (p.Name == sync.OutlookPropertyNameId || p.Name == sync.OutlookPropertyNameSynced)
{
userProperties.Remove(i);
}
}
finally
{
if (p != null)
Marshal.ReleaseComObject(p);
}
}
}
finally
{
if (userProperties != null)
Marshal.ReleaseComObject(userProperties);
}
}
public static string GetOutlookEmailAddress1(Outlook.ContactItem outlookContactItem)
{
return GetOutlookEmailAddress(outlookContactItem, outlookContactItem.Email1AddressType, outlookContactItem.Email1EntryID, outlookContactItem.Email1Address);
}
public static string GetOutlookEmailAddress2(Outlook.ContactItem outlookContactItem)
{
return GetOutlookEmailAddress(outlookContactItem, outlookContactItem.Email2AddressType, outlookContactItem.Email2EntryID, outlookContactItem.Email2Address);
}
public static string GetOutlookEmailAddress3(Outlook.ContactItem outlookContactItem)
{
return GetOutlookEmailAddress(outlookContactItem, outlookContactItem.Email3AddressType, outlookContactItem.Email3EntryID, outlookContactItem.Email3Address);
}
private static string GetOutlookEmailAddress(Outlook.ContactItem outlookContactItem, string emailAddressType, string emailEntryID, string emailAddress)
{
switch (emailAddressType)
{
case "EX": // Microsoft Exchange address: "/o=xxxx/ou=xxxx/cn=Recipients/cn=xxxx"
Outlook.NameSpace outlookNameSpace = null;
Outlook.Recipient recipient = null;
Outlook.AddressEntry addressEntry = null;
Outlook.ExchangeUser exchangeUser = null;
try
{
outlookNameSpace = outlookContactItem.Application.GetNamespace("mapi");
// The emailEntryID is garbage (bug in Outlook 2007 and before?) - so we cannot do GetAddressEntryFromID().
// Instead we create a temporary recipient and ask Exchange to resolve it, then get the SMTP address from it.
//Outlook.AddressEntry addressEntry = outlookNameSpace.GetAddressEntryFromID(emailEntryID);
recipient = outlookNameSpace.CreateRecipient(emailAddress);
recipient.Resolve();
if (recipient.Resolved)
{
addressEntry = recipient.AddressEntry;
if (addressEntry != null)
{
if (addressEntry.AddressEntryUserType == Outlook.OlAddressEntryUserType.olExchangeUserAddressEntry)
{
exchangeUser = addressEntry.GetExchangeUser();
if (exchangeUser != null)
{
return exchangeUser.PrimarySmtpAddress;
}
}
else
{
Logger.Log(string.Format("Unsupported AddressEntryUserType {0} for contact '{1}'.", addressEntry.AddressEntryUserType, outlookContactItem.FileAs), EventType.Debug);
}
}
}
}
catch (Exception ex)
{
// Fallback: If Exchange cannot give us the SMTP address, we give up and use the Exchange address format.
// TODO: Can we do better?
Logger.Log(string.Format("Error getting the email address of outlook contact '{0}' from Exchange format '{1}': {2}", outlookContactItem.FileAs, emailAddress, ex.Message), EventType.Warning);
return emailAddress;
}
finally
{
if (exchangeUser != null)
Marshal.ReleaseComObject(exchangeUser);
if (addressEntry != null)
Marshal.ReleaseComObject(addressEntry);
if (recipient != null)
Marshal.ReleaseComObject(recipient);
if (outlookNameSpace != null)
Marshal.ReleaseComObject(outlookNameSpace);
}
// Fallback: If Exchange cannot give us the SMTP address, we give up and use the Exchange address format.
// TODO: Can we do better?
return emailAddress;
case "SMTP":
default:
return emailAddress;
}
}
}
}
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.