managerimpl.cpp 133 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
        , _directIpAccount (NULL)
90
        , _cleaner (NULL)
91
        , _history (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
    delete _cleaner;
    _cleaner=0;
Julien Bonjean's avatar
Julien Bonjean committed
117
    _debug ("%s stop correctly.", PROGNAME);
jpbl's avatar
jpbl committed
118
119
}

120
void
121
ManagerImpl::init()
jpbl's avatar
jpbl committed
122
{
123

124
125
    // Load accounts, init map
    loadAccountMap();
126

127
    initVolume();
128

129
    if (_exist == 0) {
Julien Bonjean's avatar
Julien Bonjean committed
130
        _debug ("Cannot create config file in your home directory");
131
    }
132

133
    initAudioDriver();
134

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

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

140
    AudioLayer *audiolayer = getAudioDriver();
141

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

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

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

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


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

void ManagerImpl::terminate()
{
Julien Bonjean's avatar
Julien Bonjean committed
163
    _debug ("ManagerImpl::terminate ");
164
    saveConfig();
jpbl's avatar
jpbl committed
165

166
    unloadAccountMap();
167

Julien Bonjean's avatar
Julien Bonjean committed
168
    _debug ("Unload DTMF Key ");
169
    delete _dtmfKey;
jpbl's avatar
jpbl committed
170

Julien Bonjean's avatar
Julien Bonjean committed
171
    _debug ("Unload Audio Driver ");
172
173
    delete _audiodriver;
    _audiodriver = NULL;
jpbl's avatar
jpbl committed
174

Julien Bonjean's avatar
Julien Bonjean committed
175
    _debug ("Unload Telephone Tone ");
176
177
    delete _telephoneTone;
    _telephoneTone = NULL;
178

Julien Bonjean's avatar
Julien Bonjean committed
179
    _debug ("Unload Audio Codecs ");
180
    _codecDescriptorMap.deleteHandlePointer();
181

jpbl's avatar
jpbl committed
182
183
}

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

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

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

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

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

void
209
210
211
ManagerImpl::switchCall (const CallID& id)
{
    ost::MutexLock m (_currentCallMutex);
Julien Bonjean's avatar
Julien Bonjean committed
212
    _debug ("----- Switch current call id to %s -----", id.c_str());
213
    _currentCallId2 = id;
214

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

218
    if (id != "") {
219

220
    if(isConference(id)) {
221

222
        Conference *conf;
223

224
225
226
        ConferenceMap::iterator iter = _conferencemap.find(id);
        if(iter != _conferencemap.end())
        {
Julien Bonjean's avatar
Julien Bonjean committed
227
    	_debug("    set call recordable in audio layer");
228
229
230
231
232
    	conf = iter->second;
    	al->setRecorderInstance((Recordable*)conf);
        }
    }
    else {
233

234
235
        // set the recordable instance in audiolayer
        AccountID account_id = getAccountFromCall(id);
236
237


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

Julien Bonjean's avatar
Julien Bonjean committed
241
        _debug("    set call recordable in audio layer");
242
243
        al->setRecorderInstance((Recordable*)call);
    }
244
245
    }
    */
jpbl's avatar
jpbl committed
246
247
248
249
250
251
}


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

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

Julien Bonjean's avatar
Julien Bonjean committed
261
    _debug ("ManagerImpl::outgoingCall(%s)", call_id.c_str());
262
263

    CallID current_call_id = getCurrentCallId();
264

265
266
    if (getConfigString (HOOKS, PHONE_NUMBER_HOOK_ENABLED) ==  "1")
        _cleaner->set_phone_number_prefix (getConfigString (HOOKS, PHONE_NUMBER_HOOK_ADD_PREFIX));
267
268
    else
        _cleaner->set_phone_number_prefix ("");
Emmanuel Milou's avatar
Emmanuel Milou committed
269

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

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

275
    // in any cases we have to detach from current communication
276
    if (hasCurrentCall()) {
277

Julien Bonjean's avatar
Julien Bonjean committed
278
        _debug ("    outgoingCall: Has current call (%s) put it onhold", current_call_id.c_str());
279
280
281
282

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

        if (!isConference (current_call_id) && !participToConference (current_call_id)) {
Julien Bonjean's avatar
Julien Bonjean committed
283
            _debug ("    outgoingCall: Put the current call (%s) on hold", current_call_id.c_str());
284
285
            onHoldCall (current_call_id);
        } else if (isConference (current_call_id) && !participToConference (call_id)) {
Julien Bonjean's avatar
Julien Bonjean committed
286
            _debug ("    outgoingCall: detach main participant from conference");
287
288
            detachParticipant (default_id, current_call_id);
        }
289
290
    }

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

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

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

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

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


Julien Bonjean's avatar
Julien Bonjean committed
317
    _debug ("- Manager Action: Adding Outgoing Call %s on account %s", call_id.data(), account_id.data());
318

319
    associateCallToAccount (call_id, account_id);
320

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

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

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

Julien Bonjean's avatar
Julien Bonjean committed
337
    _debug ("ManagerImpl::answerCall(%s)", call_id.c_str());
338

339
    stopTone ();
340

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

    AccountID account_id = getAccountFromCall (call_id);
345
346

    if (account_id == AccountNULL) {
Julien Bonjean's avatar
Julien Bonjean committed
347
        _debug ("    answerCall: AccountId is null");
348
    }
349

350
    Call* call = NULL;
351

352
    call = getAccountLink (account_id)->getCall (call_id);
353

354
    if (call == NULL) {
Julien Bonjean's avatar
Julien Bonjean committed
355
        _debug ("    answerCall: Call is null");
356
    }
357
358

    // in any cases we have to detach from current communication
Alexandre Savard's avatar
Alexandre Savard committed
359
    if (hasCurrentCall()) {
360

Julien Bonjean's avatar
Julien Bonjean committed
361
        _debug ("    answerCall: Currently conversing with %s", current_call_id.c_str());
362
363
364
        // if it is not a conference and is not a conference participant

        if (!isConference (current_call_id) && !participToConference (current_call_id)) {
Julien Bonjean's avatar
Julien Bonjean committed
365
            _debug ("    answerCall: Put the current call (%s) on hold", current_call_id.c_str());
366
367
368
369
370
            onHoldCall (current_call_id);
        }

        // if we are talking to a conference and we are answering an incoming call
        else if (isConference (current_call_id) && !participToConference (call_id)) {
Julien Bonjean's avatar
Julien Bonjean committed
371
            _debug ("    answerCall: Detach main participant from conference");
372
373
            detachParticipant (default_id, current_call_id);
        }
374

Alexandre Savard's avatar
Alexandre Savard committed
375
    }
376

377

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

384
    // if it was waiting, it's waiting no more
385
    if (_dbus) _dbus->getCallManager()->callStateChanged (call_id, "CURRENT");
Emmanuel Milou's avatar
Emmanuel Milou committed
386

387
388
    // std::string codecName = Manager::instance().getCurrentCodecName (call_id);
    // if (_dbus) _dbus->getCallManager()->currentSelectedCodec (call_id, codecName.c_str());
389

390
    removeWaitingCall (call_id);
391

392
    // if we dragged this call into a conference already
393
394
395
    if (participToConference (call_id)) {

        // AccountID currentAccountId;
396
397
        // Call* call = NULL;

398
399
400
401
402
403
        // currentAccountId = getAccountFromCall (call_id);
        // call = getAccountLink (currentAccountId)->getCall (call_id);

        switchCall (call->getConfId());
    } else {
        switchCall (call_id);
404
    }
405
406

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

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

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

421
    stopTone ();
yanmorin's avatar
   
yanmorin committed
422

423
    /* Broadcast a signal over DBus */
Julien Bonjean's avatar
Julien Bonjean committed
424
    _debug ("    hangupCall: Send DBUS call state change (HUNGUP) for id %s", call_id.c_str());
425

426
    if (_dbus) _dbus->getCallManager()->callStateChanged (call_id, "HUNGUP");
427

428
    if (participToConference (call_id)) {
429

430
        Conference *conf = getConferenceFromCallID (call_id);
431

432
433
434
435
436
437
        if (conf != NULL) {
            // remove this participant
            removeParticipant (call_id);

            processRemainingParticipant (current_call_id, conf);
        }
438

439
440
441
442
    } else {
        // we are not participating to a conference, current call switched to ""
        if (!isConference (current_call_id))
            switchCall ("");
443
    }
444

Emmanuel Milou's avatar
Emmanuel Milou committed
445
    /* Direct IP to IP call */
446
447
    if (getConfigFromCall (call_id) == Call::IPtoIP) {
        returnValue = SIPVoIPLink::instance (AccountNULL)->hangup (call_id);
448
    }
449

Emmanuel Milou's avatar
Emmanuel Milou committed
450
    /* Classic call, attached to an account */
451
    else {
452
        account_id = getAccountFromCall (call_id);
453

454
        if (account_id == AccountNULL) {
Julien Bonjean's avatar
Julien Bonjean committed
455
            _debug ("! Manager Hangup Call: Call doesn't exists");
Emmanuel Milou's avatar
Emmanuel Milou committed
456
457
            return false;
        }
458

459
        returnValue = getAccountLink (account_id)->hangup (call_id);
460

461
        removeCallAccount (call_id);
Emmanuel Milou's avatar
Emmanuel Milou committed
462
    }
463

464
465
466
467
468
    int nbCalls = getCallList().size();

    AudioLayer *audiolayer = getAudioDriver();

    // stop streams
469
    if (audiolayer && (nbCalls <= 0)) {
470
471
472
473
        _debug ("    hangupCall: stop audio stream, ther is only %i call(s) remaining", nbCalls);
        audiolayer->stopStream();
    }

474
    if (_audiodriver->getLayerType() == PULSEAUDIO) {
475
476
        pulselayer = dynamic_cast<PulseLayer *> (getAudioDriver());
    }
477

478
    return returnValue;
jpbl's avatar
jpbl committed
479
480
}

Alexandre Savard's avatar
Alexandre Savard committed
481
482
483
484

bool
ManagerImpl::hangupConference (const ConfID& id)
{
Julien Bonjean's avatar
Julien Bonjean committed
485
    _debug ("ManagerImpl::hangupConference()");
Alexandre Savard's avatar
Alexandre Savard committed
486
487

    Conference *conf;
488
    ConferenceMap::iterator iter_conf = _conferencemap.find (id);
Alexandre Savard's avatar
Alexandre Savard committed
489
490
491

    AccountID currentAccountId;

492
    // Call* call = NULL;
Alexandre Savard's avatar
Alexandre Savard committed
493
494


495
496
    if (iter_conf != _conferencemap.end()) {
        conf = iter_conf->second;
Alexandre Savard's avatar
Alexandre Savard committed
497

498
499
        ParticipantSet participants = conf->getParticipantList();
        ParticipantSet::iterator iter_participant = participants.begin();
Alexandre Savard's avatar
Alexandre Savard committed
500

501
        while (iter_participant != participants.end()) {
Julien Bonjean's avatar
Julien Bonjean committed
502
            _debug ("ManagerImpl::hangupConference participant %s", (*iter_participant).c_str());
Alexandre Savard's avatar
Alexandre Savard committed
503

504
            hangupCall (*iter_participant);
Alexandre Savard's avatar
Alexandre Savard committed
505

506
507
508
            iter_participant++;

        }
Alexandre Savard's avatar
Alexandre Savard committed
509
510
511

    }

512
513
    switchCall ("");

Alexandre Savard's avatar
Alexandre Savard committed
514
515
516
517
    return true;
}


