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