managerimpl.cpp 129 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
jpbl's avatar
jpbl committed
54
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
ManagerImpl::ManagerImpl (void)
64
        : _hasTriedToRegister (false)
Yun Liu's avatar
Yun Liu committed
65
        , _config()
66
        , _currentCallId2()
67
        , _currentCallMutex()
68
69
70
        , _codecBuilder (NULL)
        , _audiodriver (NULL)
        , _dtmfKey (NULL)
Yun Liu's avatar
Yun Liu committed
71
        , _codecDescriptorMap()
72
        , _toneMutex()
73
        , _telephoneTone (NULL)
Yun Liu's avatar
Yun Liu committed
74
        , _audiofile()
75
76
        , _spkr_volume (0)
        , _mic_volume (0)
77
        , _mutex()
78
        , _dbus (NULL)
Yun Liu's avatar
Yun Liu committed
79
        , _waitingCall()
80
        , _waitingCallMutex()
81
82
83
84
        , _nbIncomingWaitingCall (0)
        , _path ("")
        , _exist (0)
        , _setupLoaded (false)
Yun Liu's avatar
Yun Liu committed
85
        , _callAccountMap()
86
        , _callAccountMapMutex()
Emmanuel Milou's avatar
Emmanuel Milou committed
87
        , _callConfigMap()
Yun Liu's avatar
Yun Liu committed
88
        , _accountMap()
89
        , _cleaner (NULL)
90
        , _history (NULL)
91
        , _directIpAccount (NULL)
jpbl's avatar
jpbl committed
92
{
93

94
    // initialize random generator for call id
95
    srand (time (NULL));
yanmorin's avatar
   
yanmorin committed
96

97
    _cleaner = new NumberCleaner ();
98
    _history = new HistoryManager ();
99

yanmorin's avatar
   
yanmorin committed
100
#ifdef TEST
101
102
103
104
    testAccountMap();
    loadAccountMap();
    testCallAccountMap();
    unloadAccountMap();
yanmorin's avatar
   
yanmorin committed
105
106
#endif

107
108
    // should be call before initConfigFile
    // loadAccountMap();, called in init() now.
jpbl's avatar
jpbl committed
109
110
111
}

// never call if we use only the singleton...
112
ManagerImpl::~ManagerImpl (void)
jpbl's avatar
jpbl committed
113
{
114
    // terminate();
115
116
117
    delete _cleaner;
    _cleaner=0;
    _debug ("%s stop correctly.\n", PROGNAME);
jpbl's avatar
jpbl committed
118
119
}

120
void
121
ManagerImpl::init()
jpbl's avatar
jpbl committed
122
{
123
124
    // Load accounts, init map
    loadAccountMap();
125

126
    initVolume();
127

128
    if (_exist == 0) {
129
        _debug ("Cannot create config file in your home directory\n");
130
    }
131

132
    initAudioDriver();
133

134
    selectAudioDriver();
jpbl's avatar
jpbl committed
135

136
137
    // Initialize the list of supported audio codecs
    initAudioCodec();
138

139
    AudioLayer *audiolayer = getAudioDriver();
140

141
    if (audiolayer != 0) {
142
        unsigned int sampleRate = audiolayer->getSampleRate();
jpbl's avatar
jpbl committed
143

144
145
146
        _debugInit ("Load Telephone Tone");
        std::string country = getConfigString (PREFERENCES, ZONE_TONE);
        _telephoneTone = new TelephoneTone (country, sampleRate);
jpbl's avatar
jpbl committed
147

148
149
        _debugInit ("Loading DTMF key");
        _dtmfKey = new DTMF (sampleRate);
150
    }
151
152

    if (audiolayer == 0)
153
        audiolayer->stopStream();
154
155
156


    // Load the history
157
    _history->load_history (getConfigInt (PREFERENCES, CONFIG_HISTORY_LIMIT));
jpbl's avatar
jpbl committed
158
159
160
161
}

