-
Notifications
You must be signed in to change notification settings - Fork 0
/
SkypePopDialog.cs
368 lines (327 loc) · 11.8 KB
/
SkypePopDialog.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
using System;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;
using SKYPE4COMLib;
using SlidingHost;
using Application = System.Windows.Forms.Application;
namespace SkypePop
{
public partial class SkypePopDialog : SlideDialog
{
#region Private members
private readonly Font _bold = new Font("Consolas", 11f, FontStyle.Bold);
private readonly Font _regular = new Font("Consolas", 11f, FontStyle.Regular);
private Skype _skype;
private ChatCollection _chats;
private Chat _currentChat;
private Color _color1 = ColorTranslator.FromHtml("#234177");
private Color _color2 = ColorTranslator.FromHtml("#514124");
private Timer _skypeReconnectTime;
#endregion
#region Constructor
public SkypePopDialog(Form poOwner, float pfStep)
: base(poOwner, pfStep)
{
try
{
InitializeComponent();
SlideDirection = (SkypePopSettings.Default.Side == Constants.SideRight) ? SLIDE_DIRECTION.LEFT : SLIDE_DIRECTION.RIGHT;
HookupMouseEnterLeaveEvents(this);
Updater.CheckUpdates();
_skypeReconnectTime = new Timer {Interval = 30000};
_skypeReconnectTime.Tick += _skypeReconnectTime_Tick;
}
catch (Exception ex)
{
Utility.HandleException(ex);
}
}
#endregion
#region Event handlers
void SkypePopDialog_AttachmentStatus(TAttachmentStatus Status)
{
if (Status == TAttachmentStatus.apiAttachNotAvailable)
{
InitSkype();
}
}
private void skype_MessageStatus(ChatMessage msg, TChatMessageStatus status)
{
try
{
if (status == TChatMessageStatus.cmsReceived)
{
var chatName = string.Empty;
if (msg.Chat.Members.Count > 2)
chatName = "[" + GetChatName(msg.Chat.FriendlyName) + "]";
AppendMessage(msg.FromDisplayName, chatName, msg.Body);
if (_skype.CurrentUserStatus != TUserStatus.cusDoNotDisturb)
{
Slide(true);
}
int index = IndexOf(msg.Chat);
if (index == -1)
{
RefreshActiveChats();
}
}
}
catch (Exception ex)
{
Utility.HandleException(ex);
}
}
private void btnHide_Click(object sender, EventArgs e)
{
try
{
context.Show(MousePosition.X, MousePosition.Y);
}
catch (Exception ex)
{
Utility.HandleException(ex);
}
}
private void btnClose_Click(object sender, EventArgs e)
{
try
{
Slide(false);
}
catch (Exception ex)
{
Utility.HandleException(ex);
}
}
private void cmbActiveChats_SelectedIndexChanged(object sender, EventArgs e)
{
try
{
SetSelected(cmbActiveChats.Text);
txtSend.Focus();
}
catch (Exception ex)
{
Utility.HandleException(ex);
}
}
private void txtSend_KeyDown(object sender, KeyEventArgs e)
{
try
{
if (_currentChat != null &&
!string.IsNullOrEmpty(txtSend.Text) &&
e.KeyValue == 13)
{
_currentChat.SendMessage(txtSend.Text);
AppendMessage("Me", txtSend.Text);
txtSend.Text = string.Empty;
}
}
catch (Exception ex)
{
Utility.HandleException(ex);
}
}
private void txtSend_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 13)
{
e.Handled = true;
}
}
private void miExit_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void miSettings_Click(object sender, EventArgs e)
{
try
{
var settings = new Settings();
settings.ShowDialog(this);
}
catch (Exception ex)
{
Utility.HandleException(ex);
}
}
#endregion
#region Private methods
private void AppendMessage(string from, string message)
{
txtReceived.SelectionColor = _color1;
txtReceived.SelectionFont = _bold;
txtReceived.AppendText(from + " : ");
txtReceived.SelectionFont = _regular;
txtReceived.SelectionColor = _color2;
txtReceived.AppendText(message + Environment.NewLine);
txtReceived.ScrollToCaret();
}
private void AppendMessage(string from, string chatName, string message)
{
txtReceived.SelectionColor = _color1;
txtReceived.SelectionFont = _bold;
txtReceived.AppendText(from + chatName + Environment.NewLine);
txtReceived.SelectionFont = _regular;
txtReceived.SelectionColor = _color2;
txtReceived.AppendText(message + Environment.NewLine);
txtReceived.ScrollToCaret();
}
private void SetSelected(string chatNameToSelect)
{
var found = false;
for (int i = 1; i < _chats.Count + 1; i++)
{
string chatName = GetChatName(_chats[i].FriendlyName);
if (chatName == chatNameToSelect)
{
cmbActiveChats.SelectedIndex = i - 1;
_currentChat = _chats[i];
found = true;
break;
}
}
if (found)
CueProvider.SetCue(txtSend, "Type a message to " + GetChatName(_currentChat.FriendlyName) + " here");
else
CueProvider.SetCue(txtSend, "No active chats are running");
if (!found && _chats.Count > 0)
{
cmbActiveChats.SelectedIndex = 0;
_currentChat = _chats[1];
}
FocusOut();
}
public void FocusOut()
{
btnHide.Focus();
}
private void RefreshActiveChats()
{
var currentlySelected = cmbActiveChats.Text;
cmbActiveChats.Items.Clear();
for (int i = 1; i < _chats.Count + 1; i++)
{
string chatName = GetChatName(_chats[i].FriendlyName);
cmbActiveChats.Items.Add(chatName);
if (chatName == currentlySelected)
{
cmbActiveChats.SelectedIndex = i - 1;
}
}
SetSelected(currentlySelected);
}
#endregion
#region Handling focus
protected override bool IsInFocus()
{
return txtSend.Focused || base.IsInFocus();
}
private void HookupMouseEnterLeaveEvents(Control control)
{
context.MouseEnter += new EventHandler(childControl_MouseEnter);
context.MouseLeave += new EventHandler(childControl_MouseLeave);
foreach (Control childControl in control.Controls)
{
childControl.MouseEnter += new EventHandler(childControl_MouseEnter);
childControl.MouseLeave += new EventHandler(childControl_MouseLeave);
// Recurse on this child to get all of its descendents.
HookupMouseEnterLeaveEvents(childControl);
}
}
private void childControl_MouseLeave(object sender, EventArgs e)
{
IsMouseOver = false;
}
private void childControl_MouseEnter(object sender, EventArgs e)
{
IsMouseOver = true;
}
#endregion
#region Skype utility methods
public bool InitSkype()
{
try
{
if (IsProcessOpen("Skype"))
{
_skype = new Skype();
// Use skype protocol version 7
_skype.Attach(7, false);
// Listen
_skype.MessageStatus += skype_MessageStatus;
((_ISkypeEvents_Event)_skype).AttachmentStatus += new _ISkypeEvents_AttachmentStatusEventHandler(SkypePopDialog_AttachmentStatus);
_chats = _skype.ActiveChats;
RefreshActiveChats();
if (_chats.Count > 0)
{
cmbActiveChats.SelectedIndex = 0;
}
return true;
}
else
{
CueProvider.SetCue(txtSend, "Waiting untill Skype starts... ");
_skypeReconnectTime.Enabled = true;
return false;
}
}
catch(Exception ex)
{
CueProvider.SetCue(txtSend, "Waiting untill Skype starts... ");
_skypeReconnectTime.Enabled = true;
return false;
}
}
void _skypeReconnectTime_Tick(object sender, EventArgs e)
{
if(InitSkype())
_skypeReconnectTime.Enabled = false;
}
public bool IsProcessOpen(string name)
{
//here we're going to get a list of all running processes on
//the computer
foreach (Process clsProcess in Process.GetProcesses())
{
//now we're going to see if any of the running processes
//match the currently running processes. Be sure to not
//add the .exe to the name you provide, i.e: NOTEPAD,
//not NOTEPAD.EXE or false is always returned even if
//notepad is running.
//Remember, if you have the process running more than once,
//say IE open 4 times the loop thr way it is now will close all 4,
//if you want it to just close the first one it finds
//then add a return; after the Kill
if (clsProcess.ProcessName== name)
{
//if the process is found to be running then we
//return a true
return true;
}
}
//otherwise we return a false
return false;
}
private int IndexOf(Chat newChat)
{
int index = -1;
for (int i = 1; i < _chats.Count + 1; i++)
{
if (_chats[i].Name == newChat.Name)
return i - 1;
}
_chats = _skype.ActiveChats;
return index;
}
private string GetChatName(string chatName)
{
if (chatName.IndexOf("|") > 0)
chatName = chatName.Substring(0, chatName.IndexOf("|") - 1);
return chatName;
}
#endregion
}
}