From d965783a5384fbc393c2bc0c00c1f318af8a8eab Mon Sep 17 00:00:00 2001 From: Dominik Schmidt Date: Sun, 23 Jun 2013 23:14:16 +0200 Subject: [PATCH] Centralize plugin loading logic in Tomahawk::Utils::PluginLoader --- src/libtomahawk/accounts/AccountManager.cpp | 64 +++++-------------- src/libtomahawk/accounts/AccountManager.h | 4 +- .../infosystem/InfoSystemWorker.cpp | 37 +++-------- src/libtomahawk/infosystem/InfoSystemWorker.h | 2 +- src/libtomahawk/utils/PluginLoader.cpp | 36 +++++++++++ src/libtomahawk/utils/PluginLoader.h | 4 +- 6 files changed, 66 insertions(+), 81 deletions(-) diff --git a/src/libtomahawk/accounts/AccountManager.cpp b/src/libtomahawk/accounts/AccountManager.cpp index 2654f0b19..86a259147 100644 --- a/src/libtomahawk/accounts/AccountManager.cpp +++ b/src/libtomahawk/accounts/AccountManager.cpp @@ -35,9 +35,6 @@ #include "TomahawkSettings.h" #include "LocalConfigStorage.h" -#include -#include -#include #include #include #include @@ -91,7 +88,7 @@ AccountManager::init() connect( TomahawkSettings::instance(), SIGNAL( changed() ), SLOT( onSettingsChanged() ) ); - loadPluginFactories( Tomahawk::Utils::PluginLoader( "account" ).pluginPaths() ); + loadPluginFactories(); // We include the resolver factory manually, not in a plugin ResolverAccountFactory* f = new ResolverAccountFactory(); @@ -103,15 +100,20 @@ AccountManager::init() void -AccountManager::loadPluginFactories( const QStringList& paths ) +AccountManager::loadPluginFactories() { - foreach ( QString fileName, paths ) + QHash< QString, QObject* > plugins = Tomahawk::Utils::PluginLoader( "account" ).loadPlugins(); + foreach ( QObject* plugin, plugins.values() ) { - if ( !QLibrary::isLibrary( fileName ) ) - continue; - - tDebug() << Q_FUNC_INFO << "Trying to load plugin:" << fileName; - loadPluginFactory( fileName ); + AccountFactory* accountfactory = qobject_cast( plugin ); + if ( accountfactory ) + { + tDebug() << Q_FUNC_INFO << "Loaded plugin factory:" << plugins.key( plugin ) << accountfactory->factoryId() << accountfactory->prettyName(); + m_accountFactories[ accountfactory->factoryId() ] = accountfactory; + } else + { + tDebug() << Q_FUNC_INFO << "Loaded invalid plugin.." << plugins.key( plugin ); + } } } @@ -144,29 +146,6 @@ AccountManager::factoryForAccount( Account* account ) const } -void -AccountManager::loadPluginFactory( const QString& path ) -{ - QPluginLoader loader( path ); - QObject* plugin = loader.instance(); - if ( !plugin ) - { - tDebug() << Q_FUNC_INFO << "Error loading plugin:" << loader.errorString(); - } - - AccountFactory* accountfactory = qobject_cast( plugin ); - if ( accountfactory ) - { - tDebug() << Q_FUNC_INFO << "Loaded plugin factory:" << loader.fileName() << accountfactory->factoryId() << accountfactory->prettyName(); - m_accountFactories[ accountfactory->factoryId() ] = accountfactory; - } else - { - tDebug() << Q_FUNC_INFO << "Loaded invalid plugin.." << loader.fileName(); - } -} - - - void AccountManager::enableAccount( Account* account ) { @@ -250,21 +229,10 @@ AccountManager::loadFromConfig() ConfigStorage* localCS = new LocalConfigStorage( this ); m_configStorageById.insert( localCS->id(), localCS ); - QStringList configStoragePlugins = Tomahawk::Utils::PluginLoader( "configstorage" ).pluginPaths(); - foreach( const QString& pluginPath, configStoragePlugins ) + QList< QObject* > configStoragePlugins = Tomahawk::Utils::PluginLoader( "configstorage" ).loadPlugins().values(); + foreach( QObject* plugin, configStoragePlugins ) { - QPluginLoader loader; - if ( !QLibrary::isLibrary( pluginPath ) ) - continue; - - loader.setFileName( pluginPath ); - if ( !loader.load() ) - { - tDebug() << "Error loading ConfigStorage plugin" << loader.errorString() << loader.staticInstances().count(); - continue; - } - - ConfigStorage* cs = qobject_cast< ConfigStorage* >( loader.instance() ); + ConfigStorage* cs = qobject_cast< ConfigStorage* >( plugin ); if ( !cs ) continue; diff --git a/src/libtomahawk/accounts/AccountManager.h b/src/libtomahawk/accounts/AccountManager.h index 1e24fbc93..b219d8e81 100644 --- a/src/libtomahawk/accounts/AccountManager.h +++ b/src/libtomahawk/accounts/AccountManager.h @@ -125,11 +125,9 @@ private slots: void onSettingsChanged(); private: - void loadPluginFactories( const QStringList &paths ); - void loadPluginFactory( const QString &path ); + void loadPluginFactories(); QString factoryFromId( const QString& accountId ) const; - Account* loadPlugin( const QString& accountId ); void hookupAccount( Account* ) const; diff --git a/src/libtomahawk/infosystem/InfoSystemWorker.cpp b/src/libtomahawk/infosystem/InfoSystemWorker.cpp index 2c0967457..3ca764bfb 100644 --- a/src/libtomahawk/infosystem/InfoSystemWorker.cpp +++ b/src/libtomahawk/infosystem/InfoSystemWorker.cpp @@ -32,13 +32,9 @@ #include "utils/PluginLoader.h" #include "Source.h" - #include -#include -#include #include #include -#include namespace Tomahawk { @@ -85,7 +81,7 @@ InfoSystemWorker::init( Tomahawk::InfoSystem::InfoSystemCache* cache ) m_shortLinksWaiting = 0; m_cache = cache; - loadInfoPlugins( Tomahawk::Utils::PluginLoader( "infoplugin" ).pluginPaths() ); + loadInfoPlugins(); } @@ -169,37 +165,22 @@ InfoSystemWorker::removeInfoPlugin( Tomahawk::InfoSystem::InfoPluginPtr plugin ) void -InfoSystemWorker::loadInfoPlugins( const QStringList& pluginPaths ) +InfoSystemWorker::loadInfoPlugins() { - tDebug() << Q_FUNC_INFO << "Attempting to load the following plugin paths:" << pluginPaths; - - if ( pluginPaths.isEmpty() ) - return; - - foreach ( const QString fileName, pluginPaths ) + QHash< QString, QObject* > plugins = Tomahawk::Utils::PluginLoader( "infoplugin" ).loadPlugins(); + foreach ( QObject* plugin, plugins.values() ) { - if ( !QLibrary::isLibrary( fileName ) ) - continue; - - tDebug() << Q_FUNC_INFO << "Trying to load plugin:" << fileName; - - QPluginLoader loader( fileName ); - QObject* plugin = loader.instance(); - if ( !plugin ) - { - tDebug() << Q_FUNC_INFO << "Error loading plugin:" << loader.errorString(); - continue; - } - InfoPlugin* infoPlugin = qobject_cast< InfoPlugin* >( plugin ); if ( infoPlugin ) { - tDebug() << Q_FUNC_INFO << "Loaded info plugin:" << loader.fileName(); - infoPlugin->setFriendlyName( loader.fileName() ); + tDebug() << Q_FUNC_INFO << "Loaded info plugin:" << plugins.key( plugin ); + infoPlugin->setFriendlyName( plugins.key( plugin ) ); addInfoPlugin( InfoPluginPtr( infoPlugin ) ); } else - tDebug() << Q_FUNC_INFO << "Loaded invalid plugin:" << loader.fileName(); + { + tDebug() << Q_FUNC_INFO << "Loaded invalid plugin:" << plugins.key( plugin ); + } } } diff --git a/src/libtomahawk/infosystem/InfoSystemWorker.h b/src/libtomahawk/infosystem/InfoSystemWorker.h index 65cfbd4ed..df966e064 100644 --- a/src/libtomahawk/infosystem/InfoSystemWorker.h +++ b/src/libtomahawk/infosystem/InfoSystemWorker.h @@ -68,7 +68,7 @@ public slots: void addInfoPlugin( Tomahawk::InfoSystem::InfoPluginPtr plugin ); void removeInfoPlugin( Tomahawk::InfoSystem::InfoPluginPtr plugin ); - void loadInfoPlugins( const QStringList &pluginPaths ); + void loadInfoPlugins(); void getShortUrl( Tomahawk::InfoSystem::InfoPushData data ); void shortLinkReady( QUrl longUrl, QUrl shortUrl, QVariant callbackObj ); diff --git a/src/libtomahawk/utils/PluginLoader.cpp b/src/libtomahawk/utils/PluginLoader.cpp index db66d4583..f1a056f99 100644 --- a/src/libtomahawk/utils/PluginLoader.cpp +++ b/src/libtomahawk/utils/PluginLoader.cpp @@ -29,6 +29,8 @@ #include #include +#include +#include #include "DllMacro.h" @@ -55,6 +57,40 @@ PluginLoader::~PluginLoader() } +const QHash< QString, QObject* > PluginLoader::loadPlugins() const +{ + tLog() << "Load plugins of type" << d_ptr->type; + + const QString errorMessage("Error loading plugin: %1: %2"); + + QHash< QString, QObject* > plugins; + + foreach( const QString& pluginPath, pluginPaths() ) + { +// tDebug() << Q_FUNC_INFO << "Trying to load plugin:" << pluginPath; + + if ( !QLibrary::isLibrary( pluginPath ) ) + { + tLog() << Q_FUNC_INFO << errorMessage.arg( pluginPath, "Not a library" ); + continue; + } + + QPluginLoader loader( pluginPath ); + + QObject* plugin = loader.instance(); + if ( !plugin ) + { + tLog() << Q_FUNC_INFO << errorMessage.arg( pluginPath, loader.errorString() ); + continue; + } + + plugins.insert( loader.fileName(), plugin ); + } + + return plugins; +} + + const QList< QDir > PluginLoader::pluginDirs() { diff --git a/src/libtomahawk/utils/PluginLoader.h b/src/libtomahawk/utils/PluginLoader.h index 484137fae..2cc495eb0 100644 --- a/src/libtomahawk/utils/PluginLoader.h +++ b/src/libtomahawk/utils/PluginLoader.h @@ -38,12 +38,14 @@ public: PluginLoader( const QString& type ); virtual ~PluginLoader(); + const QHash< QString, QObject* > loadPlugins() const; + +private: const QStringList pluginFilenames( const QString& name = "*" ) const; const QStringList pluginPaths( const QString& name = "*" ) const; static const QList< QDir > pluginDirs(); -private: Q_DECLARE_PRIVATE( PluginLoader ); PluginLoaderPrivate* d_ptr; };