managerimpl.cpp 117 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
#define DIRECT_IP_CALL	"IP CALL"
jpbl's avatar
jpbl committed
55
56
57
58
59
60

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

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

63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
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();
79

yanmorin's avatar
   
yanmorin committed
80
#ifdef TEST
81
82
83
84
	testAccountMap();
	loadAccountMap();
	testCallAccountMap();
	unloadAccountMap();
yanmorin's avatar
   
yanmorin committed
85
86
#endif

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

// never call if we use only the singleton...
92
93
94
95
96
ManagerImpl::~ManagerImpl (void) {
	// terminate();
	delete _cleaner;
	_cleaner = 0;
	_debug ("%s stop correctly.", PROGNAME);
jpbl's avatar
jpbl committed
97
98
}

99
void ManagerImpl::init () {
100

101
102
	// Load accounts, init map
	loadAccountMap();
103

104
	initVolume();
105

106
107
108
	if (_exist == 0) {
		_debug ("Cannot create config file in your home directory");
	}
109

110
	initAudioDriver();
jpbl's avatar
jpbl committed
111

112
	selectAudioDriver();
113

114
115
	// Initialize the list of supported audio codecs
	initAudioCodec();
116

117
	AudioLayer *audiolayer = getAudioDriver();
jpbl's avatar
jpbl committed
118

119
120
	if (audiolayer != 0) {
		unsigned int sampleRate = audiolayer->getSampleRate();
jpbl's avatar
jpbl committed
121

122
123
124
		_debugInit ("Load Telephone Tone");
		std::string country = getConfigString(PREFERENCES, ZONE_TONE);
		_telephoneTone = new TelephoneTone(country, sampleRate);
125

126
127
128
		_debugInit ("Loading DTMF key");
		_dtmfKey = new DTMF(sampleRate);
	}
129

130
131
	if (audiolayer == 0)
		audiolayer->stopStream();
132

133
134
	// Load the history
	_history->load_history(getConfigInt(PREFERENCES, CONFIG_HISTORY_LIMIT));
jpbl's avatar
jpbl committed
135
136
}

137
138
139
void ManagerImpl::terminate () {
	_debug ("ManagerImpl::terminate ");
	saveConfig();
jpbl's avatar
jpbl committed
140

141
	unloadAccountMap();
142

143
144
	_debug ("Unload DTMF Key ");
	delete _dtmfKey;
jpbl's avatar
jpbl committed
145

146
147
148
	_debug ("Unload Audio Driver ");
	delete _audiodriver;
	_audiodriver = NULL;
jpbl's avatar
jpbl committed
149

150
151
152
	_debug ("Unload Telephone Tone ");
	delete _telephoneTone;
	_telephoneTone = NULL;
153

154
155
	_debug ("Unload Audio Codecs ");
	_codecDescriptorMap.deleteHandlePointer();
156

jpbl's avatar
jpbl committed
157
158
}

159
160
bool ManagerImpl::isCurrentCall (const CallID& callId) {
	return (_currentCallId2 == callId ? true : false);
jpbl's avatar
jpbl committed
161
162
}

163
164
bool ManagerImpl::hasCurrentCall () {
	// _debug ("ManagerImpl::hasCurrentCall current call ID = %s", _currentCallId2.c_str());
165

166
167
168
	if (_currentCallId2 != "") {
		return true;
	}
169

170
	return false;
jpbl's avatar
jpbl committed
171
172
}

173
const CallID&
174
175
ManagerImpl::getCurrentCallId () {
	return _currentCallId2;
jpbl's avatar
jpbl committed
176
177
}

178
179
180
181
void ManagerImpl::switchCall (const CallID& id) {
	ost::MutexLock m(_currentCallMutex);
	_debug ("----- Switch current call id to %s -----", id.c_str());
	_currentCallId2 = id;
182

183
184
	/*
	 AudioLayer *al = getAudioDriver();
185

186
	 if (id != "") {
187

188
	 if(isConference(id)) {
189

190
	 Conference *conf;
191

192
193
194
195
196
197
198
199
200
	 ConferenceMap::iterator iter = _conferencemap.find(id);
	 if(iter != _conferencemap.end())
	 {
	 _debug("    set call recordable in audio layer");
	 conf = iter->second;
	 al->setRecorderInstance((Recordable*)conf);
	 }
	 }
	 else {
201

202
203
	 // set the recordable instance in audiolayer
	 AccountID account_id = getAccountFromCall(id);
204
205


206
207
	 Call *call = NULL;
	 call = getAccountLink (account_id)->getCall(id);
208

209
210
211
212
213
	 _debug("    set call recordable in audio layer");
	 al->setRecorderInstance((Recordable*)call);
	 }
	 }
	 */
jpbl's avatar
jpbl committed
214
215
216
217
218
}

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

