/
OutlookSynchronizerHelper.vb
196 lines (166 loc) · 7.13 KB
/
OutlookSynchronizerHelper.vb
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
Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports DevExpress.XtraScheduler
Imports DevExpress.XtraScheduler.Exchange
Imports DevExpress.XtraScheduler.Outlook
Namespace SyncWithOutlook
Public Class OutlookSynchronizerHelper
Private _storage As SchedulerStorage
Private _calendarFolder As String
Private _outlookEntryIDFieldName As String
Private exportSynchronizer As AppointmentExportSynchronizer = Nothing
Private importSynchronizer As AppointmentImportSynchronizer = Nothing
Public Sub New()
Me.New(Nothing, "", "")
End Sub
Private isInitialSynchronization As Boolean = False
Private isSynchronization As Boolean = False
Public Sub New(ByVal storage As SchedulerStorage, Optional ByVal calendarFolder As String = "", Optional ByVal outlookEntryIDFieldName As String = "")
If storage IsNot Nothing Then
Me.Storage = storage
End If
If calendarFolder IsNot Nothing Then
Me.CalendarFolder = calendarFolder
End If
If outlookEntryIDFieldName IsNot Nothing Then
Me.OutlookEntryIDFieldName = outlookEntryIDFieldName
End If
End Sub
Public Property CalendarFolder() As String
Get
Return _calendarFolder
End Get
Set(ByVal value As String)
_calendarFolder = value
If importSynchronizer IsNot Nothing Then
TryCast(importSynchronizer, ISupportCalendarFolders).CalendarFolderName = _calendarFolder
End If
If exportSynchronizer IsNot Nothing Then
TryCast(exportSynchronizer, ISupportCalendarFolders).CalendarFolderName = _calendarFolder
End If
End Set
End Property
Public Property OutlookEntryIDFieldName() As String
Get
Return _outlookEntryIDFieldName
End Get
Set(ByVal value As String)
_outlookEntryIDFieldName = value
If importSynchronizer IsNot Nothing Then
importSynchronizer.ForeignIdFieldName = _outlookEntryIDFieldName
End If
If exportSynchronizer IsNot Nothing Then
exportSynchronizer.ForeignIdFieldName = _outlookEntryIDFieldName
End If
End Set
End Property
Public Property Storage() As SchedulerStorage
Get
Return _storage
End Get
Set(ByVal value As SchedulerStorage)
If _storage IsNot Nothing Then
UnsubscribeFromStorageEvents()
End If
_storage = value
exportSynchronizer = _storage.CreateOutlookExportSynchronizer()
importSynchronizer = _storage.CreateOutlookImportSynchronizer()
TryCast(importSynchronizer, ISupportCalendarFolders).CalendarFolderName = _calendarFolder
TryCast(exportSynchronizer, ISupportCalendarFolders).CalendarFolderName = _calendarFolder
importSynchronizer.ForeignIdFieldName = _outlookEntryIDFieldName
importSynchronizer.ForeignIdFieldName = _outlookEntryIDFieldName
SubscribeToStorageEvents()
End Set
End Property
' an internal cache to store inserted appointments references
Private insertedAppointments As New List(Of Appointment)()
' an internal cache to store deleted appointments IDs
Private deletedAppointments As New List(Of String)()
' an internal cache to store changed appointments references
Private changedAppointments As New List(Of Appointment)()
Private Sub SubscribeToStorageEvents()
If importSynchronizer IsNot Nothing Then
AddHandler importSynchronizer.AppointmentSynchronizing, AddressOf Synchronizer_AppointmentSynchronizing
End If
If exportSynchronizer IsNot Nothing Then
AddHandler exportSynchronizer.AppointmentSynchronizing, AddressOf Synchronizer_AppointmentSynchronizing
End If
AddHandler _storage.AppointmentsInserted, AddressOf Storage_AppointmentsInserted
AddHandler _storage.AppointmentsChanged, AddressOf Storage_AppointmentsChanged
AddHandler _storage.AppointmentDeleting, AddressOf Storage_AppointmentDeleting
End Sub
Private Sub UnsubscribeFromStorageEvents()
insertedAppointments.Clear()
deletedAppointments.Clear()
changedAppointments.Clear()
If importSynchronizer IsNot Nothing Then
RemoveHandler importSynchronizer.AppointmentSynchronizing, AddressOf Synchronizer_AppointmentSynchronizing
End If
If exportSynchronizer IsNot Nothing Then
RemoveHandler exportSynchronizer.AppointmentSynchronizing, AddressOf Synchronizer_AppointmentSynchronizing
End If
RemoveHandler _storage.AppointmentsInserted, AddressOf Storage_AppointmentsInserted
RemoveHandler _storage.AppointmentsChanged, AddressOf Storage_AppointmentsChanged
RemoveHandler _storage.AppointmentDeleting, AddressOf Storage_AppointmentDeleting
End Sub
Private Sub Storage_AppointmentsInserted(ByVal sender As Object, ByVal e As PersistentObjectsEventArgs)
If isSynchronization Then
Return
End If
For i As Integer = 0 To e.Objects.Count - 1
Dim newAppointment As Appointment = TryCast(e.Objects(i), Appointment)
newAppointment.CustomFields(OutlookEntryIDFieldName) = Nothing
insertedAppointments.Add(TryCast(e.Objects(i), Appointment))
Next i
End Sub
Private Sub Storage_AppointmentsChanged(ByVal sender As Object, ByVal e As PersistentObjectsEventArgs)
If isSynchronization Then
Return
End If
For i As Integer = 0 To e.Objects.Count - 1
changedAppointments.Add(TryCast(e.Objects(i), Appointment))
Next i
End Sub
Private Sub Storage_AppointmentDeleting(ByVal sender As Object, ByVal e As PersistentObjectCancelEventArgs)
If isSynchronization Then
Return
End If
Dim currentAppointment As Appointment = TryCast(e.Object, Appointment)
If currentAppointment.CustomFields(OutlookEntryIDFieldName) IsNot Nothing AndAlso (currentAppointment.Type = AppointmentType.Normal OrElse currentAppointment.Type = AppointmentType.Pattern) Then
deletedAppointments.Add(currentAppointment.CustomFields(OutlookEntryIDFieldName).ToString())
End If
End Sub
Private Sub Synchronizer_AppointmentSynchronizing(ByVal sender As Object, ByVal e As AppointmentSynchronizingEventArgs)
AnalyzeAndHandleCurrentOperation(TryCast(e, DevExpress.XtraScheduler.Outlook.OutlookAppointmentSynchronizingEventArgs))
End Sub
Private Sub AnalyzeAndHandleCurrentOperation(ByVal eventArgs As DevExpress.XtraScheduler.Outlook.OutlookAppointmentSynchronizingEventArgs)
Select Case eventArgs.Operation
Case SynchronizeOperation.Create
eventArgs.Cancel = eventArgs.OutlookAppointment IsNot Nothing AndAlso deletedAppointments.Contains(eventArgs.OutlookAppointment.EntryID)
Case SynchronizeOperation.Delete
If isInitialSynchronization Then
eventArgs.Cancel = True
Else
eventArgs.Cancel = eventArgs.Appointment IsNot Nothing AndAlso insertedAppointments.Contains(eventArgs.Appointment)
End If
Case SynchronizeOperation.Replace
eventArgs.Cancel = eventArgs.Appointment IsNot Nothing AndAlso changedAppointments.Contains(eventArgs.Appointment)
Case Else
End Select
End Sub
' synchronization methods
Public Sub PerformCalendarsSynchronization(ByVal isInitial As Boolean)
isInitialSynchronization = isInitial
isSynchronization = True
importSynchronizer.Synchronize()
changedAppointments.Clear()
exportSynchronizer.Synchronize()
insertedAppointments.Clear()
deletedAppointments.Clear()
isSynchronization = False
End Sub
End Class
End Namespace