yanmorin's avatar
   
yanmorin committed
518
//THREAD=Main
519
bool
yanmorin's avatar
   
yanmorin committed
520
ManagerImpl::cancelCall (const CallID& id)
jpbl's avatar
jpbl committed
521
{
Emmanuel Milou's avatar
Emmanuel Milou committed
522
523
    AccountID accountid;
    bool returnValue;
yanmorin's avatar
   
yanmorin committed
524

525
    stopTone ();
526

Emmanuel Milou's avatar
Emmanuel Milou committed
527
    /* Direct IP to IP call */
528

Emmanuel Milou's avatar
Emmanuel Milou committed
529
    if (getConfigFromCall (id) == Call::IPtoIP) {
530
        returnValue = SIPVoIPLink::instance (AccountNULL)->cancel (id);
Emmanuel Milou's avatar
Emmanuel Milou committed
531
532
533
    }

    /* Classic call, attached to an account */
534
    else {
535
536
        accountid = getAccountFromCall (id);

Emmanuel Milou's avatar
Emmanuel Milou committed
537
        if (accountid == AccountNULL) {
Julien Bonjean's avatar
Julien Bonjean committed
538
            _debug ("! Manager Cancel Call: Call doesn't exists");
Emmanuel Milou's avatar
Emmanuel Milou committed
539
540
            return false;
        }
541
542
543
544

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

        removeCallAccount (id);
Emmanuel Milou's avatar
Emmanuel Milou committed
545
    }
546

Emmanuel Milou's avatar
Emmanuel Milou committed
547
    // it could be a waiting call?
548
549
550
    removeWaitingCall (id);

    switchCall ("");
Emmanuel Milou's avatar
Emmanuel Milou committed
551
552

    return returnValue;
jpbl's avatar
jpbl committed
553
554
}

