From 9a5618a1123e46698176802cbe29758a3f0bd165 Mon Sep 17 00:00:00 2001 From: yanmorin <yanmorin> Date: Wed, 28 Sep 2005 19:55:19 +0000 Subject: [PATCH] Try to reuse the config code, but I was not able to I create a Conf::Config to replace it... --- configure.ac | 1 + src/Makefile.am | 4 +- src/config/Makefile.am | 6 ++ src/config/config.cpp | 132 +++++++++++++++++++++++++++++++ src/config/config.h | 81 +++++++++++++++++++ src/configitem.h | 24 +++--- src/configurationtree.h | 12 +-- src/gui/guiframework.cpp | 8 +- src/gui/guiframework.h | 4 +- src/gui/server/request.h | 4 + src/gui/server/requestconfig.cpp | 32 +++++--- src/gui/server/requestconfig.h | 2 + src/managerimpl.cpp | 16 +++- src/managerimpl.h | 4 +- utilspp/singleton/Makefile.am | 2 - 15 files changed, 292 insertions(+), 40 deletions(-) create mode 100644 src/config/Makefile.am create mode 100644 src/config/config.cpp create mode 100644 src/config/config.h diff --git a/configure.ac b/configure.ac index 192effed77..bb351e1c3f 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 1cae08ff2b..7d1779b756 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 0000000000..d72225850f --- /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 0000000000..b1984cb2b8 --- /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 0000000000..7dfad3613c --- /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 4de9e2cbe9..a04d6e9e57 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 701f6c1f85..81d91fdff8 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 045e3fe201..f6c6653f9b 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 15a6b8f497..459fe35f96 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 8b72e7d4c0..d8f131764f 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 2de5ff377a..d43dd42edc 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 69b0495802..62dc65aacd 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 0e9ba08342..afa0f2bf4f 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 07b7b7d1c0..f89de1c77d 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 70e9772eb2..7f8091d273 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 -- GitLab