diff --git a/src/account.cpp b/src/account.cpp index 3dd17f06387872ae8dd5d9b38ef064b5ff29d11c..c83697569b5ab62e82ac31389d0ec6488f7abfa8 100644 --- a/src/account.cpp +++ b/src/account.cpp @@ -41,6 +41,7 @@ Account::~Account() void Account::initConfig(Conf::ConfigTree& config) { + /* std::string section(_accountID); std::string type_str("string"); std::string type_int("int"); @@ -48,6 +49,7 @@ Account::initConfig(Conf::ConfigTree& config) { config.addConfigTreeItem(section, Conf::ConfigTreeItem(CONFIG_ACCOUNT_ENABLE,"1", type_int)); config.addConfigTreeItem(section, Conf::ConfigTreeItem(CONFIG_ACCOUNT_AUTO_REGISTER, "1", type_int)); config.addConfigTreeItem(section, Conf::ConfigTreeItem(CONFIG_ACCOUNT_ALIAS, _("My account"), type_str)); + */ } diff --git a/src/config/config.cpp b/src/config/config.cpp index 136e9043ebe01977fa916e6186b21ab68c9d8136..40658dde7c183aff40356ecddf374d5591648e59 100644 --- a/src/config/config.cpp +++ b/src/config/config.cpp @@ -19,6 +19,7 @@ */ #include "config.h" +#include "../global.h" #include <fstream> namespace Conf { @@ -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.. * If the section doesn't exists, create it @@ -78,10 +95,11 @@ std::string ConfigTree::getConfigTreeItemValue(const std::string& section, const std::string& itemName) { ConfigTreeItem* item = getConfigTreeItem(section, itemName); - if (item!=NULL) { + if (item != NULL) { return item->getValue(); } else { - throw ConfigTreeItemException(); + _debug("Option doesn't exist: [%s] %s\n", section.c_str(), itemName.c_str()); + //throw ConfigTreeItemException(); } return ""; } @@ -90,13 +108,7 @@ ConfigTree::getConfigTreeItemValue(const std::string& section, const std::string int ConfigTree::getConfigTreeItemIntValue(const std::string& section, const std::string& itemName) { - ConfigTreeItem* item = getConfigTreeItem(section, itemName); - if (item!=NULL && item->getType() == "int") { - return atoi(item->getValue().data()); - } else { - throw ConfigTreeItemException(); - } - return 0; + return atoi(getConfigTreeItemValue(section, itemName).data()); } bool @@ -133,17 +145,26 @@ 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 Élimier les 45,000 classes qui servent à rien pour Conf. */ 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); 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); 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); return true; @@ -230,7 +251,6 @@ ConfigTree::populateFromFile(const std::string& fileName) { // If the line is a section pos = line.find(']'); section = line.substr(1, pos - 1); - } else if (line[0] != '#') { // If the line is "key=value" and doesn't begin with '#'(comments) diff --git a/src/config/config.h b/src/config/config.h index b201ac27117182ec0c073d232013930a9cb7185d..9b28fd50dc70774518f4897e09b15889d3c372b0 100644 --- a/src/config/config.h +++ b/src/config/config.h @@ -78,20 +78,36 @@ public: void addConfigTreeItem(const std::string& section, const ConfigTreeItem item); /** - * Verify an item is there. If it's not, add it with the provided - * default value + * Set a configuration value. * - * @param section Section - * @param + * @param section Write to this [section] of the .ini file + * @param itemName The itemName= in the .ini file + * @param value The value to assign to that itemName */ - void verifyConfigTreeItem(const std::string& section, const std::string& itemName, - const std::string& defaultValue, const std::string& type); bool setConfigTreeItem(const std::string& section, const std::string& itemName, const std::string& value); - // throw a ConfigTreeItemException if not found + /** + * Get a value. + * + * This function does all the validity tests, so none are needed throughout + * the program. + * + * @param section The name of the [section] in the .ini file. + * @param itemName The name of the item= in the .ini file. + * @return The value of the corresponding item. The default value if the section exists + * but the item doesn't. + */ std::string getConfigTreeItemValue(const std::string& section, const std::string& itemName); int getConfigTreeItemIntValue(const std::string& section, const std::string& itemName); + + /** + * Flush data to .ini file + */ bool saveConfigTree(const std::string& fileName); + + /** + * Load data (and fill ConfigTree) from disk + */ int populateFromFile(const std::string& fileName); bool getConfigTreeItemToken(const std::string& section, const std::string& itemName, TokenList& arg); @@ -99,6 +115,9 @@ public: private: ConfigTreeItem* getConfigTreeItem(const std::string& section, const std::string& itemName); + /** + * List of sections. Each sections has an ItemList as child + */ SectionMap _sections; friend class ConfigTreeIterator; diff --git a/src/iaxaccount.cpp b/src/iaxaccount.cpp index 88c12f5b4ee78a7f50a0cf1514ef34c417f49b79..b9e19c339410caf9cf72667b0643b218eadb83f7 100644 --- a/src/iaxaccount.cpp +++ b/src/iaxaccount.cpp @@ -42,9 +42,9 @@ IAXAccount::registerAccount() IAXVoIPLink* tmplink = dynamic_cast<IAXVoIPLink*> (_link); if (tmplink) { // Stuff needed for IAX registration - tmplink->setHost(Manager::instance().getConfigString(_accountID,IAX_HOST)); - tmplink->setUser(Manager::instance().getConfigString(_accountID,IAX_USER)); - tmplink->setPass(Manager::instance().getConfigString(_accountID,IAX_PASS)); + tmplink->setHost(Manager::instance().getConfigString(_accountID, IAX_HOST)); + tmplink->setUser(Manager::instance().getConfigString(_accountID, IAX_USER)); + tmplink->setPass(Manager::instance().getConfigString(_accountID, IAX_PASS)); } _registered = _link->setRegister(); } @@ -85,6 +85,7 @@ IAXAccount::terminate() void IAXAccount::initConfig(Conf::ConfigTree& config) { + /* std::string section(_accountID); std::string type_str("string"); std::string type_int("int"); @@ -93,11 +94,12 @@ IAXAccount::initConfig(Conf::ConfigTree& config) Account::initConfig(config); // IAX specific - config.addConfigTreeItem(section, Conf::ConfigTreeItem(CONFIG_ACCOUNT_TYPE, "IAX", type_str)); - config.addConfigTreeItem(section, Conf::ConfigTreeItem(IAX_FULL_NAME, "", type_str)); - config.addConfigTreeItem(section, Conf::ConfigTreeItem(IAX_HOST, "", type_str)); - config.addConfigTreeItem(section, Conf::ConfigTreeItem(IAX_USER, "", type_str)); - config.addConfigTreeItem(section, Conf::ConfigTreeItem(IAX_PASS, "", type_str)); + config.verifyConfigTreeItem(section, CONFIG_ACCOUNT_TYPE, "IAX", type_str); + config.verifyConfigTreeItem(section, IAX_FULL_NAME, "", type_str); + config.verifyConfigTreeItem(section, IAX_HOST, "", type_str); + config.verifyConfigTreeItem(section, IAX_USER, "", type_str); + config.verifyConfigTreeItem(section, IAX_PASS, "", type_str); + */ } void diff --git a/src/managerimpl.cpp b/src/managerimpl.cpp index af34a57324eda0a6c456fad316ccda8fac71fc50..2aad56021cc6ac905429b1af51571180ab83fd78 100644 --- a/src/managerimpl.cpp +++ b/src/managerimpl.cpp @@ -91,7 +91,7 @@ ManagerImpl::ManagerImpl (void) #endif // should be call before initConfigFile - loadAccountMap(); + // loadAccountMap();, called in init() now. } // never call if we use only the singleton... @@ -109,6 +109,9 @@ ManagerImpl::~ManagerImpl (void) void ManagerImpl::init() { + // Load accounts, init map + loadAccountMap(); + initVolume(); if (_exist == 0) { @@ -147,10 +150,10 @@ void ManagerImpl::terminate() delete _dtmfKey; _debug("Unload Audio Driver\n"); - delete _audiodriver; _audiodriver = 0; + delete _audiodriver; _audiodriver = NULL; _debug("Unload Telephone Tone\n"); - delete _telephoneTone; _telephoneTone = 0; + delete _telephoneTone; _telephoneTone = NULL; } bool @@ -1004,8 +1007,6 @@ ManagerImpl::behindNat(const std::string& svr, int port) * Initialization: Main Thread * @return 1: ok -1: error directory - 0: unable to load the setting - 2: file doesn't exist yet */ int ManagerImpl::createSettingsPath (void) { @@ -1021,7 +1022,7 @@ ManagerImpl::createSettingsPath (void) { // Load user's configuration _path = _path + DIR_SEPARATOR_STR + PROGNAME + "rc"; - return _config.populateFromFile(_path); + return 1; } /** @@ -1063,7 +1064,10 @@ ManagerImpl::initConfigFile (void) initConfigAccount(); - _exist = createSettingsPath(); + if (createSettingsPath() == 1) { + _exist = _config.populateFromFile(_path); + } + _setupLoaded = (_exist == 2 ) ? false : true; } @@ -1308,6 +1312,7 @@ ManagerImpl::getCallStatus(const std::string& sequenceId) } //THREAD=Main +/* Unused, Deprecated */ bool ManagerImpl::getConfigAll(const std::string& sequenceId) { @@ -1379,7 +1384,7 @@ ManagerImpl::getConfigList(const std::string& sequenceId, const std::string& nam { bool returnValue = false; TokenList tk; - if (name=="codecdescriptor") { + if (name == "codecdescriptor") { CodecMap map = _codecDescriptorMap.getMap(); CodecMap::iterator iter = map.begin(); @@ -1397,7 +1402,7 @@ ManagerImpl::getConfigList(const std::string& sequenceId, const std::string& nam iter++; } returnValue = true; - } else if (name=="ringtones") { + } else if (name == "ringtones") { // add empty line std::ostringstream str; str << 1; @@ -1413,13 +1418,13 @@ ManagerImpl::getConfigList(const std::string& sequenceId, const std::string& nam // home directory path = std::string(HOMEDIR) + DIR_SEPARATOR_STR + "." + PROGDIR + DIR_SEPARATOR_STR + RINGDIR; getDirListing(sequenceId, path, &nbFile); - } else if (name=="audiodevice") { + } else if (name == "audiodevice") { returnValue = getAudioDeviceList(sequenceId, AudioLayer::InputDevice | AudioLayer::OutputDevice); - } else if (name=="audiodevicein") { + } else if (name == "audiodevicein") { returnValue = getAudioDeviceList(sequenceId, AudioLayer::InputDevice); - } else if (name=="audiodeviceout") { + } else if (name == "audiodeviceout") { returnValue = getAudioDeviceList(sequenceId, AudioLayer::OutputDevice); - } else if (name=="countrytones") { + } else if (name == "countrytones") { returnValue = getCountryTones(sequenceId); } return returnValue; @@ -1694,7 +1699,8 @@ ManagerImpl::setAccountDetails( const ::DBus::String& accountID, } saveConfig(); - + + /** @todo Then, reset the VoIP link with the new information */ } void @@ -1742,9 +1748,9 @@ ManagerImpl::setSwitch(const std::string& switchName, std::string& message) { _toneMutex.enterMutex(); _debug("Unload Telephone Tone\n"); - delete _telephoneTone; _telephoneTone = 0; + delete _telephoneTone; _telephoneTone = NULL; _debug("Unload DTMF Key\n"); - delete _dtmfKey; _dtmfKey = 0; + delete _dtmfKey; _dtmfKey = NULL; _debug("Load Telephone Tone\n"); std::string country = getConfigString(PREFERENCES, ZONE_TONE); @@ -1836,8 +1842,43 @@ ManagerImpl::loadAccountMap() { _debugStart("Load account:"); short nbAccount = 0; + TokenList sections = _config.getSections(); + std::string accountType; 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... short nbAccountSIP = ACCOUNT_SIP_COUNT_DEFAULT; for (short iAccountSIP = 0; iAccountSIP<nbAccountSIP; iAccountSIP++) { @@ -1865,6 +1906,7 @@ ManagerImpl::loadAccountMap() } } _debugEnd("\n"); + */ return nbAccount; } diff --git a/src/sipaccount.cpp b/src/sipaccount.cpp index b56d37546f71c93a41e8d81d41636f2c0f28ab09..c52aa49986fc00e96e276f0f4a974256dc7ac809 100644 --- a/src/sipaccount.cpp +++ b/src/sipaccount.cpp @@ -95,6 +95,7 @@ SIPAccount::terminate() void SIPAccount::initConfig(Conf::ConfigTree& config) { + /* std::string section(_accountID); std::string type_str("string"); std::string type_int("int"); @@ -103,15 +104,16 @@ SIPAccount::initConfig(Conf::ConfigTree& config) Account::initConfig(config); // SIP specific - config.addConfigTreeItem(section, Conf::ConfigTreeItem(CONFIG_ACCOUNT_TYPE, "SIP", type_str)); - config.addConfigTreeItem(section, Conf::ConfigTreeItem(SIP_FULL_NAME, "", type_str)); - config.addConfigTreeItem(section, Conf::ConfigTreeItem(SIP_USER_PART, "", type_str)); - 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)); - 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)); + config.verifyConfigTreeItem(section, CONFIG_ACCOUNT_TYPE, "SIP", type_str); + config.verifyConfigTreeItem(section, SIP_FULL_NAME, "", type_str); + config.verifyConfigTreeItem(section, SIP_USER_PART, "", type_str); + config.verifyConfigTreeItem(section, SIP_HOST_PART, "", type_str); + config.verifyConfigTreeItem(section, SIP_AUTH_NAME, "", type_str); + config.verifyConfigTreeItem(section, SIP_PASSWORD, "", type_str); + config.verifyConfigTreeItem(section, SIP_PROXY, "", type_str); + config.verifyConfigTreeItem(section, SIP_STUN_SERVER, "stun.fwdnet.net:3478", type_str); + config.verifyConfigTreeItem(section, SIP_USE_STUN, "0", type_int); + */ } void