managerimpl.cpp 116 KB
Newer Older
jpbl's avatar
jpbl committed
1
/*
2
3
 *  Copyright (C) 2004-2007 Savoir-Faire Linux inc.
 *  Author: Alexandre Bourget <alexandre.bourget@savoirfairelinux.com>
jpbl's avatar
jpbl committed
4
 *  Author: Yan Morin <yan.morin@savoirfairelinux.com>
5
 *  Author: Laurielle Lea <laurielle.lea@savoirfairelinux.com>
6
 *  Author: Emmanuel Milou <emmanuel.milou@savoirfairelinux.com>
7
 *  Author: Alexandre Savard <alexandre.savard@savoirfairelinux.com>
8
 *  Author: Guillaume Carmel-Archambault <guillaume.carmel-archambault@savoirfairelinux.com>
9
 *
jpbl's avatar
jpbl committed
10
11
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
12
 *  the Free Software Foundation; either version 3 of the License, or
jpbl's avatar
jpbl committed
13
 *  (at your option) any later version.
14
 *
jpbl's avatar
jpbl committed
15
16
17
18
 *  This program 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 General Public License for more details.
19
 *
jpbl's avatar
jpbl committed
20
21
22
23
24
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

25
26
27
28
29
#include "managerimpl.h"

#include "account.h"
#include "dbus/callmanager.h"
#include "user_cfg.h"
30
#include "global.h"
31
#include "sip/sipaccount.h"
32

33
34
35
36
37
38
39
40
41
42
#include "audio/audiolayer.h"
#include "audio/alsa/alsalayer.h"
#include "audio/pulseaudio/pulselayer.h"
#include "audio/sound/tonelist.h"
#include "history/historymanager.h"
#include "accountcreator.h" // create new account
#include "sip/sipvoiplink.h"
#include "manager.h"
#include "dbus/configurationmanager.h"

43
44
#include "conference.h"

jpbl's avatar
jpbl committed
45
46
47
48
49
#include <errno.h>
#include <time.h>
#include <cstdlib>
#include <iostream>
#include <fstream>
50
#include <sstream>
jpbl's avatar
jpbl committed
51
#include <sys/types.h> // mkdir(2)
52
53
#include <sys/stat.h>  // mkdir(2)
#include <pwd.h>       // getpwuid
54

55
#define DIRECT_IP_CALL	"IP CALL"
jpbl's avatar
jpbl committed
56
57
58
59
60
61

#define fill_config_str(name, value) \
  (_config.addConfigTreeItem(section, Conf::ConfigTreeItem(std::string(name), std::string(value), type_str)))
#define fill_config_int(name, value) \
  (_config.addConfigTreeItem(section, Conf::ConfigTreeItem(std::string(name), std::string(value), type_int)))

62
63
#define MD5_APPEND(pms,buf,len) pj_md5_update(pms, (const pj_uint8_t*)buf, len)

64
65
#define find_in_map(X, Y)  if((iter = map_cpy.find(X)) != map_cpy.end()) { Y = iter->second; }

66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
ManagerImpl::ManagerImpl (void) :
	_hasTriedToRegister(false), _config(), _currentCallId2(),
			_currentCallMutex(), _codecBuilder(NULL), _audiodriver(NULL),
			_dtmfKey(NULL), _codecDescriptorMap(), _toneMutex(),
			_telephoneTone(NULL), _audiofile(), _spkr_volume(0),
			_mic_volume(0), _mutex(), _dbus(NULL), _waitingCall(),
			_waitingCallMutex(), _nbIncomingWaitingCall(0), _path(""),
			_exist(0), _setupLoaded(false), _callAccountMap(),
			_callAccountMapMutex(), _callConfigMap(), _accountMap(),
			_directIpAccount(NULL), _cleaner(NULL), _history(NULL) {

	// initialize random generator for call id
	srand(time(NULL));

	_cleaner = new NumberCleaner();
	_history = new HistoryManager();
82

yanmorin's avatar
   
yanmorin committed
83
#ifdef TEST
84
85
86
87
	testAccountMap();
	loadAccountMap();
	testCallAccountMap();
	unloadAccountMap();
yanmorin's avatar
   
yanmorin committed
88
89
#endif

90
91
	// should be call before initConfigFile
	// loadAccountMap();, called in init() now.
jpbl's avatar
jpbl committed
92
93
94
}

// never call if we use only the singleton...
95
96
ManagerImpl::~ManagerImpl (void) {
	// terminate();
97
98
99
100
	delete _cleaner; _cleaner = NULL;
	delete _history; _history = NULL;

	_debug ("Manager: %s stop correctly.", PROGNAME);
jpbl's avatar
jpbl committed
101
102
}

103
void ManagerImpl::init () {
104

105
106
	// Load accounts, init map
	loadAccountMap();
107

108
	initVolume();
109

110
	if (_exist == 0) {
111
		_warn ("Manager: Cannot create config file in your home directory");
112
	}
113

114
	initAudioDriver();
jpbl's avatar
jpbl committed
115

116
	selectAudioDriver();
117

118
119
	// Initialize the list of supported audio codecs
	initAudioCodec();
120

121
	AudioLayer *audiolayer = getAudioDriver();
jpbl's avatar
jpbl committed
122

123
124
	if (audiolayer != 0) {
		unsigned int sampleRate = audiolayer->getSampleRate();
jpbl's avatar
jpbl committed
125

126
		_debugInit ("Manager: Load telephone tone");
127
128
		std::string country = getConfigString(PREFERENCES, ZONE_TONE);
		_telephoneTone = new TelephoneTone(country, sampleRate);
129

130
		_debugInit ("Manager: Loading DTMF key");
131
132
		_dtmfKey = new DTMF(sampleRate);
	}
133

134
135
	if (audiolayer == 0)
		audiolayer->stopStream();
136

137
138
	// Load the history
	_history->load_history(getConfigInt(PREFERENCES, CONFIG_HISTORY_LIMIT));
jpbl's avatar
jpbl committed
139
140
}

141
void ManagerImpl::terminate () {
142
143

	_debug ("Manager: Terminate ");
144
	saveConfig();
jpbl's avatar
jpbl committed
145

146
	unloadAccountMap();
147

148
	_debug ("Manager: Unload DTMF key");
149
	delete _dtmfKey;
jpbl's avatar
jpbl committed
150

151
152
153
154
155
	_debug("Manager: Unload telephone tone");
	delete _telephoneTone; _telephoneTone = NULL;

	_debug ("Manager: Unload audio driver");
	delete _audiodriver; _audiodriver = NULL;
jpbl's avatar
jpbl committed
156

157
158
	_debug ("Manager: Unload telephone tone");
	delete _telephoneTone; _telephoneTone = NULL;
159

160
	_debug ("Manager: Unload audio codecs ");
161
	_codecDescriptorMap.deleteHandlePointer();
162

jpbl's avatar
jpbl committed
163
164
}

165
166
bool ManagerImpl::isCurrentCall (const CallID& callId) {
	return (_currentCallId2 == callId ? true : false);
jpbl's avatar
jpbl committed
167
168
}

169
170
bool ManagerImpl::hasCurrentCall () {
	// _debug ("ManagerImpl::hasCurrentCall current call ID = %s", _currentCallId2.c_str());
171

172
173
174
	if (_currentCallId2 != "") {
		return true;
	}
175

176
	return false;
jpbl's avatar
jpbl committed
177
178
}

179
const CallID&
180
181
ManagerImpl::getCurrentCallId () {
	return _currentCallId2;
jpbl's avatar
jpbl committed
182
183
}

184
185
186
187
void ManagerImpl::switchCall (const CallID& id) {
	ost::MutexLock m(_currentCallMutex);
	_debug ("----- Switch current call id to %s -----", id.c_str());
	_currentCallId2 = id;
jpbl's avatar
jpbl committed
188
189
190
191
192
}

///////////////////////////////////////////////////////////////////////////////
// Management of events' IP-phone user
///////////////////////////////////////////////////////////////////////////////
193
/* Main Thread */
Emmanuel Milou's avatar
Emmanuel Milou committed
194

