1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-05-04 15:26:38 +02:00

Centralize plugin loading logic in Tomahawk::Utils::PluginLoader

This commit is contained in:
Dominik Schmidt 2013-06-23 23:14:16 +02:00 committed by Teo Mrnjavac
parent 93bbf44669
commit d965783a53
6 changed files with 66 additions and 81 deletions

View File

@ -35,9 +35,6 @@
#include "TomahawkSettings.h"
#include "LocalConfigStorage.h"
#include <QLibrary>
#include <QDir>
#include <QPluginLoader>
#include <QCoreApplication>
#include <QSet>
#include <QTimer>
@ -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<AccountFactory*>( 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<AccountFactory*>( 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;

View File

@ -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;

View File

@ -32,13 +32,9 @@
#include "utils/PluginLoader.h"
#include "Source.h"
#include <QCoreApplication>
#include <QDir>
#include <QLibrary>
#include <QNetworkConfiguration>
#include <QNetworkProxy>
#include <QPluginLoader>
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 );
}
}
}

View File

@ -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 );

View File

@ -29,6 +29,8 @@
#include <QDir>
#include <QCoreApplication>
#include <QLibrary>
#include <QPluginLoader>
#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()
{

View File

@ -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;
};