managerimpl.h 37 KB
Newer Older
jpbl's avatar
jpbl committed
1
/*
2
 *  Copyright (C) 2004, 2005, 2006, 2009, 2008, 2009, 2010 Savoir-Faire Linux Inc.
jpbl's avatar
jpbl committed
3
4
 *  Author: Yan Morin <yan.morin@savoirfairelinux.com>
 *  Author: Laurielle Lea <laurielle.lea@savoirfairelinux.com>
5
 *  Author: Emmanuel Milou <emmanuel.milou@savoirfairelinux.com>
6
 *  Author: Guillaume Carmel-Archambault <guillaume.carmel-archambault@savoirfairelinux.com>
jpbl's avatar
jpbl committed
7
8
9
 *
 *  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
10
 *  the Free Software Foundation; either version 3 of the License, or
jpbl's avatar
jpbl committed
11
12
13
14
15
16
17
18
19
20
 *  (at your option) any later version.
 *
 *  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.
 *
 *  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.
21
22
23
24
25
26
27
28
29
30
31
 *
 *  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
32
33
 */

34
35
#ifndef __SFL_MANAGER_H__
#define __SFL_MANAGER_H__
jpbl's avatar
jpbl committed
36
37
38

#include <string>
#include <vector>
yanmorin's avatar
   
yanmorin committed
39
#include <set>
yanmorin's avatar
   
yanmorin committed
40
#include <map>
jpbl's avatar
jpbl committed
41
#include <cc++/thread.h>
42
#include "dbus/dbusmanager.h"
jpbl's avatar
jpbl committed
43
44
45

#include "observer.h"
#include "config/config.h"
yanmorin's avatar
   
yanmorin committed
46

yanmorin's avatar
   
yanmorin committed
47
#include "account.h"
yanmorin's avatar
   
yanmorin committed
48
#include "call.h"
49
#include "conference.h"
50
#include "numbercleaner.h"
jpbl's avatar
jpbl committed
51

52
53
54
55
#include "audio/sound/tonelist.h"  // for Tone::TONEID declaration
#include "audio/sound/audiofile.h" // AudioFile class contained by value here 
#include "audio/sound/dtmf.h" // DTMF class contained by value here
#include "audio/codecs/codecDescriptor.h" // CodecDescriptor class contained by value here
jpbl's avatar
jpbl committed
56

57
58
#include "audio/mainbuffer.h"

jpbl's avatar
jpbl committed
59
60
61
62
class AudioLayer;
class GuiFramework;
class TelephoneTone;
class VoIPLink;
63

64
// class Conference;
yanmorin's avatar
   
yanmorin committed
65

jpbl's avatar
jpbl committed
66
67
68
69
#ifdef USE_ZEROCONF
class DNSService;
#endif

70
class HistoryManager;
71
class SIPAccount;
jpbl's avatar
jpbl committed
72

73
/** Define a type for a AccountMap container */
yanmorin's avatar
   
yanmorin committed
74
typedef std::map<AccountID, Account*> AccountMap;
75
76

/** Define a type for a CallID to AccountID Map inside ManagerImpl */
yanmorin's avatar
   
yanmorin committed
77
typedef std::map<CallID, AccountID> CallAccountMap;
yanmorin's avatar
   
yanmorin committed
78

Emmanuel Milou's avatar
Emmanuel Milou committed
79
80
typedef std::map<CallID, Call::CallConfiguration> CallConfigMap;

81
/** Define a type for CallID vector (waiting list, incoming not answered) */
yanmorin's avatar
   
yanmorin committed
82
83
typedef std::set<CallID> CallIDSet;

84
/** To send multiple string */
jpbl's avatar
jpbl committed
85
86
typedef std::list<std::string> TokenList;

87
88
/** To store conference objects by call ids 
    used to retreive the conference according to a call */
89
90
91
typedef std::map<CallID, Conference*> ConferenceCallMap;

/** To store conference objects by conference ids */
92
93
94
95
typedef std::map<CallID, Conference*> ConferenceMap;

static CallID default_conf = "conf"; 

96
97
98
99
100
101
102
103
104
105
106
107
static char * mapStateToChar[] = {
    (char*) "UNREGISTERED",
    (char*) "TRYING",
    (char*) "REGISTERED",
    (char*) "ERROR",
    (char*) "ERRORAUTH",
    (char*) "ERRORNETWORK",
    (char*) "ERRORHOST",
    (char*) "ERROREXISTSTUN",    
    (char*) "ERRORCONFSTUN"    
};

108
/** Manager (controller) of sflphone daemon */
jpbl's avatar
jpbl committed
109
class ManagerImpl {
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
  public:
    ManagerImpl (void);
    ~ManagerImpl (void);

    /**
     * Initialisation of thread (sound) and map.
     * Init a new VoIPLink, audio codec and audio driver
     */
    void init (void);

    /**
     * Terminate all thread (sound, link) and unload AccountMap
     */
    void terminate (void);

    /**
     * Set user interface manager.
     * @param man The DBUS interface implementation
     */
    void setDBusManager (DBusManagerImpl* man) { _dbus = man; }

    /**
     * Accessor to audiodriver.
     * it's multi-thread and use mutex internally
     * @return AudioLayer*  The audio layer object
     */
    AudioLayer* getAudioDriver(void) const { return _audiodriver; }

