sipaccount.cpp 4.6 KB
Newer Older
yanmorin's avatar
 
yanmorin committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 *  Copyright (C) 2006 Savoir-Faire Linux inc.
 *  Author: Yan Morin <yan.morin@savoirfairelinux.com>
 *                                                                              
 *  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
 *  the Free Software Foundation; either version 2 of the License, or
 *  (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.
 */
#include "sipaccount.h"
yanmorin's avatar
 
yanmorin committed
20
#include "sipvoiplink.h"
yanmorin's avatar
 
yanmorin committed
21
#include "manager.h"
yanmorin's avatar
 
yanmorin committed
22

yanmorin's avatar
 
yanmorin committed
23 24
#define SIP_FULL_NAME      "SIP.fullName"
#define SIP_USER_PART      "SIP.userPart"
yanmorin's avatar
 
yanmorin committed
25
#define SIP_AUTH_NAME      "SIP.username"
yanmorin's avatar
 
yanmorin committed
26 27 28 29 30 31
#define SIP_PASSWORD       "SIP.password"
#define SIP_HOST_PART      "SIP.hostPart"
#define SIP_PROXY          "SIP.proxy"
#define SIP_STUN_SERVER    "STUN.STUNserver"
#define SIP_USE_STUN       "STUN.useStun"

yanmorin's avatar
 
yanmorin committed
32 33 34 35

SIPAccount::SIPAccount(const AccountID& accountID)
 : Account(accountID)
{
yanmorin's avatar
 
yanmorin committed
36
  createVoIPLink();
yanmorin's avatar
 
yanmorin committed
37 38 39 40 41 42 43 44 45 46 47
}


SIPAccount::~SIPAccount()
{
}

/* virtual Account function implementation */
bool
SIPAccount::createVoIPLink()
{
yanmorin's avatar
 
yanmorin committed
48
  if (!_link) {
yanmorin's avatar
 
yanmorin committed
49
    _link = new SIPVoIPLink(_accountID);
yanmorin's avatar
 
yanmorin committed
50 51
  }
  return (_link != 0 ? true : false);
yanmorin's avatar
 
yanmorin committed
52 53 54 55 56
}

bool
SIPAccount::registerAccount()
{
yanmorin's avatar
 
yanmorin committed
57
  if (_link && !_registered) {
yanmorin's avatar
 
yanmorin committed
58 59 60 61 62 63 64 65
    SIPVoIPLink* tmplink = dynamic_cast<SIPVoIPLink*> (_link);
    if (tmplink) {
      tmplink->setProxy(Manager::instance().getConfigString(_accountID,SIP_PROXY));
      tmplink->setUserPart(Manager::instance().getConfigString(_accountID,SIP_USER_PART));
      tmplink->setAuthName(Manager::instance().getConfigString(_accountID,SIP_AUTH_NAME));
      tmplink->setPassword(Manager::instance().getConfigString(_accountID,SIP_PASSWORD));
    }
    _registered = _link->setRegister();
yanmorin's avatar
 
yanmorin committed
66 67
  }
  return _registered;
yanmorin's avatar
 
yanmorin committed
68 69 70 71 72
}

bool
SIPAccount::unregisterAccount()
{
yanmorin's avatar
 
yanmorin committed
73
  if (_link && _registered) {
yanmorin's avatar
 
yanmorin committed
74
    _registered = _link->setUnregister();
yanmorin's avatar
 
yanmorin committed
75 76
  }
  return !_registered;
yanmorin's avatar
 
yanmorin committed
77 78 79 80 81
}

bool
SIPAccount::init()
{
yanmorin's avatar
 
yanmorin committed
82
  if (_link && !_enabled) {
yanmorin's avatar
 
yanmorin committed
83 84 85
    _link->setFullName(Manager::instance().getConfigString(_accountID,SIP_FULL_NAME));
    _link->setHostName(Manager::instance().getConfigString(_accountID,SIP_HOST_PART));
    int useStun = Manager::instance().getConfigInt(_accountID,SIP_USE_STUN);
yanmorin's avatar
 
yanmorin committed
86
    
yanmorin's avatar
 
yanmorin committed
87 88
    SIPVoIPLink* tmplink = dynamic_cast<SIPVoIPLink*> (_link);
    if (tmplink) {
yanmorin's avatar
 
yanmorin committed
89
      tmplink->setStunServer(Manager::instance().getConfigString(_accountID,SIP_STUN_SERVER));
yanmorin's avatar
 
yanmorin committed
90 91
      tmplink->setUseStun( useStun!=0 ? true : false);
    }
yanmorin's avatar
 
yanmorin committed
92 93 94 95
    _link->init();
    _enabled = true;
    return true;
  }
yanmorin's avatar
 
yanmorin committed
96 97 98 99 100 101
  return false;
}

bool
SIPAccount::terminate()
{
yanmorin's avatar
 
yanmorin committed
102 103 104 105 106
  if (_link && _enabled) {
    _link->terminate();
    _enabled = false;
    return true;
  }
yanmorin's avatar
 
yanmorin committed
107 108 109
  return false;
}

yanmorin's avatar
 
yanmorin committed
110 111 112 113 114 115 116 117 118
void 
SIPAccount::initConfig(Conf::ConfigTree& config)
{
  std::string section(_accountID);
  std::string type_str("string");
  std::string type_int("int");
  
  config.addConfigTreeItem(section, Conf::ConfigTreeItem(CONFIG_ACCOUNT_TYPE, "SIP", type_str));
  config.addConfigTreeItem(section, Conf::ConfigTreeItem(CONFIG_ACCOUNT_ENABLE,"1", type_int));
yanmorin's avatar
 
yanmorin committed
119
  config.addConfigTreeItem(section, Conf::ConfigTreeItem(CONFIG_ACCOUNT_AUTO_REGISTER, "1", type_int));
yanmorin's avatar
 
yanmorin committed
120 121
  config.addConfigTreeItem(section, Conf::ConfigTreeItem(SIP_FULL_NAME, "", type_str));
  config.addConfigTreeItem(section, Conf::ConfigTreeItem(SIP_USER_PART, "", type_str));
yanmorin's avatar
 
yanmorin committed
122 123 124
  config.addConfigTreeItem(section, Conf::ConfigTreeItem(SIP_HOST_PART, "", type_str));
  config.addConfigTreeItem(section, Conf::ConfigTreeItem(SIP_AUTH_NAME, "", type_str));
  config.addConfigTreeItem(section, Conf::ConfigTreeItem(SIP_PASSWORD, "", type_str));
yanmorin's avatar
 
yanmorin committed
125 126 127 128 129
  config.addConfigTreeItem(section, Conf::ConfigTreeItem(SIP_PROXY, "", type_str));
  config.addConfigTreeItem(section, Conf::ConfigTreeItem(SIP_STUN_SERVER, "stun.fwdnet.net:3478", type_str));
  config.addConfigTreeItem(section, Conf::ConfigTreeItem(SIP_USE_STUN, "0", type_int));
}

yanmorin's avatar
 
yanmorin committed
130 131 132 133 134 135
void
SIPAccount::loadConfig() 
{
  _shouldInitOnStart = Manager::instance().getConfigInt(_accountID, CONFIG_ACCOUNT_ENABLE) ? true : false;
  _shouldRegisterOnStart = Manager::instance().getConfigInt(_accountID, CONFIG_ACCOUNT_AUTO_REGISTER) ? true : false;
}