Salome HOME
Replace boost::shared_ptr<ModelAPI_Feature> on FeaturePtr
[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 <Model_Events.h>
12 #include <Events_Loop.h>
13 #include <Events_Error.h>
14 #include <Config_FeatureMessage.h>
15 #include <Config_ModuleReader.h>
16
17 #include <TDF_CopyTool.hxx>
18 #include <TDF_DataSet.hxx>
19 #include <TDF_RelocationTable.hxx>
20 #include <TDF_ClosureTool.hxx>
21
22
23 using namespace std;
24
25 static Model_PluginManager* myImpl = new Model_PluginManager();
26
27 FeaturePtr Model_PluginManager::createFeature(string theFeatureID)
28 {
29   if (this != myImpl) return myImpl->createFeature(theFeatureID);
30
31   LoadPluginsInfo();
32   if (myPlugins.find(theFeatureID) != myPlugins.end()) {
33     myCurrentPluginName = myPlugins[theFeatureID];
34     if (myPluginObjs.find(myCurrentPluginName) == myPluginObjs.end()) {
35       // load plugin library if not yet done
36       Config_ModuleReader::loadLibrary(myCurrentPluginName);
37     }
38     if (myPluginObjs.find(myCurrentPluginName) != myPluginObjs.end()) {
39       FeaturePtr aCreated = 
40         myPluginObjs[myCurrentPluginName]->createFeature(theFeatureID);
41       if (!aCreated) {
42         Events_Error::send(string("Can not initialize feature '") + theFeatureID +
43           "' in plugin '" + myCurrentPluginName + "'");
44       }
45       return aCreated;
46     } else {
47       Events_Error::send(string("Can not load plugin '") + myCurrentPluginName + "'");
48     }
49   }
50
51   return FeaturePtr(); // return nothing
52 }
53
54 boost::shared_ptr<ModelAPI_Document> Model_PluginManager::rootDocument()
55 {
56   return boost::shared_ptr<ModelAPI_Document>(
57     Model_Application::getApplication()->getDocument("root"));
58 }
59
60 bool Model_PluginManager::hasRootDocument()
61 {
62   return Model_Application::getApplication()->hasDocument("root");
63 }
64
65 boost::shared_ptr<ModelAPI_Document> Model_PluginManager::currentDocument()
66 {
67   if (!myCurrentDoc || !Model_Application::getApplication()->hasDocument(myCurrentDoc->id()))
68     myCurrentDoc = rootDocument();
69   return myCurrentDoc;
70 }
71
72 void Model_PluginManager::setCurrentDocument(boost::shared_ptr<ModelAPI_Document> theDoc)
73 {
74   myCurrentDoc = theDoc;
75 }
76
77 boost::shared_ptr<ModelAPI_Document> Model_PluginManager::copy(
78   boost::shared_ptr<ModelAPI_Document> theSource, std::string theID) 
79 {
80   // create a new document
81   boost::shared_ptr<Model_Document> aNew = boost::dynamic_pointer_cast<Model_Document>(
82     Model_Application::getApplication()->getDocument(theID));
83   // make a copy of all labels
84   TDF_Label aSourceRoot = 
85     boost::dynamic_pointer_cast<Model_Document>(theSource)->document()->Main().Father();
86   TDF_Label aTargetRoot = aNew->document()->Main().Father();
87   Handle(TDF_DataSet) aDS = new TDF_DataSet;
88   aDS->AddLabel(aSourceRoot);
89   TDF_ClosureTool::Closure(aDS);
90   Handle(TDF_RelocationTable) aRT = new TDF_RelocationTable;
91   aRT->SetRelocation(aSourceRoot, aTargetRoot);
92   TDF_CopyTool::Copy(aDS, aRT);
93
94   aNew->synchronizeFeatures();
95   return aNew;
96 }
97
98 Model_PluginManager::Model_PluginManager()
99 {
100   myPluginsInfoLoaded = false;
101   myCheckTransactions = true;
102   ModelAPI_PluginManager::SetPluginManager(boost::shared_ptr<ModelAPI_PluginManager>(this));
103   // register the configuration reading listener
104   Events_Loop* aLoop = Events_Loop::loop();
105   static Events_ID FeatureEvent = Events_Loop::eventByName("FeatureRegisterEvent");
106   aLoop->registerListener(this, FeatureEvent);
107   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_FEATURE_CREATED));
108   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_FEATURE_UPDATED));
109   aLoop->registerListener(this, Events_Loop::eventByName(EVENT_FEATURE_DELETED));
110 }
111
112 void Model_PluginManager::processEvent(const Events_Message* theMessage)
113 {
114   static Events_ID FeatureEvent = Events_Loop::eventByName("FeatureRegisterEvent");
115   if (theMessage->eventID() == FeatureEvent) {
116     const Config_FeatureMessage* aMsg =
117       dynamic_cast<const Config_FeatureMessage*>(theMessage);
118     if (aMsg) {
119       // proccess the plugin info, load plugin
120       if (myPlugins.find(aMsg->id()) == myPlugins.end()) {
121         myPlugins[aMsg->id()] = aMsg->pluginLibrary();
122       }
123     }
124     // plugins information was started to load, so, it will be loaded
125     myPluginsInfoLoaded = true;
126   } else { // create/update/delete
127     if (myCheckTransactions && !rootDocument()->isOperation())
128       Events_Error::send("Modification of data structure outside of the transaction");
129   }
130 }
131
132 void Model_PluginManager::LoadPluginsInfo()
133 {
134   if (myPluginsInfoLoaded) // nothing to do
135     return;
136
137   // Read plugins information from XML files
138   Config_ModuleReader aXMLReader("FeatureRegisterEvent");
139   aXMLReader.readAll();
140 }
141
142 void Model_PluginManager::registerPlugin(ModelAPI_Plugin* thePlugin)
143 {
144   myPluginObjs[myCurrentPluginName] = thePlugin;
145 }