221
222
bool ManagerImpl::outgoingCall (const std::string& account_id,
		const CallID& call_id, const std::string& to) {
223

224
225
226
	std::string pattern, to_cleaned;
	Call::CallConfiguration callConfig;
	SIPVoIPLink *siplink;
227

228
	_debug ("Manager: New outgoing call %s", call_id.c_str());
229

230
	CallID current_call_id = getCurrentCallId();
Emmanuel Milou's avatar
Emmanuel Milou committed
231

232
233
234
235
236
	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
237

238
	to_cleaned = _cleaner->clean(to);
239

240
241
	/* Check what kind of call we are dealing with */
	check_call_configuration(call_id, to_cleaned, &callConfig);
242

243
244
	// in any cases we have to detach from current communication
	if (hasCurrentCall()) {
245

246
		_debug ("    outgoingCall: Has current call (%s) put it onhold", current_call_id.c_str());
247

248
		// if this is not a conferenceand this and is not a conference participant
249

250
251
252
253
254
255
256
257
258
259
		if (!isConference(current_call_id) && !participToConference(
				current_call_id)) {
			_debug ("    outgoingCall: Put the current call (%s) on hold", current_call_id.c_str());
			onHoldCall(current_call_id);
		} else if (isConference(current_call_id) && !participToConference(
				call_id)) {
			_debug ("    outgoingCall: detach main participant from conference");
			detachParticipant(default_id, current_call_id);
		}
	}
260

261
	if (callConfig == Call::IPtoIP) {
262
		_debug ("Manager: Start IP to IP call");
263
264
		/* We need to retrieve the sip voiplink instance */
		siplink = SIPVoIPLink::instance("");
265

266
267
268
269
270
271
		if (siplink->new_ip_to_ip_call(call_id, to_cleaned)) {
			switchCall(call_id);
			return true;
		} else {
			callFailure(call_id);
		}
272

273
274
		return false;
	}
275

276
	if (!accountExists(account_id)) {
277
		_error ("Manager: Error: Account doesn't exist in new outgoing call");
278
279
		return false;
	}
280

281
	if (getAccountFromCall(call_id) != AccountNULL) {
282
		_error ("Manager: Error: Call id already exists in outgoing call");
283
284
		return false;
	}
285

286
	_debug ("Manager: Action: Adding Outgoing Call %s on account %s", call_id.data(), account_id.data());
287

288
	associateCallToAccount(call_id, account_id);
289

290
291
292
293
294
	if (getAccountLink(account_id)->newOutgoingCall(call_id, to_cleaned)) {
		switchCall(call_id);
		return true;
	} else {
		callFailure(call_id);
295
		_debug ("Manager: Error: An error occur, the call was not created");
296
	}
297

298
	return false;
jpbl's avatar
jpbl committed
299
300
}

yanmorin's avatar
   
yanmorin committed
301
//THREAD=Main : for outgoing Call
302
bool ManagerImpl::answerCall (const CallID& call_id) {
303

304
	_debug ("ManagerImpl::answerCall(%s)", call_id.c_str());
305

306
	stopTone();
307

308
309
	// store the current call id
	CallID current_call_id = getCurrentCallId();
310

311
	AccountID account_id = getAccountFromCall(call_id);
312

313
314
315
	if (account_id == AccountNULL) {
		_debug ("    answerCall: AccountId is null");
	}
316

317
	Call* call = NULL;
318

319
	call = getAccountLink(account_id)->getCall(call_id);
320

321
322
323
	if (call == NULL) {
		_debug ("    answerCall: Call is null");
	}
324

325
326
	// in any cases we have to detach from current communication
	if (hasCurrentCall()) {
327

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

331
332
333
334
335
		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);
		}
336

337
338
339
340
341
342
		// 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);
		}
343

