-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathLyncModel.cs
277 lines (211 loc) · 8.27 KB
/
LyncModel.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
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
/* Copyright (C) 2012 Modality Systems - All Rights Reserved
* You may use, distribute and modify this code under the
* terms of the Microsoft Public License, a copy of which
* can be seen at: http://www.microsoft.com/en-us/openness/licenses.aspx
*
* http://www.LyncAutoAnswer.com
*/
using System;
using System.Collections.Generic;
using Microsoft.Lync.Model;
using System.IO;
using System.Windows.Media.Imaging;
namespace LyncUISupressionWrapper
{
public class LyncModel : ILyncModel
{
#region Fields
Contact _contact;
#endregion
#region Events
public event EventHandler<StateChangedEventArgs> StateChanged = delegate { };
private void OnStateChanged(ApplicationState pState)
{
StateChanged(this, new StateChangedEventArgs(pState));
}
public event EventHandler<VideoAvailabilityChangedEventArgs> VideoAvailabilityChanged = delegate { };
private void OnVideoAvailabilityChanged(VideoAvailabilityChangedEventArgs e)
{
VideoAvailabilityChanged(null, e);
}
public event EventHandler<PresenceInformationEventArgs> PresenceChanged = delegate { };
private void OnPresenceChanged(ContactAvailability presence)
{
PresenceChanged(null, new PresenceInformationEventArgs(presence));
}
public event EventHandler<StringValueInformationEventArgs> ActivityChanged = delegate { };
private void OnActivityChanged(string activity)
{
ActivityChanged(null, new StringValueInformationEventArgs(activity));
}
public event EventHandler<StringValueInformationEventArgs> DisplayNameChanged = delegate { };
private void OnDisplayNameChanged(string name)
{
DisplayNameChanged(null, new StringValueInformationEventArgs(name));
}
public event EventHandler<PhotoChangedEventArgs> PhotoChanged = delegate { };
private void OnPhotoChanged(BitmapImage image)
{
PhotoChanged(null, new PhotoChangedEventArgs(image));
}
#endregion
#region Properties
private ApplicationState _state = ApplicationState.SigningIn;
public ApplicationState State
{
get
{
return _state;
}
private set
{
_state = value;
OnStateChanged(_state);
}
}
private static ILyncModel _instance = null;
public static ILyncModel Instance
{
get
{
if (_instance == null)
_instance = new LyncModel();
return _instance;
}
}
#endregion
#region Constructor
private LyncModel()
{
// TODO - Catch instances where Lync signs in/out (e.g. network connectivity issues)
}
#endregion
#region Public Methods
public void SignIn(string userAtHost, string domainAndUsername, string password)
{
Lync.SignedIn += Lync_SignedIn;
Lync.SignInFailed += Lync_SignInFailed;
Lync.CallAccepted += Lync_CallAccepted;
Lync.VideoAvailabilityChanged += Lync_VideoAvailabilityChanged;
Lync.CallEnded += Lync_CallEnded;
Lync.CallDeclined += Lync_CallDeclined;
Lync.Initialize();
Lync.SignIn(userAtHost, domainAndUsername, password);
}
public void SubscribeForInformationUpdates(string sipUri)
{
_contact = Lync.LyncContactManager.GetContactByUri(sipUri);
_contact.ContactInformationChanged += contact_ContactInformationChanged;
var subscription = Lync.LyncContactManager.CreateSubscription();
subscription.AddContact(_contact);
List<ContactInformationType> informationItems = new List<ContactInformationType>();
informationItems.Add(ContactInformationType.Availability);
informationItems.Add(ContactInformationType.PersonalNote);
informationItems.Add(ContactInformationType.DisplayName);
informationItems.Add(ContactInformationType.Photo);
subscription.Subscribe(ContactSubscriptionRefreshRate.High, informationItems);
}
public void StartCall(string sipUri)
{
Lync.PlaceCall(sipUri);
State = ApplicationState.CallInProgress;
}
public void SignOut()
{
Lync.SignOut();
}
public void Shutdown()
{
Lync.ShutDown();
}
#endregion
#region Private Methods
private ContactAvailability GetPresence()
{
return (ContactAvailability)_contact.GetContactInformation(ContactInformationType.Availability);
}
private string GetPersonalNote()
{
return (string)_contact.GetContactInformation(ContactInformationType.PersonalNote);
}
private string GetDisplayName()
{
return (string)_contact.GetContactInformation(ContactInformationType.DisplayName);
}
private BitmapImage GetPhoto()
{
Stream photoStream;
try
{
photoStream = _contact.GetContactInformation(ContactInformationType.Photo) as Stream;
}
catch (NotReadyException)
{
//TODO: is it really true that Lync returns from GetContactInformation before it is ready to show the photo stream?
System.Threading.Thread.Sleep(1000);
photoStream = _contact.GetContactInformation(ContactInformationType.Photo) as Stream;
}
catch (ItemNotFoundException)
{
//no picture available
return null;
}
BitmapImage userImageBitMap = new BitmapImage();
if (photoStream != null)
{
userImageBitMap.BeginInit();
userImageBitMap.StreamSource = photoStream;
userImageBitMap.EndInit();
userImageBitMap.Freeze();
return userImageBitMap;
}
return null;
}
#endregion
#region Lync Event Handlers
void Lync_SignedIn(object sender, EventArgs e)
{
State = ApplicationState.NoCall;
}
private void Lync_SignInFailed(object sender, EventArgs e)
{
State = ApplicationState.SignInFailed;
}
void Lync_CallAccepted(object sender, EventArgs e)
{
State = ApplicationState.CallInProgress;
}
void Lync_VideoAvailabilityChanged(object sender, VideoAvailabilityChangedEventArgs e)
{
OnVideoAvailabilityChanged(e);
}
void Lync_CallEnded(object sender, EventArgs e)
{
State = ApplicationState.NoCall;
}
void Lync_CallDeclined(object sender, EventArgs e)
{
State = ApplicationState.NoCall;
}
void contact_ContactInformationChanged(object sender, ContactInformationChangedEventArgs e)
{
if (e.ChangedContactInformation.Contains(ContactInformationType.Availability))
{
OnPresenceChanged(GetPresence());
}
if (e.ChangedContactInformation.Contains(ContactInformationType.PersonalNote))
{
OnActivityChanged(GetPersonalNote());
}
if (e.ChangedContactInformation.Contains(ContactInformationType.DisplayName))
{
OnDisplayNameChanged(GetDisplayName());
}
if (e.ChangedContactInformation.Contains(ContactInformationType.Photo))
{
OnPhotoChanged(GetPhoto());
}
}
#endregion
}
}