Skip to content
Newer
Older
100644 278 lines (211 sloc) 8.27 KB
f2f56b0 ShutDown, SignOut, Copyright
Tom Morgan authored Aug 10, 2012
1 /* Copyright (C) 2012 Modality Systems - All Rights Reserved
2 * You may use, distribute and modify this code under the
3 * terms of the Microsoft Public License, a copy of which
4 * can be seen at: http://www.microsoft.com/en-us/openness/licenses.aspx
5 *
6 * http://www.LyncAutoAnswer.com
7 */
8
9 using System;
befc72e Added Initial Code
Tom Morgan authored Jul 27, 2012
10 using System.Collections.Generic;
2aec6f1 Code Tidy
Tom Morgan authored Aug 1, 2012
11 using Microsoft.Lync.Model;
5eb3062 Added Photo and DisplayName properties
Tom Morgan authored Aug 2, 2012
12 using System.IO;
13 using System.Windows.Media.Imaging;
14
befc72e Added Initial Code
Tom Morgan authored Jul 27, 2012
15
16 namespace LyncUISupressionWrapper
17 {
18 public class LyncModel : ILyncModel
19 {
20 #region Fields
21
22 Contact _contact;
23
24 #endregion
25
26 #region Events
27
28 public event EventHandler<StateChangedEventArgs> StateChanged = delegate { };
29 private void OnStateChanged(ApplicationState pState)
30 {
31 StateChanged(this, new StateChangedEventArgs(pState));
32 }
33
34 public event EventHandler<VideoAvailabilityChangedEventArgs> VideoAvailabilityChanged = delegate { };
35 private void OnVideoAvailabilityChanged(VideoAvailabilityChangedEventArgs e)
36 {
37 VideoAvailabilityChanged(null, e);
38 }
39
40 public event EventHandler<PresenceInformationEventArgs> PresenceChanged = delegate { };
41 private void OnPresenceChanged(ContactAvailability presence)
42 {
43 PresenceChanged(null, new PresenceInformationEventArgs(presence));
44 }
45
5b48bc9 refactor of the way contact information is passed back
Tom Morgan authored Aug 2, 2012
46 public event EventHandler<StringValueInformationEventArgs> ActivityChanged = delegate { };
47 private void OnActivityChanged(string activity)
48 {
49 ActivityChanged(null, new StringValueInformationEventArgs(activity));
50 }
51
52 public event EventHandler<StringValueInformationEventArgs> DisplayNameChanged = delegate { };
53 private void OnDisplayNameChanged(string name)
54 {
55 DisplayNameChanged(null, new StringValueInformationEventArgs(name));
56 }
57
58 public event EventHandler<PhotoChangedEventArgs> PhotoChanged = delegate { };
59 private void OnPhotoChanged(BitmapImage image)
60 {
61 PhotoChanged(null, new PhotoChangedEventArgs(image));
62 }
63
64
befc72e Added Initial Code
Tom Morgan authored Jul 27, 2012
65 #endregion
66
67 #region Properties
68
69 private ApplicationState _state = ApplicationState.SigningIn;
70 public ApplicationState State
71 {
72 get
73 {
74 return _state;
75 }
76 private set
77 {
78 _state = value;
79 OnStateChanged(_state);
80 }
81 }
82
83 private static ILyncModel _instance = null;
84 public static ILyncModel Instance
85 {
86 get
87 {
88 if (_instance == null)
89 _instance = new LyncModel();
90
91 return _instance;
92 }
93 }
94
95 #endregion
96
97 #region Constructor
98
99 private LyncModel()
100 {
101 // TODO - Catch instances where Lync signs in/out (e.g. network connectivity issues)
102 }
103
104 #endregion
105
106 #region Public Methods
107
108 public void SignIn(string userAtHost, string domainAndUsername, string password)
109 {
110 Lync.SignedIn += Lync_SignedIn;
111 Lync.SignInFailed += Lync_SignInFailed;
112 Lync.CallAccepted += Lync_CallAccepted;
113 Lync.VideoAvailabilityChanged += Lync_VideoAvailabilityChanged;
114 Lync.CallEnded += Lync_CallEnded;
115 Lync.CallDeclined += Lync_CallDeclined;
116
117 Lync.Initialize();
118
119 Lync.SignIn(userAtHost, domainAndUsername, password);
120 }
121
5b48bc9 refactor of the way contact information is passed back
Tom Morgan authored Aug 2, 2012
122 public void SubscribeForInformationUpdates(string sipUri)
befc72e Added Initial Code
Tom Morgan authored Jul 27, 2012
123 {
124 _contact = Lync.LyncContactManager.GetContactByUri(sipUri);
125 _contact.ContactInformationChanged += contact_ContactInformationChanged;
5b48bc9 refactor of the way contact information is passed back
Tom Morgan authored Aug 2, 2012
126
befc72e Added Initial Code
Tom Morgan authored Jul 27, 2012
127 var subscription = Lync.LyncContactManager.CreateSubscription();
128 subscription.AddContact(_contact);
5eb3062 Added Photo and DisplayName properties
Tom Morgan authored Aug 2, 2012
129
5b48bc9 refactor of the way contact information is passed back
Tom Morgan authored Aug 2, 2012
130 List<ContactInformationType> informationItems = new List<ContactInformationType>();
131 informationItems.Add(ContactInformationType.Availability);
132 informationItems.Add(ContactInformationType.PersonalNote);
133 informationItems.Add(ContactInformationType.DisplayName);
134 informationItems.Add(ContactInformationType.Photo);
5eb3062 Added Photo and DisplayName properties
Tom Morgan authored Aug 2, 2012
135
5b48bc9 refactor of the way contact information is passed back
Tom Morgan authored Aug 2, 2012
136 subscription.Subscribe(ContactSubscriptionRefreshRate.High, informationItems);
5eb3062 Added Photo and DisplayName properties
Tom Morgan authored Aug 2, 2012
137 }
138
5b48bc9 refactor of the way contact information is passed back
Tom Morgan authored Aug 2, 2012
139
befc72e Added Initial Code
Tom Morgan authored Jul 27, 2012
140 public void StartCall(string sipUri)
141 {
142 Lync.PlaceCall(sipUri);
143 State = ApplicationState.CallInProgress;
144 }
145
f2f56b0 ShutDown, SignOut, Copyright
Tom Morgan authored Aug 10, 2012
146
147
148 public void SignOut()
149 {
150 Lync.SignOut();
151 }
152
153 public void Shutdown()
befc72e Added Initial Code
Tom Morgan authored Jul 27, 2012
154 {
f2f56b0 ShutDown, SignOut, Copyright
Tom Morgan authored Aug 10, 2012
155 Lync.ShutDown();
befc72e Added Initial Code
Tom Morgan authored Jul 27, 2012
156 }
157
158 #endregion
159
160 #region Private Methods
161
162 private ContactAvailability GetPresence()
163 {
164 return (ContactAvailability)_contact.GetContactInformation(ContactInformationType.Availability);
165 }
166
5b48bc9 refactor of the way contact information is passed back
Tom Morgan authored Aug 2, 2012
167 private string GetPersonalNote()
168 {
169 return (string)_contact.GetContactInformation(ContactInformationType.PersonalNote);
170 }
171
172 private string GetDisplayName()
173 {
174 return (string)_contact.GetContactInformation(ContactInformationType.DisplayName);
175 }
176
177 private BitmapImage GetPhoto()
178 {
179 Stream photoStream;
180
181 try
182 {
183 photoStream = _contact.GetContactInformation(ContactInformationType.Photo) as Stream;
184 }
185 catch (NotReadyException)
186 {
187 //TODO: is it really true that Lync returns from GetContactInformation before it is ready to show the photo stream?
188 System.Threading.Thread.Sleep(1000);
189 photoStream = _contact.GetContactInformation(ContactInformationType.Photo) as Stream;
190 }
191 catch (ItemNotFoundException)
192 {
193 //no picture available
194 return null;
195 }
196
197
198 BitmapImage userImageBitMap = new BitmapImage();
199
200 if (photoStream != null)
201 {
202 userImageBitMap.BeginInit();
203 userImageBitMap.StreamSource = photoStream;
204 userImageBitMap.EndInit();
205 userImageBitMap.Freeze();
206 return userImageBitMap;
207 }
208 return null;
209
210 }
211
befc72e Added Initial Code
Tom Morgan authored Jul 27, 2012
212 #endregion
213
214 #region Lync Event Handlers
215
216 void Lync_SignedIn(object sender, EventArgs e)
217 {
218 State = ApplicationState.NoCall;
219 }
220
221 private void Lync_SignInFailed(object sender, EventArgs e)
222 {
223 State = ApplicationState.SignInFailed;
224 }
225
226 void Lync_CallAccepted(object sender, EventArgs e)
227 {
228 State = ApplicationState.CallInProgress;
229 }
230
231 void Lync_VideoAvailabilityChanged(object sender, VideoAvailabilityChangedEventArgs e)
232 {
233 OnVideoAvailabilityChanged(e);
234 }
235
236 void Lync_CallEnded(object sender, EventArgs e)
237 {
238 State = ApplicationState.NoCall;
239 }
240
241 void Lync_CallDeclined(object sender, EventArgs e)
242 {
243 State = ApplicationState.NoCall;
244 }
245
246
247 void contact_ContactInformationChanged(object sender, ContactInformationChangedEventArgs e)
248 {
249 if (e.ChangedContactInformation.Contains(ContactInformationType.Availability))
250 {
251 OnPresenceChanged(GetPresence());
252 }
5b48bc9 refactor of the way contact information is passed back
Tom Morgan authored Aug 2, 2012
253 if (e.ChangedContactInformation.Contains(ContactInformationType.PersonalNote))
254 {
255 OnActivityChanged(GetPersonalNote());
256 }
257 if (e.ChangedContactInformation.Contains(ContactInformationType.DisplayName))
258 {
259 OnDisplayNameChanged(GetDisplayName());
260 }
261 if (e.ChangedContactInformation.Contains(ContactInformationType.Photo))
262 {
263 OnPhotoChanged(GetPhoto());
264 }
befc72e Added Initial Code
Tom Morgan authored Jul 27, 2012
265 }
266
267
268
269
270 #endregion
271
272
273
274
275 }
276
277 }
Something went wrong with that request. Please try again.