diff --git a/configure.ac b/configure.ac index 192effed779834eac790bfc3c2dc6d0db6cca80d..bb351e1c3f74390eec12d92dea189d5ad19f30bd 100644 --- a/configure.ac +++ b/configure.ac @@ -193,6 +193,7 @@ src/audio/pacpp/include/Makefile \ src/audio/pacpp/include/portaudiocpp/Makefile \ src/audio/pacpp/source/Makefile \ src/audio/pacpp/source/portaudiocpp/Makefile \ +src/config/Makefile \ src/gui/Makefile \ src/gui/qt/Makefile \ src/gui/official/Makefile \ diff --git a/src/Makefile.am b/src/Makefile.am index 1cae08ff2bb25cb7f409b7cb3b059b8e3a4b9dbc..7d1779b756b980bc58e3a0a50bf7c316e2c7b7a0 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -5,11 +5,11 @@ maintener_source = endif if USE_ZEROCONF -SUBDIRS = audio gui zeroconf +SUBDIRS = audio gui zeroconf config ZEROCONFLIB = zeroconf/libzeroconf.la ZEROCONFFLAGS = -DUSE_ZEROCONF else -SUBDIRS = audio gui +SUBDIRS = audio gui config ZEROCONFLIB = ZEROCONFFLAGS = endif diff --git a/src/config/Makefile.am b/src/config/Makefile.am new file mode 100644 index 0000000000000000000000000000000000000000..d72225850fc3415ab1c5818cace8bc740401fe29 --- /dev/null +++ b/src/config/Makefile.am @@ -0,0 +1,6 @@ +SUBDIRS = + +noinst_LTLIBRARIES = libconfig.la + +libconfig_la_SOURCES = \ + config.cpp config.h diff --git a/src/config/config.cpp b/src/config/config.cpp new file mode 100644 index 0000000000000000000000000000000000000000..b1984cb2b822924ab6a5a07bbdcd5f65ea467fa0 --- /dev/null +++ b/src/config/config.cpp @@ -0,0 +1,132 @@ +/** + * Copyright (C) 2005 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 "config.h" +namespace Conf { + +// ctor +Config::Config() +{ +} + +// dtor +Config::~Config() +{ + // erase every new ItemMap (by CreateSection) + SectionMap::iterator iter = _sections.begin(); + while(iter != _sections.end()) { + delete iter->second; + iter->second = NULL; + iter++; + } +} + +/** + * Create the section only if it doesn't exists + */ +void +Config::createSection(const std::string& section) { + // if we doesn't find the item, create it + if (_sections.find(section) == _sections.end()) { + _sections[section] = new ItemMap; + } +} + +/** + * Add the config item only if it exists.. + * If the section doesn't exists, create it + */ +void +Config::addConfigItem(const std::string& section, const ConfigItem& item) +{ + // if we doesn't find the item, create it + SectionMap::iterator iter = _sections.find(section); + if ( iter == _sections.end()) { + _sections[section] = new ItemMap; + iter = _sections.find(section); + } + // be prudent here + if (iter!=NULL && iter != _sections.end()) { + std::string name = item.getName(); + + if ( iter->second->find(name) == iter->second->end()) { + (*(iter->second))[name] = item; + } + } +} + +// throw a ConfigItemException if not found +std::string +Config::getConfigItemValue(const std::string& section, const std::string& itemName) +{ + ConfigItem* item = getConfigItem(section, itemName); + if (item!=NULL) { + return item->getValue(); + } else { + throw new ConfigItemException(); + } + return ""; +} + +// throw a ConfigItemException if not found +int +Config::getConfigItemIntValue(const std::string& section, const std::string& itemName) +{ + ConfigItem* item = getConfigItem(section, itemName); + if (item!=NULL && item->getType() == "int") { + return atoi(item->getValue().data()); + } else { + throw new ConfigItemException(); + } + return 0; +} + +/** + * Return a ConfigItem or NULL if not found + */ +ConfigItem* +Config::getConfigItem(const std::string& section, const std::string& itemName) { + SectionMap::iterator iter = _sections.find(section); + if ( iter == _sections.end()) { + return NULL; + } + ItemMap::iterator iterItem = iter->second->find(itemName); + if ( iterItem == iter->second->end()) { + return NULL; + } + return &(iterItem->second); +} + +/** + * Set the configItem if found, else do nothing + */ +void +Config::setConfigItem(const std::string& section, const std::string& itemName, const std::string& value) { + SectionMap::iterator iter = _sections.find(section); + if ( iter == _sections.end()) { + return; + } + ItemMap::iterator iterItem = iter->second->find(itemName); + if ( iterItem == iter->second->end()) { + return; + } + iterItem->second.setValue(value); +} + +} // end namespace Config diff --git a/src/config/config.h b/src/config/config.h new file mode 100644 index 0000000000000000000000000000000000000000..7dfad3613cd1e6ef768b0b305051e42af071bfda --- /dev/null +++ b/src/config/config.h @@ -0,0 +1,81 @@ +/** + * Copyright (C) 2005 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. + */ + +#ifndef __CONFIG_CONFIG_H_ +#define __CONFIG_CONFIG_H_ + +#include <map> +#include <string> + +namespace Conf { + +class ConfigItem; +typedef std::map<std::string, ConfigItem> ItemMap; +typedef std::map<std::string, ItemMap*> SectionMap; + +class ConfigItemException { +public: + ConfigItemException() {} + ~ConfigItemException() {} +}; + +class Config { +public: + Config(); + ~Config(); + + void createSection(const std::string& section); + void addConfigItem(const std::string& section, const ConfigItem &item); + void setConfigItem(const std::string& section, const std::string& itemName, const std::string& value); + + // throw a ConfigItemException if not found + std::string getConfigItemValue(const std::string& section, const std::string& itemName); + int getConfigItemIntValue(const std::string& section, const std::string& itemName); + +private: + ConfigItem* getConfigItem(const std::string& section, const std::string& itemName); + + SectionMap _sections; +}; + +class ConfigItem { +public: + ConfigItem() : _defaultValue(""), _type("string") {} + ConfigItem(const std::string& name, const std::string& value, const std::string& defaultValue, const std::string& type) : + _name(name), _value(value), + _defaultValue(defaultValue), _type(type) {} + ~ConfigItem(); + + void setValue(const std::string& value) { _value = value; } + const std::string getName() const { return _name; } + const std::string getValue() const { return _value; } + const std::string getDefaultValue() const { return _defaultValue; } + const std::string getType() const { return _type; } + +private: + std::string _name; + std::string _value; + std::string _defaultValue; + std::string _type; +}; + + +} // end namespace Config + +#endif diff --git a/src/configitem.h b/src/configitem.h index 4de9e2cbe9869188b5771e776a41f86b7ab77dd8..a04d6e9e5773a32c15d0346ed15462e6ee14689d 100644 --- a/src/configitem.h +++ b/src/configitem.h @@ -23,25 +23,27 @@ #include <string> #include <stdio.h> +using namespace std; + class ConfigItem { public: ConfigItem (void); - ConfigItem (const std::string& ); - ConfigItem (const std::string& , const std::string& ); + ConfigItem (const string& ); + ConfigItem (const string& , const string& ); ~ConfigItem (void); - std::string* key (void) { return _key; } - std::string* value (void) { return _value; } + string* key (void) { return _key; } + string* value (void) { return _value; } ConfigItem* head (void) { return _head; } void setHead (ConfigItem *h) { _head = h; } - std::string* getValueByKey (const std::string& ); - ConfigItem* getItemByKey (const std::string& ); - void setValue (const std::string& ); - void setValueByKey (const std::string& , const std::string& ); - void saveToFile (std::fstream*); + string* getValueByKey (const string& ); + ConfigItem* getItemByKey (const string& ); + void setValue (const string& ); + void setValueByKey (const string& , const string& ); + void saveToFile (fstream*); private: - std::string* _key; - std::string* _value; + string* _key; + string* _value; ConfigItem* _next; ConfigItem* _head; void init (void); diff --git a/src/configurationtree.h b/src/configurationtree.h index 701f6c1f8549baf0084a806f719811df0bf139ed..81d91fdff8c9aac40ad2354d948e1cc10527744e 100644 --- a/src/configurationtree.h +++ b/src/configurationtree.h @@ -41,14 +41,14 @@ class ConfigurationTree { public: ConfigurationTree (void); - ConfigurationTree (const std::string&); + ConfigurationTree (const string&); ~ConfigurationTree (void); ConfigSection* head (void) { return this->_head; } - int populateFromFile(const std::string& ); - int saveToFile (const std::string& ); - int setValue (const std::string& , const std::string& , int); - int setValue(const std::string& , const std::string& , const std::string& ); - std::string getValue (const std::string& , const std::string& ); + int populateFromFile(const string& ); + int saveToFile (const string& ); + int setValue (const string& , const string& , int); + int setValue(const string& , const string& , const string& ); + string getValue (const string& , const string& ); private: ConfigSection *_head; diff --git a/src/gui/guiframework.cpp b/src/gui/guiframework.cpp index 045e3fe201c4557004927cd605c76ff418d20024..f6c6653f9b89ab7159d87811ac4c7b54c0d80e6f 100644 --- a/src/gui/guiframework.cpp +++ b/src/gui/guiframework.cpp @@ -234,15 +234,15 @@ GuiFramework::getConfigAll(const std::string& sequenceId) } bool -GuiFramework::getConfig(const std::string& sequenceId, const std::string& name) +GuiFramework::getConfig(const std::string& section, const std::string& name, TokenList& arg) { - return Manager::instance().getConfig(sequenceId, name); + return Manager::instance().getConfig(section, name, arg); } bool -GuiFramework::setConfig(const std::string& name, const std::string& value) +GuiFramework::setConfig(const std::string& section, const std::string& name, const std::string& value) { - return Manager::instance().setConfig(name, value); + return Manager::instance().setConfig(section, name, value); } bool diff --git a/src/gui/guiframework.h b/src/gui/guiframework.h index 15a6b8f497abd0a988411bd8919aec0b053988b2..459fe35f96f1adc4640812106cce16994fe03334 100644 --- a/src/gui/guiframework.h +++ b/src/gui/guiframework.h @@ -85,8 +85,8 @@ public: bool attachZeroconfEvents(const std::string& sequenceId); bool getCallStatus(const std::string& sequenceId); bool getConfigAll(const std::string& sequenceId); - bool getConfig(const std::string& sequenceId, const std::string& name); - bool setConfig(const std::string& name, const std::string& value); + bool getConfig(const std::string& section, const std::string& name, TokenList& arg); + bool setConfig(const std::string& section, const std::string& name, const std::string& value); bool getConfigList(const std::string& sequenceId, const std::string& name); // Observer methods diff --git a/src/gui/server/request.h b/src/gui/server/request.h index 8b72e7d4c0d5e201f864d8c8732ec666d95bbf6d..d8f131764f4053f4ebbbbb0630eefebd9d91e12a 100644 --- a/src/gui/server/request.h +++ b/src/gui/server/request.h @@ -45,6 +45,10 @@ public: ResponseMessage response(code, _sequenceId, message); return response; } + ResponseMessage message(const std::string &code, TokenList& arg) { + ResponseMessage response(code, _sequenceId, arg); + return response; + } std::string sequenceId () const { return _sequenceId; } protected: diff --git a/src/gui/server/requestconfig.cpp b/src/gui/server/requestconfig.cpp index 2de5ff377ac7c0a4997de5f96bd2c650c2bffc73..d43dd42edc579b5fc029aab6fbf5b0614d1d1ad3 100644 --- a/src/gui/server/requestconfig.cpp +++ b/src/gui/server/requestconfig.cpp @@ -64,10 +64,18 @@ RequestConfigGetAll::execute() RequestConfigGet::RequestConfigGet(const std::string &sequenceId, const TokenList& argList) : RequestGlobal(sequenceId,argList) { TokenList::iterator iter = _argList.begin(); + bool argsAreValid = false; if (iter != _argList.end()) { - _name = *iter; + _section = *iter; _argList.pop_front(); - } else { + iter++; + if (iter != _argList.end()) { + _name = *iter; + _argList.pop_front(); + argsAreValid = true; + } + } + if (!argsAreValid) { throw RequestConstructorException(); } } @@ -75,8 +83,9 @@ RequestConfigGet::RequestConfigGet(const std::string &sequenceId, const TokenLis ResponseMessage RequestConfigGet::execute() { - if (GUIServer::instance().getConfig(_sequenceId, _name)) { - return message("200", "OK"); + TokenList arg; + if (GUIServer::instance().getConfig(_section, _name, arg)) { + return message("200", arg); } else { return message("500","Server Error"); } @@ -86,16 +95,21 @@ RequestConfigSet::RequestConfigSet(const std::string &sequenceId, const TokenLis { TokenList::iterator iter = _argList.begin(); - // get two strings arguments + // get three strings arguments bool argsAreValid = false; if (iter != _argList.end()) { - _name = *iter; + _section = *iter; _argList.pop_front(); iter++; if (iter != _argList.end()) { - _value = *iter; + _name = *iter; _argList.pop_front(); - argsAreValid = true; + iter++; + if (iter != _argList.end()) { + _value = *iter; + _argList.pop_front(); + argsAreValid = true; + } } } if (!argsAreValid) { @@ -106,7 +120,7 @@ RequestConfigSet::RequestConfigSet(const std::string &sequenceId, const TokenLis ResponseMessage RequestConfigSet::execute() { - if (GUIServer::instance().setConfig(_name, _value)) { + if (GUIServer::instance().setConfig(_section, _name, _value)) { return message("200", "OK"); } else { return message("500","Server Error"); diff --git a/src/gui/server/requestconfig.h b/src/gui/server/requestconfig.h index 69b0495802a0d91434fcb6623b35f47a50cddb03..62dc65aacd133fcea814ff6c4c9fbb7d5940112f 100644 --- a/src/gui/server/requestconfig.h +++ b/src/gui/server/requestconfig.h @@ -56,6 +56,7 @@ public: RequestConfigGet(const std::string &sequenceId, const TokenList& argList); ResponseMessage execute(); private: + std::string _section; std::string _name; }; @@ -65,6 +66,7 @@ public: RequestConfigSet(const std::string &sequenceId, const TokenList& argList); ResponseMessage execute(); private: + std::string _section; std::string _name; std::string _value; }; diff --git a/src/managerimpl.cpp b/src/managerimpl.cpp index 0e9ba08342d053d7668738cc739c74cb32b9cce1..afa0f2bf4ff95278aa93b6f48446095423fdc28d 100644 --- a/src/managerimpl.cpp +++ b/src/managerimpl.cpp @@ -1139,14 +1139,14 @@ ManagerImpl::getConfigAll(const std::string& sequenceId) } bool -ManagerImpl::getConfig(const std::string& sequenceId, const std::string& name) +ManagerImpl::getConfig(const std::string& section, const std::string& name, TokenList& arg) { bool returnValue = false; return returnValue; } bool -ManagerImpl::setConfig(const std::string& name, const std::string& value) +ManagerImpl::setConfig(const std::string& section, const std::string& name, const std::string& value) { bool returnValue = false; return returnValue; @@ -1156,6 +1156,18 @@ bool ManagerImpl::getConfigList(const std::string& sequenceId, const std::string& name) { bool returnValue = false; + if (name=="") { + + returnValue = true; + } else if (name=="") { + returnValue = true; + } else if (name=="") { + returnValue = true; + } else if (name=="") { + returnValue = true; + } else if (name=="") { + returnValue = true; + } return returnValue; } diff --git a/src/managerimpl.h b/src/managerimpl.h index 07b7b7d1c002fdc5457acd417b550effc593f2fd..f89de1c77def53f7e0289bd6c4feaba96f2313ff 100644 --- a/src/managerimpl.h +++ b/src/managerimpl.h @@ -197,8 +197,8 @@ public: bool attachZeroconfEvents(const std::string& sequenceId, const Pattern::Observer &observer); bool getCallStatus(const std::string& sequenceId); bool getConfigAll(const std::string& sequenceId); - bool getConfig(const std::string& sequenceId, const std::string& name); - bool setConfig(const std::string& name, const std::string& value); + bool getConfig(const std::string& section, const std::string& name, TokenList& arg); + bool setConfig(const std::string& section, const std::string& name, const std::string& value); bool getConfigList(const std::string& sequenceId, const std::string& name); diff --git a/utilspp/singleton/Makefile.am b/utilspp/singleton/Makefile.am index 70e9772eb23fb0ac5e4ef9fa999b138eb40243a7..7f8091d27318796938506c9e3ba1778bd6d51640 100644 --- a/utilspp/singleton/Makefile.am +++ b/utilspp/singleton/Makefile.am @@ -21,5 +21,3 @@ pkginclude_HEADERS = \ pkgincludedir=$(includedir)/utilspp/singleton - -noinst_HEADERS = guiserversingleton.h