managerimpl.cpp 120 KB
Newer Older
jpbl's avatar
jpbl committed
1
/*
2
 *  Copyright (C) 2004, 2005, 2006, 2009, 2008, 2009, 2010 Savoir-Faire Linux Inc.
3
 *  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
 *  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.
23
24
25
26
27
28
29
30
31
32
33
 *
 *  Additional permission under GNU GPL version 3 section 7:
 *
 *  If you modify this program, or any covered work, by linking or
 *  combining it with the OpenSSL project's OpenSSL library (or a
 *  modified version of that library), containing parts covered by the
 *  terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
 *  grants you additional permission to convey the resulting work.
 *  Corresponding Source for a non-source form of such a combination
 *  shall include the source code for the parts of OpenSSL used as well
 *  as that of the covered work.
jpbl's avatar
jpbl committed
34
35
 */

36
37
38
39
40
#include "managerimpl.h"

#include "account.h"
#include "dbus/callmanager.h"
#include "user_cfg.h"
41
#include "global.h"
42
#include "sip/sipaccount.h"
43

44
45
46
47
48
49
50
51
52
53
#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"

54
55
#include "conference.h"

jpbl's avatar
jpbl committed
56
57
58
59
60
#include <errno.h>
#include <time.h>
#include <cstdlib>
#include <iostream>
#include <fstream>
61
#include <sstream>
jpbl's avatar
jpbl committed
62
#include <sys/types.h> // mkdir(2)
63
64
#include <sys/stat.h>  // mkdir(2)
#include <pwd.h>       // getpwuid
65

66
#define DIRECT_IP_CALL	"IP CALL"
jpbl's avatar
jpbl committed
67
68
69
70
71
72

#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)))

73
74
#define MD5_APPEND(pms,buf,len) pj_md5_update(pms, (const pj_uint8_t*)buf, len)

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

77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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();
93

yanmorin's avatar
   
yanmorin committed
94
#ifdef TEST
95
96
97
98
	testAccountMap();
	loadAccountMap();
	testCallAccountMap();
	unloadAccountMap();
yanmorin's avatar
   
yanmorin committed
99
100
#endif

101
102
	// should be call before initConfigFile
	// loadAccountMap();, called in init() now.
jpbl's avatar
jpbl committed
103
104
105
}

// never call if we use only the singleton...
106
107
ManagerImpl::~ManagerImpl (void) {
	// terminate();
108
109
110
111
	delete _cleaner; _cleaner = NULL;
	delete _history; _history = NULL;

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

114
void ManagerImpl::init () {
115

116
117
	// Load accounts, init map
	loadAccountMap();
118

119
	initVolume();
120

121
	if (_exist == 0) {
122
		_warn ("Manager: Cannot create config file in your home directory");
123
	}
124

125
	initAudioDriver();
jpbl's avatar
jpbl committed
126

127
	selectAudioDriver();
128

129
130
	// Initialize the list of supported audio codecs
	initAudioCodec();
131

132
	AudioLayer *audiolayer = getAudioDriver();
jpbl's avatar
jpbl committed
133

134
135
	if (audiolayer != 0) {
		unsigned int sampleRate = audiolayer->getSampleRate();
jpbl's avatar
jpbl committed
136

137
		_debugInit ("Manager: Load telephone tone");
138
139
		std::string country = getConfigString(PREFERENCES, ZONE_TONE);
		_telephoneTone = new TelephoneTone(country, sampleRate);
140

141
		_debugInit ("Manager: Loading DTMF key");
142
143
		_dtmfKey = new DTMF(sampleRate);
	}
144

145
146
	if (audiolayer == 0)
		audiolayer->stopStream();
147

148
149
	// Load the history
	_history->load_history(getConfigInt(PREFERENCES, CONFIG_HISTORY_LIMIT));
jpbl's avatar
jpbl committed
150
151
}

152
void ManagerImpl::terminate () {
153
154

	_debug ("Manager: Terminate ");
155
	saveConfig();
jpbl's avatar
jpbl committed
156

157
	unloadAccountMap();
158

159
	_debug ("Manager: Unload DTMF key");
160
	delete _dtmfKey;
jpbl's avatar
jpbl committed
161

162
163
164
165
166
	_debug("Manager: Unload telephone tone");
	delete _telephoneTone; _telephoneTone = NULL;

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

168
169
	_debug ("Manager: Unload telephone tone");
	delete _telephoneTone; _telephoneTone = NULL;
170

171
	_debug ("Manager: Unload audio codecs ");
172
	_codecDescriptorMap.deleteHandlePointer();
173

jpbl's avatar
jpbl committed
174
175
}

176
177
bool ManagerImpl::isCurrentCall (const CallID& callId) {
	return (_currentCallId2 == callId ? true : false);
jpbl's avatar
jpbl committed
178
179
}

180
181
bool ManagerImpl::hasCurrentCall () {
	// _debug ("ManagerImpl::hasCurrentCall current call ID = %s", _currentCallId2.c_str());
182

183
184
185
	if (_currentCallId2 != "") {
		return true;
	}
186

187
	return false;
jpbl's avatar
jpbl committed
188
189
}

190
const CallID&
191
192
ManagerImpl::getCurrentCallId () {
	return _currentCallId2;
jpbl's avatar
jpbl committed
193
194
}

195
196
197
198
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
199
200
201
202
203
}

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