344
	}
345

346
347
348
349
350
	if (!getAccountLink(account_id)->answer(call_id)) {
		// error when receiving...
		removeCallAccount(call_id);
		return false;
	}
Alexandre Savard's avatar
Alexandre Savard committed
351

352
353
354
	// if it was waiting, it's waiting no more
	if (_dbus)
		_dbus->getCallManager()->callStateChanged(call_id, "CURRENT");
Emmanuel Milou's avatar
Emmanuel Milou committed
355

356
357
	// std::string codecName = Manager::instance().getCurrentCodecName (call_id);
	// if (_dbus) _dbus->getCallManager()->currentSelectedCodec (call_id, codecName.c_str());
358

359
	removeWaitingCall(call_id);
360

361
362
	// if we dragged this call into a conference already
	if (participToConference(call_id)) {
363

364
365
		// AccountID currentAccountId;
		// Call* call = NULL;
366

367
368
		// currentAccountId = getAccountFromCall (call_id);
		// call = getAccountLink (currentAccountId)->getCall (call_id);
369

370
371
372
373
		switchCall(call->getConfId());
	} else {
		switchCall(call_id);
	}
374

375
	return true;
jpbl's avatar
jpbl committed
376
377
}

yanmorin's avatar
   
yanmorin committed
378
//THREAD=Main
379
bool ManagerImpl::hangupCall (const CallID& call_id) {
380
381
382

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

383
384
385
	PulseLayer *pulselayer;
	AccountID account_id;
	bool returnValue = true;
Emmanuel Milou's avatar
Emmanuel Milou committed
386

387
388
	// store the current call id
	CallID current_call_id = getCurrentCallId();
389

390
	stopTone();
yanmorin's avatar
   
yanmorin committed
391

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

395
396
	if (_dbus)
		_dbus->getCallManager()->callStateChanged(call_id, "HUNGUP");
397

398
	if (participToConference(call_id)) {
399

400
		Conference *conf = getConferenceFromCallID(call_id);
401

402
403
404
		if (conf != NULL) {
			// remove this participant
			removeParticipant(call_id);
405

406
407
			processRemainingParticipant(current_call_id, conf);
		}
408

409
410
411
412
413
	} else {
		// we are not participating to a conference, current call switched to ""
		if (!isConference(current_call_id))
			switchCall("");
	}
414

415
416
417
418
419
420
421
	/* 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);
422

423
		if (account_id == AccountNULL) {
424

425
			_error ("Manager: Error: account id is NULL in hangup");
426
427
			returnValue = false;
		} else {
428

429
430
431
			returnValue = getAccountLink(account_id)->hangup(call_id);
			removeCallAccount(call_id);
		}
432
	}
433

434
	int nbCalls = getCallList().size();
435

436
	AudioLayer *audiolayer = getAudioDriver();
437

438
439
	// stop streams
	if (audiolayer && (nbCalls <= 0)) {
440
		_debug ("Manager: stop audio stream, ther is only %i call(s) remaining", nbCalls);
441
442
		audiolayer->stopStream();
	}
443

444
445
446
	if (_audiodriver->getLayerType() == PULSEAUDIO) {
		pulselayer = dynamic_cast<PulseLayer *> (getAudioDriver());
	}
447

448
	return returnValue;
jpbl's avatar
jpbl committed
449
450
}

451
bool ManagerImpl::hangupConference (const ConfID& id) {
452
453

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

455
456
	Conference *conf;
	ConferenceMap::iterator iter_conf = _conferencemap.find(id);
Alexandre Savard's avatar
Alexandre Savard committed
457

458
	AccountID currentAccountId;
Alexandre Savard's avatar
Alexandre Savard committed
459

460
	// Call* call = NULL;
Alexandre Savard's avatar
Alexandre Savard committed
461
462


463
464
	if (iter_conf != _conferencemap.end()) {
		conf = iter_conf->second;
Alexandre Savard's avatar
Alexandre Savard committed
465

466
467
		ParticipantSet participants = conf->getParticipantList();
		ParticipantSet::iterator iter_participant = participants.begin();
Alexandre Savard's avatar
Alexandre Savard committed
468

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

472
			hangupCall(*iter_participant);
Alexandre Savard's avatar
Alexandre Savard committed
473

474
			iter_participant++;
475

476
		}
Alexandre Savard's avatar
Alexandre Savard committed
477

478
	}
Alexandre Savard's avatar
Alexandre Savard committed
479

480
	switchCall("");
481

482
	return true;
Alexandre Savard's avatar
Alexandre Savard committed
483
484
}

yanmorin's avatar
   
yanmorin committed
485
//THREAD=Main
486
487
488
bool ManagerImpl::cancelCall (const CallID& id) {
	AccountID accountid;
	bool returnValue;
yanmorin's avatar
   
yanmorin committed
489

490
491
	_debug("Manager: Cancel call");

492
	stopTone();
493

494
	/* Direct IP to IP call */
495

496
497
498
	if (getConfigFromCall(id) == Call::IPtoIP) {
		returnValue = SIPVoIPLink::instance(AccountNULL)->cancel(id);
	}
Emmanuel Milou's avatar
Emmanuel Milou committed
499

500
501
502
	/* Classic call, attached to an account */
	else {
		accountid = getAccountFromCall(id);
503

504
505
506
507
		if (accountid == AccountNULL) {
			_debug ("! Manager Cancel Call: Call doesn't exists");
			return false;
		}
508

509
		returnValue = getAccountLink(accountid)->cancel(id);
510

511
512
		removeCallAccount(id);
	}
513

514
515
	// it could be a waiting call?
	removeWaitingCall(id);
516

517
	switchCall("");
Emmanuel Milou's avatar
Emmanuel Milou committed
518

519
	return returnValue;
jpbl's avatar
jpbl committed
520
521
}

