Menu

[r1519]: / branches / vs2013 / GoogleContactsSync / AppointmentPropertiesUtils.cs  Maximize  Restore  History

Download this file

330 lines (309 with data), 14.6 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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
using System;
using System.Collections.Generic;
using System.Text;
using Outlook = Microsoft.Office.Interop.Outlook;
using Google.GData.Extensions;
using Google.GData.Calendar;
using System.Collections;
using System.Runtime.InteropServices;
using System.IO;
namespace GoContactSyncMod
{
internal static class AppointmentPropertiesUtils
{
public static string GetOutlookId(Outlook.AppointmentItem outlookAppointment)
{
return outlookAppointment.EntryID;
}
public static string GetGoogleId(EventEntry googleAppointment)
{
string id = googleAppointment.Id.ToString();
if (id == null)
throw new Exception();
return id;
}
public static void SetGoogleOutlookAppointmentId(string syncProfile, EventEntry googleAppointment, Outlook.AppointmentItem outlookAppointment)
{
if (outlookAppointment.EntryID == null)
throw new Exception("Must save outlook Appointment before getting id");
SetGoogleOutlookAppointmentId(syncProfile, googleAppointment, GetOutlookId(outlookAppointment));
}
public static void SetGoogleOutlookAppointmentId(string syncProfile, EventEntry googleAppointment, string outlookAppointmentId)
{
// check if exists
bool found = false;
foreach (var p in googleAppointment.ExtensionElements)
{
if (p is ExtendedProperty && ((ExtendedProperty)p).Name == "gos:oid:" + syncProfile + "")
{
((ExtendedProperty)p).Value = outlookAppointmentId;
found = true;
break;
}
}
if (!found)
{
Google.GData.Extensions.ExtendedProperty prop = new ExtendedProperty(outlookAppointmentId, "gos:oid:" + syncProfile + "");
prop.Value = outlookAppointmentId;
googleAppointment.ExtensionElements.Add(prop);
}
}
public static string GetGoogleOutlookAppointmentId(string syncProfile, EventEntry googleAppointment)
{
// get extended prop
foreach (var p in googleAppointment.ExtensionElements)
{
if (p is ExtendedProperty && ((ExtendedProperty)p).Name == "gos:oid:" + syncProfile + "")
return ((ExtendedProperty)p).Value;
}
return null;
}
public static void ResetGoogleOutlookAppointmentId(string syncProfile, EventEntry googleAppointment)
{
// get extended prop
foreach (var p in googleAppointment.ExtensionElements)
{
if (p is ExtendedProperty && ((ExtendedProperty)p).Name == "gos:oid:" + syncProfile + "")
{
// remove
googleAppointment.ExtensionElements.Remove(p);
return;
}
}
}
/// <summary>
/// Sets the syncId of the Outlook Appointment and the last sync date.
/// Please assure to always call this function when saving OutlookItem
/// </summary>
/// <param name="sync"></param>
/// <param name="outlookAppointment"></param>
/// <param name="googleAppointment"></param>
public static void SetOutlookGoogleAppointmentId(Syncronizer sync, Outlook.AppointmentItem outlookAppointment, EventEntry googleAppointment)
{
if (googleAppointment.Id.Uri == null)
throw new NullReferenceException("GoogleAppointment must have a valid Id");
//check if outlook Appointment aready has google id property.
Outlook.UserProperties userProperties = outlookAppointment.UserProperties;
try
{
Outlook.UserProperty prop = userProperties[sync.OutlookPropertyNameId];
if (prop == null)
prop = userProperties.Add(sync.OutlookPropertyNameId, Outlook.OlUserPropertyType.olText, true);
try
{
prop.Value = googleAppointment.Id.Uri.Content;
}
finally
{
Marshal.ReleaseComObject(prop);
}
}
finally
{
Marshal.ReleaseComObject(userProperties);
}
//save last google's updated date as property
/*prop = outlookAppointment.UserProperties[OutlookPropertyNameUpdated];
if (prop == null)
prop = outlookAppointment.UserProperties.Add(OutlookPropertyNameUpdated, Outlook.OlUserPropertyType.olDateTime, null, null);
prop.Value = googleAppointment.Updated;*/
//Also set the OutlookLastSync date when setting a match between Outlook and Google to assure the lastSync updated when Outlook Appointment is saved afterwards
SetOutlookLastSync(sync, outlookAppointment);
}
public static void SetOutlookLastSync(Syncronizer sync, Outlook.AppointmentItem outlookAppointment)
{
//save sync datetime
Outlook.UserProperties userProperties = outlookAppointment.UserProperties;
try
{
Outlook.UserProperty prop = userProperties[sync.OutlookPropertyNameSynced];
if (prop == null)
prop = userProperties.Add(sync.OutlookPropertyNameSynced, Outlook.OlUserPropertyType.olDateTime, true);
try
{
prop.Value = DateTime.Now;
}
finally
{
Marshal.ReleaseComObject(prop);
}
}
finally
{
Marshal.ReleaseComObject(userProperties);
}
}
public static DateTime? GetOutlookLastSync(Syncronizer sync, Outlook.AppointmentItem outlookAppointment)
{
DateTime? result = null;
Outlook.UserProperties userProperties = outlookAppointment.UserProperties;
try
{
Outlook.UserProperty prop = userProperties[sync.OutlookPropertyNameSynced];
if (prop != null)
{
try
{
result = (DateTime)prop.Value;
}
finally
{
Marshal.ReleaseComObject(prop);
}
}
}
finally
{
Marshal.ReleaseComObject(userProperties);
}
return result;
}
public static string GetOutlookGoogleAppointmentId(Syncronizer sync, Outlook.AppointmentItem outlookAppointment)
{
string id = null;
Outlook.UserProperties userProperties = outlookAppointment.UserProperties;
try
{
Outlook.UserProperty idProp = userProperties[sync.OutlookPropertyNameId];
if (idProp != null)
{
try
{
id = (string)idProp.Value;
if (id == null)
throw new Exception();
}
finally
{
Marshal.ReleaseComObject(idProp);
}
}
}
finally
{
Marshal.ReleaseComObject(userProperties);
}
return id;
}
public static void ResetOutlookGoogleAppointmentId(Syncronizer sync, Outlook.AppointmentItem outlookAppointment)
{
Outlook.UserProperties userProperties = outlookAppointment.UserProperties;
try
{
Outlook.UserProperty idProp = userProperties[sync.OutlookPropertyNameId];
try
{
Outlook.UserProperty lastSyncProp = userProperties[sync.OutlookPropertyNameSynced];
try
{
if (idProp == null && lastSyncProp == null)
return;
List<int> indexesToBeRemoved = new List<int>();
IEnumerator en = userProperties.GetEnumerator();
en.Reset();
int index = 1; // 1 based collection
while (en.MoveNext())
{
Outlook.UserProperty userProperty = en.Current as Outlook.UserProperty;
if (userProperty == idProp || userProperty == lastSyncProp)
{
indexesToBeRemoved.Add(index);
//outlookAppointment.UserProperties.Remove(index);
//Don't return to remove both properties, googleId and lastSynced
//return;
}
index++;
Marshal.ReleaseComObject(userProperty);
}
for (int i = indexesToBeRemoved.Count - 1; i >= 0; i--)
userProperties.Remove(indexesToBeRemoved[i]);
//throw new Exception("Did not find prop.");
}
finally
{
if (lastSyncProp != null)
Marshal.ReleaseComObject(lastSyncProp);
}
}
finally
{
if (idProp != null)
Marshal.ReleaseComObject(idProp);
}
}
finally
{
Marshal.ReleaseComObject(userProperties);
}
}
public static string GetOutlookEmailAddress(string subject, Outlook.Recipient recipient)
{
string emailAddress = recipient.Address != null ? recipient.Address : recipient.Name;
switch (recipient.AddressEntry.AddressEntryUserType)
{
case Outlook.OlAddressEntryUserType.olExchangeUserAddressEntry: // Microsoft Exchange address: "/o=xxxx/ou=xxxx/cn=Recipients/cn=xxxx"
try
{
// 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);
//try
//{
recipient.Resolve();
if (recipient.Resolved)
{
Outlook.AddressEntry addressEntry = recipient.AddressEntry;
if (addressEntry != null)
{
try
{
if (addressEntry.AddressEntryUserType == Outlook.OlAddressEntryUserType.olExchangeUserAddressEntry)
{
Outlook.ExchangeUser exchangeUser = addressEntry.GetExchangeUser();
if (exchangeUser != null)
{
try
{
return exchangeUser.PrimarySmtpAddress;
}
finally
{
Marshal.ReleaseComObject(exchangeUser);
}
}
}
else
{
Logger.Log(string.Format("Unsupported AddressEntryUserType {0} for email '{1}' in appointment '{2}'.", addressEntry.AddressEntryUserType, addressEntry.Address, subject), EventType.Debug);
}
}
finally
{
Marshal.ReleaseComObject(addressEntry);
}
}
}
//}
//finally
//{
// if (recipient != null)
// Marshal.ReleaseComObject(recipient);
//}
}
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 appointment '{0}' from Exchange format '{1}': {2}", subject, emailAddress, ex.Message), EventType.Warning);
return emailAddress;
}
// 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 Outlook.OlAddressEntryUserType.olSmtpAddressEntry:
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.