void ManagerImpl::terminate()
{
162
    _debug ("ManagerImpl::terminate \n");
163
    saveConfig();
jpbl's avatar
jpbl committed
164

165
    unloadAccountMap();
166

167
    _debug ("Unload DTMF Key \n");
168
    delete _dtmfKey;
jpbl's avatar
jpbl committed
169

170
171
172
    _debug ("Unload Audio Driver \n");
    delete _audiodriver;
    _audiodriver = NULL;
jpbl's avatar
jpbl committed
173

174
175
176
    _debug ("Unload Telephone Tone \n");
    delete _telephoneTone;
    _telephoneTone = NULL;
177

178
    _debug ("Unload Audio Codecs \n");
179
    _codecDescriptorMap.deleteHandlePointer();
180

jpbl's avatar
jpbl committed
181
182
}

yanmorin's avatar
   
yanmorin committed
183
bool
184
185
186
ManagerImpl::isCurrentCall (const CallID& callId)
{
    return (_currentCallId2 == callId ? true : false);
jpbl's avatar
jpbl committed
187
188
}

yanmorin's avatar
   
yanmorin committed
189
bool
190
191
ManagerImpl::hasCurrentCall()
{
192
    _debug ("ManagerImpl::hasCurrentCall current call ID = %s\n", _currentCallId2.c_str());
193
194
195
196
197
198

    if (_currentCallId2 != "") {
        return true;
    }

    return false;
jpbl's avatar
jpbl committed
199
200
}

201
const CallID&
202
203
204
ManagerImpl::getCurrentCallId()
{
    return _currentCallId2;
jpbl's avatar
jpbl committed
205
206
207
}

void
208
209
210
ManagerImpl::switchCall (const CallID& id)
{
    ost::MutexLock m (_currentCallMutex);
211
    _debug("------------------------- SWITCH %s ---------------------------\n", id.c_str());
212
    _currentCallId2 = id;
213

214
215
    /*
    AudioLayer *al = getAudioDriver();
216

217
    if (id != "") {
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
	if(isConference(id)) {

	    Conference *conf;

	    ConferenceMap::iterator iter = _conferencemap.find(id);
	    if(iter != _conferencemap.end())
	    {
		_debug("    set call recordable in audio layer\n");
		conf = iter->second;
		al->setRecorderInstance((Recordable*)conf);
	    }
	}
	else {

	    // set the recordable instance in audiolayer
	    AccountID account_id = getAccountFromCall(id);
	

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

	    _debug("    set call recordable in audio layer\n");
	    al->setRecorderInstance((Recordable*)call);
	}
    }
    */
jpbl's avatar
jpbl committed
245
246
247
248
249
250
}


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

253
bool
254
ManagerImpl::outgoingCall (const std::string& account_id, const CallID& call_id, const std::string& to)
yanmorin's avatar
   