206
207
bool ManagerImpl::outgoingCall (const std::string& account_id,
		const CallID& call_id, const std::string& to) {
208

209
210
211
	std::string pattern, to_cleaned;
	Call::CallConfiguration callConfig;
	SIPVoIPLink *siplink;
212

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

215
	CallID current_call_id = getCurrentCallId();
Emmanuel Milou's avatar
Emmanuel Milou committed
216

217
218
219
220
221
	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
222

223
	to_cleaned = _cleaner->clean(to);
224

225
226
	/* Check what kind of call we are dealing with */
	check_call_configuration(call_id, to_cleaned, &callConfig);
227

228
229
	// in any cases we have to detach from current communication
	if (hasCurrentCall()) {
230

231
		_debug ("Manager: Has current call (%s) put it onhold", current_call_id.c_str());
232
		// if this is not a conferenceand this and is not a conference participant
233
		if (!isConference(current_call_id) && !participToConference(current_call_id)) {
234
			onHoldCall(current_call_id);
235
		} else if (isConference(current_call_id) && !participToConference(call_id)) {
236
237
238
			detachParticipant(default_id, current_call_id);
		}
	}
239

240
	if (callConfig == Call::IPtoIP) {
241
		_debug ("Manager: Start IP2IP call");
242
243
		/* We need to retrieve the sip voiplink instance */
		siplink = SIPVoIPLink::instance("");
244

245
246
247
248
249
250
		if (siplink->new_ip_to_ip_call(call_id, to_cleaned)) {
			switchCall(call_id);
			return true;
		} else {
			callFailure(call_id);
		}
251

252
253
		return false;
	}
254

255
	_debug("Manager: Selecting account %s", account_id.c_str());
256
	if (!accountExists(account_id)) {
257
		_error ("Manager: Error: Account doesn't exist in new outgoing call");
258
259
		return false;
	}
260

261
	if (getAccountFromCall(call_id) != AccountNULL) {
262
		_error ("Manager: Error: Call id already exists in outgoing call");
263
264
		return false;
	}
265

266
	_debug ("Manager: Adding Outgoing Call %s on account %s", call_id.data(), account_id.data());
267
	associateCallToAccount(call_id, account_id);
268

269
	if (getAccountLink(account_id)->newOutgoingCall(call_id, to_cleaned)) {
270
271
	    switchCall(call_id);
	    return true;
272
	} else {
273
274
	    callFailure(call_id);
	    _debug ("Manager: Error: An error occur, the call was not created");
275
	}
276

277
	return false;
jpbl's avatar
jpbl committed
278
279
}

yanmorin's avatar
   
yanmorin committed
280
//THREAD=Main : for outgoing Call
281
bool ManagerImpl::answerCall (const CallID& call_id) {
282

283
	_debug ("ManagerImpl: Answer call %s", call_id.c_str());
284

285
	stopTone();
286

287
288
	// store the current call id
	CallID current_call_id = getCurrentCallId();
289

290
	AccountID account_id = getAccountFromCall(call_id);
291

292
293
294
	if (account_id == AccountNULL) {
		_debug ("    answerCall: AccountId is null");
	}
295

296
	Call* call = NULL;
297

298
	call = getAccountLink(account_id)->getCall(call_id);
299

300
301
302
	if (call == NULL) {
		_debug ("    answerCall: Call is null");
	}
303

304
305
	// in any cases we have to detach from current communication
	if (hasCurrentCall()) {
306

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

310
311
312
313
314
		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);
		}