    /**
     * Get a descriptor map of codec available
     * @return CodecDescriptor  The internal codec map
     */
    CodecDescriptor& getCodecDescriptorMap(void) {return _codecDescriptorMap;}

    /**
     * Functions which occur with a user's action
     * Place a new call
     * @param accountId	The account to make tha call with
     * @param id  The call identifier
     * @param to  The recipient of the call
     * @return bool true on success
     *		  false otherwise
     */
    bool outgoingCall(const AccountID& accountId, const CallID& id, const std::string& to);

    /**
     * Functions which occur with a user's action
     * Answer the call
     * @param id  The call identifier
     */
    bool answerCall(const CallID& id);

    /**
     * Functions which occur with a user's action
     * Hangup the call
     * @param id  The call identifier
     */
    bool hangupCall(const CallID& id);

Alexandre Savard's avatar
Alexandre Savard committed
169
170
171
172
173
174
175
176

    /**
     * Functions which occur with a user's action
     * Hangup the conference (hangup every participants)
     * @param id  The call identifier
     */
    bool hangupConference(const ConfID& id);

177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
    /**
     * Functions which occur with a user's action
     * Cancel the call
     * @param id  The call identifier
     */
    bool cancelCall(const CallID& id);

    /**
     * Functions which occur with a user's action
     * Put the call on hold
     * @param id  The call identifier
     */
    bool onHoldCall(const CallID& id);

    /**
     * Functions which occur with a user's action
     * Put the call off hold
     * @param id  The call identifier
     */
    bool offHoldCall(const CallID& id);

    /**
     * Functions which occur with a user's action
     * Transfer the call
     * @param id  The call identifier
     * @param to  The recipient of the transfer
     */
    bool transferCall(const CallID& id, const std::string& to);

206
207
208
209
210
211
212
213
214
215
    /**
     * Notify the client the transfer is successful
     */
    void transferSucceded();

    /**
     * Notify the client that the transfer failed
     */
    void transferFailed();

216
217
218
219
220
221
222
    /**
     * Functions which occur with a user's action
     * Refuse the call
     * @param id  The call identifier
     */
    bool refuseCall(const CallID& id);

223
224
225
226
227
    /**
     * Create a new conference given two participant
     * @param the first participant ID
     * @param the second participant ID
     */ 
228
    Conference* createConference(const CallID& id1, const CallID& id2);
229

230
231
232
233
    /**
     * Delete this conference
     * @param the conference ID
     */ 
234
235
    void removeConference(const CallID& conference_id);

236
237
238
239
    /**
     * Return the conference id for which this call is attached
     * @ param the call id
     */
240
241
    Conference* getConferenceFromCallID(const CallID& call_id);

242
243
244
245
    /**
     * Hold every participant to a conference
     * @param the conference id
     */
246
247
    void holdConference(const CallID& conferece_id);

248
249
250
251
    /**
     * Unhold all conference participants 
     * @param the conference id
     */
252
253
    void unHoldConference(const CallID& conference_id);

254
255
256
257
    /**
     * Test if this id is a conference (usefull to test current call)
     * @param the call id
     */
258
259
    bool isConference(const CallID& call_id);

260
261
262
263
    /**
     * Test if a call id particips to this conference
     * @param the call id
     */
264
265
    bool participToConference(const CallID& call_id);

266
267
268
269
270
    /**
     * Add a participant to a conference
     * @param the call id
     * @param the conference id
     */
271
    void addParticipant(const CallID& call_id, const CallID& conference_id);
272

273
274
275
276
    /**
     * Bind the main participant to a conference (mainly called on a double click action)
     * @param the conference id
     */
277
278
    void addMainParticipant(const CallID& conference_id);

279
280
281
282
283
    /**
     * Join two participants to create a conference
     * @param the fist call id
     * @param the second call id
     */
284
285
    void joinParticipant(const CallID& call_id1, const CallID& call_id2);

286
287
288
289
290
    /**
     * Detach a participant from a conference, put the call on hold, do not hangup it
     * @param call id
     * @param the current call id
     */
291
    void detachParticipant(const CallID& call_id, const CallID& current_call_id);
292

293
294
295
296
    /**
     * Remove the conference participant from a conference
     * @param call id
     */
297
    void removeParticipant(const CallID& call_id);
298
299
300
301
302
303
304
    
    /**
     * Process remaining participant given a conference and the current call id.
     * Mainly called when a participant is detached or hagned up
     * @param current call id
     * @param conference pointer
     */
305
306
    void processRemainingParticipant(CallID current_call_id, Conference *conf);

307
308
309
    /**
     * Join two conference together into one unique conference
     */
310
311
    void joinConference(const CallID& conf_id1, const CallID& conf_id2);

312
313
    void addStream(const CallID& call_id);

314
315
    void removeStream(const CallID& call_id);

316
317
    /**
     * Save config to file
318
319
320
321
322
323
324
     * @return true on success
     *	    false otherwise
     */
    bool saveConfig (void);

    /**
     * Send registration to all enabled accounts
325
326
     * @return 0 on registration success
     *          1 otherelse
327
     */
328
    int initRegisterAccounts();
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345

    /**
     * @return true if we tried to register once
     */
    bool _hasTriedToRegister;

    /**
     * Handle choice of the DTMF-send-way
     * @param   id: callid of the line.
     * @param   code: pressed key.
     */
    bool sendDtmf(const CallID& id, char code);

    /**
     * Play the dtmf-associated sound
     * @param code  The pressed key
     */
346
    bool playDtmf (char code);
347
348
349
350
351
352
353
354
355

    /**
     * Play a ringtone
     * @return bool True on success
     *	      false otherwise
     */
    bool playTone ();

    /**
356
     * Play a special ringtone ( BUSY ) if there's at least one message on the voice mail
357
358
359
360
361
362
363
364
     * @return bool True on success
     *	      false otherwise
     */
    bool playToneWithMessage ();

    /**
     * Acts on the audio streams and audio files
     */
365
    void stopTone (void);
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403

    /**
     * When receiving a new incoming call, add it to the callaccount map
     * and notify user
     * @param call A call pointer
     * @param accountId an account id
     * @return bool True if the call was added correctly
     */
    bool incomingCall(Call* call, const AccountID& accountId);

    /**
     * Notify the user that the recipient of the call has answered and the put the
     * call in Current state
     * @param id  The call identifier
     */
    void peerAnsweredCall(const CallID& id);

    /**
     * Rings back because the outgoing call is ringing and the put the
     * call in Ringing state
     * @param id  The call identifier
     */
    void peerRingingCall(const CallID& id);

    /**
     * Put the call in Hungup state, remove the call from the list
     * @param id  The call identifier
     */
    void peerHungupCall(const CallID& id);

    /**
     * Notify the client with an incoming message
     * @param accountId	The account identifier
     * @param message The content of the message
     */
    void incomingMessage(const AccountID& accountId, const std::string& message);

    /**
404
     * Notify the client he has voice mails
405
406
407
     * @param accountId	  The account identifier
     * @param nb_msg The number of messages
     */
Emmanuel Milou's avatar
Emmanuel Milou committed
408
    void startVoiceMessageNotification(const AccountID& accountId, int nb_msg);
409

410
411
    /**
     * Notify the client through DBus that registration state has been updated
412
     */
413
    void connectionStatusNotification(void);
414
415
416

    /**
     * ConfigurationManager - Send registration request
417
     * @param accountId The account to register/unregister
418
     * @param enable The flag for the type of registration
419
420
421
     *		 0 for unregistration request
     *		 1 for registration request
     */
422
    void sendRegister( const ::std::string& accountId , const int32_t& enable);
423
424
425

    bool getCallStatus(const std::string& sequenceId);

426
427
    /**
     * Get account list
428
429
430
431
     * @return std::vector<std::string> A list of accoundIDs
     */
    std::vector< std::string >  getAccountList();

432
433
434
435
436
    /**
     * Set the account order in the config file
     */
    void setAccountsOrder (const std::string& order);

437
438
439
440
441
442
    /**
     * Load the accounts order set by the user from the sflphonedrc config file
     * @return std::vector<std::string> A vector containing the account ID's
     */
    std::vector<std::string> loadAccountOrder ();

443
444
445
446
447
448
449
    /**
     * Retrieve details about a given account
     * @param accountID	  The account identifier
     * @return std::map< std::string, std::string > The account details
     */
    std::map< std::string, std::string > getAccountDetails(const AccountID& accountID);

450
451
452
453
454
455
456
    /**
     * Retrieve details about a given call
     * @param callID	  The account identifier
     * @return std::map< std::string, std::string > The call details
     */
    std::map< std::string, std::string > getCallDetails(const CallID& callID);

457
458
459
460
461
462
    /**
     * Get call list
     * @return std::vector<std::string> A list of call IDs
     */
    std::vector< std::string >  getCallList (void);

463
464
465
466
467
468
469
470
471
472
473
474
475
    /**
     * Retrieve details about a given call
     * @param callID	  The account identifier
     * @return std::map< std::string, std::string > The call details
     */
    std::map< std::string, std::string > getConferenceDetails(const CallID& callID);

    /**
     * Get call list
     * @return std::vector<std::string> A list of call IDs
     */
    std::vector< std::string >  getConferenceList (void);

476
477
478
479
480
481
482

    /**
     * Get a list of participant to a conference
     * @return std::vector<std::string> A list of call IDs
     */
    std::vector< std::string >  getParticipantList (const std::string& confID);

483
484
485
486
487
488
489
    /**
     * Save the details of an existing account, given the account ID
     * This will load the configuration map with the given data.
     * It will also register/unregister links where the 'Enabled' switched.
     * @param accountID	  The account identifier
     * @param details	  The account parameters
     */
490
    void setAccountDetails( const ::std::string& accountID,
491
	const std::map< ::std::string, ::std::string >& details );
492
493
494
495

    /**
     * Add a new account, and give it a new account ID automatically
     * @param details The new account parameters
jquentin's avatar
jquentin committed
496
     * @return The account Id given to the new account
497
     */
jquentin's avatar
jquentin committed
498
    std::string addAccount(const std::map< ::std::string, ::std::string >& details);
499
500
501
502
503
504
505
506

    /**
     * Delete an existing account, unregister VoIPLink associated, and
     * purge from configuration.
     * @param accountID	The account unique ID
     */
    void removeAccount(const AccountID& accountID);

507
508
509
510
511
512
513