yanmorin committed
255
{
256
    std::string pattern, to_cleaned;
Emmanuel Milou's avatar
Emmanuel Milou committed
257
258
    Call::CallConfiguration callConfig;
    SIPVoIPLink *siplink;
259

260
261
262
    _debug ("ManagerImpl::outgoingCall(%s)\n", call_id.c_str());

    CallID current_call_id = getCurrentCallId();
263

264
265
    if (getConfigString (HOOKS, PHONE_NUMBER_HOOK_ENABLED) ==  "1")
        _cleaner->set_phone_number_prefix (getConfigString (HOOKS, PHONE_NUMBER_HOOK_ADD_PREFIX));
266
267
    else
        _cleaner->set_phone_number_prefix ("");
268

269
    to_cleaned = _cleaner->clean (to);
Emmanuel Milou's avatar
Emmanuel Milou committed
270
271

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

274
    // in any cases we have to detach from current communication
275
276
    if (hasCurrentCall()) {
	
277
278
	_debug ("    outgoingCall: Has current call (%s) put it onhold\n", current_call_id.c_str());

279
	// if this is not a conferenceand this and is not a conference participant
280
	if (!isConference(current_call_id) && !participToConference(current_call_id))
281
	{
282
283
	    _debug ("    outgoingCall: Put the current call (%s) on hold\n", current_call_id.c_str());
	    onHoldCall (current_call_id);
284
	}
285
	else if (isConference(current_call_id) && !participToConference(call_id))
286
	{
287
	    _debug ("    outgoingCall: detach main participant from conference\n");
288
	    detachParticipant(default_id, current_call_id);
289
290
291
	}
    }

Emmanuel Milou's avatar
Emmanuel Milou committed
292
    if (callConfig == Call::IPtoIP) {
293
        _debug ("    outgoingCall: Start IP to IP call\n");
Emmanuel Milou's avatar
Emmanuel Milou committed
294
        /* We need to retrieve the sip voiplink instance */
295
296
        siplink = SIPVoIPLink::instance ("");

297
298
        if (siplink->new_ip_to_ip_call (call_id, to_cleaned)) {
            switchCall (call_id);
Emmanuel Milou's avatar
Emmanuel Milou committed
299
            return true;
300
        } else {
301
            callFailure (call_id);
Emmanuel Milou's avatar
Emmanuel Milou committed
302
        }
303

Emmanuel Milou's avatar
Emmanuel Milou committed
304
        return false;
305
    }
306

307
    if (!accountExists (account_id)) {
308
        _debug ("! Manager Error: Outgoing Call: account doesn't exist\n");
Emmanuel Milou's avatar
Emmanuel Milou committed
309
310
        return false;
    }
311

312
    if (getAccountFromCall (call_id) != AccountNULL) {
313
        _debug ("! Manager Error: Outgoing Call: call id already exists\n");
Emmanuel Milou's avatar
Emmanuel Milou committed
314
315
        return false;
    }
316
317


318
    _debug ("- Manager Action: Adding Outgoing Call %s on account %s\n", call_id.data(), account_id.data());
319

320
    associateCallToAccount (call_id, account_id);
321

322
323
    if (getAccountLink (account_id)->newOutgoingCall (call_id, to_cleaned)) {
        switchCall (call_id);
Emmanuel Milou's avatar
Emmanuel Milou committed
324
325
        return true;
    } else {
326
        callFailure (call_id);
327
        _debug ("! Manager Error: An error occur, the call was not created\n");
Emmanuel Milou's avatar
Emmanuel Milou committed
328
    }
329

Emmanuel Milou's avatar
Emmanuel Milou committed
330
    return false;
jpbl's avatar
jpbl committed
331
332
}

yanmorin's avatar
   
yanmorin committed
333
//THREAD=Main : for outgoing Call
334
bool
335
ManagerImpl::answerCall (const CallID& call_id)
jpbl's avatar
jpbl committed
336
{
337

338
    _debug("ManagerImpl::answerCall(%s)", call_id.c_str());
339
340

    stopTone (true);
341

342
343
344
345
346
    // store the current call id
    CallID current_call_id = getCurrentCallId();

    AccountID account_id = getAccountFromCall (call_id);
    if(account_id == AccountNULL) {
347
        _debug("    answerCall: AccountId is null\n");
348
349
    }
    
350
351
352
    Call* call = NULL;
    call = getAccountLink (account_id)->getCall (call_id);
    if (call == NULL) {
353
        _debug("    answerCall: Call is null\n");
354
    }
355
356

    // in any cases we have to detach from current communication
Alexandre Savard's avatar
Alexandre Savard committed
357
358
    if (hasCurrentCall()) {
	
359
	_debug ("    answerCall: Currently conversing with %s\n", current_call_id.c_str());	
360
361
	// if it is not a conference and is not a conference participant
	if (!isConference(current_call_id) && !participToConference(current_call_id))
Alexandre Savard's avatar
Alexandre Savard committed
362
	{
363
364
	    _debug ("    answerCall: Put the current call (%s) on hold\n", current_call_id.c_str());
	    onHoldCall (current_call_id);
Alexandre Savard's avatar
Alexandre Savard committed
365
	}
366
367
	// if we are talking to a conference and we are answering an incoming call
	else if (isConference(current_call_id) && !participToConference(call_id))
Alexandre Savard's avatar
Alexandre Savard committed
368
	{
369
	    _debug ("    answerCall: Detach main participant from conference\n");
370
	    detachParticipant(default_id, current_call_id);
Alexandre Savard's avatar
Alexandre Savard committed
371
	}
372

Alexandre Savard's avatar
Alexandre Savard committed
373
    }
374

375
    if (!getAccountLink (account_id)->answer (call_id)) {
376
        // error when receiving...
377
        removeCallAccount (call_id);
378
379
        return false;
    }
Alexandre Savard's avatar
Alexandre Savard committed
380

381
    // if it was waiting, it's waiting no more
382
    if (_dbus) _dbus->getCallManager()->callStateChanged (call_id, "CURRENT");
383
        
384
385
    std::string codecName = Manager::instance().getCurrentCodecName (call_id);
    if (_dbus) _dbus->getCallManager()->currentSelectedCodec (call_id, codecName.c_str());
386
        
387
    removeWaitingCall (call_id);
388

389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
    // if we dragged this call into a conference already
    if ( participToConference(call_id) ) {
	 
	// AccountID currentAccountId;
        // Call* call = NULL;

	// currentAccountId = getAccountFromCall (call_id);
	// call = getAccountLink (currentAccountId)->getCall (call_id);
	
	switchCall(call->getConfId());
    }
    else
    {
	switchCall(call_id);
    }
404
405

    return true;
jpbl's avatar
jpbl committed
406
407
}

