From ddb17717eb034ce2e2bc1bf65a1b407693dc1da3 Mon Sep 17 00:00:00 2001 From: sbh Date: Thu, 15 Jan 2015 18:51:07 +0300 Subject: [PATCH] Issue #356: plugins dependency mechanism implemented --- CMakeLists.txt | 2 +- src/Config/Config_Keywords.h | 2 +- src/Config/Config_ModuleReader.cpp | 59 ++++++++++++------------------ src/Config/Config_ModuleReader.h | 20 +++++----- src/Config/plugins.xml | 2 +- src/NewGeom/NewGeom_Module.cpp | 11 ++++++ src/NewGeom/NewGeom_Module.h | 2 + 7 files changed, 48 insertions(+), 50 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 468e790f4..0ce69fcb8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -60,11 +60,11 @@ ADD_SUBDIRECTORY (src/XGUI) ADD_SUBDIRECTORY (src/GeomApp) ADD_SUBDIRECTORY (src/ExchangePlugin) ADD_SUBDIRECTORY (src/GeomValidators) -ADD_SUBDIRECTORY (src/ConnectorPlugin) IF(${HAVE_SALOME}) ADD_SUBDIRECTORY (src/NewGeom) + ADD_SUBDIRECTORY (src/ConnectorPlugin) ENDIF(${HAVE_SALOME}) ENABLE_TESTING() diff --git a/src/Config/Config_Keywords.h b/src/Config/Config_Keywords.h index 9fe94e769..a00d77080 100644 --- a/src/Config/Config_Keywords.h +++ b/src/Config/Config_Keywords.h @@ -84,7 +84,7 @@ const static char* PLUGINS_MODULE = "module"; const static char* PLUGIN_CONFIG = "configuration"; const static char* PLUGIN_LIBRARY = "library"; const static char* PLUGIN_SCRIPT = "script"; -const static char* PLUGIN_PLATFORM = "platform"; +const static char* PLUGIN_DEPENDENCY = "dependency"; const static char* PLUGIN_PLATFORM_SALOME = "salome"; const static char* PLUGIN_PLATFORM_NEWGEOM = "openparts"; diff --git a/src/Config/Config_ModuleReader.cpp b/src/Config/Config_ModuleReader.cpp index 526b9965f..0b0245c5a 100644 --- a/src/Config/Config_ModuleReader.cpp +++ b/src/Config/Config_ModuleReader.cpp @@ -31,18 +31,12 @@ #endif std::map Config_ModuleReader::myPluginTypes; +std::set Config_ModuleReader::myDependencyModules; Config_ModuleReader::Config_ModuleReader(const char* theEventGenerated) : Config_XMLReader(PLUGIN_FILE), myEventGenerated(theEventGenerated) { - myHaveSalome = false; - char* anEnv = getenv("SALOME_ROOT_DIR"); - std::string value = normalize(anEnv); - if (!value.empty()) { - myHaveSalome = true; - } - } Config_ModuleReader::~Config_ModuleReader() @@ -70,8 +64,7 @@ std::string Config_ModuleReader::getModuleName() void Config_ModuleReader::processNode(xmlNodePtr theNode) { if (isNode(theNode, NODE_PLUGIN, NULL)) { - bool isAvailable = isAvaliableOnThisPlatform(getProperty(theNode, PLUGIN_PLATFORM)); - if (!isAvailable) + if (!hasRequiredModules(theNode)) return; std::string aPluginConf = getProperty(theNode, PLUGIN_CONFIG); std::string aPluginLibrary = getProperty(theNode, PLUGIN_LIBRARY); @@ -91,6 +84,18 @@ bool Config_ModuleReader::processChildren(xmlNodePtr theNode) return isNode(theNode, NODE_PLUGINS, NULL); } +bool Config_ModuleReader::hasRequiredModules(xmlNodePtr theNode) const +{ + std::string aRequiredModule = normalize(getProperty(theNode, PLUGIN_DEPENDENCY)); + if(aRequiredModule.empty()) + return true; + std::set::iterator it = myDependencyModules.begin(); + for ( ; it != myDependencyModules.end(); it++ ) { + if (*it == aRequiredModule) return true; + } + return false; +} + std::list Config_ModuleReader::importPlugin(const std::string& thePluginLibrary, const std::string& thePluginXmlConf) { @@ -124,11 +129,11 @@ std::string Config_ModuleReader::addPlugin(const std::string& aPluginLibrary, if(!aPluginName.empty()) { myPluginTypes[aPluginName] = aType; } - + addDependencyModule(aPluginName); return aPluginName; } -void Config_ModuleReader::loadPlugin(const std::string thePluginName) +void Config_ModuleReader::loadPlugin(const std::string& thePluginName) { PluginType aType = Config_ModuleReader::Binary; if(myPluginTypes.find(thePluginName) != myPluginTypes.end()) { @@ -146,30 +151,7 @@ void Config_ModuleReader::loadPlugin(const std::string thePluginName) } } -bool Config_ModuleReader::isAvaliableOnThisPlatform(const std::string& thePluginPlatform) -{ - bool result = true; - PluginPlatform aPlatform = All; - std::string aPlatformName = normalize(thePluginPlatform) ; - if (aPlatformName == PLUGIN_PLATFORM_SALOME) { - aPlatform = Salome; - } else if (aPlatformName == PLUGIN_PLATFORM_NEWGEOM) { - aPlatform = OpenParts; - } else if (!thePluginPlatform.empty()) { - Events_Error::send("Unknown platform: " + thePluginPlatform); - } - if (aPlatform == All) { - result = true; - } else if (myHaveSalome) { - result = aPlatform == Salome; - } else { - result = aPlatform == OpenParts; - } - return result; - -} - -void Config_ModuleReader::loadScript(const std::string theFileName) +void Config_ModuleReader::loadScript(const std::string& theFileName) { /* aquire python thread */ PyGILState_STATE gstate = PyGILState_Ensure(); @@ -199,7 +181,7 @@ void Config_ModuleReader::loadScript(const std::string theFileName) PyGILState_Release(gstate); } -void Config_ModuleReader::loadLibrary(const std::string theLibName) +void Config_ModuleReader::loadLibrary(const std::string& theLibName) { std::string aFileName = library(theLibName); if (aFileName.empty()) @@ -219,3 +201,8 @@ void Config_ModuleReader::loadLibrary(const std::string theLibName) } } +void Config_ModuleReader::addDependencyModule(const std::string& theModuleName) +{ + myDependencyModules.insert(normalize(theModuleName)); +} + diff --git a/src/Config/Config_ModuleReader.h b/src/Config/Config_ModuleReader.h index c32eb37d9..cdfae0a43 100644 --- a/src/Config/Config_ModuleReader.h +++ b/src/Config/Config_ModuleReader.h @@ -15,6 +15,7 @@ #include #include +#include #include class Config_ModuleReader : public Config_XMLReader @@ -24,11 +25,6 @@ class Config_ModuleReader : public Config_XMLReader Intrenal = 1, Python = 2 }; - enum PluginPlatform { - All = 0, - OpenParts = 1, - Salome = 2 - }; public: CONFIG_EXPORT Config_ModuleReader(const char* theEventGenerated = 0); @@ -38,17 +34,20 @@ class Config_ModuleReader : public Config_XMLReader CONFIG_EXPORT std::string getModuleName(); - CONFIG_EXPORT static void loadPlugin(const std::string thePluginName); + CONFIG_EXPORT static void loadPlugin(const std::string& thePluginName); /// loads the library with specific name, appends "lib*.dll" or "*.so" depending on the platform - CONFIG_EXPORT static void loadLibrary(const std::string theLibName); + CONFIG_EXPORT static void loadLibrary(const std::string& theLibName); /// loads the python module with specified name - CONFIG_EXPORT static void loadScript(const std::string theFileName); + CONFIG_EXPORT static void loadScript(const std::string& theFileName); + // extends set of modules, which will be used for dependency + // checking (if there is no required module in the set, a plugin will not be loaded) + CONFIG_EXPORT static void addDependencyModule(const std::string& theModuleName); protected: void processNode(xmlNodePtr aNode); bool processChildren(xmlNodePtr aNode); - bool isAvaliableOnThisPlatform(const std::string& thePluginPlatform); + bool hasRequiredModules(xmlNodePtr aNode) const; std::list importPlugin(const std::string& thePluginLibrary, const std::string& thePluginFile); std::string addPlugin(const std::string& aPluginLibrary, @@ -58,9 +57,8 @@ class Config_ModuleReader : public Config_XMLReader private: std::map myFeaturesInFiles; static std::map myPluginTypes; + static std::set myDependencyModules; const char* myEventGenerated; - bool myHaveSalome; - }; #endif /* CONFIG_XMLMODULEREADER_H_ */ diff --git a/src/Config/plugins.xml b/src/Config/plugins.xml index 0f4a25586..e8d43ed02 100644 --- a/src/Config/plugins.xml +++ b/src/Config/plugins.xml @@ -6,7 +6,7 @@ - + diff --git a/src/NewGeom/NewGeom_Module.cpp b/src/NewGeom/NewGeom_Module.cpp index c9d875f13..969295555 100644 --- a/src/NewGeom/NewGeom_Module.cpp +++ b/src/NewGeom/NewGeom_Module.cpp @@ -30,6 +30,7 @@ #include #include +#include #include #include @@ -102,6 +103,7 @@ NewGeom_Module::~NewGeom_Module() void NewGeom_Module::initialize(CAM_Application* theApp) { LightApp_Module::initialize(theApp); + inspectSalomeModules(); myWorkshop->startApplication(); LightApp_Application* anApp = dynamic_cast(theApp); @@ -452,3 +454,12 @@ void NewGeom_Module::preferencesChanged(const QString& theSection, const QString aProp->setValue(aValue); } + +void NewGeom_Module::inspectSalomeModules() +{ + QStringList aModuleNames; + getApp()->modules(aModuleNames, false); + foreach(QString eachModule, aModuleNames) { + Config_ModuleReader::addDependencyModule(eachModule.toStdString()); + } +} diff --git a/src/NewGeom/NewGeom_Module.h b/src/NewGeom/NewGeom_Module.h index 800d6b168..83719845e 100644 --- a/src/NewGeom/NewGeom_Module.h +++ b/src/NewGeom/NewGeom_Module.h @@ -92,6 +92,8 @@ Q_OBJECT void setIsOpened(bool theOpened) { myIsOpened = theOpened; } + void inspectSalomeModules(); + public slots: virtual bool activateModule(SUIT_Study* theStudy); virtual bool deactivateModule(SUIT_Study* theStudy); -- 2.39.2