    /**
     * Deletes all credentials defined for an account
     * @param accountID The account unique ID
     */
    void deleteAllCredential(const AccountID& accountID);
    
Alexandre Savard's avatar
Alexandre Savard committed
514
515
516
517
518
519
520
    /**
     * Get current codec name
     * @param call id
     * @return std::string The codec name
     */
    std::string getCurrentCodecName(const CallID& id);

521
    /**
522
     * Set input audio plugin
523
524
525
526
527
     * @param audioPlugin The audio plugin
     */
    void setInputAudioPlugin(const std::string& audioPlugin);

    /**
528
     * Set output audio plugin
529
530
531
532
533
534
535
536
537
538
539
     * @param audioPlugin The audio plugin
     */
    void setOutputAudioPlugin(const std::string& audioPlugin);

    /**
     * Get list of supported audio output device
     * @return std::vector<std::string> A list of the audio devices supporting playback
     */
    std::vector<std::string> getAudioOutputDeviceList(void);

    /**
540
     * Set audio device
541
     * @param index The index of the soundcard
542
     * @param the type of stream, either SFL_PCM_PLAYBACK, SFL_PCM_CAPTURE, SFL_PCM_RINGTONE
543
     */
544
    void setAudioDevice(const int index, const int streamType);
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564

    /**
     * Get list of supported audio input device
     * @return std::vector<std::string> A list of the audio devices supporting capture
     */
    std::vector<std::string> getAudioInputDeviceList(void);

    /**
     * Get string array representing integer indexes of output and input device
     * @return std::vector<std::string> A list of the current audio devices
     */
    std::vector<std::string> getCurrentAudioDevicesIndex();

    /**
     * Get index of an audio device
     * @param name The string description of an audio device
     * @return int  His index
     */
    int getAudioDeviceIndex( const std::string name );

565
    /**
566
567
568
     * Get current alsa plugin
     * @return std::string  The Alsa plugin
     */
569
    std::string getCurrentAudioOutputPlugin( void );
570

571
572
573
574
575
576
577
578
    std::string getEchoCancelState(void);

    void setEchoCancelState(std::string state);

    std::string getNoiseSuppressState(void);

    void setNoiseSuppressState(std::string state);

579
580
581
582
583
584
585
    /**
     * Convert a list of payload in a special format, readable by the server.
     * Required format: payloads separated with one slash.
     * @return std::string The serializabled string
     */
    std::string serialize(std::vector<std::string> v);

586
587
    std::vector<std::string> unserialize(std::string v);

588
589
590
591
592
    /**
     * Tells if IAX2 support is enabled
     * @return int 1 if IAX2 is enabled
     *	       0 otherwise
     */
593
    int isIax2Enabled( void );
594
595
596

    /**
     * Ringtone option.
597
     * If ringtone is enabled, ringtone on incoming call use custom choice. If not, only standart tone.
598
599
600
     * @return int	1 if enabled
     *	        0 otherwise
     */
601
    int isRingtoneEnabled( void );
602
603

    /**
604
     * Set the ringtone option
605
606
     * Inverse current value
     */
607
    void ringtoneEnabled( void );
608
609

    /**
610
     * Get the ringtone
611
612
613
614
615
616
617
618
619
620
     * @return gchar* The file name selected as a ringtone
     */
    std::string getRingtoneChoice( void );

    /**
     * Set a ringtone
     * @param tone The file name of the ringtone
     */
    void setRingtoneChoice( const std::string& );

621
622
623
624
625
626
627
628
629
630
    /**
     * Get the recording path from configuration tree
     * @return the string correspoding to the path
     */
    std::string getRecordPath( void );

    /**
     * Set the recoding path in the configuration tree
     * @param a string reresenting the path
     */
alexandresavard's avatar
alexandresavard committed
631
    void setRecordPath( const std::string& recPath);
632

633
634
635
636
637
638
639
640
641
642
643
644
    /** 
     * Set a credential for a given account. If it 
     * does not exist yet, it will be created.
     */
    void setCredential (const std::string& accountID, const int32_t& index, const std::map< std::string, std::string >& details);

    /**
     * Retreive the value set in the configuration file.
     * @return True if credentials hashing is enabled.
     */
    bool getMd5CredentialHashing(void);
    
645
    /**
646
     * Tells if the user wants to display the dialpad or not
647
648
649
650
651
652
653
654
     * @return int 1 if dialpad has to be displayed
     *	       0 otherwise
     */
    int getDialpad( void );

    /**
     * Set the dialpad visible or not
     */
Emmanuel Milou's avatar
Emmanuel Milou committed
655
    void setDialpad (bool display);
656

657
    /**
658
     * Tells if the user wants to display the volume controls or not
659
660
661
662
663
664
665
666
     * @return int 1 if the controls have to be displayed
     *	       0 otherwise
     */
    int getVolumeControls( void );

