1
/***************************************************************************
2
* Copyright (C) 2009 by Savoir-Faire Linux *
3
* Author : Jérémy Quentin <jeremy.quentin@savoirfairelinux.com> *
4
* Emmanuel Lepage Vallee <emmanuel.lepage@savoirfairelinux.com>*
6
* This program is free software; you can redistribute it and/or modify *
7
* it under the terms of the GNU General Public License as published by *
8
* the Free Software Foundation; either version 3 of the License, or *
9
* (at your option) any later version. *
11
* This program is distributed in the hope that it will be useful, *
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14
* GNU General Public License for more details. *
16
* You should have received a copy of the GNU General Public License *
17
* along with this program; if not, write to the *
18
* Free Software Foundation, Inc., *
19
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
20
***************************************************************************/
26
#include <QtCore/QDebug>
27
#include <QtCore/QString>
30
#include "sflphone_const.h"
33
#include "configurationmanager_interface_singleton.h"
35
///Match state name to user readable string
36
const QString& account_state_name(const QString& s)
38
static const QString registered = "Registered" ;
39
static const QString notRegistered = "Not Registered" ;
40
static const QString trying = "Trying..." ;
41
static const QString error = "Error" ;
42
static const QString authenticationFailed = "Authentication Failed" ;
43
static const QString networkUnreachable = "Network unreachable" ;
44
static const QString hostUnreachable = "Host unreachable" ;
45
static const QString stunConfigurationError = "Stun configuration error" ;
46
static const QString stunServerInvalid = "Stun server invalid" ;
47
static const QString invalid = "Invalid" ;
49
if(s == QString(ACCOUNT_STATE_REGISTERED) )
51
if(s == QString(ACCOUNT_STATE_UNREGISTERED) )
52
return notRegistered ;
53
if(s == QString(ACCOUNT_STATE_TRYING) )
55
if(s == QString(ACCOUNT_STATE_ERROR) )
57
if(s == QString(ACCOUNT_STATE_ERROR_AUTH) )
58
return authenticationFailed ;
59
if(s == QString(ACCOUNT_STATE_ERROR_NETWORK) )
60
return networkUnreachable ;
61
if(s == QString(ACCOUNT_STATE_ERROR_HOST) )
62
return hostUnreachable ;
63
if(s == QString(ACCOUNT_STATE_ERROR_CONF_STUN) )
64
return stunConfigurationError ;
65
if(s == QString(ACCOUNT_STATE_ERROR_EXIST_STUN) )
66
return stunServerInvalid ;
71
Account::Account():m_pAccountId(NULL),m_pAccountDetails(NULL)
75
///Build an account from it'id
76
Account* Account::buildExistingAccountFromId(const QString& _accountId)
78
qDebug() << "Building an account from id: " << _accountId;
79
ConfigurationManagerInterface& configurationManager = ConfigurationManagerInterfaceSingleton::getInstance();
80
Account* a = new Account();
81
a->m_pAccountId = new QString(_accountId);
82
MapStringString* aDetails = new MapStringString(configurationManager.getAccountDetails(_accountId).value());
84
if (!aDetails->count()) {
85
qDebug() << "Account not found";
88
a->m_pAccountDetails = aDetails;
92
///Build an account from it's name / alias
93
Account* Account::buildNewAccountFromAlias(const QString& alias)
95
qDebug() << "Building an account from alias: " << alias;
96
Account* a = new Account();
97
a->m_pAccountDetails = new MapStringString();
98
a->setAccountDetail(ACCOUNT_ALIAS,alias);
109
/*****************************************************************************
113
****************************************************************************/
115
///IS this account new
116
bool Account::isNew() const
118
return (m_pAccountId == NULL);
121
///Get this account ID
122
const QString& Account::getAccountId() const
125
qDebug() << "Error : getting AccountId of a new account.";
127
qDebug() << "Account not configured";
128
return EMPTY_STRING; //WARNING May explode
131
return *m_pAccountId;
134
///Get this account details
135
const MapStringString& Account::getAccountDetails() const
137
return *m_pAccountDetails;
141
const QString& Account::getStateName(const QString& state) const
143
return (const QString&)account_state_name(state);
146
///Get an account detail
147
const QString& Account::getAccountDetail(const QString& param) const
149
if (!m_pAccountDetails) {
150
qDebug() << "The account list is not set";
151
return EMPTY_STRING; //May crash, but better than crashing now
153
if (m_pAccountDetails->find(param) != m_pAccountDetails->end())
154
return (*m_pAccountDetails)[param];
156
qDebug() << "Account details not found, there is " << m_pAccountDetails->count() << " details available";
162
const QString& Account::getAlias() const
164
return getAccountDetail(ACCOUNT_ALIAS);
167
///Is this account enabled
168
bool Account::isEnabled() const
170
return (getAccountDetail(ACCOUNT_ENABLED) == ACCOUNT_ENABLED_TRUE);
173
///Is this account registered
174
bool Account::isRegistered() const
176
return (getAccountDetail(ACCOUNT_STATUS) == ACCOUNT_STATE_REGISTERED);
180
/*****************************************************************************
184
****************************************************************************/
186
///Set account details
187
void Account::setAccountDetails(const MapStringString& m)
189
*m_pAccountDetails = m;
192
///Set a specific detail
193
void Account::setAccountDetail(const QString& param, const QString& val)
195
(*m_pAccountDetails)[param] = val;
198
///Set the account id
199
void Account::setAccountId(const QString& id)
201
qDebug() << "Setting accountId = " << m_pAccountId;
203
qDebug() << "Error : setting AccountId of an existing account.";
204
m_pAccountId = new QString(id);
207
///Set account enabled
208
void Account::setEnabled(bool checked)
210
setAccountDetail(ACCOUNT_ENABLED, checked ? ACCOUNT_ENABLED_TRUE : ACCOUNT_ENABLED_FALSE);
213
/*****************************************************************************
217
****************************************************************************/
219
///Update the account
220
void Account::updateState()
223
ConfigurationManagerInterface & configurationManager = ConfigurationManagerInterfaceSingleton::getInstance();
224
MapStringString details = configurationManager.getAccountDetails(getAccountId()).value();
225
QString status = details[ACCOUNT_STATUS];
226
setAccountDetail(ACCOUNT_STATUS, status); //Update -internal- object state
230
/*****************************************************************************
234
****************************************************************************/
236
///Are both account the same
237
bool Account::operator==(const Account& a)const
239
return *m_pAccountId == *a.m_pAccountId;