/
OutlookSynchronizerHelper.cs
160 lines (130 loc) · 7.17 KB
/
OutlookSynchronizerHelper.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DevExpress.XtraScheduler;
using DevExpress.XtraScheduler.Exchange;
using DevExpress.XtraScheduler.Outlook;
namespace SyncWithOutlook {
public class OutlookSynchronizerHelper {
private SchedulerStorage _storage;
private string _calendarFolder;
private string _outlookEntryIDFieldName;
private AppointmentExportSynchronizer exportSynchronizer = null;
private AppointmentImportSynchronizer importSynchronizer = null;
public OutlookSynchronizerHelper() : this(null, "", "") { }
private bool isInitialSynchronization = false;
private bool isSynchronization = false;
public OutlookSynchronizerHelper(SchedulerStorage storage, string calendarFolder = "", string outlookEntryIDFieldName = "") {
if(storage != null) Storage = storage;
if(calendarFolder != null) CalendarFolder = calendarFolder;
if(outlookEntryIDFieldName != null) OutlookEntryIDFieldName = outlookEntryIDFieldName;
}
public string CalendarFolder {
get { return _calendarFolder; }
set {
_calendarFolder = value;
if(importSynchronizer != null) (importSynchronizer as ISupportCalendarFolders).CalendarFolderName = _calendarFolder;
if(exportSynchronizer != null) (exportSynchronizer as ISupportCalendarFolders).CalendarFolderName = _calendarFolder;
}
}
public string OutlookEntryIDFieldName {
get { return _outlookEntryIDFieldName; }
set {
_outlookEntryIDFieldName = value;
if(importSynchronizer != null) importSynchronizer.ForeignIdFieldName = _outlookEntryIDFieldName;
if(exportSynchronizer != null) exportSynchronizer.ForeignIdFieldName = _outlookEntryIDFieldName;
}
}
public SchedulerStorage Storage {
get { return _storage; }
set {
if(_storage != null) UnsubscribeFromStorageEvents();
_storage = value;
exportSynchronizer = _storage.CreateOutlookExportSynchronizer();
importSynchronizer = _storage.CreateOutlookImportSynchronizer();
(importSynchronizer as ISupportCalendarFolders).CalendarFolderName = _calendarFolder;
(exportSynchronizer as ISupportCalendarFolders).CalendarFolderName = _calendarFolder;
importSynchronizer.ForeignIdFieldName = _outlookEntryIDFieldName;
importSynchronizer.ForeignIdFieldName = _outlookEntryIDFieldName;
SubscribeToStorageEvents();
}
}
// an internal cache to store inserted appointments references
List<Appointment> insertedAppointments = new List<Appointment>();
// an internal cache to store deleted appointments IDs
List<string> deletedAppointments = new List<string>();
// an internal cache to store changed appointments references
List<Appointment> changedAppointments = new List<Appointment>();
private void SubscribeToStorageEvents() {
if(importSynchronizer != null) importSynchronizer.AppointmentSynchronizing += Synchronizer_AppointmentSynchronizing;
if(exportSynchronizer != null) exportSynchronizer.AppointmentSynchronizing += Synchronizer_AppointmentSynchronizing;
_storage.AppointmentsInserted += Storage_AppointmentsInserted;
_storage.AppointmentsChanged += Storage_AppointmentsChanged;
_storage.AppointmentDeleting += Storage_AppointmentDeleting;
}
private void UnsubscribeFromStorageEvents() {
insertedAppointments.Clear();
deletedAppointments.Clear();
changedAppointments.Clear();
if(importSynchronizer != null) importSynchronizer.AppointmentSynchronizing -= Synchronizer_AppointmentSynchronizing;
if(exportSynchronizer != null) exportSynchronizer.AppointmentSynchronizing -= Synchronizer_AppointmentSynchronizing;
_storage.AppointmentsInserted -= Storage_AppointmentsInserted;
_storage.AppointmentsChanged -= Storage_AppointmentsChanged;
_storage.AppointmentDeleting -= Storage_AppointmentDeleting;
}
void Storage_AppointmentsInserted(object sender, PersistentObjectsEventArgs e) {
if(isSynchronization) return;
for(int i = 0; i < e.Objects.Count; i++) {
Appointment newAppointment = e.Objects[i] as Appointment;
newAppointment.CustomFields[OutlookEntryIDFieldName] = null;
insertedAppointments.Add(e.Objects[i] as Appointment);
}
}
void Storage_AppointmentsChanged(object sender, PersistentObjectsEventArgs e) {
if(isSynchronization) return;
for(int i = 0; i < e.Objects.Count; i++) {
changedAppointments.Add(e.Objects[i] as Appointment);
}
}
void Storage_AppointmentDeleting(object sender, PersistentObjectCancelEventArgs e) {
if(isSynchronization) return;
Appointment currentAppointment = e.Object as Appointment;
if(currentAppointment.CustomFields[OutlookEntryIDFieldName] != null && (currentAppointment.Type == AppointmentType.Normal || currentAppointment.Type == AppointmentType.Pattern)) {
deletedAppointments.Add(currentAppointment.CustomFields[OutlookEntryIDFieldName].ToString());
}
}
void Synchronizer_AppointmentSynchronizing(object sender, AppointmentSynchronizingEventArgs e) {
AnalyzeAndHandleCurrentOperation(e as DevExpress.XtraScheduler.Outlook.OutlookAppointmentSynchronizingEventArgs);
}
void AnalyzeAndHandleCurrentOperation(DevExpress.XtraScheduler.Outlook.OutlookAppointmentSynchronizingEventArgs eventArgs) {
switch(eventArgs.Operation) {
case SynchronizeOperation.Create:
eventArgs.Cancel = eventArgs.OutlookAppointment != null && deletedAppointments.Contains(eventArgs.OutlookAppointment.EntryID);
break;
case SynchronizeOperation.Delete:
if(isInitialSynchronization)
eventArgs.Cancel = true;
else
eventArgs.Cancel = eventArgs.Appointment != null && insertedAppointments.Contains(eventArgs.Appointment);
break;
case SynchronizeOperation.Replace:
eventArgs.Cancel = eventArgs.Appointment != null && changedAppointments.Contains(eventArgs.Appointment);
break;
default:
break;
}
}
// synchronization methods
public void PerformCalendarsSynchronization(bool isInitial) {
isInitialSynchronization = isInitial;
isSynchronization = true;
importSynchronizer.Synchronize();
changedAppointments.Clear();
exportSynchronizer.Synchronize();
insertedAppointments.Clear();
deletedAppointments.Clear();
isSynchronization = false;
}
}
}