    /**
     * Set the volume controls ( mic and speaker ) visible or not
     */
667
    void setVolumeControls (bool display);
668

alexandresavard's avatar
alexandresavard committed
669
    /**
alexandresavard's avatar
alexandresavard committed
670
     * Set recording on / off
alexandresavard's avatar
alexandresavard committed
671
672
673
674
675
     * Start recording
     * @param id  The call identifier
     */
    void setRecordingCall(const CallID& id);

alexandresavard's avatar
alexandresavard committed
676
677
678
679
680
    /**
     * Return true if the call is currently recorded
     */
    bool isRecording(const CallID& id);

681
    /**
682
683
     * Set the maximum number of days to keep in the history
     * @param calls The number of days
684
     */
685
    void setHistoryLimit (const int& days);
686
687

    /**
688
689
     * Get the maximum number of days to keep in the history
     * @return double The number of days 
690
     */
691
692
693
694
    int getHistoryLimit (void);

    void setHistoryEnabled (void);

695
	std::string getHistoryEnabled (void);
696

697

698
699
700
701
702
703
704
705
706
707
708
709
710
711
    /**
     * Configure the start-up option
     * @return int	1 if SFLphone should start in the system tray
     *	        0 otherwise
     */
    int isStartHidden( void );

    /**
     * Configure the start-up option
     * At startup, SFLphone can be displayed or start hidden in the system tray
     */
    void startHidden( void );

    /**
712
     * Configure the popup behaviour
713
     * @return int	1 if it should popup on incoming calls
714
     *		0 if it should never popups
715
716
717
718
     */
    int popupMode( void );

    /**
719
     * Configure the popup behaviour
720
721
722
723
724
     * When SFLphone is in the system tray, you can configure when it popups
     * Never or only on incoming calls
     */
    void switchPopupMode( void );

725
726
727
728
729
730
731
732
733
734
    /**
     * Determine whether or not the search bar (history) should be displayed
     */
    int getSearchbar( void );

    /**
     * Configure the search bar behaviour
     */
    void setSearchbar( void );

735
736
737
738
739
740
741
742
743
    /**
     * Set the desktop notification level
     */
    void setNotify( void );

    /**
     * Get the desktop notification level
     * @return int The notification level
     */
744
    int32_t getNotify( void );
745
746
747
748
749
750

    /**
     * Set the desktop mail notification level
     */
    void setMailNotify( void );

751

Emmanuel Milou's avatar
Emmanuel Milou committed
752
    /**
753
     * Addressbook configuration
Emmanuel Milou's avatar
Emmanuel Milou committed
754
755
756
757
     */
    std::map<std::string, int32_t> getAddressbookSettings (void);

    /**
758
     * Addressbook configuration
Emmanuel Milou's avatar
Emmanuel Milou committed
759
     */
760
761
    void setAddressbookSettings (const std::map<std::string, int32_t>& settings);

762
    /**
763
764
765
766
767
768
     * Addressbook list
     */
    void setAddressbookList(const std::vector<  std::string >& list);

    /**
     * Addressbook list
Emmanuel Milou's avatar
Emmanuel Milou committed
769
     */
770
771
    std::vector <std::string> getAddressbookList( void );

772
    /**
773
     * Hook configuration
774
775
776
777
     */
    std::map<std::string, std::string> getHookSettings (void);

    /**
778
     * Hook configuration
779
780
781
     */
     void setHookSettings (const std::map<std::string, std::string>& settings);

Emmanuel Milou's avatar
Emmanuel Milou committed
782

783
    /**
784
785
786
787
788
     * Get the audio manager
     * @return int The audio manager
     *		    0 ALSA
     *		    1 PULSEAUDIO
     */
789
    int32_t getAudioManager( void );
790
791
792
793

    /**
     * Set the audio manager
     */
794
    void setAudioManager( const int32_t& api );
795
796

    void switchAudioManager( void );
797

798
799
    void audioSamplingRateChanged( void );

800
    /**
801
802
803
     * Get the desktop mail notification level
     * @return int The mail notification level
     */
804
    int32_t getMailNotify( void );
805

806
807
808
809
810
811
812
813
814
    /**
     * Retrieve the formatted list of codecs payload in the user config file and
     * load in the active list of codecs
     * @return std::vector<std::string>	  The vector containing the active codecs
     */
    std::vector<std::string> retrieveActiveCodecs( void );

    /**
     * Get the list of the active codecs
815
     * @return std::vector< ::std::string >  The list of active codecs
816
817
     */
    std::vector< ::std::string > getActiveCodecList( void );
818
819
820
821
822
823

    /*
     * Notify the client that an error occured
     * @param errCode The error code. Could be: ALSA_CAPTURE_ERROR
     *					       ALSA_PLAYBACK_ERROR
     */
824
    void notifyErrClient( const int32_t& errCode );
825
826
827
828
829
830
831
832
833
834

    /**
     * Retrieve in the configuration tree the value of a parameter in a specific section
     * @param section	The section to look in
     * @param name	The name of the parameter you want to get
     * @param arg	Undocumented
     * @return bool	true on success
     *			false otherwise
     */
    bool getConfig(const std::string& section, const std::string& name, TokenList& arg);
835

836
837
838
    /**
     * Change a specific value in the configuration tree.
     * This value will then be saved in the user config file sflphonedrc
839
     * @param section	The section name
840
     * @param name	The parameter name
841
     * @param value	The new string value
842
843
844
845
     * @return bool	true on success
     *		      false otherwise
     */
    bool setConfig(const std::string& section, const std::string& name, const std::string& value);
846

847
848
849
    /**
     * Change a specific value in the configuration tree.
     * This value will then be saved in the user config file sflphonedrc
850
     * @param section	The section name
851
     * @param name	The parameter name
852
     * @param value	The new int value
853
854
855
856
     * @return bool	true on success
     *		      false otherwise
     */
    bool setConfig(const std::string& section, const std::string& name, int value);
857
858
859
860
861
862
863
864
865
866
867
868
    