yanmorin's avatar
   
yanmorin committed
555
//THREAD=Main
556
bool
557
ManagerImpl::onHoldCall (const CallID& call_id)
jpbl's avatar
jpbl committed
558
{
559
    AccountID account_id;
Emmanuel Milou's avatar
Emmanuel Milou committed
560
    bool returnValue;
yanmorin's avatar
   
yanmorin committed
561

Julien Bonjean's avatar
Julien Bonjean committed
562
    _debug ("ManagerImpl::onHoldCall(%s)", call_id.c_str());
563

564
    stopTone ();
565

566
567
    CallID current_call_id = getCurrentCallId();

Julien Bonjean's avatar
Julien Bonjean committed
568
    _debug ("    onHoldCall: try to put call %s on hold", call_id.c_str());
569

570
    /* Direct IP to IP call */
571

572
    if (getConfigFromCall (call_id) == Call::IPtoIP) {
573
        returnValue = SIPVoIPLink::instance (AccountNULL)-> onhold (call_id);
Emmanuel Milou's avatar
Emmanuel Milou committed
574
    }
575

Emmanuel Milou's avatar
Emmanuel Milou committed
576
    /* Classic call, attached to an account */
577
    else {
578
        account_id = getAccountFromCall (call_id);
579

580
        if (account_id == AccountNULL) {
Julien Bonjean's avatar
Julien Bonjean committed
581
            _debug ("    onHoldCall: Account ID %s or callid %s doesn't exists", account_id.c_str(), call_id.c_str());
582
583
            return false;
        }
584

585
        returnValue = getAccountLink (account_id)->onhold (call_id);
Emmanuel Milou's avatar
Emmanuel Milou committed
586
    }
587

588
    removeWaitingCall (call_id);
589

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

592
593
594
    if (current_call_id == call_id) {

        switchCall ("");
595
    }
596

597
    if (_dbus) _dbus->getCallManager()->callStateChanged (call_id, "HOLD");
598

Emmanuel Milou's avatar
Emmanuel Milou committed
599
    return returnValue;
yanmorin's avatar
   
yanmorin committed
600
601
602
}

//THREAD=Main
603
bool
604
ManagerImpl::offHoldCall (const CallID& call_id)
yanmorin's avatar
   