yanmorin's avatar
   
yanmorin committed
408
//THREAD=Main
409
bool
410
ManagerImpl::hangupCall (const CallID& call_id)
jpbl's avatar
jpbl committed
411
{
412
    _debug ("ManagerImpl::hangupCall(%s)\n", call_id.c_str());
413
    PulseLayer *pulselayer;
414
    AccountID account_id;
415
    bool returnValue;
416
    AudioLayer *audiolayer;
Emmanuel Milou's avatar
Emmanuel Milou committed
417

418
    // store the current call id
419
420
    CallID current_call_id = getCurrentCallId();

421
    stopTone (false);
422
    // switchCall (call_id);
yanmorin's avatar
   
yanmorin committed
423

424
    /* Broadcast a signal over DBus */
425
426
    _debug("    hangupCall: Send DBUS call state change (HUNGUP) for id %s\n", call_id.c_str());
    if (_dbus) _dbus->getCallManager()->callStateChanged (call_id, "HUNGUP");
427

428
429
    int nbCalls = getCallList().size();

430
    audiolayer = getAudioDriver();
431
    // stop streamx
432
433
    if (! (nbCalls >= 1))
    {
434
	_debug("    hangupCall: stop audio stream, ther is only %i call(s) remaining\n", nbCalls);
435
        audiolayer->stopStream();
436
    }
437
   
438
    if(participToConference(call_id))
439
    {
440

441
	Conference *conf = getConferenceFromCallID(call_id);
442

443
	if(conf != NULL)
444
	{
445
446
	    // remove this participant
	    removeParticipant(call_id);
447

448
	    processRemainingParticipant(current_call_id, conf);
449
	}
450
    }
451
452
453
454
455
456
    else
    {
	// we are not participating to a conference, current call switched to ""
	if (!isConference(current_call_id))
	    switchCall("");
    }
457

Emmanuel Milou's avatar
Emmanuel Milou committed
458
    /* Direct IP to IP call */
459
460
    if (getConfigFromCall (call_id) == Call::IPtoIP) {
        returnValue = SIPVoIPLink::instance (AccountNULL)->hangup (call_id);
461
    }
Emmanuel Milou's avatar
Emmanuel Milou committed
462
    /* Classic call, attached to an account */
463
    else {
464
        account_id = getAccountFromCall (call_id);
465

466
        if (account_id == AccountNULL) 
467
	{
468
            _debug ("! Manager Hangup Call: Call doesn't exists\n");
Emmanuel Milou's avatar
Emmanuel Milou committed
469
470
            return false;
        }
471

472
        returnValue = getAccountLink (account_id)->hangup (call_id);
473

474
        removeCallAccount (call_id);
Emmanuel Milou's avatar
Emmanuel Milou committed
475
    }
476

477
    if (_audiodriver->getLayerType() == PULSEAUDIO && getConfigInt (PREFERENCES , CONFIG_PA_VOLUME_CTRL)) {
478
        pulselayer = dynamic_cast<PulseLayer *> (getAudioDriver());
479
480

        if (pulselayer)  pulselayer->restorePulseAppsVolume();
481
    }
482

483
    return returnValue;
jpbl's avatar
jpbl committed
484
485
}