yanmorin's avatar
   
yanmorin committed
522
//THREAD=Main
523
524
525
bool ManagerImpl::onHoldCall (const CallID& call_id) {
	AccountID account_id;
	bool returnValue;
yanmorin's avatar
   
yanmorin committed
526

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

529
	stopTone();
530

531
	CallID current_call_id = getCurrentCallId();
532

533

534
	/* Direct IP to IP call */
535

536
537
538
	if (getConfigFromCall(call_id) == Call::IPtoIP) {
		returnValue = SIPVoIPLink::instance(AccountNULL)-> onhold(call_id);
	}
539

540
541
542
	/* Classic call, attached to an account */
	else {
		account_id = getAccountFromCall(call_id);
543

544
		if (account_id == AccountNULL) {
545
			_debug ("Manager: Account ID %s or callid %s doesn't exists in call onHold", account_id.c_str(), call_id.c_str());
546
547
			return false;
		}
548

549
550
		returnValue = getAccountLink(account_id)->onhold(call_id);
	}
551

552
	removeWaitingCall(call_id);
553

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

556
	if (current_call_id == call_id) {
557

558
559
		switchCall("");
	}
560

561
562
	if (_dbus)
		_dbus->getCallManager()->callStateChanged(call_id, "HOLD");
563

564
	return returnValue;
yanmorin's avatar
   
yanmorin committed
565
566
567
}

