-
Notifications
You must be signed in to change notification settings - Fork 26
/
plugin.cpp
575 lines (425 loc) · 19.8 KB
/
plugin.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
/*******************************************************************************
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
//==============================================================================
#include "cliinterface.h"
#include "coreinterface.h"
#include "datastoreinterface.h"
#include "filehandlinginterface.h"
#include "filetypeinterface.h"
#ifndef CLI_VERSION
#include "guiinterface.h"
#include "i18ninterface.h"
#endif
#include "plugin.h"
#include "plugininterface.h"
#include "pluginmanager.h"
#ifndef CLI_VERSION
#include "preferencesinterface.h"
#endif
#include "solverinterface.h"
#ifndef CLI_VERSION
#include "viewinterface.h"
#include "windowinterface.h"
#endif
//==============================================================================
#include <QDir>
#include <QLibrary>
#include <QPluginLoader>
#include <QSettings>
//==============================================================================
namespace OpenCOR {
//==============================================================================
Plugin::Plugin(const QString &pFileName, PluginInfo *pInfo,
const QString &pErrorMessage, bool pLoad,
PluginManager *pPluginManager) :
mName(name(pFileName)),
mInfo(pInfo),
mErrorMessage(pErrorMessage),
mInstance(0)
{
if (pInfo) {
// We are dealing with a plugin, so try to load it, but only if the user
// wants
if (pLoad) {
// Make sure that the plugin's dependencies, if any, are loaded
// before loading the plugin itself
// Note: normally, we would only do this on Windows systems since,
// on Windows, a shared library's dependencies must be loaded
// before the shared library itself can be loaded, while on
// Linux/macOS, it's possible to load a shared library even if
// its dependencies are not loaded. Still, it doesn't harm
// doing the same on Linux/macOS...
bool pluginDependenciesLoaded = true;
mStatusErrors = QString();
foreach (const QString &dependency, pInfo->dependencies()) {
Plugin *pluginDependency = pPluginManager->plugin(dependency);
if ( !pluginDependency
|| ( pluginDependency
&& (pluginDependency->status() != Loaded))) {
// Either the plugin dependency couldn't be found or it
// could be found but it isn't loaded
pluginDependenciesLoaded = false;
mStatus = MissingOrInvalidDependencies;
if (!mStatusErrors.isEmpty())
mStatusErrors += '\n';
mStatusErrors += (pPluginManager->guiMode()?QString():QString(" "))
+" - "+dependency;
}
}
if (statusErrorsCount() == 1) {
// There is only one error, so remove the leading " - "
mStatusErrors = mStatusErrors.remove(0, pPluginManager->guiMode()?3:6);
}
// Check whether all of the plugin's dependencies, if any, were
// loaded, and if so then try to load the plugin itself
if (pluginDependenciesLoaded) {
// All the plugin's dependencies, if any, were loaded, so try to
// load the plugin itself
QPluginLoader pluginLoader(pFileName);
if (pluginLoader.load()) {
mInstance = pluginLoader.instance();
// Make sure that the plugin uses the right version of the
// official interfaces it supports
bool unloadPlugin = true;
CliInterface *cliInterface = qobject_cast<CliInterface *>(mInstance);
CoreInterface *coreInterface = qobject_cast<CoreInterface *>(mInstance);
PluginInterface *pluginInterface = qobject_cast<PluginInterface *>(mInstance);
if ( ( cliInterface
&& (interfaceVersion(pFileName, "cliInterfaceVersion") != cliInterfaceVersion()))
|| ( coreInterface
&& (interfaceVersion(pFileName, "coreInterfaceVersion") != coreInterfaceVersion()))
|| ( qobject_cast<DataStoreInterface *>(mInstance)
&& (interfaceVersion(pFileName, "dataStoreInterfaceVersion") != dataStoreInterfaceVersion()))
|| ( qobject_cast<FileHandlingInterface *>(mInstance)
&& (interfaceVersion(pFileName, "fileHandlingInterfaceVersion") != fileHandlingInterfaceVersion()))
|| ( qobject_cast<FileTypeInterface *>(mInstance)
&& (interfaceVersion(pFileName, "fileTypeInterfaceVersion") != fileTypeInterfaceVersion()))
#ifndef CLI_VERSION
|| ( qobject_cast<GuiInterface *>(mInstance)
&& (interfaceVersion(pFileName, "guiInterfaceVersion") != guiInterfaceVersion()))
|| ( qobject_cast<I18nInterface *>(mInstance)
&& (interfaceVersion(pFileName, "i18nInterfaceVersion") != i18nInterfaceVersion()))
#endif
|| ( pluginInterface
&& (interfaceVersion(pFileName, "pluginInterfaceVersion") != pluginInterfaceVersion()))
#ifndef CLI_VERSION
|| ( qobject_cast<PreferencesInterface *>(mInstance)
&& (interfaceVersion(pFileName, "preferencesInterfaceVersion") != preferencesInterfaceVersion()))
#endif
|| ( qobject_cast<SolverInterface *>(mInstance)
&& (interfaceVersion(pFileName, "solverInterfaceVersion") != solverInterfaceVersion()))
#ifndef CLI_VERSION
|| ( qobject_cast<ViewInterface *>(mInstance)
&& (interfaceVersion(pFileName, "viewInterfaceVersion") != viewInterfaceVersion()))
|| ( qobject_cast<WindowInterface *>(mInstance)
&& (interfaceVersion(pFileName, "windowInterfaceVersion") != windowInterfaceVersion()))
#endif
) {
// One or several of the official interfaces used by the
// plugin are too old
mStatus = OldPlugin;
} else {
// Check for the Core plugin/interface and CLI support
if (coreInterface && mName.compare(CorePluginName)) {
// We are dealing with a plugin that supports the
// Core interface, but it's not the Core plugin
mStatus = NotCorePlugin;
} else if (!coreInterface && !mName.compare(CorePluginName)) {
// We are dealing with the Core plugin, but it
// doesn't support the Core interface
mStatus = InvalidCorePlugin;
} else if (cliInterface && !pInfo->hasCliSupport()) {
// We are dealing with a plugin that supports the
// CLI interface, but it doesn't claim to have CLI
// support
mStatus = NotCliPluginNoCliSupport;
} else if (!cliInterface && pInfo->hasCliSupport()) {
// We are dealing with a plugin that is supposed to
// have CLI support, but it doesn't support the CLI
// interface
mStatus = NotCliPluginNoCliInterface;
} else {
// So far so good, so now check whether the plugin
// defines new interfaces and, if so, keep track of
// them
static PluginInterfaces pluginsWithInterfaces = PluginInterfaces();
if ( pluginInterface
&& pluginInterface->definesPluginInterfaces()) {
pluginsWithInterfaces << pluginInterface;
}
// Check whether the plugin supports any of the
// interfaces defined by the other plugins and, if
// so, whether it is of the right version
bool pluginInterfacesOk = true;
foreach (PluginInterface *pluginWithInterfaces, pluginsWithInterfaces) {
if (!pluginWithInterfaces->pluginInterfacesOk(pFileName, mInstance)) {
pluginInterfacesOk = false;
break;
}
}
if (pluginInterfacesOk) {
unloadPlugin = false;
mStatus = Loaded;
} else {
mStatus = OldPlugin;
}
}
}
if (unloadPlugin) {
pluginLoader.unload();
mInstance = 0;
}
} else {
mStatus = NotLoaded;
mStatusErrors = pluginLoader.errorString();
}
}
} else {
// The plugin is not to be loaded, which means that it is either not
// wanted or not needed, depending on whether it is selectable
if (pInfo->isSelectable())
mStatus = NotWanted;
else
mStatus = NotNeeded;
}
} else {
// What we thought was a plugin is not actually a plugin or it is a
// plugin that uses an old version of PluginInfo, so consider it as
// invalid...
// Note: to call Plugin::pluginInfoVersion() to distinguish between the
// two cases is not good enough since an old plugin may or not
// implement the pluginInfoVersion() method, which means that
// Plugin::pluginInfoVersion() would return 0 whether it's a
// library that is not a plugin or a plugin that uses an old
// version of PluginInfo. So, instead, we call Plugin::info()
// since we know it will only work for a plugin that uses an old
// version of PluginInfo...
mStatus = Plugin::info(pFileName)?OldPlugin:NotPlugin;
if (mStatus == NotPlugin) {
// Apparently, we are not dealing with a plugin, so load it so that
// we can retrieve its corresponding error
QPluginLoader pluginLoader(pFileName);
pluginLoader.load();
mStatusErrors = pluginLoader.errorString();
}
}
}
//==============================================================================
Plugin::~Plugin()
{
// Delete some internal objects
delete mInfo;
}
//==============================================================================
bool Plugin::compare(Plugin *pPlugin1, Plugin *pPlugin2)
{
// Determine which of the two plugins should be first based on their name
// Note: the comparison is case insensitive, so that it's easier for people
// to find a plugin (when we list them)...
return pPlugin1->name().compare(pPlugin2->name(), Qt::CaseInsensitive) < 0;
}
//==============================================================================
QString Plugin::name() const
{
// Return the plugin's name
return mName;
}
//==============================================================================
PluginInfo * Plugin::info() const
{
// Return the plugin's information
return mInfo;
}
//==============================================================================
QString Plugin::errorMessage() const
{
// Return the plugin's error message
return mErrorMessage;
}
//==============================================================================
QObject * Plugin::instance() const
{
// Return the plugin's instance
return mInstance;
}
//==============================================================================
Plugin::Status Plugin::status() const
{
// Return the plugin's status
return mStatus;
}
//==============================================================================
QString Plugin::statusErrors() const
{
// Return the plugin's status errors
return mStatusErrors;
}
//==============================================================================
int Plugin::statusErrorsCount() const
{
// Return the number of plugin's status errors
int res = mStatusErrors.count('\n');
if (res)
return res+1;
else
return !mStatusErrors.isEmpty();
}
//==============================================================================
QString Plugin::name(const QString &pFileName)
{
// Return the plugin's name based on its file name
return QFileInfo(pFileName).baseName().remove(0, PluginPrefix.length());
// Note: we must remove the plugin prefix part from the plugin file name...
}
//==============================================================================
QString Plugin::fileName(const QString &pPluginsDir, const QString &pName)
{
// Return the plugin's file name based on its name
return pPluginsDir+"/"+PluginPrefix+pName+PluginExtension;
// Note: we must add the plugin prefix part to the plugin file name...
}
//==============================================================================
int Plugin::pluginInfoVersion(const QString &pFileName)
{
// Return the version of PluginInfo used by the plugin
typedef int (*PluginInfoVersionFunc)();
#ifdef Q_OS_WIN
QString origPath = QDir::currentPath();
QDir::setCurrent(QFileInfo(pFileName).canonicalPath());
#endif
QLibrary plugin(pFileName);
PluginInfoVersionFunc function = (PluginInfoVersionFunc) plugin.resolve("pluginInfoVersion");
#ifdef Q_OS_WIN
QDir::setCurrent(origPath);
#endif
return function?function():0;
}
//==============================================================================
int Plugin::interfaceVersion(const QString &pFileName,
const QString &pFunctionName)
{
// Return the version of the given interface used by the plugin
typedef int (*InterfaceVersionFunc)();
#ifdef Q_OS_WIN
QString origPath = QDir::currentPath();
QDir::setCurrent(QFileInfo(pFileName).canonicalPath());
#endif
QLibrary plugin(pFileName);
InterfaceVersionFunc function = (InterfaceVersionFunc) plugin.resolve(qPrintable(pFunctionName));
#ifdef Q_OS_WIN
QDir::setCurrent(origPath);
#endif
return function?function():0;
}
//==============================================================================
PluginInfo * Plugin::info(const QString &pFileName, QString *pErrorMessage)
{
// Return the plugin's information
// Note: to retrieve a plugin's information, we must, on both Windows and
// Linux, be able to load any plugin on which a plugin depends. On
// Windows, we do this (by keeping track of the current directory
// and) by going to the directory where our plugins are located.
// However, this approach doesn't work on Linux, so instead we rely
// on RPATH to get the result we are after...
typedef void * (*PluginInfoFunc)();
#ifdef Q_OS_WIN
QString origPath = QDir::currentPath();
QDir::setCurrent(QFileInfo(pFileName).canonicalPath());
#endif
QLibrary plugin(pFileName);
PluginInfoFunc function = (PluginInfoFunc) plugin.resolve(qPrintable(name(pFileName)+"PluginInfo"));
#ifdef Q_OS_WIN
QDir::setCurrent(origPath);
#endif
// Check whether the plugin information could be retrieved and, if not,
// retrieve the error and format it a bit
if (function) {
if (pErrorMessage)
*pErrorMessage = QString();
return static_cast<PluginInfo *>(function());
} else {
if (pErrorMessage) {
*pErrorMessage = plugin.errorString();
(*pErrorMessage)[0] = (*pErrorMessage)[0].toLower();
if (!pErrorMessage->endsWith('.'))
*pErrorMessage += '.';
pErrorMessage->replace('\n', ";");
pErrorMessage->replace(" ", " ");
int errorMessageSize = pErrorMessage->size();
int from = 0;
int pos;
while ((pos = pErrorMessage->indexOf(':', from)) != -1) {
pos += 2;
if (pos < errorMessageSize)
(*pErrorMessage)[pos] = (*pErrorMessage)[pos].toLower();
from = pos;
}
}
return 0;
}
}
//==============================================================================
static const auto SettingsLoad = QStringLiteral("Load");
//==============================================================================
bool Plugin::load(const QString &pName)
{
// Retrieve the plugin's loading requirement
QSettings settings;
settings.beginGroup(SettingsPlugins);
settings.beginGroup(pName);
bool res = settings.value(SettingsLoad, true).toBool();
settings.endGroup();
settings.endGroup();
return res;
}
//==============================================================================
void Plugin::setLoad(const QString &pName, bool pToBeLoaded)
{
// Keep track of the plugin's loading requirement
// Note: see the Plugin::load's note...
QSettings settings;
settings.beginGroup(SettingsPlugins);
settings.beginGroup(pName);
settings.setValue(SettingsLoad, pToBeLoaded);
settings.endGroup();
settings.endGroup();
}
//==============================================================================
QStringList Plugin::fullDependencies(const QString &pPluginsDir,
const QString &pName, int pLevel)
{
// Return the given plugin's full dependencies
QStringList res = QStringList();
// Recursively look for the plugin's full dependencies
PluginInfo *pluginInfo = Plugin::info(Plugin::fileName(pPluginsDir, pName));
if (!pluginInfo)
return res;
foreach (const QString &plugin, pluginInfo->dependencies())
res << fullDependencies(pPluginsDir, plugin, pLevel+1);
delete pluginInfo;
// Add the current plugin to the list, but only if it is not the original
// plugin, otherwise remove any duplicates
if (pLevel)
res << pName;
else
res.removeDuplicates();
return res;
}
//==============================================================================
} // namespace OpenCOR
//==============================================================================
// End of file
//==============================================================================