summaryrefslogtreecommitdiff
path: root/plugins/VoiceService/src/VoiceCall.cpp
blob: c6f579f2d46cbab102e5744e0b312bb015a5d1ae (plain)
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
/*
Copyright (C) 2006 Ricardo Pescuma Domenecci

This is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.

This is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Library General Public License for more details.

You should have received a copy of the GNU Library General Public
License along with this file; see the file license.txt.  If
not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
*/

#include "stdafx.h"

static wchar_t *popupTitles[] = {
	LPGENW("Voice call started"),
	LPGENW("Voice call ringing"),
	LPGENW("Voice call"),
	LPGENW("Voice call on hold"),
	LPGENW("Voice call ended"),
	LPGENW("Voice call busy"),
};

static wchar_t *stateTexts[] = {
	LPGENW("Call from %s has started"),
	LPGENW("Call from %s is ringing"),
	LPGENW("Calling %s"),
	LPGENW("Call from %s is on hold"),
	LPGENW("Call from %s has ended"),
	LPGENW("%s is busy"),
};

/////////////////////////////////////////////////////////////////////////////////////////
// VoiceCall class members

VoiceCall::VoiceCall(VoiceProvider *module, const char *id)	: 
	CDlgBase(g_plugin, IDD_VOICECALL),
	module(module),
	id(mir_strdup(id)), 
	m_btnAnswer(this, IDC_ANSWERBTN),
	m_btnDrop(this, IDC_DROPBTN),
	m_lblStatus(this, IDC_STATUS),
	m_calltimer(this, 1),
	m_lblContactName(this, IDC_DESCR),
	m_lblAddress(this, IDC_ADDRESS)
{
	hContact = NULL;
	name[0] = 0;
	number[0] = 0;
	displayName[0] = 0;
	state = -1;
	end_time = 0;
	clistBlinking = false;
	incoming = false;
	secure = false;
	hwnd = NULL;
	m_nsec = 0;

	m_btnAnswer.OnClick = Callback(this, &VoiceCall::OnCommand_Answer);
	m_btnDrop.OnClick = Callback(this, &VoiceCall::OnCommand_Drop);
	m_calltimer.OnEvent = Callback(this, &VoiceCall::OnCallTimer);

	CreateDisplayName();
	Show(SW_SHOWNOACTIVATE);
}

VoiceCall::~VoiceCall()
{
	RemoveNotifications();
	mir_free(id);
	id = NULL;
	DeleteObject(hContactNameFont);
}

bool VoiceCall::OnInitDialog()
{
	Button_SetIcon_IcoLib(m_hwnd, IDC_DROPBTN, g_plugin.getIconHandle(IDI_ACTION_DROP));
	Button_SetIcon_IcoLib(m_hwnd, IDC_ANSWERBTN, g_plugin.getIconHandle(IDI_ACTION_ANSWER));

	HFONT hf = (HFONT) SendMessage(m_lblContactName.GetHwnd(), WM_GETFONT, 0,0);
	LOGFONT lf;
	GetObject(hf, sizeof(LOGFONT), &lf);
	lf.lfWeight = FW_BOLD;
	hContactNameFont = CreateFontIndirect(&lf);
	SendMessage(m_lblContactName.GetHwnd(), WM_SETFONT, (WPARAM)hContactNameFont, 0);

	return true;
}

bool VoiceCall::OnClose()
{
	Button_FreeIcon_IcoLib(m_hwnd, IDC_DROPBTN);
	Button_FreeIcon_IcoLib(m_hwnd, IDC_ANSWERBTN);

	calls.remove(this);
	return false;
}

void VoiceCall::OnCommand_Answer(CCtrlButton *)
{
	Answer();
}

void VoiceCall::OnCommand_Drop(CCtrlButton *)
{
	if (state == VOICE_STATE_ENDED || state == VOICE_STATE_BUSY)
		Close();
	else
		Drop();
}

void VoiceCall::OnCallTimer(CTimer *)
{
	m_nsec++;
	wchar_t text[256], hrs[11];
	hrs[0] = 0;
	int mins = m_nsec / 60;
	int hours = m_nsec / 3600;
	if (hours)
		mir_snwprintf(hrs, _countof(hrs), L"%d:", hours);
	mir_snwprintf(text, _countof(text), L"%s%d:%02d", hrs, (mins) % 60, m_nsec % 60);
	m_lblStatus.SetText(text);
}

void VoiceCall::AppendCallerID(MCONTACT aHContact, const wchar_t *aName, const wchar_t *aNumber)
{
	bool changed = false;

	if (aHContact != NULL) {
		hContact = aHContact;
		changed = true;

		AVATARCACHEENTRY *ace = (AVATARCACHEENTRY *)CallService(MS_AV_GETAVATARBITMAP, hContact, 0);
		if (ace && (INT_PTR)ace != CALLSERVICE_NOTFOUND && (ace->dwFlags & AVS_BITMAP_VALID))
			SendMessage(GetDlgItem(m_hwnd, IDC_AVATAR), STM_SETIMAGE, (WPARAM)IMAGE_BITMAP, (LPARAM)ace->hbmPic);
	}

	if (!IsEmptyW(aName)) {
		lstrcpyn(name, aName, _countof(name));
		changed = true;
	}

	if (!IsEmptyW(aNumber)) {
		lstrcpyn(number, aNumber, _countof(number));
		changed = true;
	}

	if (changed)
		CreateDisplayName();
}