195
196
bool ManagerImpl::outgoingCall (const std::string& account_id,
		const CallID& call_id, const std::string& to) {
197

198
199
200
	std::string pattern, to_cleaned;
	Call::CallConfiguration callConfig;
	SIPVoIPLink *siplink;
201

202
	_debug ("Manager: New outgoing call %s to %s", call_id.c_str(), to.c_str());
203

204
	CallID current_call_id = getCurrentCallId();
Emmanuel Milou's avatar
Emmanuel Milou committed
205

206
207
208
209
210
	if (getConfigString(HOOKS, PHONE_NUMBER_HOOK_ENABLED) == "1")
		_cleaner->set_phone_number_prefix(getConfigString(HOOKS,
				PHONE_NUMBER_HOOK_ADD_PREFIX));
	else
		_cleaner->set_phone_number_prefix("");
Emmanuel Milou's avatar
Emmanuel Milou committed
211

212
	to_cleaned = _cleaner->clean(to);
213

214
215
	/* Check what kind of call we are dealing with */
	check_call_configuration(call_id, to_cleaned, &callConfig);
216

217
218
	// in any cases we have to detach from current communication
	if (hasCurrentCall()) {
219

220
		_debug ("Manager: Has current call (%s) put it onhold", current_call_id.c_str());
221
		// if this is not a conferenceand this and is not a conference participant
222
		if (!isConference(current_call_id) && !participToConference(current_call_id)) {
223
			onHoldCall(current_call_id);
224
		} else if (isConference(current_call_id) && !participToConference(call_id)) {
225
226
227
			detachParticipant(default_id, current_call_id);
		}
	}
228

229
	if (callConfig == Call::IPtoIP) {
230
		_debug ("Manager: Start IP2IP call");
231
232
		/* We need to retrieve the sip voiplink instance */
		siplink = SIPVoIPLink::instance("");
233

234
235
236
237
238
239
		if (siplink->new_ip_to_ip_call(call_id, to_cleaned)) {
			switchCall(call_id);
			return true;
		} else {
			callFailure(call_id);
		}
240

241
242
		return false;
	}
243

244
	_debug("Manager: Selecting account %s", account_id.c_str());
245
	if (!accountExists(account_id)) {
246
		_error ("Manager: Error: Account doesn't exist in new outgoing call");
247
248
		return false;
	}
249

250
	if (getAccountFromCall(call_id) != AccountNULL) {
251
		_error ("Manager: Error: Call id already exists in outgoing call");
252
253
		return false;
	}
254

255
	_debug ("Manager: Adding Outgoing Call %s on account %s", call_id.data(), account_id.data());
256
	associateCallToAccount(call_id, account_id);
257

258
	if (getAccountLink(account_id)->newOutgoingCall(call_id, to_cleaned)) {
259
260
	    switchCall(call_id);
	    return true;
261
	} else {
262
263
	    callFailure(call_id);
	    _debug ("Manager: Error: An error occur, the call was not created");
264
	}
265

266
	return false;
jpbl's avatar
jpbl committed
267
268
}

