Menu

[r1586]: / trunk / GoogleContactsSync / AppointmentPropertiesUtils.cs  Maximize  Restore  History

Download this file

518 lines (469 with data), 21.8 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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
using Google.Apis.Calendar.v3.Data;
using Serilog;
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using Outlook = Microsoft.Office.Interop.Outlook;
namespace GoContactSyncMod
{
internal static class AppointmentPropertiesUtils
{
internal static string GetOutlookId(Outlook.AppointmentItem oa)
{
return oa.EntryID;
}
internal static string GetGoogleId(Event ga)
{
var id = ga.Id.ToString();
if (id == null)
{
throw new Exception();
}
return id;
}
internal static bool SetGoogleOutlookId(Event ga, Outlook.AppointmentItem oa)
{
var oid = GetOutlookId(oa);
if (oid == null)
{
throw new Exception("Must save Outlook appointment before getting id");
}
return SetGoogleOutlookId(ga, oid);
}
internal static bool SetGoogleOutlookId(Event ga, string oid)
{
var key = OutlookPropertiesUtils.GetKey();
if (ga.ExtendedProperties == null)
{
ga.ExtendedProperties = new Event.ExtendedPropertiesData();
}
if (ga.ExtendedProperties.Private__ == null)
{
ga.ExtendedProperties.Private__ = new Dictionary<string, string>();
}
// check if exists
foreach (var p in ga.ExtendedProperties.Private__)
{
if (p.Key == key)
{
if (ga.ExtendedProperties.Private__[p.Key] != oid)
{
ga.ExtendedProperties.Private__[p.Key] = oid;
return true;
}
return false;
}
}
//not found
var prop = new KeyValuePair<string, string>(key, oid);
ga.ExtendedProperties.Private__.Add(prop);
return true;
}
internal static string GetGoogleOutlookId(Event ga)
{
var key = OutlookPropertiesUtils.GetKey();
string ret = null;
// get extended prop
if (ga.ExtendedProperties != null)
{
//First try to get private property
ret = GetExtendedProperty(ga.ExtendedProperties.Private__, key);
if (ret != null)
return ret;
else //Then try to get Shared property
return GetExtendedProperty(ga.ExtendedProperties.Shared, key);
}
return null;
}
private static string GetExtendedProperty(IDictionary<string, string> properties, string key)
{
if (properties != null)
{
foreach (var p in properties)
{
if (p.Key == key)
{
return p.Value;
}
}
}
return null;
}
internal static void ResetGoogleOutlookId(Event ga)
{
var key = OutlookPropertiesUtils.GetKey();
if (ga.ExtendedProperties != null)
{
try
{ //First try to remove Shared property
RemoveExtendedPropertie(ga.ExtendedProperties.Shared, key);
}
catch (Exception ex)
{
Log.Verbose(ex, "Error removing ExtendedProperties.Shared (key " + key + ")");
}
//Then remove private property
RemoveExtendedPropertie(ga.ExtendedProperties.Private__, key);
}
//return false;
}
private static void RemoveExtendedPropertie(IDictionary<string,string> properties, string key)
{
if (properties != null)
{
// get extended prop
foreach (var p in properties)
{
if (p.Key == key)
{
// remove
properties.Remove(p);
return;
}
}
}
//return false;
}
/// <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="oa"></param>
/// <param name="e"></param>
internal static bool SetOutlookGoogleId(Outlook.AppointmentItem oa, Event e)
{
if (e.Id == null)
{
throw new NullReferenceException("GoogleAppointment must have a valid Id");
}
Outlook.UserProperties userProps = null;
try
{
userProps = oa.UserProperties;
return OutlookPropertiesUtils.SetOutlookGoogleId(userProps, e.Id, e.ETag);
}
finally
{
if (userProps != null)
{
Marshal.ReleaseComObject(userProps);
}
}
}
internal static DateTime? GetOutlookLastSync(Outlook.AppointmentItem oa)
{
Outlook.UserProperties up = null;
try
{
up = oa.UserProperties;
return OutlookPropertiesUtils.GetOutlookLastSync(up);
}
finally
{
if (up != null)
{
Marshal.ReleaseComObject(up);
}
}
}
internal static string GetOutlookLastEtag(Outlook.AppointmentItem oa)
{
Outlook.UserProperties up = null;
try
{
up = oa.UserProperties;
return OutlookPropertiesUtils.GetOutlookLastEtag(up);
}
finally
{
if (up != null)
{
Marshal.ReleaseComObject(up);
}
}
}
internal static string GetOutlookGoogleId(Outlook.AppointmentItem oa)
{
Outlook.UserProperties up = null;
try
{
up = oa.UserProperties;
return OutlookPropertiesUtils.GetOutlookPropertyValue<string>(Synchronizer.OutlookPropertyNameId, up, Outlook.OlFormatText.olFormatTextText);
}
finally
{
if (up != null)
{
Marshal.ReleaseComObject(up);
}
}
}
internal static void ResetOutlookGoogleId(Outlook.AppointmentItem oa)
{
Outlook.UserProperties up = null;
try
{
up = oa.UserProperties;
OutlookPropertiesUtils.ResetOutlookGoogleId(up);
}
finally
{
if (up != null)
{
Marshal.ReleaseComObject(up);
}
}
}
internal static DateTime GetOutlookLastUpdated(AppointmentMatch match)
{
var lastUpdated = match.OutlookAppointment.LastModificationTime;
if (match.OutlookAppointment.IsRecurring)
{
//adding Outlook exception is not changing modification date of the parent
//first scan exceptions
Outlook.RecurrencePattern rp = null;
Outlook.Exceptions exceptions = null;
try
{
rp = match.OutlookAppointment.GetRecurrence();
exceptions = rp.Exceptions;
if (exceptions != null)
{
for (var i = exceptions.Count; i > 0; i--)
{
Outlook.Exception e = null;
Outlook.AppointmentItem ola = null;
try
{
e = exceptions[i];
if (e.Deleted &&
//ToDo: Check, if we Don't sync deleted instances, if they are in the past, otherwise they will be synchronized again and again and again
AppointmentSync.InSyncPeriod(e.OriginalDate)) //In Sync Range?
//e.OriginalDate >= DateTime.Now) //Or only in future?
{
//for deleted exception it is not possible to get
//their modification date
//==> Update it always, to not overlook a cancelled/deleted recurrence exception
//ToDo: Check, but only if the recurrence exception is within sync range, to not update it again and again (especially for old exceptions in the past)
//ToDo: Or only the ones in the future?
lastUpdated = DateTime.Now;
Log.Debug($"Deleted Outlook recurrence exception found in the future (original date {e.OriginalDate.ToString()}, trying to delete it again (if not yet done): {match.OutlookAppointment.ToLogString()}.");
break;
}
else
{ //Maybe it is possible to get the deletion date via the e.AppointmentItem.LastModificationTime???
try
{
ola = e.AppointmentItem;
}
catch
{
}
if (ola != null)
{
if (ola.LastModificationTime > lastUpdated)
{
lastUpdated = ola.LastModificationTime;
}
}
}
}
finally
{
if (ola != null)
{
Marshal.ReleaseComObject(ola);
}
if (e != null)
{
Marshal.ReleaseComObject(e);
}
}
}
}
}
finally
{
if (exceptions != null)
{
Marshal.ReleaseComObject(exceptions);
}
if (rp != null)
{
Marshal.ReleaseComObject(rp);
}
}
}
if (lastUpdated == null)
lastUpdated = DateTime.MinValue;
if (lastUpdated.Kind == DateTimeKind.Utc)
lastUpdated = TimeZoneInfo.ConvertTimeFromUtc(lastUpdated, TimeZoneInfo.Local);
lastUpdated = lastUpdated.AddSeconds(-lastUpdated.Second);
return lastUpdated;
}
internal static DateTime GetGoogleLastUpdated(AppointmentMatch match, Synchronizer sync)
{
DateTime lastUpdated = DateTime.MinValue;
var now = DateTime.Now;
if (match.GoogleAppointment.UpdatedDateTimeOffset.HasValue)
{
lastUpdated = match.GoogleAppointment.UpdatedDateTimeOffset.Value.DateTime;
//consider GoogleAppointmentExceptions, because if they are updated, the master appointment doesn't have a new Saved TimeStamp
foreach (var ga in match.GoogleAppointmentExceptions)
{
if (ga.UpdatedDateTimeOffset != null)
{
var lastUpdatedGoogleException =ga.UpdatedDateTimeOffset.Value;
if (lastUpdatedGoogleException > lastUpdated)
{
lastUpdated = lastUpdatedGoogleException.DateTime;
}
}
else//ga.UpdatedDateTimeOffset == null, happens for cancelled events
{
var f_ga = sync.GetGoogleAppointment(ga.Id);
if (f_ga.UpdatedDateTimeOffset != null)
{
var lastUpdatedGoogleException = f_ga.UpdatedDateTimeOffset.Value;
if (lastUpdatedGoogleException > lastUpdated)
{
lastUpdated = lastUpdatedGoogleException.DateTime;
}
}
else if (match.OutlookAppointment.IsRecurring && match.OutlookAppointment.RecurrenceState == Outlook.OlRecurrenceState.olApptMaster)
{
Outlook.AppointmentItem oa = null;
Outlook.RecurrencePattern rp = null;
Outlook.Exceptions outlookExceptions = null;
try
{
rp = match.OutlookAppointment.GetRecurrence();
outlookExceptions = rp.Exceptions;
DateTime date = AppointmentSync.outlookDateInvalid;//set any invalid date
if (ga.OriginalStartTime != null)
{
oa = GetOccurrence(ga.OriginalStartTime, rp, ref date, match.OutlookAppointment);
if (oa != null && oa.MeetingStatus != Outlook.OlMeetingStatus.olMeetingCanceled && oa.MeetingStatus != Outlook.OlMeetingStatus.olMeetingReceivedAndCanceled)
{
lastUpdated = now;
break; //no need to search further, already newest date set
}
if (outlookExceptions != null && date != AppointmentSync.outlookDateInvalid)
{
bool found = false;
for (var j = outlookExceptions.Count; j > 0; j--)
{
Outlook.Exception e = null;
try
{
e = outlookExceptions[j];
if (e.Deleted && e.OriginalDate.Date == date.Date)
{
found = true; //found deleted counterpart
break;
}
}
finally
{
if (e != null)
{
Marshal.ReleaseComObject(e);
}
}
}
if (!found)
{
Log.Debug($"Deleted Google recurrence exception found without deleted Outlook counterpart (original date {date.ToString()}, trying to delete it again (if not yet done): {ga.ToLogString()}.");
lastUpdated = DateTime.Now;
break;
}
}
}
}
catch (Exception ex) when ((uint)ex.HResult == 0x80004005)
{
//most likely there is deleted Outlook exception
lastUpdated = now;
break; //no need to search further, already newest date set
}
finally
{
if (rp != null)
{
Marshal.ReleaseComObject(rp);
rp = null;
}
if (oa != null)
{
Marshal.ReleaseComObject(oa);
oa = null;
}
if (outlookExceptions != null)
{
Marshal.ReleaseComObject(outlookExceptions);
outlookExceptions = null;
}
}
}
}
}
}
else
{
//Maybe complete new Google Appointment, therefore now LastUpdate???
lastUpdated = now;
}
if (lastUpdated == null)
lastUpdated = DateTime.MinValue;
if (lastUpdated.Kind == DateTimeKind.Utc)
lastUpdated = TimeZoneInfo.ConvertTimeFromUtc(lastUpdated, TimeZoneInfo.Local);
//lastUpdated = lastUpdated.AddSeconds(-lastUpdated.Second); //Not needed for Outlook Appointments, because appointments can hold seconds (contacts cannot)
return lastUpdated;
}
internal static Outlook.AppointmentItem GetOccurrence(EventDateTime originalStartTime, Outlook.RecurrencePattern rp, ref DateTime date, Outlook.AppointmentItem oa)
{
Outlook.AppointmentItem oe = null;
if (!string.IsNullOrEmpty(originalStartTime.Date))
{
date = DateTime.Parse(originalStartTime.Date);
}
else if (originalStartTime.DateTimeDateTimeOffset != null)
{
date = originalStartTime.DateTimeDateTimeOffset.Value.DateTime;
}
if (!string.IsNullOrEmpty(originalStartTime.Date) || originalStartTime.DateTimeDateTimeOffset != null)
{
//ToDo: Check, if we Don't sync deleted instances, if they are in the past, otherwise they will be synchronized again and again and again
if (AppointmentSync.InSyncPeriod(date)) //In sync range?
//if (date >= now.Date) //or only in the future?
{
try
{
//First try with OriginalDateTime (always 12am for deleted exceptions)
oe = rp.GetOccurrence(date);
}
catch (COMException ex) when ((uint)ex.HResult == 0x80004005)
{
try
{//Second try with OriginalDate without Time
oe = rp.GetOccurrence(date.Date);
}
catch (COMException ex2) when ((uint)ex2.HResult == 0x80004005)
{
try
{//Third try with OriginalDate+slave.Start.TimeOfDay (consider original TimeOfDay)
oe = rp.GetOccurrence(date.Add(oa.Start.TimeOfDay));
}
catch (COMException ex3) when ((uint)ex3.HResult == 0x80004005)
{
Log.Debug(ex3, $"Representing outlook occurrence also not found: {oa.ToLogString()} - {originalStartTime}: {ex3}"); ;
}
}
}
}
}
return oe;
}
}
}
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.