yanmorin committed
605
{
606

607
    AccountID account_id;
608
    bool returnValue, is_rec;
Emmanuel Milou's avatar
Emmanuel Milou committed
609
    std::string codecName;
610

611
    is_rec = false;
612

Julien Bonjean's avatar
Julien Bonjean committed
613
    _debug ("ManagerImpl::offHoldCall(%s)", call_id.c_str());
614

615
    stopTone ();
616

617
    CallID current_call_id = getCurrentCallId();
618

619
    //Place current call on hold if it isn't
620
621
622
623

    if (hasCurrentCall()) {
        // if this is not a conferenceand this and is not a conference participant
        if (!isConference (current_call_id) && !participToConference (current_call_id)) {
Julien Bonjean's avatar
Julien Bonjean committed
624
            _debug ("    offHoldCall: put current call (%s) on hold", current_call_id.c_str());
625
626
            onHoldCall (current_call_id);
        } else if (isConference (current_call_id) && !participToConference (call_id)) {
Julien Bonjean's avatar
Julien Bonjean committed
627
            _debug ("    offHoldCall Put current conference (%s) on hold", current_call_id.c_str());
628
629
            detachParticipant (default_id, current_call_id);
        }
Emmanuel Milou's avatar
Emmanuel Milou committed
630
    }
631

632
    // switch current call id to id since sipvoip link need it to amke a call
633
    // switchCall(id);
634

Emmanuel Milou's avatar
Emmanuel Milou committed
635
    /* Direct IP to IP call */
636
    if (getConfigFromCall (call_id) == Call::IPtoIP) {
637
        // is_rec = SIPVoIPLink::instance (AccountNULL)-> isRecording (call_id);
638
        returnValue = SIPVoIPLink::instance (AccountNULL)-> offhold (call_id);
Emmanuel Milou's avatar
Emmanuel Milou committed
639
    }
alexandresavard's avatar
alexandresavard committed
640

Emmanuel Milou's avatar
Emmanuel Milou committed
641
    /* Classic call, attached to an account */
642
    else {
643
        account_id = getAccountFromCall (call_id);
644

645
        if (account_id == AccountNULL) {
Julien Bonjean's avatar
Julien Bonjean committed
646
            _debug ("Manager OffHold Call: Call doesn't exists");
Emmanuel Milou's avatar
Emmanuel Milou committed
647
648
            return false;
        }
649

Julien Bonjean's avatar
Julien Bonjean committed
650
        _debug ("Setting OFFHOLD, Account %s, callid %s", account_id.c_str(), call_id.c_str());
651

652
        is_rec = getAccountLink (account_id)->getCall (call_id)->isRecording();
653
        returnValue = getAccountLink (account_id)->offhold (call_id);
Emmanuel Milou's avatar
Emmanuel Milou committed
654
    }
alexandresavard's avatar
alexandresavard committed
655

656

657
    if (_dbus) {
658
        if (is_rec)
659
            _dbus->getCallManager()->callStateChanged (call_id, "UNHOLD_RECORD");
660
        else
661
            _dbus->getCallManager()->callStateChanged (call_id, "UNHOLD_CURRENT");
662

Emmanuel Milou's avatar
Emmanuel Milou committed
663
    }
664

665
666
667
    if (participToConference (call_id)) {

        AccountID currentAccountId;
668
669
        Call* call = NULL;

670
671
672
673
674
675
676
677
        currentAccountId = getAccountFromCall (call_id);
        call = getAccountLink (currentAccountId)->getCall (call_id);

        switchCall (call->getConfId());

    } else {
        switchCall (call_id);
        _audiodriver->flushMain();
678
    }
679

680

681
    // codecName = getCurrentCodecName (call_id);
Julien Bonjean's avatar
Julien Bonjean committed
682
    // _debug("ManagerImpl::hangupCall(): broadcast codec name %s ",codecName.c_str());
683

684
    // if (_dbus) _dbus->getCallManager()->currentSelectedCodec (call_id,codecName.c_str());
685
686


Emmanuel Milou's avatar
Emmanuel Milou committed
687
    return returnValue;
jpbl's avatar
jpbl committed
688
689
}

yanmorin's avatar
   
yanmorin committed
690
//THREAD=Main
691
bool
692
ManagerImpl::transferCall (const CallID& call_id, const std::string& to)
jpbl's avatar
jpbl committed
693
{
Emmanuel Milou's avatar
Emmanuel Milou committed
694
695
    AccountID accountid;
    bool returnValue;
696

697
    stopTone ();
698

699
700
    CallID current_call_id = getCurrentCallId();

701
702
703
    if (participToConference (call_id)) {

        _debug("Particip to a conference\n");
704

705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
        Conference *conf = getConferenceFromCallID (call_id);

        if (conf != NULL) {
            // remove this participant
            removeParticipant (call_id);

            processRemainingParticipant (current_call_id, conf);
        }
    } else {

        _debug("Do not Particip to a conference\n");

        // we are not participating to a conference, current call switched to ""
        if (!isConference (current_call_id))
            switchCall ("");
    }
721

722
    /* Direct IP to IP call */
723
724
    if (getConfigFromCall (call_id) == Call::IPtoIP) {
        returnValue = SIPVoIPLink::instance (AccountNULL)-> transfer (call_id, to);
Emmanuel Milou's avatar
Emmanuel Milou committed
725
726
    }
    /* Classic call, attached to an account */
727
    else {
728
        accountid = getAccountFromCall (call_id);
729

Emmanuel Milou's avatar
Emmanuel Milou committed
730
        if (accountid == AccountNULL) {
Julien Bonjean's avatar
Julien Bonjean committed
731
            _debug ("! Manager Transfer Call: Call doesn't exists");