mirror of
https://github.com/tomahawk-player/tomahawk.git
synced 2025-08-04 13:17:34 +02:00
Centralize plugin loading logic in Tomahawk::Utils::PluginLoader
This commit is contained in:
committed by
Teo Mrnjavac
parent
93bbf44669
commit
d965783a53
@@ -35,9 +35,6 @@
|
|||||||
#include "TomahawkSettings.h"
|
#include "TomahawkSettings.h"
|
||||||
#include "LocalConfigStorage.h"
|
#include "LocalConfigStorage.h"
|
||||||
|
|
||||||
#include <QLibrary>
|
|
||||||
#include <QDir>
|
|
||||||
#include <QPluginLoader>
|
|
||||||
#include <QCoreApplication>
|
#include <QCoreApplication>
|
||||||
#include <QSet>
|
#include <QSet>
|
||||||
#include <QTimer>
|
#include <QTimer>
|
||||||
@@ -91,7 +88,7 @@ AccountManager::init()
|
|||||||
|
|
||||||
connect( TomahawkSettings::instance(), SIGNAL( changed() ), SLOT( onSettingsChanged() ) );
|
connect( TomahawkSettings::instance(), SIGNAL( changed() ), SLOT( onSettingsChanged() ) );
|
||||||
|
|
||||||
loadPluginFactories( Tomahawk::Utils::PluginLoader( "account" ).pluginPaths() );
|
loadPluginFactories();
|
||||||
|
|
||||||
// We include the resolver factory manually, not in a plugin
|
// We include the resolver factory manually, not in a plugin
|
||||||
ResolverAccountFactory* f = new ResolverAccountFactory();
|
ResolverAccountFactory* f = new ResolverAccountFactory();
|
||||||
@@ -103,15 +100,20 @@ AccountManager::init()
|
|||||||
|
|
||||||
|
|
||||||
void
|
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 ) )
|
AccountFactory* accountfactory = qobject_cast<AccountFactory*>( plugin );
|
||||||
continue;
|
if ( accountfactory )
|
||||||
|
{
|
||||||
tDebug() << Q_FUNC_INFO << "Trying to load plugin:" << fileName;
|
tDebug() << Q_FUNC_INFO << "Loaded plugin factory:" << plugins.key( plugin ) << accountfactory->factoryId() << accountfactory->prettyName();
|
||||||
loadPluginFactory( fileName );
|
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
|
void
|
||||||
AccountManager::enableAccount( Account* account )
|
AccountManager::enableAccount( Account* account )
|
||||||
{
|
{
|
||||||
@@ -250,21 +229,10 @@ AccountManager::loadFromConfig()
|
|||||||
ConfigStorage* localCS = new LocalConfigStorage( this );
|
ConfigStorage* localCS = new LocalConfigStorage( this );
|
||||||
m_configStorageById.insert( localCS->id(), localCS );
|
m_configStorageById.insert( localCS->id(), localCS );
|
||||||
|
|
||||||
QStringList configStoragePlugins = Tomahawk::Utils::PluginLoader( "configstorage" ).pluginPaths();
|
QList< QObject* > configStoragePlugins = Tomahawk::Utils::PluginLoader( "configstorage" ).loadPlugins().values();
|
||||||
foreach( const QString& pluginPath, configStoragePlugins )
|
foreach( QObject* plugin, configStoragePlugins )
|
||||||
{
|
{
|
||||||
QPluginLoader loader;
|
ConfigStorage* cs = qobject_cast< ConfigStorage* >( plugin );
|
||||||
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() );
|
|
||||||
if ( !cs )
|
if ( !cs )
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
|
@@ -125,11 +125,9 @@ private slots:
|
|||||||
void onSettingsChanged();
|
void onSettingsChanged();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void loadPluginFactories( const QStringList &paths );
|
void loadPluginFactories();
|
||||||
void loadPluginFactory( const QString &path );
|
|
||||||
QString factoryFromId( const QString& accountId ) const;
|
QString factoryFromId( const QString& accountId ) const;
|
||||||
|
|
||||||
|
|
||||||
Account* loadPlugin( const QString& accountId );
|
Account* loadPlugin( const QString& accountId );
|
||||||
void hookupAccount( Account* ) const;
|
void hookupAccount( Account* ) const;
|
||||||
|
|
||||||
|
@@ -32,13 +32,9 @@
|
|||||||
#include "utils/PluginLoader.h"
|
#include "utils/PluginLoader.h"
|
||||||
#include "Source.h"
|
#include "Source.h"
|
||||||
|
|
||||||
|
|
||||||
#include <QCoreApplication>
|
#include <QCoreApplication>
|
||||||
#include <QDir>
|
|
||||||
#include <QLibrary>
|
|
||||||
#include <QNetworkConfiguration>
|
#include <QNetworkConfiguration>
|
||||||
#include <QNetworkProxy>
|
#include <QNetworkProxy>
|
||||||
#include <QPluginLoader>
|
|
||||||
|
|
||||||
namespace Tomahawk
|
namespace Tomahawk
|
||||||
{
|
{
|
||||||
@@ -85,7 +81,7 @@ InfoSystemWorker::init( Tomahawk::InfoSystem::InfoSystemCache* cache )
|
|||||||
m_shortLinksWaiting = 0;
|
m_shortLinksWaiting = 0;
|
||||||
m_cache = cache;
|
m_cache = cache;
|
||||||
|
|
||||||
loadInfoPlugins( Tomahawk::Utils::PluginLoader( "infoplugin" ).pluginPaths() );
|
loadInfoPlugins();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -169,37 +165,22 @@ InfoSystemWorker::removeInfoPlugin( Tomahawk::InfoSystem::InfoPluginPtr plugin )
|
|||||||
|
|
||||||
|
|
||||||
void
|
void
|
||||||
InfoSystemWorker::loadInfoPlugins( const QStringList& pluginPaths )
|
InfoSystemWorker::loadInfoPlugins()
|
||||||
{
|
{
|
||||||
tDebug() << Q_FUNC_INFO << "Attempting to load the following plugin paths:" << pluginPaths;
|
QHash< QString, QObject* > plugins = Tomahawk::Utils::PluginLoader( "infoplugin" ).loadPlugins();
|
||||||
|
foreach ( QObject* plugin, plugins.values() )
|
||||||
if ( pluginPaths.isEmpty() )
|
|
||||||
return;
|
|
||||||
|
|
||||||
foreach ( const QString fileName, pluginPaths )
|
|
||||||
{
|
{
|
||||||
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 );
|
InfoPlugin* infoPlugin = qobject_cast< InfoPlugin* >( plugin );
|
||||||
if ( infoPlugin )
|
if ( infoPlugin )
|
||||||
{
|
{
|
||||||
tDebug() << Q_FUNC_INFO << "Loaded info plugin:" << loader.fileName();
|
tDebug() << Q_FUNC_INFO << "Loaded info plugin:" << plugins.key( plugin );
|
||||||
infoPlugin->setFriendlyName( loader.fileName() );
|
infoPlugin->setFriendlyName( plugins.key( plugin ) );
|
||||||
addInfoPlugin( InfoPluginPtr( infoPlugin ) );
|
addInfoPlugin( InfoPluginPtr( infoPlugin ) );
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
tDebug() << Q_FUNC_INFO << "Loaded invalid plugin:" << loader.fileName();
|
{
|
||||||
|
tDebug() << Q_FUNC_INFO << "Loaded invalid plugin:" << plugins.key( plugin );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -68,7 +68,7 @@ public slots:
|
|||||||
|
|
||||||
void addInfoPlugin( Tomahawk::InfoSystem::InfoPluginPtr plugin );
|
void addInfoPlugin( Tomahawk::InfoSystem::InfoPluginPtr plugin );
|
||||||
void removeInfoPlugin( Tomahawk::InfoSystem::InfoPluginPtr plugin );
|
void removeInfoPlugin( Tomahawk::InfoSystem::InfoPluginPtr plugin );
|
||||||
void loadInfoPlugins( const QStringList &pluginPaths );
|
void loadInfoPlugins();
|
||||||
|
|
||||||
void getShortUrl( Tomahawk::InfoSystem::InfoPushData data );
|
void getShortUrl( Tomahawk::InfoSystem::InfoPushData data );
|
||||||
void shortLinkReady( QUrl longUrl, QUrl shortUrl, QVariant callbackObj );
|
void shortLinkReady( QUrl longUrl, QUrl shortUrl, QVariant callbackObj );
|
||||||
|
@@ -29,6 +29,8 @@
|
|||||||
|
|
||||||
#include <QDir>
|
#include <QDir>
|
||||||
#include <QCoreApplication>
|
#include <QCoreApplication>
|
||||||
|
#include <QLibrary>
|
||||||
|
#include <QPluginLoader>
|
||||||
|
|
||||||
#include "DllMacro.h"
|
#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 >
|
const QList< QDir >
|
||||||
PluginLoader::pluginDirs()
|
PluginLoader::pluginDirs()
|
||||||
{
|
{
|
||||||
|
@@ -38,12 +38,14 @@ public:
|
|||||||
PluginLoader( const QString& type );
|
PluginLoader( const QString& type );
|
||||||
virtual ~PluginLoader();
|
virtual ~PluginLoader();
|
||||||
|
|
||||||
|
const QHash< QString, QObject* > loadPlugins() const;
|
||||||
|
|
||||||
|
private:
|
||||||
const QStringList pluginFilenames( const QString& name = "*" ) const;
|
const QStringList pluginFilenames( const QString& name = "*" ) const;
|
||||||
const QStringList pluginPaths( const QString& name = "*" ) const;
|
const QStringList pluginPaths( const QString& name = "*" ) const;
|
||||||
|
|
||||||
static const QList< QDir > pluginDirs();
|
static const QList< QDir > pluginDirs();
|
||||||
|
|
||||||
private:
|
|
||||||
Q_DECLARE_PRIVATE( PluginLoader );
|
Q_DECLARE_PRIVATE( PluginLoader );
|
||||||
PluginLoaderPrivate* d_ptr;
|
PluginLoaderPrivate* d_ptr;
|
||||||
};
|
};
|
||||||
|
Reference in New Issue
Block a user