Alexandre Savard's avatar
Alexandre Savard committed
486
487
488
489
490
491
492
493
494
495
496

bool
ManagerImpl::hangupConference (const ConfID& id)
{
    _debug ("ManagerImpl::hangupConference()\n");

    Conference *conf;
    ConferenceMap::iterator iter_conf = _conferencemap.find(id);

    AccountID currentAccountId;

497
    // Call* call = NULL;
Alexandre Savard's avatar
Alexandre Savard committed
498
499
500
501
502

    if(iter_conf != _conferencemap.end())
    {
	conf = iter_conf->second;
 
503
504
	ParticipantSet participants = conf->getParticipantList();
	ParticipantSet::iterator iter_participant = participants.begin();
Alexandre Savard's avatar
Alexandre Savard committed
505

506
	while(iter_participant != participants.end())
Alexandre Savard's avatar
Alexandre Savard committed
507
	{
508
	    _debug("ManagerImpl::hangupConference participant %s\n", (*iter_participant).c_str());
Alexandre Savard's avatar
Alexandre Savard committed
509

510
	    hangupCall (*iter_participant);
Alexandre Savard's avatar
Alexandre Savard committed
511
512
513
514
515
516
517

	    iter_participant++;

	}

    }

518
519
    switchCall ("");

Alexandre Savard's avatar
Alexandre Savard committed
520
521
522
523
    return true;
}


yanmorin's avatar
   
yanmorin committed
524
//THREAD=Main
525
bool
yanmorin's avatar
   
yanmorin committed
526
ManagerImpl::cancelCall (const CallID& id)
jpbl's avatar
jpbl committed
527
{
Emmanuel Milou's avatar
Emmanuel Milou committed
528
529
    AccountID accountid;
    bool returnValue;
yanmorin's avatar
   
yanmorin committed
530

531
    stopTone (true);
532

Emmanuel Milou's avatar
Emmanuel Milou committed
533
    /* Direct IP to IP call */
534

Emmanuel Milou's avatar
Emmanuel Milou committed
535
    if (getConfigFromCall (id) == Call::IPtoIP) {
536
        returnValue = SIPVoIPLink::instance (AccountNULL)->cancel (id);
Emmanuel Milou's avatar
Emmanuel Milou committed
537
538
539
    }

    /* Classic call, attached to an account */
540
    else {
541
542
        accountid = getAccountFromCall (id);

Emmanuel Milou's avatar
Emmanuel Milou committed
543
        if (accountid == AccountNULL) {
544
            _debug ("! Manager Cancel Call: Call doesn't exists\n");
Emmanuel Milou's avatar
Emmanuel Milou committed
545
546
            return false;
        }
547
548
549
550

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

        removeCallAccount (id);
Emmanuel Milou's avatar
Emmanuel Milou committed
551
    }
552

Emmanuel Milou's avatar
Emmanuel Milou committed
553
    // it could be a waiting call?
554
555
556
    removeWaitingCall (id);

    switchCall ("");
Emmanuel Milou's avatar
Emmanuel Milou committed
557
558

    return returnValue;
jpbl's avatar
jpbl committed
559
560
}

yanmorin's avatar
   
