LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
AmosePluginsManager.cpp
Go to the documentation of this file.
1// Copyright 2002-2020 CEA LIST
2// SPDX-FileCopyrightText: 2022 CEA LIST <gael.de-chalendar@cea.fr>
3//
4// SPDX-License-Identifier: MIT
5
7#include "common/LimaCommon.h"
10
11#include <iostream>
12#include <QFile>
13#include <QDir>
14
15using namespace Lima;
16using namespace Lima::Common;
17using namespace Lima::Common::Misc;
18
19AmosePluginsManager::AmosePluginsManager()
20{
21// loadPlugins();
22}
23
24bool AmosePluginsManager::loadPlugins(const QString& configDirs)
25{
27 LINFO << "AmosePluginsManager::loadPlugins" << configDirs;
28
30
31 QStringList forbiddenPlugins;
32 QStringList alreadyLoaded;
33 // DynamicLibrariesManager::changeable().addSearchPath("c:\amose\lib");;
34 // open LIMA_CONF/plugins file
35
36 QStringList configDirsList = configDirs.split(LIMA_PATH_SEPARATOR);
37 if (configDirsList.isEmpty())
38 {
39 // Look for LIMA_CONF directory.
40 configDirsList = buildConfigurationDirectoriesList(QStringList({"lima"}),
41 QStringList());
42 }
43 for(auto it = configDirsList.begin(); it != configDirsList.end(); ++it)
44 {
45 // Deduce plugins directory.
46 QString stdPluginsDir(*it);
47 stdPluginsDir.append("/plugins");
48 QDir pluginsDir(stdPluginsDir);
49 LINFO << "AmosePluginsManager::loadPlugins in folder" << stdPluginsDir;
50
51 // For each file under plugins directory, read plugins names and deduce shared libraries to load.
52 QStringList pluginsFiles = pluginsDir.entryList(QDir::Files);
53 Q_FOREACH(QString pluginsFile, pluginsFiles)
54 {
55 LINFO << "AmosePluginsManager::loadPlugins loading plugins file "
56 << pluginsDir.path()+"/"+pluginsFile.toUtf8().data();
57 // Open plugin file.
58 QFile file(pluginsDir.path() + "/" + pluginsFile);
59 if (!file.open(QIODevice::ReadOnly))
60 {
62 LERROR << "AmosePluginsManager::loadPlugins: cannot open plugins file "
63 << pluginsFile.toUtf8().data();
64 std::cerr << "AmosePluginsManager::loadPlugins: cannot open plugins file "
65 << pluginsFile.toUtf8().data();
66 return false;
67 }
68
69 // For each entry, call load library
70 while (!file.atEnd())
71 {
72 // Remove whitespace characters from the start and the end.
73 QString line = QString(file.readLine()).trimmed();
74
75 // Allow empty and comment lines.
76 if ( !line.isEmpty() && !line.startsWith('#')
77 && !forbiddenPlugins.contains(line)
78 && !alreadyLoaded.contains(line)
79 )
80 {
81 LDEBUG << "AmosePluginsManager::loadPlugins loading plugin '" << line.toStdString().c_str() << "'";
82
83 if (!DynamicLibrariesManager::changeable().loadLibrary(line.toStdString().c_str()))
84 {
85 LERROR << "AmosePluginsManager::loadLibrary(\""
86 << line.toStdString() << "\") failed while handling"
87 << (pluginsDir.path() + "/" + pluginsFile) << ".";
88 std::cerr << "AmosePluginsManager::loadLibrary(\""
89 << line.toStdString() << "\") failed while handling"
90 << (pluginsDir.path() + "/" + pluginsFile).toStdString() << ".";
91 return false;
92 }
93 else
94 {
95 alreadyLoaded << line;
96 }
97 }
98 else if (alreadyLoaded.contains(line))
99 {
100 LDEBUG << "AmosePluginsManager::loadLibrary plugin" << line
101 << "was alreadyLoaded in another plugins dir. Plugins file:"
102 << pluginsDir.path() + "/" + pluginsFile;
103 }
104 else if (forbiddenPlugins.contains(line))
105 {
106 LDEBUG << "AmosePluginsManager::loadLibrary plugin" << line
107 << "was set to be forbidden in anothe plugins dir. Plugins file:"
108 << pluginsDir.path() + "/" + pluginsFile;
109 }
110 else if (line.startsWith('#'))
111 {
112 line.remove(0,1);
113 LDEBUG << "AmosePluginsManager::loadLibrary plugin" << line
114 << "forbidden from now on. Plugins file:"
115 << pluginsDir.path() + "/" + pluginsFile;
116 forbiddenPlugins << line;
117 }
118 }
119 }
120 }
121 return true;
122}
handle dynamic library opening (wrapper around QLibrary)
#define LIMA_PATH_SEPARATOR
Definition FileUtils.h:25
#define LDEBUG
Definition LimaCommon.h:157
#define LINFO
Definition LimaCommon.h:158
#define LERROR
Definition LimaCommon.h:161
#define ABSTRACTFACTORYPATTERNLOGINIT
Definition LimaCommon.h:198
bool loadPlugins(const QString &configDirs="")
Load plugins in the 'plugins' subdir of the semicolon separated config dirs.
static DynamicLibrariesManager & changeable()
singleton accessor
Definition Singleton.h:71
QStringList buildConfigurationDirectoriesList(const QStringList &projects, const QStringList &paths)
Build a list of configuration directories from a list of project names and a list of paths.
NAUTITIA.