315

316
317
318
319
320
321
		// 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);
		}
322

323
	}
324

325
326
327
328
329
	if (!getAccountLink(account_id)->answer(call_id)) {
		// error when receiving...
		removeCallAccount(call_id);
		return false;
	}
Alexandre Savard's avatar
Alexandre Savard committed
330

331
332
333
	// if it was waiting, it's waiting no more
	if (_dbus)
		_dbus->getCallManager()->callStateChanged(call_id, "CURRENT");
Emmanuel Milou's avatar
Emmanuel Milou committed
334

335
336
	// std::string codecName = Manager::instance().getCurrentCodecName (call_id);
	// if (_dbus) _dbus->getCallManager()->currentSelectedCodec (call_id, codecName.c_str());
337

338
	removeWaitingCall(call_id);
339

340
341
	// if we dragged this call into a conference already
	if (participToConference(call_id)) {
342

343
344
		// AccountID currentAccountId;
		// Call* call = NULL;
345

346
347
		// currentAccountId = getAccountFromCall (call_id);
		// call = getAccountLink (currentAccountId)->getCall (call_id);
348

349
350
351
352
		switchCall(call->getConfId());
	} else {
		switchCall(call_id);
	}
353

354
	return true;
jpbl's avatar
jpbl committed
355
356
}

yanmorin's avatar
   
yanmorin committed
357
//THREAD=Main
358
bool ManagerImpl::hangupCall (const CallID& call_id) {
359
360
361

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

362
363
364
	PulseLayer *pulselayer;
	AccountID account_id;
	bool returnValue = true;
Emmanuel Milou's avatar
Emmanuel Milou committed
365

366
367
	// store the current call id
	CallID current_call_id = getCurrentCallId();
368

369
	stopTone();
yanmorin's avatar
   
yanmorin committed
370

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

374
375
	if (_dbus)
		_dbus->getCallManager()->callStateChanged(call_id, "HUNGUP");
376

377
	if (participToConference(call_id)) {
378

379
		Conference *conf = getConferenceFromCallID(call_id);
380

381
382
383
		if (conf != NULL) {
			// remove this participant
			removeParticipant(call_id);
384

385
386
			processRemainingParticipant(current_call_id, conf);
		}
387

388
389
390
391
392
	} else {
		// we are not participating to a conference, current call switched to ""
		if (!isConference(current_call_id))
			switchCall("");
	}
393

394
395
396
397
398
399
400
	/* 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);
401

402
		// Account may be NULL if call have not been sent yet
403
		if (account_id == AccountNULL) {
404
			_error ("Manager: Error: account id is NULL in hangup");
405
406
407
408
409
			returnValue = false;
		} else {
			returnValue = getAccountLink(account_id)->hangup(call_id);
			removeCallAccount(call_id);
		}
410
	}
411

412
	int nbCalls = getCallList().size();
413

414
	AudioLayer *audiolayer = getAudioDriver();
415

416
417
	// stop streams
	if (audiolayer && (nbCalls <= 0)) {
418
		_debug ("Manager: stop audio stream, ther is only %i call(s) remaining", nbCalls);
419
420
		audiolayer->stopStream();
	}
421

422
423
424
	if (_audiodriver->getLayerType() == PULSEAUDIO) {
		pulselayer = dynamic_cast<PulseLayer *> (getAudioDriver());
	}
425

426
	return returnValue;
jpbl's avatar
jpbl committed
427
428
}

429
bool ManagerImpl::hangupConference (const ConfID& id) {
430
431

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

433
434
	Conference *conf;
	ConferenceMap::iterator iter_conf = _conferencemap.find(id);
Alexandre Savard's avatar
Alexandre Savard committed
435

436
	AccountID currentAccountId;
Alexandre Savard's avatar
Alexandre Savard committed
437

438
	// Call* call = NULL;
Alexandre Savard's avatar
Alexandre Savard committed
439
440


441
442
	if (iter_conf != _conferencemap.end()) {
		conf = iter_conf->second;
Alexandre Savard's avatar
Alexandre Savard committed
443

444
445
		ParticipantSet participants = conf->getParticipantList();
		ParticipantSet::iterator iter_participant = participants.begin();
Alexandre Savard's avatar
Alexandre Savard committed
446

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

450
			hangupCall(*iter_participant);
Alexandre Savard's avatar
Alexandre Savard committed
451

452
			iter_participant++;
453

454
		}
Alexandre Savard's avatar
Alexandre Savard committed
455

456
	}
Alexandre Savard's avatar
Alexandre Savard committed
457

458
	switchCall("");
459

460
	return true;
Alexandre Savard's avatar
Alexandre Savard committed
461
462
}

yanmorin's avatar
   
yanmorin committed
463
//THREAD=Main
464
465
466
bool ManagerImpl::cancelCall (const CallID& id) {
	AccountID accountid;
	bool returnValue;
yanmorin's avatar
   
yanmorin committed
467

468
469
	_debug("Manager: Cancel call");

470
	stopTone();
471

472
	/* Direct IP to IP call */
473

474
475
476
	if (getConfigFromCall(id) == Call::IPtoIP) {
		returnValue = SIPVoIPLink::instance(AccountNULL)->cancel(id);
	}
Emmanuel Milou's avatar
Emmanuel Milou committed
477

478
479
480
	/* Classic call, attached to an account */
	else {
		accountid = getAccountFromCall(id);
481

482
483
484
485
		if (accountid == AccountNULL) {
			_debug ("! Manager Cancel Call: Call doesn't exists");
			return false;
		}
486

487
		returnValue = getAccountLink(accountid)->cancel(id);
488

489
490
		removeCallAccount(id);
	}
491

492
493
	// it could be a waiting call?
	removeWaitingCall(id);
494

495
	switchCall("");
Emmanuel Milou's avatar
Emmanuel Milou committed
496

497
	return returnValue;
jpbl's avatar
jpbl committed
498
499
}