yanmorin's avatar
   
yanmorin committed
269
//THREAD=Main : for outgoing Call
270
bool ManagerImpl::answerCall (const CallID& call_id) {
271

272
	_debug ("ManagerImpl: Answer call %s", call_id.c_str());
273

274
	stopTone();
275

276
277
	// store the current call id
	CallID current_call_id = getCurrentCallId();
278

279
	AccountID account_id = getAccountFromCall(call_id);
280

281
282
283
	if (account_id == AccountNULL) {
		_debug ("    answerCall: AccountId is null");
	}
284

285
	Call* call = NULL;
286

287
	call = getAccountLink(account_id)->getCall(call_id);
288

289
290
291
	if (call == NULL) {
		_debug ("    answerCall: Call is null");
	}
292

293
294
	// in any cases we have to detach from current communication
	if (hasCurrentCall()) {
295

296
297
		_debug ("    answerCall: Currently conversing with %s", current_call_id.c_str());
		// if it is not a conference and is not a conference participant
298

299
300
301
302
303
		if (!isConference(current_call_id) && !participToConference(
				current_call_id)) {
			_debug ("    answerCall: Put the current call (%s) on hold", current_call_id.c_str());
			onHoldCall(current_call_id);
		}
304

305
306
307
308
309
310
		// if we are talking to a conference and we are answering an incoming call
		else if (isConference(current_call_id)
				&& !participToConference(call_id)) {
			_debug ("    answerCall: Detach main participant from conference");
			detachParticipant(default_id, current_call_id);
		}
311

312
	}
313

314
315
316
317
318
	if (!getAccountLink(account_id)->answer(call_id)) {
		// error when receiving...
		removeCallAccount(call_id);
		return false;
	}
Alexandre Savard's avatar
Alexandre Savard committed
319

320
321
322
	// if it was waiting, it's waiting no more
	if (_dbus)
		_dbus->getCallManager()->callStateChanged(call_id, "CURRENT");
Emmanuel Milou's avatar
Emmanuel Milou committed
323

324
325
	// std::string codecName = Manager::instance().getCurrentCodecName (call_id);
	// if (_dbus) _dbus->getCallManager()->currentSelectedCodec (call_id, codecName.c_str());
326

327
	removeWaitingCall(call_id);
328

329
330
	// if we dragged this call into a conference already
	if (participToConference(call_id)) {
331

332
333
		// AccountID currentAccountId;
		// Call* call = NULL;
334

335
336
		// currentAccountId = getAccountFromCall (call_id);
		// call = getAccountLink (currentAccountId)->getCall (call_id);
337

338
339
340
341
		switchCall(call->getConfId());
	} else {
		switchCall(call_id);
	}
342

343
	return true;
jpbl's avatar
jpbl committed
344
345
}

