summaryrefslogtreecommitdiff
path: root/plugins/VoiceService/src/hooks.cpp
blob: fb03ef57cce18772b6cf2c6b86a3e7ee3de5360b (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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
/*
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 IconItem mainIcons[] = {
	{ LPGEN("Main"),     "main",    IDI_MAIN    },
	{ LPGEN("Dialpad"),  "dialpad", IDI_DIALPAD },
	{ LPGEN("Secure"),   "secure",  IDI_SECURE  },
};

static IconItem stateIcons[NUM_STATES] = {
	{ LPGEN("Talking"),  "talking", IDI_TALKING },
	{ LPGEN("Ringing"),  "ringing", IDI_RINGING },
	{ LPGEN("Calling"),  "calling", IDI_CALLING },
	{ LPGEN("On Hold"),  "onhold",  IDI_ON_HOLD },
	{ LPGEN("Ended"),    "ended",   IDI_ENDED   },
	{ LPGEN("Busy"),     "busy", 	  IDI_BUSY    },
	{ LPGEN("Ready"),     "ready", 	  IDI_BUSY    },
};

static IconItem actionIcons[] = {
	{ LPGEN("Make Voice Call"),   "call",   IDI_ACTION_CALL   },
	{ LPGEN("Answer Voice Call"), "answer", IDI_ACTION_ANSWER },
	{ LPGEN("Hold Voice Call"),   "hold",   IDI_ACTION_HOLD   },
	{ LPGEN("Drop Voice Call"),   "drop",   IDI_ACTION_DROP   },
};

SoundDescr g_sounds[NUM_STATES] = {
	{ "voice_started", LPGENW("Started talking") },
	{ "voice_ringing", LPGENW("Ringing") },
	{ "voice_calling", LPGENW("Calling a contact") },
	{ "voice_holded",  LPGENW("Put a call on Hold") },
	{ "voice_ended",   LPGENW("End of call") },
	{ "voice_busy",    LPGENW("Busy signal") },
	{ "voice_ready",   LPGENW("Ready") },
};

/////////////////////////////////////////////////////////////////////////////////////////

static vector<HGENMENU> hCMCalls;
static HGENMENU hCMCall = NULL;
static HGENMENU hCMAnswer = NULL;
static HGENMENU hCMDrop = NULL;
static HGENMENU hCMHold = NULL;

OBJLIST<VoiceProvider> modules(1, PtrKeySortT);
OBJLIST<VoiceCall> calls(1, PtrKeySortT);

static INT_PTR CListDblClick(WPARAM wParam, LPARAM lParam);

static INT_PTR Service_CanCall(WPARAM wParam, LPARAM lParam);
static INT_PTR Service_Call(WPARAM wParam, LPARAM lParam);
static INT_PTR CMAnswer(WPARAM wParam, LPARAM lParam);
static INT_PTR CMHold(WPARAM wParam, LPARAM lParam);
static INT_PTR CMDrop(WPARAM wParam, LPARAM lParam);

class CallingMethod
{
public:
	VoiceProvider *provider;
	MCONTACT hContact;
	wchar_t number[128];

	CallingMethod(VoiceProvider *provider, MCONTACT hContact, const wchar_t *number = NULL)
		: provider(provider), hContact(hContact)
	{
		if (number == NULL)
			this->number[0] = 0;
		else
			wcsncpy_s(this->number, number, _TRUNCATE);
	}

	void Call()
	{
		provider->Call(hContact, number);
	}
};

static int sttCompareCallingMethods(const CallingMethod *p1, const CallingMethod *p2)
{
	if (p1->hContact != p2->hContact)
		return (int)p2->hContact - (int)p1->hContact;

	BOOL noNum1 = (IsEmptyW(p1->number) ? 1 : 0);
	BOOL noNum2 = (IsEmptyW(p2->number) ? 1 : 0);
	if (noNum1 != noNum2)
		return noNum2 - noNum1;

	if (!noNum1) {
		int numDif = lstrcmp(p1->number, p2->number);
		if (numDif != 0)
			return numDif;
	}

	BOOL isProto1 = Proto_IsProtoOnContact(p1->hContact, p1->provider->name);
	BOOL isProto2 = Proto_IsProtoOnContact(p2->hContact, p2->provider->name);
	if (isProto1 != isProto2)
		return isProto2 - isProto1;

	return lstrcmp(p1->provider->description, p2->provider->description);
}

static void AddMethodsFrom(OBJLIST<CallingMethod> *list, MCONTACT hContact)
{
	for (auto &provider: modules)
		if (provider->CanCall(hContact))
			list->insert(new CallingMethod(provider, hContact));
}

static void AddMethodsFrom(OBJLIST<CallingMethod> *list, MCONTACT hContact, const wchar_t *number)
{
	for (auto &provider: modules)
		if (provider->CanCall(number))
			list->insert(new CallingMethod(provider, hContact, number));
}

static void BuildCallingMethodsList(OBJLIST<CallingMethod> *list, MCONTACT hContact)
{
	AddMethodsFrom(list, hContact);

	// Fetch contact number
	char *proto = Proto_GetBaseAccountName(hContact);

	CMStringW protoNumber(db_get_wsm(hContact, proto, "Number"));
	if (!protoNumber.IsEmpty())
		AddMethodsFrom(list, hContact, protoNumber);

	for (int i = 0; ; i++) {
		char tmp[128];
		mir_snprintf(tmp, "MyPhone%d", i);

		CMStringW number(db_get_wsm(hContact, "UserInfo", tmp));
		if (!number.IsEmpty())
			AddMethodsFrom(list, hContact, number);

		if (number.IsEmpty() && i >= 4)
			break;
	}
}

// Functions ////////////////////////////////////////////////////////////////////////////

static MCONTACT ConvertMetacontact(MCONTACT hContact)
{
	MCONTACT hTmp = db_mc_getMostOnline(hContact);
	if (hTmp != NULL)
		return hTmp;

	return hContact;
}

static void AddAccount(PROTOACCOUNT *acc)
{
	if (!acc->IsEnabled())
		return;
	if (IsEmptyA(acc->szModuleName))
		return;
	if (!ProtoServiceExists(acc->szModuleName, PS_VOICE_CAPS))
		return;

	int flags = CallProtoService(acc->szModuleName, PS_VOICE_CAPS, 0, 0);

	if ((flags & VOICE_CAPS_VOICE) == 0)
		return;

	VOICE_MODULE vm = { 0 };
	vm.cbSize = sizeof(vm);
	vm.name = acc->szModuleName;
	vm.description = acc->tszAccountName;
	vm.flags = flags;
	VoiceRegister((WPARAM)&vm, 0);
}

/////////////////////////////////////////////////////////////////////////////////////////

static int AccListChanged(WPARAM wParam, LPARAM lParam)
{
	PROTOACCOUNT *acc = (PROTOACCOUNT *)lParam;
	if (acc == NULL)
		return 0;

	VoiceProvider *provider = FindModule(acc->szModuleName);

	switch (wParam) {
	case PRAC_ADDED:
		AddAccount(acc);
		break;

	case PRAC_CHANGED:
		if (provider != NULL)
			wcsncpy_s(provider->description, acc->tszAccountName, _TRUNCATE);
		break;

	case PRAC_CHECKED:
		{
			BOOL enabled = acc->IsEnabled();

			if (!enabled) {
				if (provider != NULL)
					VoiceUnregister((WPARAM)acc->szModuleName, 0);
			}
			else {
				if (provider == NULL)
					AddAccount(acc);
			}
			break;
		}
	case PRAC_REMOVED:
		if (provider != NULL)
			VoiceUnregister((WPARAM)acc->szModuleName, 0);
		break;
	}

	return 0;
}

/////////////////////////////////////////////////////////////////////////////////////////

static INT_PTR Service_CallItem(WPARAM wParam, LPARAM, LPARAM param)
{
	MCONTACT hContact = (MCONTACT)wParam;
	int index = (int)param;

	if (hContact == NULL)
		return -1;

	hContact = ConvertMetacontact(hContact);

	OBJLIST<CallingMethod> methods(10, sttCompareCallingMethods);
	BuildCallingMethodsList(&methods, hContact);

	if (index < 0 || index >= methods.getCount())
		return -2;

	methods[index].Call();
	return 0;
}

static int PreBuildContactMenu(WPARAM wParam, LPARAM)
{
	Menu_ShowItem(hCMCall, false);
	Menu_ShowItem(hCMAnswer, false);
	Menu_ShowItem(hCMHold, false);
	Menu_ShowItem(hCMDrop, false);
	for (auto &it : hCMCalls)
		Menu_ShowItem(it, false);

	MCONTACT hContact = (MCONTACT)wParam;
	if (hContact == NULL)
		return -1;

	hContact = ConvertMetacontact(hContact);

	// There is a current call already?
	VoiceCall *call = FindVoiceCall(hContact);
	if (call == nullptr) {
		OBJLIST<CallingMethod> methods(10, sttCompareCallingMethods);
		BuildCallingMethodsList(&methods, hContact);

		if (methods.getCount() == 1) {
			CallingMethod *method = &methods[0];

			wchar_t name[128];
			if (!IsEmptyW(method->number))
				mir_snwprintf(name, TranslateT("Call %s with %s"),
				method->number, method->provider->description);
			else
				mir_snwprintf(name, TranslateT("Call with %s"),
				method->provider->description);

			Menu_ModifyItem(hCMCall, name);
			Menu_ShowItem(hCMCall, true);
		}
		else if (methods.getCount() > 1) {
			Menu_ModifyItem(hCMCall, TranslateT("Call"));
			Menu_ShowItem(hCMCall, true);

			for (int i = 0; i < methods.getCount(); ++i) {
				CallingMethod *method = &methods[i];

				HICON hIcon = method->provider->GetIcon();

				wchar_t name[128];
				if (!IsEmptyW(method->number))
					mir_snwprintf(name, TranslateT("%s with %s"),
					method->number, method->provider->description);
				else
					mir_snwprintf(name, TranslateT("with %s"),
					method->provider->description);

				char service[128];
				mir_snprintf(service, "VoiceService/ContactMenu/Call_%d", i);

				if (i == hCMCalls.size()) {
					CreateServiceFunctionParam(service, Service_CallItem, i);

					CMenuItem mi(&g_plugin);
					mi.position = i;
					mi.flags = CMIF_UNICODE;
					mi.name.w = name;
					mi.hIcon = hIcon;
					mi.pszService = service;
					mi.root = hCMCall;
					hCMCalls.push_back(Menu_AddContactMenuItem(&mi));
				}
				else Menu_ModifyItem(hCMCalls[i], name, hIcon);

				method->provider->ReleaseIcon(hIcon);
			}
		}
	}
	else {
		switch (call->state) {
		case VOICE_STATE_CALLING:
			Menu_ShowItem(hCMDrop, true);
			break;

		case VOICE_STATE_TALKING:
			if (call->module->CanHold())
				Menu_ShowItem(hCMHold, true);
			Menu_ShowItem(hCMDrop, true);
			break;

		case VOICE_STATE_RINGING:
		case VOICE_STATE_ON_HOLD:
			Menu_ShowItem(hCMAnswer, true);
			Menu_ShowItem(hCMDrop, true);
			break;
		}
	}

	return 0;
}

/////////////////////////////////////////////////////////////////////////////////////////

VoiceProvider* FindModule(const char *szModule)
{
	for (auto &it : modules)
		if (strcmp(it->name, szModule) == 0)
			return it;

	return NULL;
}

static bool IsCall(VoiceCall *call, const char *szModule, const char *id)
{
	return strcmp(call->module->name, szModule) == 0
		&& call->id != NULL && strcmp(call->id, id) == 0;
}

VoiceCall* FindVoiceCall(const char *szModule, const char *id, bool add)
{
	for (auto &call : calls)
		if (IsCall(call, szModule, id))
			return call;

	if (add) {
		VoiceProvider *module = FindModule(szModule);
		if (module == NULL)
			return NULL;

		VoiceCall *tmp = new VoiceCall(module, id);
		calls.insert(tmp);
		return tmp;
	}

	return nullptr;
}

VoiceCall* FindVoiceCall(MCONTACT hContact)
{
	for (auto &call : calls)
		if (call->state != VOICE_STATE_ENDED && call->hContact == hContact)
			return call;

	return nullptr;
}

bool CanCall(MCONTACT hContact, BOOL now)
{
	for (auto &it : modules)
		if (it->CanCall(hContact, now))
			return true;

	return false;
}

bool CanCall(const wchar_t *number)
{
	for (auto &it : modules)
		if (it->CanCall(number))
			return true;

	return false;
}

bool CanCallNumber()
{
	for (auto &it : modules)
		if (it->flags & VOICE_CAPS_CALL_STRING)
			return true;

	return false;
}

bool IsFinalState(int state)
{
	return state == VOICE_STATE_ENDED || state == VOICE_STATE_BUSY;
}

VoiceCall* GetTalkingCall()
{
	for (auto &call : calls)
		if (call->state == VOICE_STATE_TALKING)
			return call;

	return NULL;
}

void HoldOtherCalls(VoiceCall *call)
{
	for (auto &other: calls) {
		if (other == call || other->state != VOICE_STATE_TALKING)
			continue;

		if (other->CanHold())
			other->Hold();
		else
			other->Drop();
	}
}

void Answer(VoiceCall *call)
{
	if (!call->CanAnswer())
		return;

	HoldOtherCalls(call);

	// Now annswer it
	call->Answer();
}

/////////////////////////////////////////////////////////////////////////////////////////

static INT_PTR Service_CanCall(WPARAM wParam, LPARAM)
{
	MCONTACT hContact = (MCONTACT)wParam;
	if (hContact == NULL)
		return -1;

	hContact = ConvertMetacontact(hContact);
	return CanCall(hContact) ? 1 : 0;
}

static INT_PTR Service_Call(WPARAM wParam, LPARAM)
{
	MCONTACT hContact = (MCONTACT)wParam;
	if (hContact == NULL)
		return -1;

	hContact = ConvertMetacontact(hContact);

	OBJLIST<CallingMethod> methods(10, sttCompareCallingMethods);
	BuildCallingMethodsList(&methods, hContact);

	if (methods.getCount() < 1)
		return -2;

	CallingMethod *method = &methods[0];
	if (!IsEmptyW(method->number))
		return -2;

	method->Call();
	return 0;
}

static INT_PTR CMAnswer(WPARAM wParam, LPARAM)
{
	MCONTACT hContact = (MCONTACT)wParam;
	if (hContact == NULL)
		return -1;

	hContact = ConvertMetacontact(hContact);

	VoiceCall *call = FindVoiceCall(hContact);
	if (call != NULL)
		Answer(call);

	return 0;
}

static INT_PTR CMHold(WPARAM wParam, LPARAM)
{
	MCONTACT hContact = (MCONTACT)wParam;
	if (hContact == NULL)
		return -1;

	hContact = ConvertMetacontact(hContact);

	VoiceCall *call = FindVoiceCall(hContact);
	if (call != NULL)
		call->Hold();

	return 0;
}

static INT_PTR CMDrop(WPARAM wParam, LPARAM)
{
	MCONTACT hContact = (MCONTACT)wParam;
	if (hContact == NULL)
		return -1;

	hContact = ConvertMetacontact(hContact);

	VoiceCall *call = FindVoiceCall(hContact);
	if (call != NULL)
		call->Drop();

	return 0;
}

/////////////////////////////////////////////////////////////////////////////////////////
// Called when all the modules are loaded

int ModulesLoaded(WPARAM, LPARAM)
{
	// add our modules to the KnownModules list
	CallService("DBEditorpp/RegisterSingleModule", (WPARAM)MODULE_NAME, 0);

	// Init icons
	g_plugin.registerIcon(LPGEN("Voice Calls"), mainIcons, "vc");
	g_plugin.registerIcon(LPGEN("Voice Calls"), stateIcons, "vc");
	g_plugin.registerIcon(LPGEN("Voice Calls"), actionIcons, "vca");

	InitOptions();

	// Add menu items
	CMenuItem mi(&g_plugin);
	mi.position = -2000020000;

	SET_UID(mi, 0x3da00738, 0x563, 0x43c9, 0x80, 0x6c, 0x69, 0x42, 0xaf, 0xe8, 0x91, 0x4b);
	mi.name.a = actionIcons[0].szDescr;
	mi.hIcolibItem = g_plugin.getIconHandle(IDI_ACTION_CALL);
	mi.pszService = MS_VOICESERVICE_CM_CALL;
	hCMCall = Menu_AddContactMenuItem(&mi);
	CreateServiceFunction(mi.pszService, Service_Call);

	SET_UID(mi, 0x646b9a8b, 0xf475, 0x4108, 0xb0, 0x57, 0x80, 0x76, 0xa4, 0xf2, 0x5e, 0x9b);
	mi.position++;
	mi.name.a = actionIcons[1].szDescr;
	mi.hIcolibItem = g_plugin.getIconHandle(IDI_ACTION_ANSWER);
	mi.pszService = MS_VOICESERVICE_CM_ANSWER;
	hCMAnswer = Menu_AddContactMenuItem(&mi);
	CreateServiceFunction(mi.pszService, CMAnswer);

	SET_UID(mi, 0x654fadef, 0xb07b, 0x4619, 0x83, 0xc, 0xf7, 0x66, 0x57, 0x60, 0x2a, 0xc8);
	mi.position++;
	mi.name.a = actionIcons[2].szDescr;
	mi.hIcolibItem = g_plugin.getIconHandle(IDI_ACTION_HOLD);
	mi.pszService = MS_VOICESERVICE_CM_HOLD;
	hCMHold = Menu_AddContactMenuItem(&mi);
	CreateServiceFunction(mi.pszService, CMHold);

	SET_UID(mi, 0x18fbd6d6, 0xe1ad, 0x48bd, 0xa8, 0xdb, 0xf, 0x5d, 0x87, 0xe3, 0x14, 0xfe);
	mi.position++;
	mi.name.a = actionIcons[3].szDescr;
	mi.hIcolibItem = g_plugin.getIconHandle(IDI_ACTION_DROP);
	mi.pszService = MS_VOICESERVICE_CM_DROP;
	hCMDrop = Menu_AddContactMenuItem(&mi);
	CreateServiceFunction(mi.pszService, CMDrop);

	HookEvent(ME_CLIST_PREBUILDCONTACTMENU, PreBuildContactMenu);

	// Util services
	CreateServiceFunction(MS_VOICESERVICE_CALL, Service_Call);
	CreateServiceFunction(MS_VOICESERVICE_CAN_CALL, Service_CanCall);

	// Sounds
	for (auto &it : g_sounds)
		g_plugin.addSound(it.szName, LPGENW("Voice Calls"), it.wszDescr);
	g_plugin.addSound("voice_dialpad", LPGENW("Voice Calls"), LPGENW("Dialpad press"));

	// Accounts
	for (auto *pa : Accounts())
		AddAccount(pa);

	HookEvent(ME_PROTO_ACCLISTCHANGED, AccListChanged);
	return 0;
}

int PreShutdown(WPARAM, LPARAM)
{
	DeInitOptions();
	return 0;
}