Menu

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

Download this file

411 lines (376 with data), 17.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
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
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.Shared == null)
{
ga.ExtendedProperties.Shared = new Dictionary<string, string>();
}
// check if exists
foreach (var p in ga.ExtendedProperties.Shared)
{
if (p.Key == key)
{
if (ga.ExtendedProperties.Shared[p.Key] != oid)
{
ga.ExtendedProperties.Shared[p.Key] = oid;
return true;
}
return false;
}
}
//not found
var prop = new KeyValuePair<string, string>(key, oid);
ga.ExtendedProperties.Shared.Add(prop);
return true;
}
internal static string GetGoogleOutlookId(Event ga)
{
var key = OutlookPropertiesUtils.GetKey();
// get extended prop
if (ga.ExtendedProperties != null && ga.ExtendedProperties.Shared != null)
{
foreach (var p in ga.ExtendedProperties.Shared)
{
if (p.Key == key)
{
return p.Value;
}
}
}
return null;
}
internal static bool ResetGoogleOutlookId(Event ga)
{
var key = OutlookPropertiesUtils.GetKey();
if (ga.ExtendedProperties != null && ga.ExtendedProperties.Shared != null)
{
// get extended prop
foreach (var p in ga.ExtendedProperties.Shared)
{
if (p.Key == key)
{
// remove
ga.ExtendedProperties.Shared.Remove(p);
return true;
}
}
}
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); //Not needed for Outlook Appointments, because appointments can hold seconds (contacts cannot)
return lastUpdated;
}
internal static DateTime GetGoogleLastUpdated(AppointmentMatch match, Synchronizer sync)
{
DateTime lastUpdated = DateTime.MinValue;
var now = DateTime.Now;
if (match.GoogleAppointment.Updated.HasValue)
{
lastUpdated = match.GoogleAppointment.Updated.Value;
//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.Updated != null)//happens for cancelled events
{
var lastUpdatedGoogleException = ga.Updated.Value;
if (lastUpdatedGoogleException > lastUpdated)
{
lastUpdated = lastUpdatedGoogleException;
}
}
else
{
var f_ga = sync.GetGoogleAppointment(ga.Id);
if (f_ga.Updated != null)
{
var lastUpdatedGoogleException = f_ga.Updated.Value;
if (lastUpdatedGoogleException > lastUpdated)
{
lastUpdated = lastUpdatedGoogleException;
}
}
else if (match.OutlookAppointment.IsRecurring && match.OutlookAppointment.RecurrenceState == Outlook.OlRecurrenceState.olApptMaster)
{
Outlook.AppointmentItem oa = null;
Outlook.RecurrencePattern rp = null;
try
{
try
{
rp = match.OutlookAppointment.GetRecurrence();
if (ga.OriginalStartTime != null)
{
if (!string.IsNullOrEmpty(ga.OriginalStartTime.Date))
{
var date = DateTime.Parse(ga.OriginalStartTime.Date);
//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?
oa = rp.GetOccurrence(date);
}
else if (ga.OriginalStartTime.DateTime != 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(ga.OriginalStartTime.DateTime.Value)) //In sync range?
//if (ga.OriginalStartTime.DateTime.Value >= now) //or only in the future?
oa = rp.GetOccurrence(ga.OriginalStartTime.DateTime.Value);
}
}
}
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 && oa.MeetingStatus != Outlook.OlMeetingStatus.olMeetingCanceled)
{
lastUpdated = now;
break; //no need to search further, already newest date set
}
}
finally
{
if (oa != null)
{
Marshal.ReleaseComObject(oa);
oa = 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;
}
}
}
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.