Salome HOME
cbb917a76fa9a7238bbd401865464e239cc4fba0
[modules/shaper.git] / src / Model / Model_PluginManager.cpp
1 // File:        Model_PluginManager.cxx
2 // Created:     20 Mar 2014
3 // Author:      Mikhail PONIKAROV
4
5 #include <Model_PluginManager.h>
6 #include <ModelAPI_Feature.h>
7 #include <ModelAPI_Plugin.h>
8 #include <Model_Data.h>
9 #include <Model_Document.h>
10 #include <Model_Application.h>
11 #include <Events_Loop.h>
12 #include <Config_FeatureMessage.h>
13 #include <Config_ModuleReader.h>
14
15 using namespace std;
16
17 static Model_PluginManager* myImpl = new Model_PluginManager();
18
19 boost::shared_ptr<ModelAPI_Feature> Model_PluginManager::createFeature(string theFeatureID)
20 {
21   if (this != myImpl) return myImpl->createFeature(theFeatureID);
22
23   LoadPluginsInfo();
24   if (myPlugins.find(theFeatureID) != myPlugins.end()) {
25     myCurrentPluginName = myPlugins[theFeatureID];
26     if (myPluginObjs.find(myCurrentPluginName) == myPluginObjs.end()) {
27       // load plugin library if not yet done
28       Config_ModuleReader::loadLibrary(myCurrentPluginName);
29     }
30     if (myPluginObjs.find(myCurrentPluginName) != myPluginObjs.end()) {
31       boost::shared_ptr<ModelAPI_Feature> aCreated = 
32         myPluginObjs[myCurrentPluginName]->createFeature(theFeatureID);
33       return aCreated;
34     }
35   }
36
37   return boost::shared_ptr<ModelAPI_Feature>(); // return nothing
38 }
39
40 boost::shared_ptr<ModelAPI_Document> Model_PluginManager::rootDocument()
41 {
42   return boost::shared_ptr<ModelAPI_Document>(
43     Model_Application::getApplication()->getDocument("root"));
44 }
45
46 bool Model_PluginManager::hasRootDocument()
47 {
48   return Model_Application::getApplication()->hasDocument("root");
49 }
50
51 boost::shared_ptr<ModelAPI_Document> Model_PluginManager::currentDocument()
52 {
53   if (!myCurrentDoc)
54     myCurrentDoc = rootDocument();
55   return myCurrentDoc;
56 }
57
58 void Model_PluginManager::setCurrentDocument(boost::shared_ptr<ModelAPI_Document> theDoc)
59 {
60   myCurrentDoc = theDoc;
61 }
62
63 Model_PluginManager::Model_PluginManager()
64 {
65   myPluginsInfoLoaded = false;
66   //TODO(sbh): Implement static method to extract event id [SEID]
67   static Events_ID aFeatureEvent = Events_Loop::eventByName("FeatureRegisterEvent");
68
69   ModelAPI_PluginManager::SetPluginManager(boost::shared_ptr<ModelAPI_PluginManager>(this));
70   // register the configuration reading listener
71   Events_Loop* aLoop = Events_Loop::loop();
72   aLoop->registerListener(this, aFeatureEvent);
73 }
74
75 void Model_PluginManager::processEvent(const Events_Message* theMessage)
76 {
77   const Config_FeatureMessage* aMsg =
78     dynamic_cast<const Config_FeatureMessage*>(theMessage);
79   if (aMsg) {
80     // proccess the plugin info, load plugin
81     if (myPlugins.find(aMsg->id()) == myPlugins.end()) {
82       myPlugins[aMsg->id()] = aMsg->pluginLibrary();
83     }
84   }
85   // plugins information was started to load, so, it will be loaded
86   myPluginsInfoLoaded = true;
87 }
88
89 void Model_PluginManager::LoadPluginsInfo()
90 {
91   if (myPluginsInfoLoaded) // nothing to do
92     return;
93
94   // Read plugins information from XML files
95   Config_ModuleReader aXMLReader("FeatureRegisterEvent");
96   aXMLReader.readAll();
97 }
98
99 void Model_PluginManager::registerPlugin(ModelAPI_Plugin* thePlugin)
100 {
101   myPluginObjs[myCurrentPluginName] = thePlugin;
102 }