| 1 |
/****************************************************************************** |
| 2 |
This source file is part of the Avogadro project. |
| 3 |
This source code is released under the 3-Clause BSD License, (see "LICENSE"). |
| 4 |
******************************************************************************/ |
| 5 |
|
| 6 |
#include "pluginmanager.h" |
| 7 |
#include "avogadrostaticqtplugins.h" |
| 8 |
|
| 9 |
#include <avogadro/qtgui/utilities.h> |
| 10 |
|
| 11 |
#include <QtCore/QCoreApplication> |
| 12 |
#include <QtCore/QDir> |
| 13 |
#include <QtCore/QFileInfo> |
| 14 |
#include <QtCore/QMutex> |
| 15 |
#include <QtCore/QPluginLoader> |
| 16 |
|
| 17 |
#include <QtCore/QDebug> |
| 18 |
|
| 19 |
namespace Avogadro::QtPlugins { |
| 20 |
|
| 21 |
PluginManager::PluginManager(QObject* p) |
| 22 |
: QObject(p), m_staticPluginsLoaded(false) |
| 23 |
{ |
| 24 |
QString libDir(QtGui::Utilities::libraryDirectory()); |
| 25 |
// http://doc.qt.digia.com/qt/deployment-plugins.html#debugging-plugins |
| 26 |
bool debugPlugins = !qgetenv("QT_DEBUG_PLUGINS").isEmpty(); |
| 27 |
|
| 28 |
// The usual base directory is the parent directory of the executable's |
| 29 |
// location. (exe is in "bin" or "MacOS" and plugins are under the parent |
| 30 |
// directory at "<libDir>/avogadro2/plugins"...) |
| 31 |
QDir baseDir(QCoreApplication::applicationDirPath() + "/.."); |
| 32 |
|
| 33 |
#ifdef __APPLE__ |
| 34 |
// But if NOT running from the installed bundle on the Mac, the plugins are |
| 35 |
// relative to the build directory instead: |
| 36 |
// |
| 37 |
if (!QFileInfo(baseDir.absolutePath() + "/Resources/qt.conf").exists()) { |
| 38 |
QDir buildDir(QCoreApplication::applicationDirPath() + "/../../../.."); |
| 39 |
baseDir = buildDir; |
| 40 |
if (debugPlugins) |
| 41 |
qDebug() << " using buildDir:" << buildDir.absolutePath(); |
| 42 |
} |
| 43 |
#endif |
| 44 |
|
| 45 |
// If the environment variable is set, use that as the base directory. |
| 46 |
QByteArray pluginDir = qgetenv("AVOGADRO_PLUGIN_DIR"); |
| 47 |
if (!pluginDir.isEmpty()) |
| 48 |
baseDir.setPath(pluginDir); |
| 49 |
if (debugPlugins) |
| 50 |
qDebug() << " baseDir:" << baseDir.absolutePath(); |
| 51 |
|
| 52 |
QDir pluginsDir(baseDir.absolutePath() + "/" + libDir + "/avogadro2/plugins"); |
| 53 |
m_pluginDirs.append(pluginsDir.absolutePath()); |
| 54 |
|
| 55 |
if (debugPlugins) { |
| 56 |
qDebug() << " pluginsDir:" << pluginsDir.absolutePath(); |
| 57 |
int count = 0; |
| 58 |
foreach (const QString& pluginPath, pluginsDir.entryList(QDir::Files)) { |
| 59 |
++count; |
| 60 |
qDebug() << " " << pluginsDir.absolutePath() + "/" + pluginPath; |
| 61 |
} |
| 62 |
|
| 63 |
if (count > 0) |
| 64 |
qDebug() << " " << count << "files found in" << pluginsDir.absolutePath(); |
| 65 |
else |
| 66 |
qDebug() << " no plugin files found in" << pluginsDir.absolutePath(); |
| 67 |
} |
| 68 |
initAvogadroPluginResources(); |
| 69 |
} |
| 70 |
|
| 71 |
PluginManager::~PluginManager() {} |
| 72 |
|
| 73 |
PluginManager* PluginManager::instance() |
| 74 |
{ |
| 75 |
static QMutex mutex; |
| 76 |
// Compiler initializes this static pointer to 0. |
| 77 |
static PluginManager* pluginManagerInstance; |
| 78 |
if (!pluginManagerInstance) { |
| 79 |
mutex.lock(); |
| 80 |
if (!pluginManagerInstance) |
| 81 |
pluginManagerInstance = new PluginManager(QCoreApplication::instance()); |
| 82 |
mutex.unlock(); |
| 83 |
} |
| 84 |
return pluginManagerInstance; |
| 85 |
} |
| 86 |
|
| 87 |
void PluginManager::load() |
| 88 |
{ |
| 89 |
foreach (const QString& dir, m_pluginDirs) |
| 90 |
load(dir); |
| 91 |
} |
| 92 |
|
| 93 |
void PluginManager::load(const QString& path) |
| 94 |
{ |
| 95 |
// Load any static plugins first. |
| 96 |
if (!m_staticPluginsLoaded) { |
| 97 |
QObjectList staticPlugins = QPluginLoader::staticInstances(); |
| 98 |
foreach (QObject* pluginInstance, staticPlugins) |
| 99 |
m_plugins.append(pluginInstance); |
| 100 |
m_staticPluginsLoaded = true; |
| 101 |
} |
| 102 |
|
| 103 |
QDir dir(path); |
| 104 |
foreach (const QString& pluginPath, dir.entryList(QDir::Files)) { |
| 105 |
QPluginLoader pluginLoader(dir.absolutePath() + "/" + pluginPath); |
| 106 |
|
| 107 |
// We only want to count plugins once, the || should not be necessary but |
| 108 |
// I found that on the Mac at least isLoaded was not always reliable (and |
| 109 |
// if it is we skip the second in the short-circuit). |
| 110 |
if (pluginLoader.isLoaded() || m_plugins.contains(pluginLoader.instance())) |
| 111 |
continue; |
| 112 |
|
| 113 |
QObject* pluginInstance = pluginLoader.instance(); |
| 114 |
|
| 115 |
// Check if the plugin loaded correctly. Keep debug output for now, should |
| 116 |
// go away once we have verified this (or added to a logger). |
| 117 |
if (!pluginInstance) { |
| 118 |
qDebug() << "Failed to load" << pluginPath << "error" |
| 119 |
<< pluginLoader.errorString(); |
| 120 |
continue; |
| 121 |
} |
| 122 |
|
| 123 |
m_plugins.append(pluginInstance); |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
} // namespace Avogadro::QtPlugins |