yanmorin's avatar
   
yanmorin committed
500
//THREAD=Main
501
502
503
bool ManagerImpl::onHoldCall (const CallID& call_id) {
	AccountID account_id;
	bool returnValue;
yanmorin's avatar
   
yanmorin committed
504

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

507
	stopTone();
508

509
	CallID current_call_id = getCurrentCallId();
510

511

512
	/* Direct IP to IP call */
513

514
515
516
	if (getConfigFromCall(call_id) == Call::IPtoIP) {
		returnValue = SIPVoIPLink::instance(AccountNULL)-> onhold(call_id);
	}
517

518
519
520
	/* Classic call, attached to an account */
	else {
		account_id = getAccountFromCall(call_id);
521

522
		if (account_id == AccountNULL) {
523
			_debug ("Manager: Account ID %s or callid %s doesn't exists in call onHold", account_id.c_str(), call_id.c_str());
524
525
			return false;
		}
526

527
528
		returnValue = getAccountLink(account_id)->onhold(call_id);
	}
529

530
	removeWaitingCall(call_id);
531

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

534
	if (current_call_id == call_id) {
535

536
537
		switchCall("");
	}
538

539
540
	if (_dbus)
		_dbus->getCallManager()->callStateChanged(call_id, "HOLD");
541

542
	return returnValue;
yanmorin's avatar
   
yanmorin committed
543
544
545
}

//THREAD=Main
546
bool ManagerImpl::offHoldCall (const CallID& call_id) {
547

548
549
550
	AccountID account_id;
	bool returnValue, is_rec;
	std::string codecName;
551

552
	is_rec = false;
553

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

556
	stopTone();
557

558
	CallID current_call_id = getCurrentCallId();
559

560
	//Place current call on hold if it isn't
561

562
563
564
565
566
567
568
569
570
571
	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
572

573
574
	// switch current call id to id since sipvoip link need it to amke a call
	// switchCall(id);
575

576
577
578
579
580
	/* 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);
	}
581

582
583
584
	/* Classic call, attached to an account */
	else {
		account_id = getAccountFromCall(call_id);
585

586
		if (account_id == AccountNULL) {
587
			_warn ("Manager: Error: Call doesn't exists in off hold");
588
589
			return false;
		}
alexandresavard's avatar
alexandresavard committed
590

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

593
594
595
		is_rec = getAccountLink(account_id)->getCall(call_id)->isRecording();
		returnValue = getAccountLink(account_id)->offhold(call_id);
	}
596

597
598
599
600
601
	if (_dbus) {
		if (is_rec)
			_dbus->getCallManager()->callStateChanged(call_id, "UNHOLD_RECORD");
		else
			_dbus->getCallManager()->callStateChanged(call_id, "UNHOLD_CURRENT");
602

603
	}
604

605
	if (participToConference(call_id)) {
606

607
608
		AccountID currentAccountId;
		Call* call = NULL;
609

610
611
		currentAccountId = getAccountFromCall(call_id);
		call = getAccountLink(currentAccountId)->getCall(call_id);
612

613
		switchCall(call->getConfId());
614

615
616
617
618
	} else {
		switchCall(call_id);
		_audiodriver->flushMain();
	}
619

620
	return returnValue;
jpbl's avatar
jpbl committed
621
622
}

