-
Notifications
You must be signed in to change notification settings - Fork 26
/
pluginmanager.cpp
294 lines (207 loc) · 9.44 KB
/
pluginmanager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*******************************************************************************
Copyright (C) The University of Auckland
OpenCOR is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenCOR is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*******************************************************************************/
//==============================================================================
// Plugin manager
//==============================================================================
#ifdef OpenCOR_MAIN
#include "cliutils.h"
#else
#include "corecliutils.h"
#endif
#include "plugin.h"
#include "pluginmanager.h"
//==============================================================================
#include <QCoreApplication>
#include <QDir>
//==============================================================================
namespace OpenCOR {
//==============================================================================
PluginManager::PluginManager(bool pGuiMode) :
mGuiMode(pGuiMode),
mPlugins(Plugins()),
mLoadedPlugins(Plugins()),
mCorePlugin(0),
mSimulationSupportPlugin(0)
{
// Retrieve OpenCOR's plugins directory
// Note #1: the plugin's directory is retrieved in main()...
// Note #2: we use QDir().canonicalPath() to ensure that our plugins
// directory uses "/" (and not a mixture of "/" and "\"), which is
// critical on Windows...
mPluginsDir = QDir(QCoreApplication::libraryPaths().first()).canonicalPath()+"/"+qAppName();
// Retrieve the list of plugins available for loading
QFileInfoList fileInfoList = QDir(mPluginsDir).entryInfoList(QStringList("*"+PluginExtension), QDir::Files);
QStringList fileNames = QStringList();
foreach (const QFileInfo &fileInfo, fileInfoList)
fileNames << fileInfo.canonicalFilePath();
// Retrieve and initialise some information about the plugins
QMap<QString, PluginInfo *> pluginsInfo = QMap<QString, PluginInfo *>();
QMap<QString, QString> pluginsError = QMap<QString, QString>();
foreach (const QString &fileName, fileNames) {
QString pluginName = Plugin::name(fileName);
QString pluginError = QString();
PluginInfo *pluginInfo = (Plugin::pluginInfoVersion(fileName) == pluginInfoVersion())?
Plugin::info(fileName, &pluginError):
0;
pluginsInfo.insert(pluginName, pluginInfo);
pluginsError.insert(pluginName, pluginError);
// Keep track of the plugin's full dependencies, if possible
// Note: if there is some plugin information, then it will get owned by
// the plugin itself. So, it will be the plugin's responsibility
// to delete it (see Plugin::~Plugin())...
if (pluginInfo)
pluginInfo->setFullDependencies(Plugin::fullDependencies(mPluginsDir, pluginName));
}
// Determine in which order the plugins files should be analysed (i.e. take
// into account the result of a plugin's loadBefore() function)
QStringList sortedFileNames = QStringList();
foreach (const QString &fileName, fileNames) {
PluginInfo *pluginInfo = pluginsInfo.value(Plugin::name(fileName));
if (pluginInfo) {
int index = sortedFileNames.count();
foreach (const QString &loadBefore, pluginInfo->loadBefore()) {
int loadBeforeIndex = sortedFileNames.indexOf(Plugin::fileName(mPluginsDir, loadBefore));
if (loadBeforeIndex < index)
index = loadBeforeIndex;
}
sortedFileNames.insert(index, fileName);
}
}
// Determine which plugins, if any, are needed by others and which, if any,
// are selectable
QStringList neededPlugins = QStringList();
QStringList wantedPlugins = QStringList();
foreach (const QString &fileName, sortedFileNames) {
QString pluginName = Plugin::name(fileName);
PluginInfo *pluginInfo = pluginsInfo.value(pluginName);
if (pluginInfo) {
// Keep track of the plugin itself, should it be selectable and
// requested by the user (if we are in GUI mode) or have CLI support
// (if we are in CLI mode)
if ( ( pGuiMode && pluginInfo->isSelectable() && Plugin::load(pluginName))
|| (!pGuiMode && pluginInfo->hasCliSupport())) {
// Keep track of the plugin's dependencies
neededPlugins << pluginsInfo.value(pluginName)->fullDependencies();
// Also keep track of the plugin itself
wantedPlugins << pluginName;
}
}
}
// We now have all our needed and wanted plugins with our needed plugins
// nicely sorted based on their dependencies with one another. So, retrieve
// their file name
QStringList plugins = neededPlugins+wantedPlugins;
QStringList pluginFileNames = QStringList();
plugins.removeDuplicates();
// Note: if anything, there should only be duplicates in neededPlugins, and
// not between neededPlugins and wantedPlugins. Then again, we better
// be safe than sorry since a selectable plugin (i.e. listed in
// wantedPlugins) might be (wrongly) needed by another plugin (i.e.
// listed in neededPlugins)...
foreach (const QString &plugin, plugins)
pluginFileNames << Plugin::fileName(mPluginsDir, plugin);
// If we are in GUI mode, then we want to know about all the plugins,
// including the ones that are not to be loaded (so that we can refer to
// them, in the plugins window, as either not wanted or not needed)
if (pGuiMode) {
pluginFileNames << fileNames;
pluginFileNames.removeDuplicates();
}
// Deal with all the plugins we need and want
foreach (const QString &pluginFileName, pluginFileNames) {
QString pluginName = Plugin::name(pluginFileName);
Plugin *plugin = new Plugin(pluginFileName,
pluginsInfo.value(pluginName),
pluginsError.value(pluginName),
plugins.contains(pluginName), this);
// Keep track of the plugin and of the Core plugin, in particular, if it
// is loaded
mPlugins << plugin;
if (plugin->status() == Plugin::Loaded) {
mLoadedPlugins << plugin;
if (!pluginName.compare(CorePluginName))
mCorePlugin = plugin;
else if (!pluginName.compare(SimulationSupportPluginName))
mSimulationSupportPlugin = plugin;
}
}
}
//==============================================================================
PluginManager::~PluginManager()
{
// Delete some internal objects
foreach (Plugin *plugin, mPlugins)
delete plugin;
}
//==============================================================================
bool PluginManager::guiMode() const
{
// Return whether we are in GUI mode
return mGuiMode;
}
//==============================================================================
Plugins PluginManager::plugins() const
{
// Return a list of all our plugins, whether loaded or not
return mPlugins;
}
//==============================================================================
Plugins PluginManager::sortedPlugins() const
{
// Return a sorted version of mPlugins
Plugins res = mPlugins;
std::sort(res.begin(), res.end(), Plugin::compare);
return res;
}
//==============================================================================
Plugins PluginManager::loadedPlugins() const
{
// Return a list of our loaded plugins
return mLoadedPlugins;
}
//==============================================================================
Plugins PluginManager::sortedLoadedPlugins() const
{
// Return a sorted version of mLoadedPlugins
Plugins res = mLoadedPlugins;
std::sort(res.begin(), res.end(), Plugin::compare);
return res;
}
//==============================================================================
Plugin * PluginManager::plugin(const QString &pName) const
{
// Return the plugin, if any, which name is the one we have been passed
foreach (Plugin *plugin, mPlugins) {
if (!pName.compare(plugin->name()))
return plugin;
}
return 0;
}
//==============================================================================
Plugin * PluginManager::corePlugin() const
{
// Return our Core plugin
return mCorePlugin;
}
//==============================================================================
Plugin * PluginManager::simulationSupportPlugin() const
{
// Return our Simulation Support plugin
return mSimulationSupportPlugin;
}
//==============================================================================
} // namespace OpenCOR
//==============================================================================
// End of file
//==============================================================================