Menu

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

Download this file

748 lines (678 with data), 35.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
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
using Google.Apis.Calendar.v3.Data;
using Serilog;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Runtime.InteropServices;
using Outlook = Microsoft.Office.Interop.Outlook;
namespace GoContactSyncMod
{
internal static class AppointmentsMatcher
{
/// <summary>
/// Time tolerance in seconds - used when comparing date modified.
/// Less than 60 seconds doesn't make sense, as the lastSync is saved without seconds and if it is compared
/// with the LastUpdate dates of Google and Outlook, in the worst case you compare e.g. 15:59 with 16:00 and
/// after truncating to minutes you compare 15:00 wiht 16:00
/// </summary>
public static int TimeTolerance = 60;
public delegate void NotificationHandler(string message);
public static event NotificationHandler NotificationReceived;
private static bool MatchAppointmentsByProperties(Outlook.AppointmentItem oa, Event ga)
{
if (oa.Subject != ga.Summary)
{
return false;
}
//check if both are all day events
if (ga.Start != null)
{
if (oa.AllDayEvent)
{
if (!string.IsNullOrEmpty(ga.Start.Date) && DateTime.Parse(ga.Start.Date) == oa.Start)
{
return true;
}
}
else
{
if (ga.Start.DateTime != null && oa.Start == ga.Start.DateTime.Value)
{
return true;
}
}
}
return false;
}
/// <summary>
/// Matches outlook and Google appointment by a) id b) properties.
/// </summary>
/// <param name="sync">Syncronizer instance</param>
/// <returns>Returns a list of match pairs (outlook appointment + Google appointment) for all appointment. Those that weren't matche will have it's peer set to null</returns>
public static List<AppointmentMatch> MatchAppointments(Synchronizer sync)
{
Log.Information("Matching Outlook and Google appointments...");
var result = new List<AppointmentMatch>();
//for each outlook appointment try to get Google appointment id from user properties
//if no match - try to match by properties
//if no match - create a new match pair without Google appointment.
var OutlookAppointmentsWithoutSyncId = new Collection<Outlook.AppointmentItem>();
#region Match first all outlookAppointments by sync id
for (var i = 1; i <= sync.OutlookAppointments.Count; i++)
{
var oa = sync.OutlookAppointments[i] as Outlook.AppointmentItem;
if (!sync.IsOutlookAppointmentToBeProcessed(oa))
{
sync.SkippedCount++;
sync.SkippedCountNotMatches++;
//Logged already within above function
//Log.Warning($"Skipped Outlook appointment because could not be processed for several reasons (see Debug log messages in log file):" + oa==null?"<null>":oa.ToLogString());
if (oa != null)
{
Marshal.ReleaseComObject(oa);
}
continue;
}
var s = $"Matching appointment {i} of {sync.OutlookAppointments.Count} by id: {oa.ToLogString()}";
NotificationReceived?.Invoke(s);
Log.Debug(s);
//try to match this appointment to one of Google appointments
var gid = AppointmentPropertiesUtils.GetOutlookGoogleId(oa);
if (gid != null)
{
var ga = sync.GetGoogleAppointmentById(gid);
if (ga != null && !ga.Status.Equals("cancelled"))
{
//we found a match by google id, that is not deleted or cancelled yet
var match = new AppointmentMatch(oa, null);
match.AddGoogleAppointment(ga);
result.Add(match);
sync.GoogleAppointments.Remove(ga);
}
else
{
OutlookAppointmentsWithoutSyncId.Add(oa);
}
}
else
{
OutlookAppointmentsWithoutSyncId.Add(oa);
}
}
#endregion
#region Match the remaining appointments by properties
for (var i = 0; i < OutlookAppointmentsWithoutSyncId.Count; i++)
{
var oa = OutlookAppointmentsWithoutSyncId[i];
var s = $"Matching appointment {i + 1} of {OutlookAppointmentsWithoutSyncId.Count} by unique properties: {oa.ToLogString()}";
NotificationReceived?.Invoke(s);
Log.Debug(s);
//no match found by id => match by subject/title
//create a default match pair with just outlook appointment.
var match = new AppointmentMatch(oa, null);
//foreach Google appointment try to match and create a match pair if found some match(es)
for (var j = sync.GoogleAppointments.Count - 1; j >= 0; j--)
{
var ga = sync.GoogleAppointments[j];
// only match if there is a appointment targetBody, else
// a matching Google appointment will be created at each sync
if (!ga.Status.Equals("cancelled") && MatchAppointmentsByProperties(oa, ga))
{
match.AddGoogleAppointment(ga);
sync.GoogleAppointments.Remove(ga);
}
}
if (match.GoogleAppointment == null)
{
string action;
if (sync.SyncOption != SyncOption.OutlookToGoogleOnly)
{
var gid = AppointmentPropertiesUtils.GetOutlookGoogleId(match.OutlookAppointment);
action = gid != null ? "Delete from Outlook" : "Add to Google";
}
else
{
action = "Add to Google";
}
Log.Debug($"No match found for Outlook appointment ({match.OutlookAppointment.ToLogString()}) => {action}");
}
result.Add(match);
}
#endregion
var googleAppointmentExceptions = new List<Event>();
//for each Google appointment that's left (they will be nonmatched) create a new match pair without outlook appointment.
for (var i = 0; i < sync.GoogleAppointments.Count; i++)
{
var ga = sync.GoogleAppointments[i];
var s = $"Adding new Google appointment {i + 1} of {sync.GoogleAppointments.Count} by unique properties: {ga.ToLogString()}";
NotificationReceived?.Invoke(s);
Log.Debug(s);
if (ga.RecurringEventId != null)
{
sync.SkippedCountNotMatches++;
googleAppointmentExceptions.Add(ga);
}
else if (ga.Status.Equals("cancelled"))
{
Log.Debug($"Skipping Google appointment found because it is cancelled: {ga.ToLogString()}");
//sync.SkippedCount++;
//sync.SkippedCountNotMatches++;
}
else if (string.IsNullOrEmpty(ga.Summary) && (ga.Start == null || (ga.Start.DateTime == null && ga.Start.Date == null)))
{
// no title or time
sync.SkippedCount++;
sync.SkippedCountNotMatches++;
Log.Warning($"Skipped Google appointment because no unique property found (Subject or StartDate):{ga.ToLogString()}");
}
else
{
string action;
if (sync.SyncOption == SyncOption.GoogleToOutlookOnly)
{
action = "Add to Outlook";
}
else
{
var oid = AppointmentPropertiesUtils.GetGoogleOutlookId(ga);
if (string.IsNullOrEmpty(oid))
{
action = "Add to Outlook";
}
else
{
action = "Delete from Google";
}
}
Log.Debug($"No match found for Google appointment ({ga.ToLogString()}) => {action}");
result.Add(new AppointmentMatch(null, ga));
}
}
//for each Google appointment exception, assign to proper match
for (var i = 0; i < googleAppointmentExceptions.Count; i++)
{
var ga = googleAppointmentExceptions[i];
var s = $"Adding Google appointment exception {i + 1} of {googleAppointmentExceptions.Count} : {ga.ToLogString()}";
NotificationReceived?.Invoke(s);
Log.Debug(s);
var found = false;
foreach (var match in result)
{
if (match.GoogleAppointment != null && ga.RecurringEventId.Equals(match.GoogleAppointment.Id))
{
match.GoogleAppointmentExceptions.Add(ga);
found = true;
break;
}
}
if (!found)
{
var log = ga.ToLogString();
Log.Debug($"No match found for Google appointment exception: {log}");
if (string.IsNullOrWhiteSpace(log))
{
ga.ToDebugLog();
}
}
}
return result;
}
public static void SyncAppointments(Synchronizer sync)
{
for (var i = 0; i < sync.Appointments.Count; i++)
{
var match = sync.Appointments[i];
var s = $"Syncing appointment {i + 1} of {sync.Appointments.Count}: {match.ToLogString()}";
NotificationReceived?.Invoke(s);
Log.Debug(s);
SyncAppointment(match, sync);
}
}
public static int RecipientsCount(Outlook.AppointmentItem oa)
{
Outlook.Recipients recipients = null;
try
{
recipients = oa.Recipients;
if (recipients != null)
{
return recipients.Count;
}
else
{
return 0;
}
}
finally
{
if (recipients != null)
{
Marshal.ReleaseComObject(recipients);
}
}
}
private static void SyncAppointmentNoGoogle(AppointmentMatch match, Synchronizer sync)
{
if (sync.SyncOption == SyncOption.OutlookToGoogleOnly)
{
match.GoogleAppointment = Factory.NewEvent();
sync.UpdateAppointment(match.OutlookAppointment, ref match.GoogleAppointment, ref match.GoogleAppointmentExceptions);
return;
}
if (sync.SyncOption == SyncOption.GoogleToOutlookOnly)
{
sync.SkippedCount++;
Log.Debug($"Outlook appointment not added to Google, because of SyncOption {sync.SyncOption}: {match.OutlookAppointment.ToLogString()}");
return;
}
var gid = AppointmentPropertiesUtils.GetOutlookGoogleId(match.OutlookAppointment);
if (!string.IsNullOrEmpty(gid))
{
if (!sync.SyncDelete)
{
return;
}
else if (!sync.PromptDelete && RecipientsCount(match.OutlookAppointment) <= 1)
{
sync.DeleteOutlookResolution = DeleteResolution.DeleteOutlook;
}
else if (sync.DeleteOutlookResolution != DeleteResolution.DeleteOutlookAlways &&
sync.DeleteOutlookResolution != DeleteResolution.KeepOutlookAlways)
{
using (var r = new ConflictResolver())
{
sync.DeleteOutlookResolution = r.ResolveDelete(match.OutlookAppointment);
}
}
switch (sync.DeleteOutlookResolution)
{
case DeleteResolution.KeepOutlook:
case DeleteResolution.KeepOutlookAlways:
AppointmentPropertiesUtils.ResetOutlookGoogleId(match.OutlookAppointment);
break;
case DeleteResolution.DeleteOutlook:
case DeleteResolution.DeleteOutlookAlways:
if (RecipientsCount(match.OutlookAppointment) > 1)
{
//ToDo:Maybe find as better way, e.g. to ask the user, if he wants to overwrite the invalid appointment
Log.Information($"Outlook Appointment not deleted, because multiple participants found, invitation maybe NOT sent by Google: {match.OutlookAppointment.ToLogString()}");
AppointmentPropertiesUtils.ResetOutlookGoogleId(match.OutlookAppointment);
break;
}
else
{
//Avoid recreating a GoogleAppointment already existing
//==> Delete this OutlookAppointment instead if previous match existed but no match exists anymore
return;
}
default:
throw new ApplicationException("Cancelled");
}
}
//create a Google appointment from Outlook appointment
match.GoogleAppointment = Factory.NewEvent();
sync.UpdateAppointment(match.OutlookAppointment, ref match.GoogleAppointment, ref match.GoogleAppointmentExceptions);
}
private static void SyncAppointmentNoOutlook(AppointmentMatch match, Synchronizer sync)
{
if (sync.SyncOption == SyncOption.GoogleToOutlookOnly)
{
match.OutlookAppointment = Synchronizer.CreateOutlookAppointmentItem(Synchronizer.SyncAppointmentsFolder);
sync.UpdateAppointment(ref match.GoogleAppointment, ref match.OutlookAppointment, match.GoogleAppointmentExceptions);
return;
}
if (sync.SyncOption == SyncOption.OutlookToGoogleOnly)
{
sync.SkippedCount++;
Log.Debug($"Google appointment not added to Outlook, because of SyncOption {sync.SyncOption}: {match.GoogleAppointment.Summary}");
return;
}
var oid = AppointmentPropertiesUtils.GetGoogleOutlookId(match.GoogleAppointment);
if (!string.IsNullOrEmpty(oid))
{
if (!sync.SyncDelete)
{
return;
}
else if (!sync.PromptDelete)
{
sync.DeleteGoogleResolution = DeleteResolution.DeleteGoogleAlways;
}
else if (sync.DeleteGoogleResolution != DeleteResolution.DeleteGoogleAlways &&
sync.DeleteGoogleResolution != DeleteResolution.KeepGoogleAlways)
{
using (var r = new ConflictResolver())
{
sync.DeleteGoogleResolution = r.ResolveDelete(match.GoogleAppointment);
}
}
switch (sync.DeleteGoogleResolution)
{
case DeleteResolution.KeepGoogle:
case DeleteResolution.KeepGoogleAlways:
AppointmentPropertiesUtils.ResetGoogleOutlookId(match.GoogleAppointment);
break;
case DeleteResolution.DeleteGoogle:
case DeleteResolution.DeleteGoogleAlways:
//Avoid recreating a OutlookAppointment already existing
//==> Delete this googleAppointment instead if previous match existed but no match exists anymore
return;
default:
throw new ApplicationException("Cancelled");
}
}
//create a Outlook appointment from Google appointment
match.OutlookAppointment = Synchronizer.CreateOutlookAppointmentItem(Synchronizer.SyncAppointmentsFolder);
sync.UpdateAppointment(ref match.GoogleAppointment, ref match.OutlookAppointment, match.GoogleAppointmentExceptions);
}
private static bool UpdatedSinceLastSync(DateTime d, DateTime s)
{
return (int)d.Subtract(s).TotalSeconds > TimeTolerance;
}
private static void SyncAppointmentBothExists(AppointmentMatch match, Synchronizer sync)
{
//ToDo: Check how to overcome appointment recurrences, which need more than 60 seconds to update and therefore get updated again and again because of time tolerance 60 seconds violated again and again
//merge appointment details
//determine if this appointment pair were synchronized
//DateTime? lastUpdated = GetOutlookPropertyValueDateTime(match.OutlookAppointment, sync.OutlookPropertyNameUpdated);
var lastSynced = AppointmentPropertiesUtils.GetOutlookLastSync(match.OutlookAppointment);
if (lastSynced.HasValue)
{
//appointment pair was syncronysed before.
//determine if Google appointment was updated since last sync
//lastSynced is stored without seconds. take that into account.
var lastUpdatedOutlook = AppointmentPropertiesUtils.GetOutlookLastUpdated(match);
var lastUpdatedGoogle = AppointmentPropertiesUtils.GetGoogleLastUpdated(match, sync);
var OutlookUpdatedSinceLastSync = UpdatedSinceLastSync(lastUpdatedOutlook, lastSynced.Value);
var GoogleUpdatedSinceLastSync = UpdatedSinceLastSync(lastUpdatedGoogle, lastSynced.Value);
//ToDo: Too many updates, check if we can use eTag
//if (!GoogleUpdatedSinceLastSync)
//{
//var etagOutlook = AppointmentPropertiesUtils.GetOutlookLastEtag(match.OutlookAppointment);
//var eTagGoogle = match.GoogleAppointment.ETag;
//if (!string.IsNullOrEmpty(etagOutlook) && !String.IsNullOrEmpty(eTagGoogle) && etagOutlook != eTagGoogle)
// GoogleUpdatedSinceLastSync = true;
//}
//check if both outlok and Google appointments where updated sync last sync
if (OutlookUpdatedSinceLastSync && GoogleUpdatedSinceLastSync)
{
switch (sync.SyncOption)
{
case SyncOption.MergeOutlookWins:
case SyncOption.OutlookToGoogleOnly:
//overwrite Google appointment
Log.Debug($"Outlook and Google appointment have been updated, Outlook appointment is overwriting Google because of SyncOption {sync.SyncOption}: {match.OutlookAppointment.ToLogString()}.");
sync.UpdateAppointment(match.OutlookAppointment, ref match.GoogleAppointment, ref match.GoogleAppointmentExceptions);
break;
case SyncOption.MergeGoogleWins:
case SyncOption.GoogleToOutlookOnly:
//overwrite outlook appointment
Log.Debug($"Outlook and Google appointment have been updated, Google appointment is overwriting Outlook because of SyncOption {sync.SyncOption}: {match.GoogleAppointment.Summary}.");
sync.UpdateAppointment(ref match.GoogleAppointment, ref match.OutlookAppointment, match.GoogleAppointmentExceptions);
break;
case SyncOption.MergePrompt:
if (RecipientsCount(match.OutlookAppointment) > 1) //Only ask, if not multiple OutlookAppointments, because then it would anyway always update from Outlook to Google
sync.ConflictResolution = ConflictResolution.OutlookWins;
//promp for sync option
else if (sync.ConflictResolution != ConflictResolution.GoogleWinsAlways &&
sync.ConflictResolution != ConflictResolution.OutlookWinsAlways &&
sync.ConflictResolution != ConflictResolution.SkipAlways)
{
using (var r = new ConflictResolver())
{
sync.ConflictResolution = r.Resolve(match.OutlookAppointment, match.GoogleAppointment, false);
}
}
switch (sync.ConflictResolution)
{
case ConflictResolution.Skip:
case ConflictResolution.SkipAlways:
Log.Information($"User skipped appointment ({match}).");
sync.SkippedCount++;
break;
case ConflictResolution.OutlookWins:
case ConflictResolution.OutlookWinsAlways:
sync.UpdateAppointment(match.OutlookAppointment, ref match.GoogleAppointment, ref match.GoogleAppointmentExceptions);
break;
case ConflictResolution.GoogleWins:
case ConflictResolution.GoogleWinsAlways:
sync.UpdateAppointment(ref match.GoogleAppointment, ref match.OutlookAppointment, match.GoogleAppointmentExceptions);
break;
default:
throw new ApplicationException("Cancelled");
}
break;
}
return;
}
//check if Outlook appointment was updated (with X second tolerance)
if (sync.SyncOption != SyncOption.GoogleToOutlookOnly)
{
//Outlook appointment was changed or changed Google appointment will be overwritten
if (sync.SyncOption == SyncOption.OutlookToGoogleOnly && GoogleUpdatedSinceLastSync)
{
Log.Debug($"Google appointment has been updated since last sync, but Outlook appointment is overwriting Google because of SyncOption {sync.SyncOption}: {match.OutlookAppointment.ToLogString()}.");
sync.UpdateAppointment(match.OutlookAppointment, ref match.GoogleAppointment, ref match.GoogleAppointmentExceptions);
return;
}
else if (OutlookUpdatedSinceLastSync)
{
sync.UpdateAppointment(match.OutlookAppointment, ref match.GoogleAppointment, ref match.GoogleAppointmentExceptions);
return;
}
//at the moment use Outlook as "master" source of appointments - in the event of a conflict Google appointment will be overwritten.
//TODO: control conflict resolution by SyncOption
}
//check if Google appointment was updated (with X second tolerance)
if (sync.SyncOption != SyncOption.OutlookToGoogleOnly)
{
//google appointment was changed or changed Outlook appointment will be overwritten
if (sync.SyncOption == SyncOption.GoogleToOutlookOnly && OutlookUpdatedSinceLastSync)
{
Log.Debug($"Outlook appointment has been updated since last sync, but Google appointment is overwriting Outlook because of SyncOption {sync.SyncOption}: {match.OutlookAppointment.ToLogString()}.");
sync.UpdateAppointment(ref match.GoogleAppointment, ref match.OutlookAppointment, match.GoogleAppointmentExceptions);
return;
}
else if (GoogleUpdatedSinceLastSync)
{
sync.UpdateAppointment(ref match.GoogleAppointment, ref match.OutlookAppointment, match.GoogleAppointmentExceptions);
return;
}
}
}
else
{
//appointments were never synced.
//merge appointments.
switch (sync.SyncOption)
{
case SyncOption.MergeOutlookWins:
case SyncOption.OutlookToGoogleOnly:
//overwrite Google appointment
sync.UpdateAppointment(match.OutlookAppointment, ref match.GoogleAppointment, ref match.GoogleAppointmentExceptions);
break;
case SyncOption.MergeGoogleWins:
case SyncOption.GoogleToOutlookOnly:
//overwrite outlook appointment
sync.UpdateAppointment(ref match.GoogleAppointment, ref match.OutlookAppointment, match.GoogleAppointmentExceptions);
break;
case SyncOption.MergePrompt:
//promp for sync option
if (sync.ConflictResolution != ConflictResolution.GoogleWinsAlways &&
sync.ConflictResolution != ConflictResolution.OutlookWinsAlways &&
sync.ConflictResolution != ConflictResolution.SkipAlways)
{
using (var r = new ConflictResolver())
{
sync.ConflictResolution = r.Resolve(match.OutlookAppointment, match.GoogleAppointment, true);
}
}
switch (sync.ConflictResolution)
{
case ConflictResolution.Skip:
case ConflictResolution.SkipAlways: //Keep both, Google AND Outlook
sync.Appointments.Add(new AppointmentMatch(match.OutlookAppointment, null));
sync.Appointments.Add(new AppointmentMatch(null, match.GoogleAppointment));
break;
case ConflictResolution.OutlookWins:
case ConflictResolution.OutlookWinsAlways:
sync.UpdateAppointment(match.OutlookAppointment, ref match.GoogleAppointment, ref match.GoogleAppointmentExceptions);
break;
case ConflictResolution.GoogleWins:
case ConflictResolution.GoogleWinsAlways:
sync.UpdateAppointment(ref match.GoogleAppointment, ref match.OutlookAppointment, match.GoogleAppointmentExceptions);
break;
default:
throw new ApplicationException("Canceled");
}
break;
}
}
}
public static void SyncAppointment(AppointmentMatch match, Synchronizer sync)
{
if (match.GoogleAppointment == null && match.OutlookAppointment != null)
{
//first check if maybe Google appointment was shifted to be outside sync range and
//that is why we have not found it
var gid = AppointmentPropertiesUtils.GetOutlookGoogleId(match.OutlookAppointment);
if (!string.IsNullOrEmpty(gid))
{
var ge = sync.GetGoogleAppointment(gid);
if (ge != null && ge.Status != "cancelled")
{
match.GoogleAppointment = ge;
SyncAppointmentBothExists(match, sync);
return;
}
}
//No Google appointment
SyncAppointmentNoGoogle(match, sync);
}
else if (match.OutlookAppointment == null && match.GoogleAppointment != null)
{
//first check if maybe Outlook appointment was shifted to be outside sync range and
//that is why we have not found it
var oid = AppointmentPropertiesUtils.GetGoogleOutlookId(match.GoogleAppointment);
if (!string.IsNullOrEmpty(oid))
{
Outlook.AppointmentItem oa;
try
{
dynamic item = Synchronizer.OutlookNameSpace.GetItemFromID(oid);
oa = item as Outlook.AppointmentItem;
if (oa != null)
{
if (oa.IsDeleted())
{
oa = null;
}
if (string.IsNullOrEmpty(oa.Subject) && oa.Start == AppointmentSync.outlookDateMin)
{
oa = null;
}
if (oa.MeetingStatus == Outlook.OlMeetingStatus.olMeetingCanceled || oa.MeetingStatus == Outlook.OlMeetingStatus.olMeetingReceivedAndCanceled)
{
oa = null;
}
Outlook.MAPIFolder fld = null;
Outlook.MAPIFolder s = null;
try
{
fld = oa.Parent as Outlook.MAPIFolder;
if (fld != null)
{
s = sync.GetAppoimentsFolder();
if (fld.FolderPath != s.FolderPath)
{
oa = null;
}
}
else
{
oa = null;
}
}
finally
{
if (fld != null)
{
Marshal.ReleaseComObject(fld);
}
if (s != null)
{
Marshal.ReleaseComObject(s);
}
}
}
}
catch
{
oa = null;
}
if (oa != null)
{
match.OutlookAppointment = oa;
SyncAppointmentBothExists(match, sync);
return;
}
}
//no Outlook appointment
SyncAppointmentNoOutlook(match, sync);
}
else if (match.OutlookAppointment != null && match.GoogleAppointment != null)
{
SyncAppointmentBothExists(match, sync);
}
}
}
internal class AppointmentMatch
{
public Outlook.AppointmentItem OutlookAppointment;
public Event GoogleAppointment;
public readonly List<Event> AllGoogleAppointmentMatches = new List<Event>(1);
public Event LastGoogleAppointment;
public List<Event> GoogleAppointmentExceptions = new List<Event>();
public AppointmentMatch(Outlook.AppointmentItem oa, Event ga)
{
OutlookAppointment = oa;
GoogleAppointment = ga;
}
public string ToLogString()
{
var name = string.Empty;
if (OutlookAppointment != null)
{
name = OutlookAppointment.ToLogString();
}
else if (GoogleAppointment != null)
{
name = GoogleAppointment.ToLogString();
}
return name;
}
public void AddGoogleAppointment(Event ga)
{
if (ga == null)
{
return;
}
//throw new ArgumentNullException("googleAppointment must not be null.");
if (GoogleAppointment == null)
{
GoogleAppointment = ga;
}
//this to avoid searching the entire collection.
//if last appointment it what we are trying to add the we have already added it earlier
if (LastGoogleAppointment == ga)
{
return;
}
if (!AllGoogleAppointmentMatches.Contains(ga))
{
AllGoogleAppointmentMatches.Add(ga);
}
LastGoogleAppointment = ga;
}
}
}
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.