yanmorin committed
561
//THREAD=Main
562
bool
563
ManagerImpl::onHoldCall (const CallID& call_id)
jpbl's avatar
jpbl committed
564
{
565
    AccountID account_id;
Emmanuel Milou's avatar
Emmanuel Milou committed
566
    bool returnValue;
yanmorin's avatar
   
yanmorin committed
567

568
    _debug("ManagerImpl::onHoldCall(%s)\n", call_id.c_str());
569

570
    stopTone (true);
571
    
572
573
574
    CallID current_call_id = getCurrentCallId();

    _debug("    onHoldCall: try to put call %s on hold\n", call_id.c_str());
575

576
    /* Direct IP to IP call */
577
    if (getConfigFromCall (call_id) == Call::IPtoIP) {
578
	returnValue = SIPVoIPLink::instance (AccountNULL)-> onhold (call_id);
Emmanuel Milou's avatar
Emmanuel Milou committed
579
    }
580

Emmanuel Milou's avatar
Emmanuel Milou committed
581
    /* Classic call, attached to an account */
582
    else {
583
584
585
586
587
588
	account_id = getAccountFromCall (call_id);
	    
	if (account_id == AccountNULL) {
	    _debug ("    onHoldCall: Account ID %s or callid %s doesn't exists\n", account_id.c_str(), call_id.c_str());
	    return false;
	}
589

590
	returnValue = getAccountLink (account_id)->onhold (call_id);
Emmanuel Milou's avatar
Emmanuel Milou committed
591
    }
592

593
    removeWaitingCall (call_id);
594

595
    // keeps current call id if the action is not holding this call or a new outgoing call
596
597
598
599
    if(current_call_id == call_id) {

	switchCall ("");
    }
600

601
    if (_dbus) _dbus->getCallManager()->callStateChanged (call_id, "HOLD");
602

Emmanuel Milou's avatar
Emmanuel Milou committed
603
    return returnValue;
yanmorin's avatar
   
yanmorin committed
604
605
606
}

//THREAD=Main
607
bool
608
ManagerImpl::offHoldCall (const CallID& call_id)
yanmorin's avatar
   
yanmorin committed
609
{
610

611
    AccountID account_id;
612
    bool returnValue, is_rec;
Emmanuel Milou's avatar
Emmanuel Milou committed
613
    std::string codecName;
614
615
616


    _debug ("ManagerImpl::offHoldCall(%s)\n", call_id.c_str());
617

618
    stopTone (false);
619

620
    CallID current_call_id = getCurrentCallId();
621

622
    //Place current call on hold if it isn't
623
    if (hasCurrentCall()) 
624
    {
625
626
627
	// if this is not a conferenceand this and is not a conference participant
	if (!isConference(current_call_id) && !participToConference(current_call_id))
	{
628
	    _debug ("    offHoldCall: put current call (%s) on hold\n", current_call_id.c_str());
629
630
	    onHoldCall (current_call_id);
	}
631
	else if (isConference(current_call_id) && !participToConference(call_id))
632
	{
633
	    _debug ("    offHoldCall Put current conference (%s) on hold\n", current_call_id.c_str());
634
	    detachParticipant(default_id, current_call_id);
635
	}
Emmanuel Milou's avatar
Emmanuel Milou committed
636
    }
637

638
    // switch current call id to id since sipvoip link need it to amke a call 
639
    // switchCall(id);
640

Emmanuel Milou's avatar
Emmanuel Milou committed
641
    /* Direct IP to IP call */
642
    if (getConfigFromCall (call_id) == Call::IPtoIP) {
643
        // is_rec = SIPVoIPLink::instance (AccountNULL)-> isRecording (call_id);
644
        returnValue = SIPVoIPLink::instance (AccountNULL)-> offhold (call_id);
Emmanuel Milou's avatar
Emmanuel Milou committed
645
    }
alexandresavard's avatar
alexandresavard committed
646

Emmanuel Milou's avatar
Emmanuel Milou committed
647
    /* Classic call, attached to an account */
648
    else {
649
        account_id = getAccountFromCall (call_id);
650

651
        if (account_id == AccountNULL) {
652
            _debug ("Manager OffHold Call: Call doesn't exists\n");
Emmanuel Milou's avatar
Emmanuel Milou committed
653
654
            return false;
        }
655

656
        _debug ("Setting OFFHOLD, Account %s, callid %s\n", account_id.c_str(), call_id.c_str());
657

658
        is_rec = getAccountLink (account_id)->getCall(call_id)->isRecording();
659
        returnValue = getAccountLink (account_id)->offhold (call_id);
Emmanuel Milou's avatar
Emmanuel Milou committed
660
    }
alexandresavard's avatar
alexandresavard committed
661

662

663
    if (_dbus) {
664
        if (is_rec)
665
            _dbus->getCallManager()->callStateChanged (call_id, "UNHOLD_RECORD");
666
        else
667
            _dbus->getCallManager()->callStateChanged (call_id, "UNHOLD_CURRENT");
668

Emmanuel Milou's avatar
Emmanuel Milou committed
669
    }
670

671
    if ( participToConference(call_id) ) {
672
673
674
675
	 
	AccountID currentAccountId;
        Call* call = NULL;

676
677
	currentAccountId = getAccountFromCall (call_id);
	call = getAccountLink (currentAccountId)->getCall (call_id);
678
679
	
	switchCall(call->getConfId());
680
	
681
    }
682
683
684
    else
    {
	switchCall(call_id);
685
	_audiodriver->getMainBuffer()->flush(default_id);
686
    }
687

688
    codecName = getCurrentCodecName (call_id);
689
    // _debug("ManagerImpl::hangupCall(): broadcast codec name %s \n",codecName.c_str());
690

691
    if (_dbus) _dbus->getCallManager()->currentSelectedCodec (call_id,codecName.c_str());
692

693
694
    

Emmanuel Milou's avatar
Emmanuel Milou committed
695
    return returnValue;
jpbl's avatar
jpbl committed
696
697
}

