Commit 92431fe9 authored by Alexandre Bourget's avatar Alexandre Bourget Committed by Alexandre Bourget
Browse files

Beginning of CONFIGURATION rework.

parent deda3bae
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
*/ */
#include "config.h" #include "config.h"
#include "../global.h"
#include <fstream> #include <fstream>
namespace Conf { namespace Conf {
...@@ -50,6 +51,22 @@ ConfigTree::createSection(const std::string& section) { ...@@ -50,6 +51,22 @@ ConfigTree::createSection(const std::string& section) {
} }
} }
/** Retrieve the sections as an array */
TokenList
ConfigTree::getSections()
{
TokenList sections;
SectionMap::iterator iter = _sections.begin();
while(iter != _sections.end()) {
// add to token list the: iter->second;
sections.push_back(iter->first);
iter++;
}
return sections;
}
/** /**
* Add the config item only if it exists.. * Add the config item only if it exists..
* If the section doesn't exists, create it * If the section doesn't exists, create it
...@@ -73,15 +90,41 @@ ConfigTree::addConfigTreeItem(const std::string& section, const ConfigTreeItem i ...@@ -73,15 +90,41 @@ ConfigTree::addConfigTreeItem(const std::string& section, const ConfigTreeItem i
} }
} }
/**
* Add the config item only if it doesn't exist. Set the default value.
*/
void
ConfigTree::verifyConfigTreeItem(const std::string& section,
const std::string& itemName,
const std::string& defaultValue,
const std::string& type)
{
// Create section if it doesn't exist.
SectionMap::iterator iter = _sections.find(section);
if ( iter == _sections.end()) {
_sections[section] = new ItemMap;
iter = _sections.find(section);
}
// Check for the item, and add it if necessary (with default value).
ItemMap::iterator iterItem = iter->second->find(itemName);
if ( iterItem == iter->second->end()) {
// It's not there, create it.
addConfigTreeItem(section, ConfigTreeItem(itemName, defaultValue, type));
}
}
// throw a ConfigTreeItemException if not found // throw a ConfigTreeItemException if not found
std::string std::string
ConfigTree::getConfigTreeItemValue(const std::string& section, const std::string& itemName) ConfigTree::getConfigTreeItemValue(const std::string& section, const std::string& itemName)
{ {
ConfigTreeItem* item = getConfigTreeItem(section, itemName); ConfigTreeItem* item = getConfigTreeItem(section, itemName);
if (item!=NULL) { if (item != NULL) {
return item->getValue(); return item->getValue();
} else { } else {
throw ConfigTreeItemException(); _debug("Unknown config option: [%s] %s\n", section.c_str(), itemName.c_str());
//throw ConfigTreeItemException();
} }
return ""; return "";
} }
...@@ -91,10 +134,11 @@ int ...@@ -91,10 +134,11 @@ int
ConfigTree::getConfigTreeItemIntValue(const std::string& section, const std::string& itemName) ConfigTree::getConfigTreeItemIntValue(const std::string& section, const std::string& itemName)
{ {
ConfigTreeItem* item = getConfigTreeItem(section, itemName); ConfigTreeItem* item = getConfigTreeItem(section, itemName);
if (item!=NULL && item->getType() == "int") { if (item != NULL && item->getType() == "int") {
return atoi(item->getValue().data()); return atoi(item->getValue().data());
} else { } else {
throw ConfigTreeItemException(); _debug("Unknown config (int) option: [%s] %s\n", section.c_str(), itemName.c_str());
//throw ConfigTreeItemException();
} }
return 0; return 0;
} }
...@@ -133,17 +177,28 @@ ConfigTree::getConfigTreeItem(const std::string& section, const std::string& ite ...@@ -133,17 +177,28 @@ ConfigTree::getConfigTreeItem(const std::string& section, const std::string& ite
} }
/** /**
* Set the configItem if found, else do nothing * Set the configItem if found, if not, *CREATE IT*
*
* @todo Faudra démêler tout ça, le verifyConfigTreeItem et setConfigTree qui font
* la même chose, et qui, inutilement, y'a plein de restrictions.
* @todo Élimier les 45,000 classes qui servent à rien pour Conf.
*/ */
bool bool
ConfigTree::setConfigTreeItem(const std::string& section, const std::string& itemName, const std::string& value) { ConfigTree::setConfigTreeItem(const std::string& section,
const std::string& itemName,
const std::string& value) {
SectionMap::iterator iter = _sections.find(section); SectionMap::iterator iter = _sections.find(section);
if ( iter == _sections.end()) { if ( iter == _sections.end()) {
return false; // Not found, create section
_sections[section] = new ItemMap;
iter = _sections.find(section);
} }
ItemMap::iterator iterItem = iter->second->find(itemName); ItemMap::iterator iterItem = iter->second->find(itemName);
if ( iterItem == iter->second->end()) { if ( iterItem == iter->second->end()) {
return false; // Item not found, create it, defaults to type "string"
addConfigTreeItem(section, ConfigTreeItem(itemName, value, "string"));
return true;
} }
iterItem->second.setValue(value); iterItem->second.setValue(value);
return true; return true;
...@@ -230,7 +285,6 @@ ConfigTree::populateFromFile(const std::string& fileName) { ...@@ -230,7 +285,6 @@ ConfigTree::populateFromFile(const std::string& fileName) {
// If the line is a section // If the line is a section
pos = line.find(']'); pos = line.find(']');
section = line.substr(1, pos - 1); section = line.substr(1, pos - 1);
} else if (line[0] != '#') { } else if (line[0] != '#') {
// If the line is "key=value" and doesn't begin with '#'(comments) // If the line is "key=value" and doesn't begin with '#'(comments)
......
...@@ -93,11 +93,11 @@ IAXAccount::initConfig(Conf::ConfigTree& config) ...@@ -93,11 +93,11 @@ IAXAccount::initConfig(Conf::ConfigTree& config)
Account::initConfig(config); Account::initConfig(config);
// IAX specific // IAX specific
config.addConfigTreeItem(section, Conf::ConfigTreeItem(CONFIG_ACCOUNT_TYPE, "IAX", type_str)); config.verifyConfigTreeItem(section, CONFIG_ACCOUNT_TYPE, "IAX", type_str);
config.addConfigTreeItem(section, Conf::ConfigTreeItem(IAX_FULL_NAME, "", type_str)); config.verifyConfigTreeItem(section, IAX_FULL_NAME, "", type_str);
config.addConfigTreeItem(section, Conf::ConfigTreeItem(IAX_HOST, "", type_str)); config.verifyConfigTreeItem(section, IAX_HOST, "", type_str);
config.addConfigTreeItem(section, Conf::ConfigTreeItem(IAX_USER, "", type_str)); config.verifyConfigTreeItem(section, IAX_USER, "", type_str);
config.addConfigTreeItem(section, Conf::ConfigTreeItem(IAX_PASS, "", type_str)); config.verifyConfigTreeItem(section, IAX_PASS, "", type_str);
} }
void void
......
...@@ -91,7 +91,7 @@ ManagerImpl::ManagerImpl (void) ...@@ -91,7 +91,7 @@ ManagerImpl::ManagerImpl (void)
#endif #endif
// should be call before initConfigFile // should be call before initConfigFile
loadAccountMap(); // loadAccountMap();, called in init() now.
} }
// never call if we use only the singleton... // never call if we use only the singleton...
...@@ -109,6 +109,9 @@ ManagerImpl::~ManagerImpl (void) ...@@ -109,6 +109,9 @@ ManagerImpl::~ManagerImpl (void)
void void
ManagerImpl::init() ManagerImpl::init()
{ {
// Load accounts, init map
loadAccountMap();
initVolume(); initVolume();
if (_exist == 0) { if (_exist == 0) {
...@@ -147,10 +150,10 @@ void ManagerImpl::terminate() ...@@ -147,10 +150,10 @@ void ManagerImpl::terminate()
delete _dtmfKey; delete _dtmfKey;
_debug("Unload Audio Driver\n"); _debug("Unload Audio Driver\n");
delete _audiodriver; _audiodriver = 0; delete _audiodriver; _audiodriver = NULL;
_debug("Unload Telephone Tone\n"); _debug("Unload Telephone Tone\n");
delete _telephoneTone; _telephoneTone = 0; delete _telephoneTone; _telephoneTone = NULL;
} }
bool bool
...@@ -1004,8 +1007,6 @@ ManagerImpl::behindNat(const std::string& svr, int port) ...@@ -1004,8 +1007,6 @@ ManagerImpl::behindNat(const std::string& svr, int port)
* Initialization: Main Thread * Initialization: Main Thread
* @return 1: ok * @return 1: ok
-1: error directory -1: error directory
0: unable to load the setting
2: file doesn't exist yet
*/ */
int int
ManagerImpl::createSettingsPath (void) { ManagerImpl::createSettingsPath (void) {
...@@ -1021,7 +1022,7 @@ ManagerImpl::createSettingsPath (void) { ...@@ -1021,7 +1022,7 @@ ManagerImpl::createSettingsPath (void) {
// Load user's configuration // Load user's configuration
_path = _path + DIR_SEPARATOR_STR + PROGNAME + "rc"; _path = _path + DIR_SEPARATOR_STR + PROGNAME + "rc";
return _config.populateFromFile(_path); return 1;
} }
/** /**
...@@ -1063,7 +1064,10 @@ ManagerImpl::initConfigFile (void) ...@@ -1063,7 +1064,10 @@ ManagerImpl::initConfigFile (void)
initConfigAccount(); initConfigAccount();
_exist = createSettingsPath(); if (createSettingsPath() == 1) {
_exist = _config.populateFromFile(_path);
}
_setupLoaded = (_exist == 2 ) ? false : true; _setupLoaded = (_exist == 2 ) ? false : true;
} }
...@@ -1308,6 +1312,7 @@ ManagerImpl::getCallStatus(const std::string& sequenceId) ...@@ -1308,6 +1312,7 @@ ManagerImpl::getCallStatus(const std::string& sequenceId)
} }
//THREAD=Main //THREAD=Main
/* Unused, Deprecated */
bool bool
ManagerImpl::getConfigAll(const std::string& sequenceId) ManagerImpl::getConfigAll(const std::string& sequenceId)
{ {
...@@ -1379,7 +1384,7 @@ ManagerImpl::getConfigList(const std::string& sequenceId, const std::string& nam ...@@ -1379,7 +1384,7 @@ ManagerImpl::getConfigList(const std::string& sequenceId, const std::string& nam
{ {
bool returnValue = false; bool returnValue = false;
TokenList tk; TokenList tk;
if (name=="codecdescriptor") { if (name == "codecdescriptor") {
CodecMap map = _codecDescriptorMap.getMap(); CodecMap map = _codecDescriptorMap.getMap();
CodecMap::iterator iter = map.begin(); CodecMap::iterator iter = map.begin();
...@@ -1397,7 +1402,7 @@ ManagerImpl::getConfigList(const std::string& sequenceId, const std::string& nam ...@@ -1397,7 +1402,7 @@ ManagerImpl::getConfigList(const std::string& sequenceId, const std::string& nam
iter++; iter++;
} }
returnValue = true; returnValue = true;
} else if (name=="ringtones") { } else if (name == "ringtones") {
// add empty line // add empty line
std::ostringstream str; std::ostringstream str;
str << 1; str << 1;
...@@ -1413,13 +1418,13 @@ ManagerImpl::getConfigList(const std::string& sequenceId, const std::string& nam ...@@ -1413,13 +1418,13 @@ ManagerImpl::getConfigList(const std::string& sequenceId, const std::string& nam
// home directory // home directory
path = std::string(HOMEDIR) + DIR_SEPARATOR_STR + "." + PROGDIR + DIR_SEPARATOR_STR + RINGDIR; path = std::string(HOMEDIR) + DIR_SEPARATOR_STR + "." + PROGDIR + DIR_SEPARATOR_STR + RINGDIR;
getDirListing(sequenceId, path, &nbFile); getDirListing(sequenceId, path, &nbFile);
} else if (name=="audiodevice") { } else if (name == "audiodevice") {
returnValue = getAudioDeviceList(sequenceId, AudioLayer::InputDevice | AudioLayer::OutputDevice); returnValue = getAudioDeviceList(sequenceId, AudioLayer::InputDevice | AudioLayer::OutputDevice);
} else if (name=="audiodevicein") { } else if (name == "audiodevicein") {
returnValue = getAudioDeviceList(sequenceId, AudioLayer::InputDevice); returnValue = getAudioDeviceList(sequenceId, AudioLayer::InputDevice);
} else if (name=="audiodeviceout") { } else if (name == "audiodeviceout") {
returnValue = getAudioDeviceList(sequenceId, AudioLayer::OutputDevice); returnValue = getAudioDeviceList(sequenceId, AudioLayer::OutputDevice);
} else if (name=="countrytones") { } else if (name == "countrytones") {
returnValue = getCountryTones(sequenceId); returnValue = getCountryTones(sequenceId);
} }
return returnValue; return returnValue;
...@@ -1694,7 +1699,8 @@ ManagerImpl::setAccountDetails( const ::DBus::String& accountID, ...@@ -1694,7 +1699,8 @@ ManagerImpl::setAccountDetails( const ::DBus::String& accountID,
} }
saveConfig(); saveConfig();
/** @todo Then, reset the VoIP link with the new information */
} }
void void
...@@ -1742,9 +1748,9 @@ ManagerImpl::setSwitch(const std::string& switchName, std::string& message) { ...@@ -1742,9 +1748,9 @@ ManagerImpl::setSwitch(const std::string& switchName, std::string& message) {
_toneMutex.enterMutex(); _toneMutex.enterMutex();
_debug("Unload Telephone Tone\n"); _debug("Unload Telephone Tone\n");
delete _telephoneTone; _telephoneTone = 0; delete _telephoneTone; _telephoneTone = NULL;
_debug("Unload DTMF Key\n"); _debug("Unload DTMF Key\n");
delete _dtmfKey; _dtmfKey = 0; delete _dtmfKey; _dtmfKey = NULL;
_debug("Load Telephone Tone\n"); _debug("Load Telephone Tone\n");
std::string country = getConfigString(PREFERENCES, ZONE_TONE); std::string country = getConfigString(PREFERENCES, ZONE_TONE);
...@@ -1836,8 +1842,43 @@ ManagerImpl::loadAccountMap() ...@@ -1836,8 +1842,43 @@ ManagerImpl::loadAccountMap()
{ {
_debugStart("Load account:"); _debugStart("Load account:");
short nbAccount = 0; short nbAccount = 0;
TokenList sections = _config.getSections();
std::string accountType;
Account* tmpAccount; Account* tmpAccount;
// iter = std::string
TokenList::iterator iter = sections.begin();
while(iter != sections.end()) {
// Check if it starts with "Account:" (SIP and IAX pour le moment)
if (iter->find("Account:") == -1) {
iter++;
continue;
}
accountType = getConfigString(*iter, CONFIG_ACCOUNT_TYPE);
if (accountType == "SIP") {
tmpAccount = AccountCreator::createAccount(AccountCreator::SIP_ACCOUNT, *iter);
}
else if (accountType == "IAX") {
tmpAccount = AccountCreator::createAccount(AccountCreator::IAX_ACCOUNT, *iter);
}
else {
_debug("Unknown %s param in config file (%s)\n", CONFIG_ACCOUNT_TYPE, accountType.c_str());
}
if (tmpAccount != NULL) {
_debugMid(" %s ", iter->c_str());
_accountMap[iter->c_str()] = tmpAccount;
nbAccount++;
}
_debugEnd("\n");
iter++;
}
/*
// SIP Loading X account... // SIP Loading X account...
short nbAccountSIP = ACCOUNT_SIP_COUNT_DEFAULT; short nbAccountSIP = ACCOUNT_SIP_COUNT_DEFAULT;
for (short iAccountSIP = 0; iAccountSIP<nbAccountSIP; iAccountSIP++) { for (short iAccountSIP = 0; iAccountSIP<nbAccountSIP; iAccountSIP++) {
...@@ -1865,6 +1906,7 @@ ManagerImpl::loadAccountMap() ...@@ -1865,6 +1906,7 @@ ManagerImpl::loadAccountMap()
} }
} }
_debugEnd("\n"); _debugEnd("\n");
*/
return nbAccount; return nbAccount;
} }
......
...@@ -103,15 +103,15 @@ SIPAccount::initConfig(Conf::ConfigTree& config) ...@@ -103,15 +103,15 @@ SIPAccount::initConfig(Conf::ConfigTree& config)
Account::initConfig(config); Account::initConfig(config);
// SIP specific // SIP specific
config.addConfigTreeItem(section, Conf::ConfigTreeItem(CONFIG_ACCOUNT_TYPE, "SIP", type_str)); config.verifyConfigTreeItem(section, CONFIG_ACCOUNT_TYPE, "SIP", type_str);
config.addConfigTreeItem(section, Conf::ConfigTreeItem(SIP_FULL_NAME, "", type_str)); config.verifyConfigTreeItem(section, SIP_FULL_NAME, "", type_str);
config.addConfigTreeItem(section, Conf::ConfigTreeItem(SIP_USER_PART, "", type_str)); config.verifyConfigTreeItem(section, SIP_USER_PART, "", type_str);
config.addConfigTreeItem(section, Conf::ConfigTreeItem(SIP_HOST_PART, "", type_str)); config.verifyConfigTreeItem(section, SIP_HOST_PART, "", type_str);
config.addConfigTreeItem(section, Conf::ConfigTreeItem(SIP_AUTH_NAME, "", type_str)); config.verifyConfigTreeItem(section, SIP_AUTH_NAME, "", type_str);
config.addConfigTreeItem(section, Conf::ConfigTreeItem(SIP_PASSWORD, "", type_str)); config.verifyConfigTreeItem(section, SIP_PASSWORD, "", type_str);
config.addConfigTreeItem(section, Conf::ConfigTreeItem(SIP_PROXY, "", type_str)); config.verifyConfigTreeItem(section, SIP_PROXY, "", type_str);
config.addConfigTreeItem(section, Conf::ConfigTreeItem(SIP_STUN_SERVER, "stun.fwdnet.net:3478", type_str)); config.verifyConfigTreeItem(section, SIP_STUN_SERVER, "stun.fwdnet.net:3478", type_str);
config.addConfigTreeItem(section, Conf::ConfigTreeItem(SIP_USE_STUN, "0", type_int)); config.verifyConfigTreeItem(section, SIP_USE_STUN, "0", type_int);
} }
void void
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment