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
|
//
// This file is part of the Marble Virtual Globe.
//
// This program is free software licensed under the GNU LGPL. You can
// find a copy of this license in LICENSE.txt in the top directory of
// the source code.
//
// Copyright 2008 Torsten Rahn <tackat@kde.org>
// Copyright 2009 Jens-Michael Hoffmann <jensmh@gmx.de>
//
// Own
#include "PluginManager.h"
// Qt
#include <QList>
#include <QPluginLoader>
#include <QTime>
#include <QMessageBox>
// Local dir
#include "MarbleDirs.h"
#include "MarbleDebug.h"
#include "RenderPlugin.h"
#include "PositionProviderPlugin.h"
#include "ParseRunnerPlugin.h"
#include "ReverseGeocodingRunnerPlugin.h"
#include "RoutingRunnerPlugin.h"
#include "SearchRunnerPlugin.h"
#include "config-marble.h"
namespace Marble
{
class PluginManagerPrivate
{
public:
PluginManagerPrivate(PluginManager* parent)
: m_pluginsLoaded(false),
m_parent(parent)
{
}
~PluginManagerPrivate();
void loadPlugins();
bool m_pluginsLoaded;
QList<const RenderPlugin *> m_renderPluginTemplates;
QList<const PositionProviderPlugin *> m_positionProviderPluginTemplates;
QList<const SearchRunnerPlugin *> m_searchRunnerPlugins;
QList<const ReverseGeocodingRunnerPlugin *> m_reverseGeocodingRunnerPlugins;
QList<RoutingRunnerPlugin *> m_routingRunnerPlugins;
QList<const ParseRunnerPlugin *> m_parsingRunnerPlugins;
PluginManager* m_parent;
static QStringList m_blacklist;
static QStringList m_whitelist;
#ifdef Q_OS_ANDROID
QStringList m_pluginPaths;
#endif
};
QStringList PluginManagerPrivate::m_blacklist;
QStringList PluginManagerPrivate::m_whitelist;
PluginManagerPrivate::~PluginManagerPrivate()
{
// nothing to do
}
PluginManager::PluginManager( QObject *parent ) : QObject( parent ),
d( new PluginManagerPrivate(this) )
{
//Checking assets:/plugins for uninstalled plugins
#ifdef Q_OS_ANDROID
installPluginsFromAssets();
#endif
}
PluginManager::~PluginManager()
{
delete d;
}
QList<const RenderPlugin *> PluginManager::renderPlugins() const
{
d->loadPlugins();
return d->m_renderPluginTemplates;
}
void PluginManager::addRenderPlugin( const RenderPlugin *plugin )
{
d->loadPlugins();
d->m_renderPluginTemplates << plugin;
emit renderPluginsChanged();
}
QList<const PositionProviderPlugin *> PluginManager::positionProviderPlugins() const
{
d->loadPlugins();
return d->m_positionProviderPluginTemplates;
}
void PluginManager::addPositionProviderPlugin( const PositionProviderPlugin *plugin )
{
d->loadPlugins();
d->m_positionProviderPluginTemplates << plugin;
emit positionProviderPluginsChanged();
}
QList<const SearchRunnerPlugin *> PluginManager::searchRunnerPlugins() const
{
d->loadPlugins();
return d->m_searchRunnerPlugins;
}
void PluginManager::addSearchRunnerPlugin( const SearchRunnerPlugin *plugin )
{
d->loadPlugins();
d->m_searchRunnerPlugins << plugin;
emit searchRunnerPluginsChanged();
}
QList<const ReverseGeocodingRunnerPlugin *> PluginManager::reverseGeocodingRunnerPlugins() const
{
d->loadPlugins();
return d->m_reverseGeocodingRunnerPlugins;
}
void PluginManager::addReverseGeocodingRunnerPlugin( const ReverseGeocodingRunnerPlugin *plugin )
{
d->loadPlugins();
d->m_reverseGeocodingRunnerPlugins << plugin;
emit reverseGeocodingRunnerPluginsChanged();
}
QList<RoutingRunnerPlugin *> PluginManager::routingRunnerPlugins() const
{
d->loadPlugins();
return d->m_routingRunnerPlugins;
}
void PluginManager::addRoutingRunnerPlugin( RoutingRunnerPlugin *plugin )
{
d->loadPlugins();
d->m_routingRunnerPlugins << plugin;
emit routingRunnerPluginsChanged();
}
QList<const ParseRunnerPlugin *> PluginManager::parsingRunnerPlugins() const
{
d->loadPlugins();
return d->m_parsingRunnerPlugins;
}
void PluginManager::addParseRunnerPlugin( const ParseRunnerPlugin *plugin )
{
d->loadPlugins();
d->m_parsingRunnerPlugins << plugin;
emit parseRunnerPluginsChanged();
}
void PluginManager::blacklistPlugin(const QString &filename)
{
PluginManagerPrivate::m_blacklist << MARBLE_SHARED_LIBRARY_PREFIX + filename;
}
void PluginManager::whitelistPlugin(const QString &filename)
{
PluginManagerPrivate::m_whitelist << MARBLE_SHARED_LIBRARY_PREFIX + filename;
}
/** Append obj to the given plugins list if it inherits both T and U */
template<class T, class U>
bool appendPlugin( QObject * obj, QPluginLoader* &loader, QList<T*> &plugins )
{
if ( qobject_cast<T*>( obj ) && qobject_cast<U*>( obj ) ) {
Q_ASSERT( obj->metaObject()->superClass() ); // all our plugins have a super class
mDebug() << obj->metaObject()->superClass()->className()
<< "plugin loaded from" << loader->fileName();
T* plugin = qobject_cast<T*>( obj );
Q_ASSERT( plugin ); // checked above
plugins << plugin;
return true;
}
return false;
}
/** Append obj to the given plugins list if it inherits both T and U */
template<class T, class U>
bool appendPlugin( QObject * obj, QPluginLoader* &loader, QList<const T*> &plugins )
{
if ( qobject_cast<T*>( obj ) && qobject_cast<U*>( obj ) ) {
Q_ASSERT( obj->metaObject()->superClass() ); // all our plugins have a super class
mDebug() << obj->metaObject()->superClass()->className()
<< "plugin loaded from" << loader->fileName();
T* plugin = qobject_cast<T*>( obj );
Q_ASSERT( plugin ); // checked above
plugins << plugin;
return true;
}
return false;
}
void PluginManagerPrivate::loadPlugins()
{
if (m_pluginsLoaded)
{
return;
}
QTime t;
t.start();
mDebug() << "Starting to load Plugins.";
QStringList pluginFileNameList = MarbleDirs::pluginEntryList( "", QDir::Files );
MarbleDirs::debug();
Q_ASSERT( m_renderPluginTemplates.isEmpty() );
Q_ASSERT( m_positionProviderPluginTemplates.isEmpty() );
Q_ASSERT( m_searchRunnerPlugins.isEmpty() );
Q_ASSERT( m_reverseGeocodingRunnerPlugins.isEmpty() );
Q_ASSERT( m_routingRunnerPlugins.isEmpty() );
Q_ASSERT( m_parsingRunnerPlugins.isEmpty() );
bool foundPlugin = false;
for( const QString &fileName: pluginFileNameList ) {
QString const baseName = QFileInfo(fileName).baseName();
if (!m_whitelist.isEmpty() && !m_whitelist.contains(baseName)) {
mDebug() << "Ignoring non-whitelisted plugin " << fileName;
continue;
}
if (m_blacklist.contains(baseName)) {
mDebug() << "Ignoring blacklisted plugin " << fileName;
continue;
}
// mDebug() << fileName << " - " << MarbleDirs::pluginPath( fileName );
QString const path = MarbleDirs::pluginPath( fileName );
#ifdef Q_OS_ANDROID
QFileInfo targetFile( path );
if ( !m_pluginPaths.contains( targetFile.canonicalFilePath() ) ) {
// @todo Delete the file here?
qDebug() << "Ignoring file " << path << " which is not among the currently installed plugins";
continue;
}
#endif
QPluginLoader* loader = new QPluginLoader( path, m_parent );
QObject * obj = loader->instance();
if ( obj ) {
bool isPlugin = appendPlugin<RenderPlugin, RenderPluginInterface>
( obj, loader, m_renderPluginTemplates );
isPlugin = isPlugin || appendPlugin<PositionProviderPlugin, PositionProviderPluginInterface>
( obj, loader, m_positionProviderPluginTemplates );
isPlugin = isPlugin || appendPlugin<SearchRunnerPlugin, SearchRunnerPlugin>
( obj, loader, m_searchRunnerPlugins ); // intentionally T==U
isPlugin = isPlugin || appendPlugin<ReverseGeocodingRunnerPlugin, ReverseGeocodingRunnerPlugin>
( obj, loader, m_reverseGeocodingRunnerPlugins ); // intentionally T==U
isPlugin = isPlugin || appendPlugin<RoutingRunnerPlugin, RoutingRunnerPlugin>
( obj, loader, m_routingRunnerPlugins ); // intentionally T==U
isPlugin = isPlugin || appendPlugin<ParseRunnerPlugin, ParseRunnerPlugin>
( obj, loader, m_parsingRunnerPlugins ); // intentionally T==U
if ( !isPlugin ) {
qWarning() << "Ignoring the following plugin since it couldn't be loaded:" << path;
mDebug() << "Plugin failure:" << path << "is a plugin, but it does not implement the "
<< "right interfaces or it was compiled against an old version of Marble. Ignoring it.";
delete loader;
} else {
foundPlugin = true;
}
} else {
qWarning() << "Ignoring to load the following file since it doesn't look like a valid Marble plugin:" << path << endl
<< "Reason:" << loader->errorString();
delete loader;
}
}
if ( !foundPlugin ) {
#ifdef Q_OS_WIN
QString pluginPaths = "Plugin Path: " + MarbleDirs::marblePluginPath();
if ( MarbleDirs::marblePluginPath().isEmpty() )
pluginPaths = "";
pluginPaths += "System Path: " + MarbleDirs::pluginSystemPath() + "\nLocal Path: " + MarbleDirs::pluginLocalPath();
QMessageBox::warning( nullptr,
"No plugins loaded",
"No plugins were loaded, please check if the plugins were installed in one of the following paths:\n" + pluginPaths
+ "\n\nAlso check if the plugin is compiled against the right version of Marble. " +
"Analyzing the debug messages inside a debugger might give more insight." );
#else
qWarning() << "No plugins loaded. Please check if the plugins were installed in the correct path,"
<< "or if any errors occurred while loading plugins.";
#endif
}
m_pluginsLoaded = true;
mDebug() << Q_FUNC_INFO << "Time elapsed:" << t.elapsed() << "ms";
}
#ifdef Q_OS_ANDROID
void PluginManager::installPluginsFromAssets() const
{
d->m_pluginPaths.clear();
QStringList copyList = MarbleDirs::pluginEntryList(QString());
QDir pluginHome(MarbleDirs::localPath());
pluginHome.mkpath(MarbleDirs::pluginLocalPath());
pluginHome.setCurrent(MarbleDirs::pluginLocalPath());
QStringList pluginNameFilter = QStringList() << "lib*.so";
QStringList const existingPlugins = QDir(MarbleDirs::pluginLocalPath()).entryList(pluginNameFilter, QDir::Files);
for(const QString &existingPlugin: existingPlugins) {
QFile::remove(existingPlugin);
}
for (const QString & file: copyList) {
QString const target = MarbleDirs::pluginLocalPath() + QLatin1Char('/') + file;
if (QFileInfo(MarbleDirs::pluginSystemPath() + QLatin1Char('/') + file).isDir()) {
pluginHome.mkpath(target);
}
else {
QFile temporaryFile(MarbleDirs::pluginSystemPath() + QLatin1Char('/') + file);
temporaryFile.copy(target);
QFileInfo targetFile(target);
d->m_pluginPaths << targetFile.canonicalFilePath();
}
}
}
#endif
}
#include "moc_PluginManager.cpp"
|