yanmorin's avatar
   
yanmorin committed
346
//THREAD=Main
347
bool ManagerImpl::hangupCall (const CallID& call_id) {
348
349
350

	_info("Manager: Hangup call %s", call_id.c_str());

351
352
353
	PulseLayer *pulselayer;
	AccountID account_id;
	bool returnValue = true;
Emmanuel Milou's avatar
Emmanuel Milou committed
354

355
356
	// store the current call id
	CallID current_call_id = getCurrentCallId();
357

358
	stopTone();
yanmorin's avatar
   
yanmorin committed
359

360
	/* Broadcast a signal over DBus */
361
	_debug ("Manager: Send DBUS call state change (HUNGUP) for id %s", call_id.c_str());
362

363
364
	if (_dbus)
		_dbus->getCallManager()->callStateChanged(call_id, "HUNGUP");
365

366
	if (participToConference(call_id)) {
367

368
		Conference *conf = getConferenceFromCallID(call_id);
369

370
371
372
		if (conf != NULL) {
			// remove this participant
			removeParticipant(call_id);
373

374
375
			processRemainingParticipant(current_call_id, conf);
		}
376

377
378
379
380
381
	} else {
		// we are not participating to a conference, current call switched to ""
		if (!isConference(current_call_id))
			switchCall("");
	}
382

383
384
385
386
387
388
389
	/* Direct IP to IP call */
	if (getConfigFromCall(call_id) == Call::IPtoIP) {
		returnValue = SIPVoIPLink::instance(AccountNULL)->hangup(call_id);
	}
	/* Classic call, attached to an account */
	else {
		account_id = getAccountFromCall(call_id);
390

391
		// Account may be NULL if call have not been sent yet
392
		if (account_id == AccountNULL) {
393
			_error ("Manager: Error: account id is NULL in hangup");
394
395
396
397
398
			returnValue = false;
		} else {
			returnValue = getAccountLink(account_id)->hangup(call_id);
			removeCallAccount(call_id);
		}
399
	}
400

401
	int nbCalls = getCallList().size();
402

403
	AudioLayer *audiolayer = getAudioDriver();
404

405
406
	// stop streams
	if (audiolayer && (nbCalls <= 0)) {
407
		_debug ("Manager: stop audio stream, ther is only %i call(s) remaining", nbCalls);
408
409
		audiolayer->stopStream();
	}
410

411
412
413
	if (_audiodriver->getLayerType() == PULSEAUDIO) {
		pulselayer = dynamic_cast<PulseLayer *> (getAudioDriver());
	}
414

415
	return returnValue;
jpbl's avatar
jpbl committed
416
417
}