    inline std::string mapStateNumberToString(RegistrationState state) {
        std::string stringRepresentation;
        if (state > NumberOfState) {
            stringRepresentation = "ERROR";
            return stringRepresentation;
        }
        
        stringRepresentation = mapStateToChar[state];
        return stringRepresentation;
    }
    
869
    /**
870
871
872
873
874
875
     * Get a int from the configuration tree
     * Throw an Conf::ConfigTreeItemException if not found
     * @param section The section name to look in
     * @param name    The parameter name
     * @return int    The int value
     */
876
     
877
    int getConfigInt(const std::string& section, const std::string& name);
pierre-luc's avatar
pierre-luc committed
878
879
880
881
882
883
884
885
886
887
888
 
  /**
     * Get a bool from the configuration tree
     * Throw an Conf::ConfigTreeItemException if not found
     * @param section The section name to look in
     * @param name    The parameter name
     * @return bool    The bool value
     */
     
    bool getConfigBool(const std::string& section, const std::string& name);
            
889
    /**
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
     * Get a string from the configuration tree
     * Throw an Conf::ConfigTreeItemException if not found
     * @param section The section name to look in
     * @param name    The parameter name
     * @return sdt::string    The string value
     */
    std::string getConfigString(const std::string& section, const std::string& name);

    /**
     * Retrieve the soundcards index in the user config file and try to open audio devices
     * with a specific alsa plugin.
     * Set the audio layer sample rate
     */
    void selectAudioDriver(void);

    /**
906
     * Handle audio sounds heard by a caller while they wait for their
907
908
909
910
911
912
913
914
915
916
917
918
919
     * connection to a called party to be completed.
     */
    void ringback ();

    /**
     * Handle played music when an incoming call occurs
     */
    void ringtone ();

    /**
     * Handle played music when a congestion occurs
     */
    void congestion ();
920

921
922
923
924
925
926
927
928
929
930
    /**
     * Handle played sound when a call can not be conpleted because of a busy recipient
     */
    void callBusy(const CallID& id);

    /**
     * Handle played sound when a failure occurs
     */
    void callFailure(const CallID& id);

931
    /**
932
     * Retrieve the current telephone tone
933
     * @return AudioLoop*   The audio tone or 0 if no tone (init before calling this function)
934
935
     */
    AudioLoop* getTelephoneTone();
936
937

    /**
938
     * Retrieve the current telephone file
939
     * @return AudioLoop* The audio file or 0 if the wav is stopped
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
     */
    AudioLoop* getTelephoneFile();

    /**
     * @return true is there is one or many incoming call waiting
     * new call, not anwsered or refused
     */
    bool incomingCallWaiting(void);

    /**
     * Notification of incoming call when you are already busy
     */
    void notificationIncomingCall(void);

    /*
     * Inline functions to manage speaker volume control
     * Read by main thread and AudioLayer thread
     * Write by main thread only
     * @return unsigned short	The volume value
     */
    unsigned short getSpkrVolume(void) { return _spkr_volume; }
961

962
963
964
965
966
967
    /*
     * Inline functions to manage speaker volume control
     * Read by main thread and AudioLayer thread
     * Write by main thread only
     * @param spkr_vol	The volume value
     */
968
    void setSpkrVolume(unsigned short spkr_vol);
969

970
971
972
973
974
975
976
    /*
     * Inline functions to manage mic volume control
     * Read by main thread and AudioLayer thread
     * Write by main thread only
     * @return unsigned short	The volume value
     */
    unsigned short getMicVolume(void) {  return _mic_volume;  }
977

978
979
980
981
982
983
    /*
     * Inline functions to manage mic volume control
     * Read by main thread and AudioLayer thread
     * Write by main thread only
     * @param mic_vol	The volume value
     */
984
    void setMicVolume(unsigned short mic_vol);
985
986
987
988
989
990

    /**
     * Init default values for the different fields in the config file.
     * Fills the local _config (Conf::ConfigTree) with the default contents.
     * Called in main.cpp, just before Manager::init().
     */
991
    void initConfigFile ( bool load_user_value=true, std::string alternate="");
992
993
994
995
996
997
998
999

    /**
     * Tell if the setup was already loaded
     * @return bool True if yes
     *		  false otherwise
     */
    bool hasLoadedSetup() { return _setupLoaded; }

1000
    /**
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
     * Return a new random callid that is not present in the list
     * @return CallID A brand new callid
     */
    CallID getNewCallID();

    /**
     * Get the current call id
     * @return CallID	The call id or ""
     */
    const CallID& getCurrentCallId();

    /**
     * Check if a call is the current one
     * @param callId the new callid
     * @return bool   True if the id is the current call
     */
    bool isCurrentCall(const CallID& callId);

Julien Bonjean's avatar
Julien Bonjean committed
1019
1020
1021
1022
1023
1024
1025
1026