void VoiceCall::CreateDisplayName()
{
	wchar_t *contact = NULL;
	if (hContact != NULL)
		contact = Clist_GetContactDisplayName(hContact, 0);

	wchar_t *nameTmp = NULL;
	if (lstrcmp(name, number) != 0)
		nameTmp = name;

	if (contact != NULL) {
		if (!IsEmptyW(number))
			mir_snwprintf(displayName, _T("%s <%s>"), contact, number);
		else
			wcsncpy_s(displayName, contact, _TRUNCATE);
	}
	else if (!IsEmptyW(nameTmp) && !IsEmptyW(number)) {
		mir_snwprintf(displayName, _T("%s <%s>"), name, number);
	}
	else if (!IsEmptyW(nameTmp)) {
		wcsncpy_s(displayName, name, _TRUNCATE);
	}
	else if (!IsEmptyW(number)) {
		wcsncpy_s(displayName, number, _TRUNCATE);
	}
	else {
		wcsncpy_s(displayName, TranslateT("Unknown number"), _TRUNCATE);
	}

	if (contact)
		m_lblContactName.SetText(contact);
	if (number[0])
		m_lblAddress.SetText(number);
}

void VoiceCall::RemoveNotifications()
{
	if (hwnd != NULL) {
		DestroyWindow(hwnd);
		hwnd = NULL;
	}
/*
	if (clistBlinking) {
		g_clistApi.pfnRemoveEvent(hContact, MEVENT(0xBABABEDA));
		clistBlinking = false;
	}*/
}

void VoiceCall::SetState(int aState)
{
	if (state == aState)
		return;
	state = aState;

	RemoveNotifications();

	switch (state) {
	case VOICE_STATE_TALKING:
		m_calltimer.Start(1000);
		m_lblStatus.SetText(L"0:00");
		m_btnAnswer.Enable(false);
		break;
	case VOICE_STATE_RINGING:
		incoming = true;
		SetCaption(TranslateT("Incoming call"));
		m_btnAnswer.Enable(true);
		m_lblStatus.SetText(TranslateT("Ringing"));
		SetWindowPos(GetHwnd(), HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);
		SetWindowPos(GetHwnd(), HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);
		break;
	case VOICE_STATE_CALLING:
		incoming = false;
		SetCaption(TranslateT("Outgoing call"));
		m_lblStatus.SetText(TranslateT("Calling"));
		m_btnAnswer.Enable(false);
		break;
	case VOICE_STATE_ON_HOLD:
		m_lblStatus.SetText(TranslateT("Holded"));
		m_btnAnswer.Enable(true);
		m_btnAnswer.SetText(TranslateT("Unhold"));
		break;
	case VOICE_STATE_ENDED:
		m_calltimer.Stop();
		m_lblStatus.SetText(TranslateT("Call ended"));
		m_btnAnswer.Enable(false);
		m_btnDrop.SetText(TranslateT("Close"));
		break;
	case VOICE_STATE_BUSY:
		m_lblStatus.SetText(TranslateT("Busy"));
		m_btnAnswer.Enable(false);
		m_btnDrop.SetText(TranslateT("Close"));
		break;
	default:
		m_lblStatus.SetText(TranslateT("Unknown state"));
		break;
	}

	if (state != VOICE_STATE_ON_HOLD)
		m_nsec = 0;

	if (IsFinished()) {
		if (end_time == 0)
			end_time = GetTickCount();

		// Remove id because providers can re-use them
		mir_free(id);
		id = NULL;
	}

	Notify();
}

void VoiceCall::Notify(bool popup, bool sound, bool /*clist*/)
{
	if (popup) {
		wchar_t text[512];
		mir_snwprintf(text, _countof(text), TranslateW(stateTexts[state]), displayName);

		ShowPopup(NULL, TranslateW(popupTitles[state]), text);
	}

	if (sound)
		Skin_PlaySound(g_sounds[state].szName);
/*
	if (clist && state == VOICE_STATE_RINGING) {
		CLISTEVENT ce = {};
		ce.hContact = hContact;
		ce.hIcon = g_plugin.getIcon(IDI_RINGING);
		ce.hDbEvent = 1001;
		ce.pszService = MS_VOICESERVICE_CLIST_DBLCLK;
		ce.lParam = (LPARAM)this;
		g_clistApi.pfnAddEvent(&ce);

		IcoLib_ReleaseIcon(ce.hIcon);

		clistBlinking = true;
	}*/
}

bool VoiceCall::IsFinished()
{
	return IsFinalState(state);
}

bool VoiceCall::CanDrop()
{
	return !IsFinished();
}

void VoiceCall::Drop()
{
	if (!CanDrop())
		return;

	RemoveNotifications();

	CallProtoService(module->name, PS_VOICE_DROPCALL, (WPARAM)id, 0);
}

bool VoiceCall::CanAnswer()
{
	return state == -1 || state == VOICE_STATE_RINGING || state == VOICE_STATE_ON_HOLD;
}

void VoiceCall::Answer()
{
	if (!CanAnswer())
		return;

	RemoveNotifications();

	CallProtoService(module->name, PS_VOICE_ANSWERCALL, (WPARAM)id, 0);
}

bool VoiceCall::CanHold()
{
	return module->CanHold() && (state == -1 || state == VOICE_STATE_TALKING);
}

void VoiceCall::Hold()
{
	if (!CanHold())
		return;

	RemoveNotifications();

	CallProtoService(module->name, PS_VOICE_HOLDCALL, (WPARAM)id, 0);
}

bool VoiceCall::CanSendDTMF()
{
	return module->CanSendDTMF() && state == VOICE_STATE_TALKING;
}

void VoiceCall::SendDTMF(wchar_t c)
{
	if (!CanSendDTMF())
		return;

	CallProtoService(module->name, PS_VOICE_SEND_DTMF, (WPARAM)id, (LPARAM)c);
}

void VoiceCall::SetNewCallHWND(HWND _h)
{
	if (_h != NULL)
		RemoveNotifications();

	hwnd = _h;
}