-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.xaml.cs
383 lines (333 loc) · 11.3 KB
/
MainWindow.xaml.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
using SKYPE4COMLib;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace SkypeAPI2
{
/// <summary>
/// Interaction logic for MainWindow.xaml
///
///
/// </summary>
public partial class MainWindow : Window
{
private Skype skype = new Skype();
private SkypeContactList contactList;
private bool onlineOnly = true;
private Call currentCall;
public MainWindow()
{
//load theme from file
LoadTheme();
//standard WPF initialisation
InitializeComponent();
//any skype-specific startup logic (e.g. setting it to silent mode)
SetupSkype();
//create/update remaining UI components
InitialiseUI();
}
private void Window_Close(object sender, CancelEventArgs e)
{
//disable silent mode again
skype.SilentMode = false;
}
/// <summary>
/// Load theme from disk. This allows us to skin the app at runtime
/// </summary>
private void LoadTheme()
{
using (FileStream fs = new FileStream("theme/theme.xaml", FileMode.Open))
{
ResourceDictionary dic = (ResourceDictionary)XamlReader.Load(fs);
Resources.MergedDictionaries.Clear();
Resources.MergedDictionaries.Add(dic);
}
}
/// <summary>
/// Skype setup code.
/// </summary>
private void SetupSkype()
{
//set silent mode - this prevent skype popping up
skype.SilentMode = true;
//subscribe to any incoming messages, so that we can log them
skype.MessageStatus += skype_MessageStatus;
skype.Attach();
}
/// <summary>
/// Called when Skype receives an incoming message
/// </summary>
/// <param name="message"></param>
/// <param name="Status"></param>
void skype_MessageStatus(ChatMessage message, TChatMessageStatus Status)
{
AddMessage(message);
}
/// <summary>
/// UI initialisation
/// </summary>
private void InitialiseUI()
{
//attach handle to XAML contact list
contactList = (SkypeContactList) this.Resources["contactList"];
//query skype to populate current contact list & message history
PopulateContactList();
PopulateMessages();
}
/// <summary>
/// Query Skype to get current users contact list
/// </summary>
private void PopulateContactList()
{
//clear any existing items
contactList.Clear();
//populate contact list
foreach(User friend in skype.Friends)
contactList.Add(new SkypeContact(friend));
}
/// <summary>
/// Query skype for any message history
/// </summary>
private void PopulateMessages()
{
messages.Items.Clear();
foreach (ChatMessage msg in skype.MissedMessages)
AddMessage(msg);
}
/// <summary>
/// Add a message to the message list
/// </summary>
/// <param name="message"></param>
private void AddMessage(ChatMessage message)
{
string sent = message.Timestamp.ToShortTimeString();
string msgText = "[" + sent + "] " + message.FromHandle + ":" + message.Body;
//TODO - sort message list, so that latest message is always at the top
messages.Items.Add(msgText);
}
#region Contact List Actions
/// <summary>
/// When the user clicks on a contact in the list, open up the call menu
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void dataGrid1_MouseUp(object sender, MouseButtonEventArgs e)
{
ContactMenuPopup();
}
/// <summary>
/// Clicking on the icon in the header toggles the contact filter between all and online-only
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void columnHeader_Click(object sender, RoutedEventArgs e)
{
DataGridColumnHeader columnHeader = sender as DataGridColumnHeader;
if (columnHeader != null)
{
//toggle online only filter
if (columnHeader.DisplayIndex == 0)
{
onlineOnly = !onlineOnly;
RefreshContactList();
}
}
}
/// <summary>
/// Filter handling for the contact list
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void CollectionViewSource_Filter(object sender, FilterEventArgs e)
{
SkypeContact contact = e.Item as SkypeContact;
if (onlineOnly && contact.Online)
e.Accepted = true;
else if (!onlineOnly)
e.Accepted = true;
else
e.Accepted = false;
}
/// <summary>
/// Refresh view
/// </summary>
private void RefreshContactList()
{
//triggers the contact list to update
CollectionViewSource.GetDefaultView(dgContactList.ItemsSource).Refresh();
}
#endregion
#region Contact Popup menu
/// <summary>
/// Menu popup, when the user clicks a contact
/// </summary>
private void ContactMenuPopup()
{
//show menus
contactMenu.Visibility = Visibility.Visible;
//hide datagrid
dgContactList.Focusable = false;
dgContactList.IsHitTestVisible = false;
dgContactList.Opacity = 0.5;
}
/// <summary>
/// Hide popup menu, when the user clicks 'cancel'
/// </summary>
private void ContactMenuHide()
{
//hide menus
contactMenu.Visibility = Visibility.Hidden;
//show datagrid
dgContactList.Focusable = true;
dgContactList.IsHitTestVisible = true;
dgContactList.Opacity = 1;
}
/// <summary>
/// Click the call button - places a call through Skype
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Call_Click(object sender, RoutedEventArgs e)
{
SkypeContact contact = (SkypeContact)dgContactList.SelectedItem;
try
{
//if no current call, then place new call
if (currentCall == null)
{
currentCall = skype.PlaceCall(contact.Handle);
callMenu.Header = "Hang Up";
}
else
{
//if existing call, hang up
currentCall.Finish();
currentCall = null;
callMenu.Header = "Call";
ContactMenuHide();
}
}
catch (Exception ex)
{
MessageBox.Show("Error: " + ex.Message);
ContactMenuHide();
}
}
/// <summary>
/// Click to IM - TODO (depending on keyboard inteface)
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Chat_Click(object sender, RoutedEventArgs e)
{
ContactMenuHide();
}
/// <summary>
/// Click to cancel the popup menu
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Cancel_Click(object sender, RoutedEventArgs e)
{
ContactMenuHide();
}
#endregion
#region Search Page
/// <summary>
/// When one of the items from the search result is selected, pop up the search menu
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void SearchResult_Click(object sender, MouseButtonEventArgs e)
{
SearchMenuPopup();
}
/// <summary>
/// Menu popup, when the user clicks a contact
/// </summary>
private void SearchMenuPopup()
{
//show menus
searchMenu.Visibility = Visibility.Visible;
//hide datagrid
searchResult.Focusable = false;
searchResult.IsHitTestVisible = false;
searchResult.Opacity = 0.5;
}
/// <summary>
/// Hide popup menu, when the user clicks 'cancel'
/// </summary>
private void SearchMenuHide()
{
//hide menus
searchMenu.Visibility = Visibility.Hidden;
//show datagrid
searchResult.Focusable = true;
searchResult.IsHitTestVisible = true;
searchResult.Opacity = 1;
}
/// <summary>
/// Search for Skype users
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Search_Click(object sender, RoutedEventArgs e)
{
//clear any old result
searchResult.Items.Clear();
//add all the new search results
IUserCollection userResult = skype.SearchForUsers(searchText.Text);
foreach (User user in userResult)
{
searchResult.Items.Add(user.Handle);
}
}
/// <summary>
/// Add the user from the search results to your Skype contact list
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void AddSearch_Click(object sender, RoutedEventArgs e)
{
try
{
//TODO - check this is the right way to do it - seems the API has changed recently
//add the selected user to your skype contacts
string userHandle = searchResult.SelectedItem.ToString();
User user = skype.get_User(searchText.Text);
user.BuddyStatus = TBuddyStatus.budFriend;
}
catch (Exception ex)
{
MessageBox.Show("Error adding contact:" + ex.Message);
}
//hide the popup menu again
SearchMenuHide();
}
/// <summary>
/// Close the search menu on Cancel
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void CancelSearch_Click(object sender, RoutedEventArgs e)
{
SearchMenuHide();
}
#endregion
}
}