    /**
     * Send registration to all enabled accounts
     * @return 0 on registration success
     *          1 otherelse
     */
    int registerAccounts();

1027
    /**
1028
1029
1030
     * Restart PJSIP
     * @param void
     * @return void
1031
     */
Emmanuel Milou's avatar
Emmanuel Milou committed
1032
    void restartPJSIP( );
1033

1034
    void unregisterCurSIPAccounts (void);
1035

1036
    void registerCurSIPAccounts (void);
1037

1038
1039
1040
1041
    /*
     * Initialize audiodriver
     */
    bool initAudioDriver(void);
1042
1043

    ost::Mutex* getAudioLayerMutex() { return &_audiolayer_mutex; }
1044
    
1045
  private:
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
    /* Transform digest to string.
    * output must be at least PJSIP_MD5STRLEN+1 bytes.
    * Helper function taken from sip_auth_client.c in 
    * pjproject-1.0.3.
    *
    * NOTE: THE OUTPUT STRING IS NOT NULL TERMINATED!
    */
    void digest2str(const unsigned char digest[], char *output);

    /** 
     * Helper function that creates an MD5 Hash from the credential
     * information provided as parameters. The hash is computed as
     * MD5(username ":" realm ":" password).
     * 
     */
    std::string computeMd5HashFromCredential(const std::string& username, const std::string& password, const std::string& realm);
1062

1063
1064
1065
1066
1067
1068
1069
1070
    /**
     * Check if a process is running with the system command
     *
     * @return 0 on success
     *          1 otherelse
     */
    int app_is_running(std::string process);

1071
    /**
1072
     * Create .PROGNAME directory in home user and create
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
     * configuration tree from the settings file if this file exists.
     *
     * @return	0 if creating file failed
     *			1 if config-file exists
     *			2 if file doesn't exist yet.
     */
    int createSettingsPath (void);

    /*
     * Initialize audiocodec with config setting
     */
    void initAudioCodec(void);

1086
    
1087
1088
1089
1090
1091
1092
1093
    /*
     * Initialize zeroconf module and scanning
     */
    void initZeroconf(void);

    /*
     * Init the volume for speakers/micro from 0 to 100 value
1094
     */
1095
1096
1097
1098
1099
1100
1101
    void initVolume();

    /**
     * Switch of current call id
     * @param id The new callid
     */
    void switchCall(const CallID& id);
1102

1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
    /*
     * Play one tone
     * @return false if the driver is uninitialize
     */
    bool playATone(Tone::TONEID toneId);

    /** The configuration tree. It contains accounts parameters, general user settings ,audio settings, ... */
    Conf::ConfigTree _config;

    /** Current Call ID */
    CallID _currentCallId2;

    /** Protected current call access */
1116
    ost::Mutex _currentCallMutex;
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133

    /** Vector of CodecDescriptor */
    CodecDescriptor* _codecBuilder;

    /** Audio layer */
    AudioLayer* _audiodriver;

    // Main thread

    DTMF* _dtmfKey;

    // map of codec (for configlist request)
    CodecDescriptor _codecDescriptorMap;

    /////////////////////
    // Protected by Mutex
    /////////////////////
1134
    ost::Mutex _toneMutex;
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
    TelephoneTone* _telephoneTone;
    AudioFile _audiofile;

    // To handle volume control
    short _spkr_volume;
    short _mic_volume;
    // End of sound variable


    // Multithread variable (protected by _mutex)
1145
    //
1146
    /** Mutex to protect access to code section */
1147
    ost::Mutex _mutex;
1148

1149
1150
    ost::Mutex _audiolayer_mutex;

1151
1152
1153
1154
1155
1156
1157
    // Multithread variable (non protected)
    DBusManagerImpl * _dbus;

    /** Waiting Call Vectors */
    CallIDSet _waitingCall;

    /** Protect waiting call list, access by many voip/audio threads */
1158
    ost::Mutex _waitingCallMutex;
1159

1160
1161
    /** Number of waiting call, synchronize with waitingcall callidvector */
    unsigned int _nbIncomingWaitingCall;
1162

1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
    /**
     * Add incoming callid to the waiting list
     * @param id CallID to add
     */
    void addWaitingCall(const CallID& id);

    /**
     * Remove incoming callid to the waiting list
     * @param id CallID to remove
     */
    void removeWaitingCall(const CallID& id);

    /**
     * Tell if a call is waiting and should be remove
     * @param id CallID to test
     * @return bool True if the call is waiting
     */
    bool isWaitingCall(const CallID& id);

    /**
1183
     * Path of the ConfigFile
1184
1185
1186
1187
1188
     */
    std::string 	_path;
    int _exist;
    int _setupLoaded;

jpbl's avatar
jpbl committed
1189
#ifdef USE_ZEROCONF
1190
1191
1192
    // DNSService contain every zeroconf services
    //  configuration detected on the network
    DNSService *_DNSService;
jpbl's avatar
jpbl committed
1193
1194
#endif

1195
1196
    /** Map to associate a CallID to the good account */
    CallAccountMap _callAccountMap;
1197

1198
    /** Mutex to lock the call account map (main thread + voiplink thread) */
1199
    ost::Mutex _callAccountMapMutex;
1200

Emmanuel Milou's avatar
Emmanuel Milou committed
1201
1202
1203
1204
1205
1206
1207
1208
    CallConfigMap _callConfigMap;

