forked from tomorgan/Lync-UISuppression-VideoAutoAnswer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
LyncModel.cs
177 lines (131 loc) · 4.73 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
using System;
using Microsoft.Lync.Model;
using System.Collections.Generic;
using System.Linq;
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));
}
#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 SubscribeForPresenceChange(string sipUri)
{
_contact = Lync.LyncContactManager.GetContactByUri(sipUri);
_contact.ContactInformationChanged += contact_ContactInformationChanged;
var subscription = Lync.LyncContactManager.CreateSubscription();
subscription.AddContact(_contact);
subscription.Subscribe(ContactSubscriptionRefreshRate.High, new List<ContactInformationType>() { ContactInformationType.Availability });
}
public void StartCall(string sipUri)
{
Lync.PlaceCall(sipUri);
State = ApplicationState.CallInProgress;
}
public void Dispose()
{
Lync.Dispose();
_instance = null;
}
#endregion
#region Private Methods
private ContactAvailability GetPresence()
{
return (ContactAvailability)_contact.GetContactInformation(ContactInformationType.Availability);
}
#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());
}
}
#endregion
}
}