418
bool ManagerImpl::hangupConference (const ConfID& id) {
419
420

	_debug ("Manager: Hangup conference %s", id.c_str());
Alexandre Savard's avatar
Alexandre Savard committed
421

422
423
	Conference *conf;
	ConferenceMap::iterator iter_conf = _conferencemap.find(id);
Alexandre Savard's avatar
Alexandre Savard committed
424

425
	AccountID currentAccountId;
Alexandre Savard's avatar
Alexandre Savard committed
426

427
	// Call* call = NULL;
Alexandre Savard's avatar
Alexandre Savard committed
428
429


430
431
	if (iter_conf != _conferencemap.end()) {
		conf = iter_conf->second;
Alexandre Savard's avatar
Alexandre Savard committed
432

433
434
		ParticipantSet participants = conf->getParticipantList();
		ParticipantSet::iterator iter_participant = participants.begin();
Alexandre Savard's avatar
Alexandre Savard committed
435

436
		while (iter_participant != participants.end()) {
437
			_debug ("Manager: Hangup onference participant %s", (*iter_participant).c_str());
Alexandre Savard's avatar
Alexandre Savard committed
438

439
			hangupCall(*iter_participant);
Alexandre Savard's avatar
Alexandre Savard committed
440

441
			iter_participant++;
442

443
		}
Alexandre Savard's avatar
Alexandre Savard committed
444

445
	}
Alexandre Savard's avatar
Alexandre Savard committed
446

447
	switchCall("");
448

449
	return true;
Alexandre Savard's avatar
Alexandre Savard committed
450
451
}

yanmorin's avatar
   
yanmorin committed
452
//THREAD=Main
453
454
455
bool ManagerImpl::cancelCall (const CallID& id) {
	AccountID accountid;
	bool returnValue;
yanmorin's avatar
   
yanmorin committed
456

457
458
	_debug("Manager: Cancel call");

459
	stopTone();
460

461
	/* Direct IP to IP call */
462

463
464
465
	if (getConfigFromCall(id) == Call::IPtoIP) {
		returnValue = SIPVoIPLink::instance(AccountNULL)->cancel(id);
	}
Emmanuel Milou's avatar
Emmanuel Milou committed
466

467
468
469
	/* Classic call, attached to an account */
	else {
		accountid = getAccountFromCall(id);
470

471
472
473
474
		if (accountid == AccountNULL) {
			_debug ("! Manager Cancel Call: Call doesn't exists");
			return false;
		}
475

476
		returnValue = getAccountLink(accountid)->cancel(id);
477

478
479
		removeCallAccount(id);
	}
480

481
482
	// it could be a waiting call?
	removeWaitingCall(id);
483

484
	switchCall("");
Emmanuel Milou's avatar
Emmanuel Milou committed
485

486
	return returnValue;
jpbl's avatar
jpbl committed
487
488
}

yanmorin's avatar
   
yanmorin committed
489
//THREAD=Main
490
491
492
bool ManagerImpl::onHoldCall (const CallID& call_id) {
	AccountID account_id;
	bool returnValue;
yanmorin's avatar
   
yanmorin committed
493

494
	_debug ("Manager: Put call %s on hold", call_id.c_str());
495

496
	stopTone();
497

498
	CallID current_call_id = getCurrentCallId();
499

500

501
	/* Direct IP to IP call */
502

503
504
505
	if (getConfigFromCall(call_id) == Call::IPtoIP) {
		returnValue = SIPVoIPLink::instance(AccountNULL)-> onhold(call_id);
	}
506

507
508
509
	/* Classic call, attached to an account */
	else {
		account_id = getAccountFromCall(call_id);
510

511
		if (account_id == AccountNULL) {
512
			_debug ("Manager: Account ID %s or callid %s doesn't exists in call onHold", account_id.c_str(), call_id.c_str());
513
514
			return false;
		}
515

516
517
		returnValue = getAccountLink(account_id)->onhold(call_id);
	}
518

519
	removeWaitingCall(call_id);
520

521
	// keeps current call id if the action is not holding this call or a new outgoing call
522

523
	if (current_call_id == call_id) {
524

525
526
		switchCall("");
	}
527

528
529
	if (_dbus)
		_dbus->getCallManager()->callStateChanged(call_id, "HOLD");
530

531
	return returnValue;
yanmorin's avatar
   
yanmorin committed
532
533
534
}