    bool associateConfigToCall (const CallID& callID, Call::CallConfiguration config);

    Call::CallConfiguration getConfigFromCall(const CallID& callID);

    bool removeCallConfig(const CallID& callID);

1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
    /** Associate a new CallID to a AccountID
     * Protected by mutex
     * @param callID the new CallID not in the list yet
     * @param accountID the known accountID present in accountMap
     * @return bool True if the new association is create
     */
    bool associateCallToAccount(const CallID& callID, const AccountID& accountID);

    /** Remove a CallID/AccountID association
     * Protected by mutex
     * @param callID the CallID to remove
     * @return bool True if association is removed
     */
    bool removeCallAccount(const CallID& callID);

1224
    /**
1225
     *Contains a list of account (sip, aix, etc) and their respective voiplink/calls */
1226
    AccountMap _accountMap;
1227
1228
    
    Account * _directIpAccount;
1229
1230
1231
1232
1233
1234
1235

    /**
     * Load the account from configuration
     * @return short Number of account
     */
    short loadAccountMap();

1236

1237
1238
1239
1240
    /**
     * Unload the account (delete them)
     */
    void unloadAccountMap();
1241
1242


1243
 public:
1244
1245
1246
1247
1248
1249
1250
1251
1252
    /**
     * Instance of the MainBuffer for the whole application
     *
     * In order to send signal to other parts of the application, one must pass through the mainbuffer.
     * Audio instances must be registered into the MainBuffer and bound together via the ManagerImpl.
     *
     */ 
     MainBuffer _mainBuffer;

1253
1254
1255
1256
1257
1258

    /**
     * Tell if there is a current call processed
     * @return bool True if there is a current call
     */
    bool hasCurrentCall();
1259
1260
1261
1262
1263
1264
1265
   
    /**
     * Return the current DBusManagerImpl
     * @return A pointer to the DBusManagerImpl instance
     */
    DBusManagerImpl * getDbusManager() { return _dbus; }
    
1266
     /**
1267
1268
1269
1270
1271
1272
     * Tell if an account exists
     * @param accountID account ID check
     * @return bool True if the account exists
     *		  false otherwise
     */
    bool accountExists(const AccountID& accountID);
1273

1274
1275
1276
    std::map<std::string, std::string> send_history_to_client (void); 

    void receive_history_from_client (std::map<std::string, std::string> history);
1277
1278
1279
1280
1281
1282
1283
    /**
     * Get an account pointer
     * @param accountID account ID to get
     * @return Account*	 The account pointer or 0
     */
    Account* getAccount(const AccountID& accountID);

1284
1285
1286
1287
1288
1289
1290
    /** Return the AccountID from a CallID
     * Protected by mutex
     * @param callID the CallID in the list
     * @return AccountID  The accountID associated or "" if the callID is not found
     */
    AccountID getAccountFromCall(const CallID& callID);

1291
1292
1293
1294
1295
    /**
     * Get the voip link from the account pointer
     * @param accountID	  Account ID to get
     * @return VoIPLink*   The voip link from the account pointer or 0
     */
Emmanuel Milou's avatar
Emmanuel Milou committed
1296
    VoIPLink* getAccountLink(const AccountID& accountID=AccountNULL);
1297

Emmanuel Milou's avatar
Emmanuel Milou committed
1298
1299
    VoIPLink* getSIPAccountLink (void);

1300
1301
    AccountID getAccountIdFromNameAndServer(const std::string& userName, const std::string& server);

1302
    int getLocalIp2IpPort();
1303

Emmanuel Milou's avatar
Emmanuel Milou committed
1304
1305
1306
1307
1308
    std::string getStunServer (void);
    void setStunServer (const std::string &server);

    int isStunEnabled (void);
    void enableStun (void);
1309

1310
    // Map containing reference between conferences and calls 
1311
1312
    ConferenceCallMap _conferencecall;

1313
    // Map containing conference pointers
1314
1315
    ConferenceMap _conferencemap;

1316
private:
Yun Liu's avatar
Yun Liu committed
1317
1318
1319
1320
1321
1322
1323

    // Copy Constructor
    ManagerImpl(const ManagerImpl& rh);

    // Assignment Operator
    ManagerImpl& operator=( const ManagerImpl& rh);

1324
1325
    NumberCleaner *_cleaner;

1326
1327
1328
    /**
      * To handle the persistent history
      */
1329
    HistoryManager * _history;
1330

Emmanuel Milou's avatar
Emmanuel Milou committed
1331
1332
1333
1334
1335
    /**
     * Check if the call is a classic call or a direct IP-to-IP call
     */
    void check_call_configuration (const CallID& id, const std::string& to, Call::CallConfiguration *callConfig);

1336
1337
1338
1339
#ifdef TEST
    bool testCallAccountMap();
    bool testAccountMap();
#endif
yanmorin's avatar
   
yanmorin committed
1340

1341
    friend class ConfigurationTest;
1342
    friend class HistoryTest;
jpbl's avatar
jpbl committed
1343
1344
};

Emmanuel Milou's avatar
Emmanuel Milou committed
1345
#endif // __MANAGER_H__