1 // Ryzom - MMORPG Framework <http://dev.ryzom.com/projects/ryzom/>
2 // Copyright (C) 2010 Winch Gate Property Limited
4 // This source file has been modified by the following contributors:
5 // Copyright (C) 2013 Laszlo KIS-ADAM (dfighter) <dfighter1985@gmail.com>
7 // This program is free software: you can redistribute it and/or modify
8 // it under the terms of the GNU Affero General Public License as
9 // published by the Free Software Foundation, either version 3 of the
10 // License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU Affero General Public License for more details.
17 // You should have received a copy of the GNU Affero General Public License
18 // along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #ifndef NL_INTERFACE_OBSERVER_H
23 #define NL_INTERFACE_OBSERVER_H
25 #include "nel/misc/types_nl.h"
26 #include "interface_manager.h"
27 #include "nel/misc/xml_auto_ptr.h"
31 * interface to define a simple observer
32 * \author Nicolas Brigand
33 * \author Nevrax France
36 class IInterfaceObserver
: public NLMISC::ICDBNode::IPropertyObserver
41 * parse the element and initalize it
42 * \param cur : pointer to the node describing this element
43 * \param ctrl : pointer to the parent group
44 * \return true if success
46 virtual bool parse (xmlNodePtr cur
, CInterfaceGroup
*parentGroup
)=0;
51 virtual void update (NLMISC::ICDBNode
* leaf
)=0;
57 class IInterfaceObserverFactory
;
60 * manager for observer factory
61 * \author Nicolas Brigand
62 * \author Nevrax France
65 class CInterfaceObserverFactoryManager
68 typedef std::map
< std::string
, IInterfaceObserverFactory
* > TFactoryMap
;
69 static CInterfaceObserverFactoryManager
* getInstance()
71 if (_GlobalInstance
== NULL
)
72 _GlobalInstance
= new CInterfaceObserverFactoryManager
;
73 return _GlobalInstance
;
76 /// map of action handler factories
77 TFactoryMap FactoryMap
;
80 CInterfaceObserverFactoryManager(){}
81 static CInterfaceObserverFactoryManager
*_GlobalInstance
;
87 * interface for observer factory
88 * \author Nicolas Brigand
89 * \author Nevrax France
92 class IInterfaceObserverFactory
98 * build the desired observer from script
99 *\param idHandler : the string identifying the handler
100 *\return a pointer to the factory instance to be used
102 static IInterfaceObserver
*create(xmlNodePtr node
,CInterfaceGroup
* parentGroup
)
104 CInterfaceManager
* iMngr
= CInterfaceManager::getInstance();
105 CXMLAutoPtr
idObserver((const char*)xmlGetProp(node
,(char unsigned*)"action"));
108 nlinfo("action not found in a observer node");
111 CXMLAutoPtr
ptr((const char*) xmlGetProp (node
, (xmlChar
*)"data"));
114 nlinfo ("no data in a observer tag");
120 IInterfaceObserver
* obs
=NULL
;
122 char * end
= ptr
.getDatas() + strlen( ptr
.getDatas() );
123 char * dataTok
= strtok( ptr
.getDatas()," ,");
124 NLMISC::ICDBNode::CTextId textId
;
128 std::string
data (dataTok
);
129 if (NLGUI::CDBManager::getInstance()->getDbProp(data
,false) == NULL
) // Full path provided ? No.
131 CInterfaceGroup
*parent
= parentGroup
;
132 while (parent
!= NULL
)
136 sTmp
= parent
->getId() + data
;
138 sTmp
= parent
->getId() + ":" + data
;
139 if (NLGUI::CDBManager::getInstance()->getDbProp(sTmp
,false) != NULL
)
144 parent
= parent
->getParent();
146 if (NLGUI::CDBManager::getInstance()->getDbProp(data
,false) == NULL
)
148 std::string sTmp
= std::string("data (")+std::string(dataTok
)+std::string(") in a observer tag do not exist, CREATING!");
149 nlinfo (sTmp
.c_str());
150 data
= (const char*)dataTok
;
151 NLGUI::CDBManager::getInstance()->getDbProp (data
, true);
156 CInterfaceObserverFactoryManager::TFactoryMap::const_iterator it
= CInterfaceObserverFactoryManager::getInstance()->FactoryMap
.find((const char *) idObserver
);
157 if (it
== CInterfaceObserverFactoryManager::getInstance()->FactoryMap
.end())
159 nlinfo("undefined observer : %s", (const char*)idObserver
);
162 obs
= it
->second
->createObserver(node
,parentGroup
);
168 textId
= NLMISC::ICDBNode::CTextId( data
);
169 if ( ! NLGUI::CDBManager::getInstance()->getDB()->addObserver(obs
,textId
) )
173 dataTok
+= strlen(dataTok
) +1;
176 dataTok
= strtok( dataTok
," ,");
184 * Create an action handler
185 *\param node : a pointer to the XML node containing the handlers parameters
186 *\return a pointer to the action handler created
188 virtual IInterfaceObserver
*createObserver(xmlNodePtr node
,CInterfaceGroup
* parent
)=0;
193 * register a new observer by defining the associated factory and insert the handler name in the global action handler map
194 * \param handler : name of the action handler class
195 * \param name : name of the action handler in the map (same as the "onaction" value in the XML script)
198 #define REGISTER_OBSERVER(observer ,name) \
199 class observer##Factory : public IInterfaceObserverFactory\
202 observer##Factory ()\
204 CInterfaceObserverFactoryManager::getInstance()->FactoryMap.insert(make_pair(std::string(name),this));\
207 IInterfaceObserver *createObserver(xmlNodePtr node,CInterfaceGroup* parent)\
209 observer * buf = new observer; \
210 buf->parse(node,parent);\
214 observer##Factory observer##FactoryInstance ;\
218 #endif // NL_INTERFACE_OBSERVER_H
220 /* End of interface_observer.h */