//THREAD=Main
535
bool ManagerImpl::offHoldCall (const CallID& call_id) {
536

537
538
539
	AccountID account_id;
	bool returnValue, is_rec;
	std::string codecName;
540

541
	is_rec = false;
542

543
	_debug ("Manager: Put call %s off hold", call_id.c_str());
544

545
	stopTone();
546

547
	CallID current_call_id = getCurrentCallId();
548

549
	//Place current call on hold if it isn't
550

551
552
553
554
555
556
557
558
559
560
	if (hasCurrentCall()) {
		// if this is not a conferenceand this and is not a conference participant
		if (!isConference(current_call_id) && !participToConference(
				current_call_id)) {
			onHoldCall(current_call_id);
		} else if (isConference(current_call_id) && !participToConference(
				call_id)) {
			detachParticipant(default_id, current_call_id);
		}
	}
alexandresavard's avatar
alexandresavard committed
561

562
563
	// switch current call id to id since sipvoip link need it to amke a call
	// switchCall(id);
564

565
566
567
568
569
	/* Direct IP to IP call */
	if (getConfigFromCall(call_id) == Call::IPtoIP) {
		// is_rec = SIPVoIPLink::instance (AccountNULL)-> isRecording (call_id);
		returnValue = SIPVoIPLink::instance(AccountNULL)-> offhold(call_id);
	}
570

571
572
573
	/* Classic call, attached to an account */
	else {
		account_id = getAccountFromCall(call_id);
574

575
		if (account_id == AccountNULL) {
576
			_warn ("Manager: Error: Call doesn't exists in off hold");
577
578
			return false;
		}
alexandresavard's avatar
alexandresavard committed
579

580
		_debug ("Manager: Setting offhold, Account %s, callid %s", account_id.c_str(), call_id.c_str());
581

582
583
584
		is_rec = getAccountLink(account_id)->getCall(call_id)->isRecording();
		returnValue = getAccountLink(account_id)->offhold(call_id);
	}
585

586
587
588
589
590
	if (_dbus) {
		if (is_rec)
			_dbus->getCallManager()->callStateChanged(call_id, "UNHOLD_RECORD");
		else
			_dbus->getCallManager()->callStateChanged(call_id, "UNHOLD_CURRENT");
591

592
	}
593

594
	if (participToConference(call_id)) {
595

596
597
		AccountID currentAccountId;
		Call* call = NULL;
598

599
600
		currentAccountId = getAccountFromCall(call_id);
		call = getAccountLink(currentAccountId)->getCall(call_id);
601

602
		switchCall(call->getConfId());
603

604
605
606
607
	} else {
		switchCall(call_id);
		_audiodriver->flushMain();
	}
608

609
	return returnValue;
jpbl's avatar
jpbl committed
610
611
}

yanmorin's avatar
   
yanmorin committed
612
//THREAD=Main
613
614
615
bool ManagerImpl::transferCall (const CallID& call_id, const std::string& to) {
	AccountID accountid;
	bool returnValue;
616

617
	_info("Manager: Transfer call %s\n", call_id.c_str());
618

619
	CallID current_call_id = getCurrentCallId();
620

621
	// Direct IP to IP call
622
	if (getConfigFromCall(call_id) == Call::IPtoIP) {
623
		returnValue = SIPVoIPLink::instance(AccountNULL)-> transfer(call_id, to);
624
	}
625
	// Classic call, attached to an account
626
	else {
627
628

	accountid = getAccountFromCall(call_id);
629

630
		if (accountid == AccountNULL) {
631
			_warn ("Manager: Call doesn't exists");
632
633
			return false;
		}
634

635
		returnValue = getAccountLink(accountid)->transfer(call_id, to);
636

637
	}
638

639
	// remove waiting call in case we make transfer without even answer
640
	removeWaitingCall(call_id);
641

642
	return returnValue;
jpbl's avatar
jpbl committed
643
644
}