yanmorin's avatar
   
yanmorin committed
623
//THREAD=Main
624
625
626
bool ManagerImpl::transferCall (const CallID& call_id, const std::string& to) {
	AccountID accountid;
	bool returnValue;
627

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

630
	CallID current_call_id = getCurrentCallId();
631

632
	// Direct IP to IP call
633
	if (getConfigFromCall(call_id) == Call::IPtoIP) {
634
		returnValue = SIPVoIPLink::instance(AccountNULL)-> transfer(call_id, to);
635
	}
636
	// Classic call, attached to an account
637
	else {
638
639

	accountid = getAccountFromCall(call_id);
640

641
		if (accountid == AccountNULL) {
642
			_warn ("Manager: Call doesn't exists");
643
644
			return false;
		}
645

646
		returnValue = getAccountLink(accountid)->transfer(call_id, to);
647

648
	}
649

650
	// remove waiting call in case we make transfer without even answer
651
	removeWaitingCall(call_id);
652

653
	return returnValue;
jpbl's avatar
jpbl committed
654
655
}

656
void ManagerImpl::transferFailed () {
657
658
659

	_debug("UserAgent: Transfer failed");

660
661
	if (_dbus)
		_dbus->getCallManager()->transferFailed();
662
663
}

664
void ManagerImpl::transferSucceded () {
665
666
667

	_debug("UserAgent: Transfer succeded");

668
669
	if (_dbus)
		_dbus->getCallManager()->transferSucceded();
670
671
672

}

yanmorin's avatar
   
yanmorin committed
673
//THREAD=Main : Call:Incoming
674
675
676
bool ManagerImpl::refuseCall (const CallID& id) {
	AccountID accountid;
	bool returnValue;
677

678
679
	_debug("Manager: Refuse call %s", id.c_str());

680
	CallID current_call_id = getCurrentCallId();
681

682
	stopTone();
683

684
	int nbCalls = getCallList().size();
685

686
	// AudioLayer* audiolayer = getAudioDriver();
687

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

691
692
693
		AudioLayer* audiolayer = getAudioDriver();
		audiolayer->stopStream();
	}
Emmanuel Milou's avatar
Emmanuel Milou committed
694

695
	/* Direct IP to IP call */
696

697
698
699
	if (getConfigFromCall(id) == Call::IPtoIP) {
		returnValue = SIPVoIPLink::instance(AccountNULL)-> refuse(id);
	}
700

701
702
703
	/* Classic call, attached to an account */
	else {
		accountid = getAccountFromCall(id);
704

705
		if (accountid == AccountNULL) {
706
			_warn ("Manager: Call doesn't exists");
707
708
			return false;
		}
709

710
		returnValue = getAccountLink(accountid)->refuse(id);
711

712
713
		removeCallAccount(id);
	}
714

715
716
717
718
	// if the call was outgoing or established, we didn't refuse it
	// so the method did nothing
	if (returnValue) {
		removeWaitingCall(id);
719

720
721
722
		if (_dbus)
			_dbus->getCallManager()->callStateChanged(id, "HUNGUP");
	}
723

724
	return returnValue;
jpbl's avatar
jpbl committed
725
726
}

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

731
	Conference* conf = new Conference();
732

733
734
	conf->add(id1);
	conf->add(id2);
735

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

739
740
	// broadcast a signal over dbus
	_dbus->getCallManager()->conferenceCreated(conf->getConfID());
741

742
	return conf;
743
744
}

745
void ManagerImpl::removeConference (const ConfID& conference_id) {
746

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

749
	Conference* conf = NULL;
750

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

754
755
756
	if (iter != _conferencemap.end()) {
		conf = iter->second;
	}
757

758
	if (conf == NULL) {
Alexandre Savard's avatar
Alexandre Savard committed
759
		_error ("Manager: Error: Conference not found");
760
761
		return;
	}
762