//THREAD=Main
568
bool ManagerImpl::offHoldCall (const CallID& call_id) {
569

570
571
572
	AccountID account_id;
	bool returnValue, is_rec;
	std::string codecName;
573

574
	is_rec = false;
575

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

578
	stopTone();
579

580
	CallID current_call_id = getCurrentCallId();
581

582
	//Place current call on hold if it isn't
583

584
585
586
587
588
589
590
591
592
593
	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
594

595
596
	// switch current call id to id since sipvoip link need it to amke a call
	// switchCall(id);
597

598
599
600
601
602
	/* 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);
	}
603

604
605
606
	/* Classic call, attached to an account */
	else {
		account_id = getAccountFromCall(call_id);
607

608
		if (account_id == AccountNULL) {
609
			_warn ("Manager: Error: Call doesn't exists in off hold");
610
611
			return false;
		}
alexandresavard's avatar
alexandresavard committed
612

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

615
616
617
		is_rec = getAccountLink(account_id)->getCall(call_id)->isRecording();
		returnValue = getAccountLink(account_id)->offhold(call_id);
	}
618

619
620
621
622
623
	if (_dbus) {
		if (is_rec)
			_dbus->getCallManager()->callStateChanged(call_id, "UNHOLD_RECORD");
		else
			_dbus->getCallManager()->callStateChanged(call_id, "UNHOLD_CURRENT");
624

625
	}
626

627
	if (participToConference(call_id)) {
628

629
630
		AccountID currentAccountId;
		Call* call = NULL;
631

632
633
		currentAccountId = getAccountFromCall(call_id);
		call = getAccountLink(currentAccountId)->getCall(call_id);
634

635
		switchCall(call->getConfId());
636

637
638
639
640
	} else {
		switchCall(call_id);
		_audiodriver->flushMain();
	}
641

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

yanmorin's avatar
   
yanmorin committed
645
//THREAD=Main
646
647
648
bool ManagerImpl::transferCall (const CallID& call_id, const std::string& to) {
	AccountID accountid;
	bool returnValue;
649

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

652
	CallID current_call_id = getCurrentCallId();
653

654
	// Direct IP to IP call
655
	if (getConfigFromCall(call_id) == Call::IPtoIP) {
656
		returnValue = SIPVoIPLink::instance(AccountNULL)-> transfer(call_id, to);
657
	}
658
	// Classic call, attached to an account
659
	else {
660
661

	accountid = getAccountFromCall(call_id);
662

663
		if (accountid == AccountNULL) {
664
			_warn ("Manager: Call doesn't exists");
665
666
			return false;
		}
667

668
		returnValue = getAccountLink(accountid)->transfer(call_id, to);
669

670
	}
671

672
	// remove waiting call in case we make transfer without even answer
673
	removeWaitingCall(call_id);
674

675
	return returnValue;
jpbl's avatar
jpbl committed
676
677
}

678
void ManagerImpl::transferFailed () {
679
680
681

	_debug("UserAgent: Transfer failed");

682
683
	if (_dbus)
		_dbus->getCallManager()->transferFailed();
684
685
}

686
void ManagerImpl::transferSucceded () {
687
688
689

	_debug("UserAgent: Transfer succeded");

690
691
	if (_dbus)
		_dbus->getCallManager()->transferSucceded();
692
693
694

}

yanmorin's avatar
   
yanmorin committed
695
//THREAD=Main : Call:Incoming
696
697
698
bool ManagerImpl::refuseCall (const CallID& id) {
	AccountID accountid;
	bool returnValue;
699

700
701
	_debug("Manager: Refuse call %s", id.c_str());

702
	CallID current_call_id = getCurrentCallId();
703

704
	stopTone();
705

706
	int nbCalls = getCallList().size();
707

708
	// AudioLayer* audiolayer = getAudioDriver();
709

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

713
714
715
		AudioLayer* audiolayer = getAudioDriver();
		audiolayer->stopStream();
	}
Emmanuel Milou's avatar
Emmanuel Milou committed
716

717
	/* Direct IP to IP call */
718

719
720
721
	if (getConfigFromCall(id) == Call::IPtoIP) {
		returnValue = SIPVoIPLink::instance(AccountNULL)-> refuse(id);
	}
722

723
724
725
	/* Classic call, attached to an account */
	else {
		accountid = getAccountFromCall(id);
726

727
		if (accountid == AccountNULL) {
728
			_warn ("Manager: Call doesn't exists");
729
730
			return false;
		}
731

732
		returnValue = getAccountLink(accountid)->refuse(id);
733

734
735
		removeCallAccount(id);
	}
736

737
738
739
740
	// if the call was outgoing or established, we didn't refuse it
	// so the method did nothing
	if (returnValue) {
		removeWaitingCall(id);
741

742
743
744
		if (_dbus)
			_dbus->getCallManager()->callStateChanged(id, "HUNGUP");
	}
745

746
	return returnValue;
jpbl's avatar
jpbl committed
747
748
}

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

753
	Conference* conf = new Conference();
754

755
756
	conf->add(id1);
	conf->add(id2);
757

758
759
	// broadcast a signal over dbus
	_dbus->getCallManager()->conferenceCreated(conf->getConfID());
760

761
	return conf;
762
763
}

764
void ManagerImpl::removeConference (const ConfID& conference_id) {
765

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

768
	Conference* conf = NULL;
769

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

773
774
775
	if (iter != _conferencemap.end()) {
		conf = iter->second;
	}
776

777
	if (conf == NULL) {
Alexandre Savard's avatar
Alexandre Savard committed
778
		_error ("Manager: Error: Conference not found");