yanmorin's avatar
   
yanmorin committed
698
//THREAD=Main
699
700
bool
ManagerImpl::transferCall (const CallID& id, const std::string& to)
jpbl's avatar
jpbl committed
701
{
Emmanuel Milou's avatar
Emmanuel Milou committed
702
703
    AccountID accountid;
    bool returnValue;
704

705
    stopTone (true);
706

Emmanuel Milou's avatar
Emmanuel Milou committed
707
    /* Direct IP to IP call */
708

Emmanuel Milou's avatar
Emmanuel Milou committed
709
    if (getConfigFromCall (id) == Call::IPtoIP) {
710
        returnValue = SIPVoIPLink::instance (AccountNULL)-> transfer (id, to);
Emmanuel Milou's avatar
Emmanuel Milou committed
711
712
713
    }

    /* Classic call, attached to an account */
714
    else {
715
716
        accountid = getAccountFromCall (id);

Emmanuel Milou's avatar
Emmanuel Milou committed
717
        if (accountid == AccountNULL) {
718
            _debug ("! Manager Transfer Call: Call doesn't exists\n");
Emmanuel Milou's avatar
Emmanuel Milou committed
719
720
            return false;
        }
721
722
723
724

        returnValue = getAccountLink (accountid)->transfer (id, to);

        removeCallAccount (id);
Emmanuel Milou's avatar
Emmanuel Milou committed
725
    }
726

727
728
729
730
731
    removeWaitingCall (id);

    switchCall ("");

    if (_dbus) _dbus->getCallManager()->callStateChanged (id, "HUNGUP");
732

Emmanuel Milou's avatar
Emmanuel Milou committed
733
    return returnValue;
jpbl's avatar
jpbl committed
734
735
}

736
737
void ManagerImpl::transferFailed()
{
738
    if (_dbus) _dbus->getCallManager()->transferFailed();
739
740
741
742
}

void ManagerImpl::transferSucceded()
{
743
    if (_dbus) _dbus->getCallManager()->transferSucceded();
744
745
746
747

}


yanmorin's avatar
   
yanmorin committed
748
//THREAD=Main : Call:Incoming
749
bool
yanmorin's avatar
   
yanmorin committed
750
ManagerImpl::refuseCall (const CallID& id)
jpbl's avatar
jpbl committed
751
{
Emmanuel Milou's avatar
Emmanuel Milou committed
752
753
    AccountID accountid;
    bool returnValue;
754

755
756
757
    stopTone (true);

    /* Direct IP to IP call */