645
void ManagerImpl::transferFailed () {
646
647
648

	_debug("UserAgent: Transfer failed");

649
650
	if (_dbus)
		_dbus->getCallManager()->transferFailed();
651
652
}

653
void ManagerImpl::transferSucceded () {
654
655
656

	_debug("UserAgent: Transfer succeded");

657
658
	if (_dbus)
		_dbus->getCallManager()->transferSucceded();
659
660
661

}

yanmorin's avatar
   
yanmorin committed
662
//THREAD=Main : Call:Incoming
663
664
665
bool ManagerImpl::refuseCall (const CallID& id) {
	AccountID accountid;
	bool returnValue;
666

667
668
	_debug("Manager: Refuse call %s", id.c_str());

669
	CallID current_call_id = getCurrentCallId();
670

671
	stopTone();
672

673
	int nbCalls = getCallList().size();
674

675
	// AudioLayer* audiolayer = getAudioDriver();
676

677
	if (nbCalls <= 1) {
Emmanuel Milou's avatar
Emmanuel Milou committed
678
		_debug ("    refuseCall: stop audio stream, there is only %i call(s) remaining", nbCalls);
679

680
681
682
		AudioLayer* audiolayer = getAudioDriver();
		audiolayer->stopStream();
	}
Emmanuel Milou's avatar
Emmanuel Milou committed
683

684
	/* Direct IP to IP call */
685

686
687
688
	if (getConfigFromCall(id) == Call::IPtoIP) {
		returnValue = SIPVoIPLink::instance(AccountNULL)-> refuse(id);
	}
689

690
691
692
	/* Classic call, attached to an account */
	else {
		accountid = getAccountFromCall(id);
693

694
		if (accountid == AccountNULL) {
695
			_warn ("Manager: Call doesn't exists");
696
697
			return false;
		}
698

699
		returnValue = getAccountLink(accountid)->refuse(id);
700

701
702
		removeCallAccount(id);
	}
703

704
705
706
707
	// if the call was outgoing or established, we didn't refuse it
	// so the method did nothing
	if (returnValue) {
		removeWaitingCall(id);
708

709
710
711
		if (_dbus)
			_dbus->getCallManager()->callStateChanged(id, "HUNGUP");
	}
712

713
	return returnValue;
jpbl's avatar
jpbl committed
714
715
}

716
Conference*
717
ManagerImpl::createConference (const CallID& id1, const CallID& id2) {
Alexandre Savard's avatar
Alexandre Savard committed
718
	_debug ("Manager: Create conference with call %s and %s", id1.c_str(), id2.c_str());
719

720
	Conference* conf = new Conference();
721

722
723
	conf->add(id1);
	conf->add(id2);
724

Alexandre Savard's avatar
Alexandre Savard committed
725
726
727
	// Add conference to map
	_conferencemap.insert( std::pair<CallID, Conference*>(conf->getConfID(), conf));

728
729
	// broadcast a signal over dbus
	_dbus->getCallManager()->conferenceCreated(conf->getConfID());
730

731
	return conf;
732
733
}

734
void ManagerImpl::removeConference (const ConfID& conference_id) {
735

Alexandre Savard's avatar
Alexandre Savard committed
736
	_debug ("Manager: Remove conference %s", conference_id.c_str());
737

738
	Conference* conf = NULL;
739

Alexandre Savard's avatar
Alexandre Savard committed
740
	_debug ("Manager: number of participant: %d", (int) _conferencemap.size());
741
	ConferenceMap::iterator iter = _conferencemap.find(conference_id);
742

743
744
745
	if (iter != _conferencemap.end()) {
		conf = iter->second;
	}
746

747
	if (conf == NULL) {
Alexandre Savard's avatar
Alexandre Savard committed
748
		_error ("Manager: Error: Conference not found");
749
750
		return;
	}
751

752
	// We now need to bind the audio to the remain participant
753

Alexandre Savard's avatar
Alexandre Savard committed
754
	// Unbind main participant audio from conference
755
	_audiodriver->getMainBuffer()->unBindAll(default_id);