mirror of
https://github.com/tomahawk-player/tomahawk.git
synced 2025-08-11 08:34:34 +02:00
Centralize plugin finding logic in Tomahawk::Utils::PluginLoader
This commit is contained in:
committed by
Teo Mrnjavac
parent
e80a5ba2e5
commit
93bbf44669
@@ -358,7 +358,7 @@ list(APPEND libSources
|
||||
utils/TomahawkCache.cpp
|
||||
utils/GuiHelpers.cpp
|
||||
utils/WeakObjectHash.cpp
|
||||
|
||||
utils/PluginLoader.cpp
|
||||
|
||||
thirdparty/kdsingleapplicationguard/kdsingleapplicationguard.cpp
|
||||
thirdparty/kdsingleapplicationguard/kdsharedmemorylocker.cpp
|
||||
|
@@ -26,6 +26,7 @@
|
||||
#include "jobview/JobStatusModel.h"
|
||||
#include "utils/Closure.h"
|
||||
#include "utils/Logger.h"
|
||||
#include "utils/PluginLoader.h"
|
||||
|
||||
#include "CredentialsManager.h"
|
||||
#include "config.h"
|
||||
@@ -90,7 +91,7 @@ AccountManager::init()
|
||||
|
||||
connect( TomahawkSettings::instance(), SIGNAL( changed() ), SLOT( onSettingsChanged() ) );
|
||||
|
||||
loadPluginFactories( findPluginFactories() );
|
||||
loadPluginFactories( Tomahawk::Utils::PluginLoader( "account" ).pluginPaths() );
|
||||
|
||||
// We include the resolver factory manually, not in a plugin
|
||||
ResolverAccountFactory* f = new ResolverAccountFactory();
|
||||
@@ -101,85 +102,6 @@ AccountManager::init()
|
||||
}
|
||||
|
||||
|
||||
QList< QDir >
|
||||
AccountManager::findPluginDirs() const
|
||||
{
|
||||
QList< QDir > pluginDirs;
|
||||
|
||||
QDir appDir( qApp->applicationDirPath() );
|
||||
#ifdef Q_WS_MAC
|
||||
if ( appDir.dirName() == "MacOS" )
|
||||
{
|
||||
// Development convenience-hack
|
||||
appDir.cdUp();
|
||||
appDir.cdUp();
|
||||
appDir.cdUp();
|
||||
}
|
||||
#endif
|
||||
|
||||
QDir libDir( CMAKE_INSTALL_PREFIX "/lib" );
|
||||
|
||||
QDir lib64Dir( appDir );
|
||||
lib64Dir.cdUp();
|
||||
lib64Dir.cd( "lib64" );
|
||||
|
||||
pluginDirs << appDir << libDir << lib64Dir << QDir( qApp->applicationDirPath() );
|
||||
return pluginDirs;
|
||||
}
|
||||
|
||||
|
||||
QStringList
|
||||
AccountManager::findPluginFactories()
|
||||
{
|
||||
QStringList paths;
|
||||
|
||||
foreach ( const QDir& pluginDir, findPluginDirs() )
|
||||
{
|
||||
tDebug() << Q_FUNC_INFO << "Checking directory for account plugins:" << pluginDir;
|
||||
foreach ( QString fileName, pluginDir.entryList( QStringList() << "*tomahawk_account_*.so"
|
||||
<< "*tomahawk_account_*.dylib"
|
||||
<< "*tomahawk_account_*.dll", QDir::Files ) )
|
||||
{
|
||||
if ( fileName.startsWith( "libtomahawk_account" ) )
|
||||
{
|
||||
const QString path = pluginDir.absoluteFilePath( fileName );
|
||||
if ( !paths.contains( path ) )
|
||||
paths << path;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return paths;
|
||||
}
|
||||
|
||||
|
||||
QStringList
|
||||
AccountManager::findConfigStoragePlugins()
|
||||
{
|
||||
QStringList paths;
|
||||
|
||||
foreach( const QDir& pluginDir, findPluginDirs() )
|
||||
{
|
||||
tDebug() << Q_FUNC_INFO << "Checking directory for ConfigStorage plugins:" << pluginDir;
|
||||
foreach ( QString fileName, pluginDir.entryList( QStringList() << "*tomahawk_configstorage_*.so"
|
||||
<< "*tomahawk_configstorage_*.dylib"
|
||||
<< "*tomahawk_configstorage_*.dll", QDir::Files ) )
|
||||
{
|
||||
if ( fileName.startsWith( "libtomahawk_configstorage" ) )
|
||||
{
|
||||
const QString path = pluginDir.absoluteFilePath( fileName );
|
||||
if ( !paths.contains( path ) )
|
||||
paths << path;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
tDebug() << Q_FUNC_INFO << "ConfigStorage plugin file paths:" << paths;
|
||||
|
||||
return paths;
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
AccountManager::loadPluginFactories( const QStringList& paths )
|
||||
{
|
||||
@@ -328,7 +250,7 @@ AccountManager::loadFromConfig()
|
||||
ConfigStorage* localCS = new LocalConfigStorage( this );
|
||||
m_configStorageById.insert( localCS->id(), localCS );
|
||||
|
||||
QStringList configStoragePlugins = findConfigStoragePlugins();
|
||||
QStringList configStoragePlugins = Tomahawk::Utils::PluginLoader( "configstorage" ).pluginPaths();
|
||||
foreach( const QString& pluginPath, configStoragePlugins )
|
||||
{
|
||||
QPluginLoader loader;
|
||||
|
@@ -125,14 +125,10 @@ private slots:
|
||||
void onSettingsChanged();
|
||||
|
||||
private:
|
||||
QList< QDir > findPluginDirs() const;
|
||||
|
||||
QStringList findPluginFactories();
|
||||
void loadPluginFactories( const QStringList &paths );
|
||||
void loadPluginFactory( const QString &path );
|
||||
QString factoryFromId( const QString& accountId ) const;
|
||||
|
||||
QStringList findConfigStoragePlugins();
|
||||
|
||||
Account* loadPlugin( const QString& accountId );
|
||||
void hookupAccount( Account* ) const;
|
||||
|
@@ -27,6 +27,9 @@
|
||||
#include "GlobalActionManager.h"
|
||||
#include "InfoSystemCache.h"
|
||||
#include "PlaylistEntry.h"
|
||||
#include "utils/TomahawkUtils.h"
|
||||
#include "utils/Logger.h"
|
||||
#include "utils/PluginLoader.h"
|
||||
#include "Source.h"
|
||||
|
||||
|
||||
@@ -82,7 +85,7 @@ InfoSystemWorker::init( Tomahawk::InfoSystem::InfoSystemCache* cache )
|
||||
m_shortLinksWaiting = 0;
|
||||
m_cache = cache;
|
||||
|
||||
loadInfoPlugins( findInfoPlugins() );
|
||||
loadInfoPlugins( Tomahawk::Utils::PluginLoader( "infoplugin" ).pluginPaths() );
|
||||
}
|
||||
|
||||
|
||||
@@ -165,48 +168,6 @@ InfoSystemWorker::removeInfoPlugin( Tomahawk::InfoSystem::InfoPluginPtr plugin )
|
||||
}
|
||||
|
||||
|
||||
QStringList
|
||||
InfoSystemWorker::findInfoPlugins()
|
||||
{
|
||||
QStringList paths;
|
||||
QList< QDir > pluginDirs;
|
||||
|
||||
QDir appDir( qApp->applicationDirPath() );
|
||||
#ifdef Q_WS_MAC
|
||||
if ( appDir.dirName() == "MacOS" )
|
||||
{
|
||||
// Development convenience-hack
|
||||
appDir.cdUp();
|
||||
appDir.cdUp();
|
||||
appDir.cdUp();
|
||||
}
|
||||
#endif
|
||||
|
||||
QDir libDir( CMAKE_INSTALL_PREFIX "/lib" );
|
||||
|
||||
QDir lib64Dir( appDir );
|
||||
lib64Dir.cdUp();
|
||||
lib64Dir.cd( "lib64" );
|
||||
|
||||
pluginDirs << appDir << libDir << lib64Dir << QDir( qApp->applicationDirPath() );
|
||||
foreach ( const QDir& pluginDir, pluginDirs )
|
||||
{
|
||||
tDebug() << Q_FUNC_INFO << "Checking directory for plugins:" << pluginDir;
|
||||
foreach ( QString fileName, pluginDir.entryList( QStringList() << "*tomahawk_infoplugin_*.so" << "*tomahawk_infoplugin_*.dylib" << "*tomahawk_infoplugin_*.dll", QDir::Files ) )
|
||||
{
|
||||
if ( fileName.startsWith( "libtomahawk_infoplugin" ) )
|
||||
{
|
||||
const QString path = pluginDir.absoluteFilePath( fileName );
|
||||
if ( !paths.contains( path ) )
|
||||
paths << path;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return paths;
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
InfoSystemWorker::loadInfoPlugins( const QStringList& pluginPaths )
|
||||
{
|
||||
|
@@ -68,7 +68,6 @@ public slots:
|
||||
|
||||
void addInfoPlugin( Tomahawk::InfoSystem::InfoPluginPtr plugin );
|
||||
void removeInfoPlugin( Tomahawk::InfoSystem::InfoPluginPtr plugin );
|
||||
QStringList findInfoPlugins();
|
||||
void loadInfoPlugins( const QStringList &pluginPaths );
|
||||
|
||||
void getShortUrl( Tomahawk::InfoSystem::InfoPushData data );
|
||||
|
136
src/libtomahawk/utils/PluginLoader.cpp
Normal file
136
src/libtomahawk/utils/PluginLoader.cpp
Normal file
@@ -0,0 +1,136 @@
|
||||
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
|
||||
*
|
||||
* Copyright 2010-2011, Christian Muehlhaeuser <muesli@tomahawk-player.org>
|
||||
* Copyright 2010-2012, Leo Franchi <lfranchi@kde.org>
|
||||
* Copyright 2010-2011, Jeff Mitchell <jeff@tomahawk-player.org>
|
||||
* Copyright 2013, Teo Mrnjavac <teo@kde.org>
|
||||
* Copyright 2013, Dominik Schmidt <domme@tomahawk-player.org>
|
||||
*
|
||||
* Tomahawk is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Tomahawk is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "PluginLoader.h"
|
||||
#include "PluginLoader_p.h"
|
||||
|
||||
#include "config.h"
|
||||
|
||||
#include "utils/Logger.h"
|
||||
|
||||
#include <QDir>
|
||||
#include <QCoreApplication>
|
||||
|
||||
#include "DllMacro.h"
|
||||
|
||||
|
||||
namespace Tomahawk
|
||||
{
|
||||
|
||||
namespace Utils
|
||||
{
|
||||
|
||||
|
||||
PluginLoader::PluginLoader( const QString& type )
|
||||
: d_ptr( new PluginLoaderPrivate( this ) )
|
||||
{
|
||||
Q_D( PluginLoader );
|
||||
|
||||
d->type = type;
|
||||
}
|
||||
|
||||
|
||||
PluginLoader::~PluginLoader()
|
||||
{
|
||||
delete d_ptr;
|
||||
}
|
||||
|
||||
|
||||
const QList< QDir >
|
||||
PluginLoader::pluginDirs()
|
||||
{
|
||||
QList< QDir > pluginDirs;
|
||||
|
||||
QDir appDir( QCoreApplication::instance()->applicationDirPath() );
|
||||
#ifdef Q_WS_MAC
|
||||
if ( appDir.dirName() == "MacOS" )
|
||||
{
|
||||
// Development convenience-hack
|
||||
appDir.cdUp();
|
||||
appDir.cdUp();
|
||||
appDir.cdUp();
|
||||
}
|
||||
#endif
|
||||
|
||||
QDir libDir( CMAKE_INSTALL_PREFIX "/lib" );
|
||||
|
||||
QDir lib64Dir( appDir );
|
||||
lib64Dir.cdUp();
|
||||
lib64Dir.cd( "lib64" );
|
||||
|
||||
pluginDirs << appDir << libDir << lib64Dir << QDir( qApp->applicationDirPath() );
|
||||
return pluginDirs;
|
||||
}
|
||||
|
||||
|
||||
const QStringList
|
||||
PluginLoader::pluginFilenames( const QString& name ) const
|
||||
{
|
||||
//TODO: ifdef!
|
||||
const QStringList extensions = QStringList()
|
||||
<< "so"
|
||||
<< "dll"
|
||||
<< "dylib";
|
||||
|
||||
|
||||
QStringList fileNames;
|
||||
foreach( const QString& extension, extensions )
|
||||
{
|
||||
fileNames << QString("libtomahawk_%1_%2.%3")
|
||||
.arg( d_ptr->type )
|
||||
.arg( name )
|
||||
.arg( extension );
|
||||
}
|
||||
|
||||
return fileNames;
|
||||
}
|
||||
|
||||
|
||||
const QStringList
|
||||
PluginLoader::pluginPaths( const QString& name ) const
|
||||
{
|
||||
const QString type = d_ptr->type;
|
||||
|
||||
QSet< QString > paths;
|
||||
foreach ( const QDir& pluginDir, pluginDirs() )
|
||||
{
|
||||
tDebug() << Q_FUNC_INFO << "Checking directory for" << type << "plugins:" << pluginDir;
|
||||
foreach ( QString fileName, pluginDir.entryList( pluginFilenames( name ), QDir::Files ) )
|
||||
{
|
||||
//TODO: do we really need to check this?!
|
||||
if ( fileName.startsWith( QString( "libtomahawk_%1" ).arg( type ) ) )
|
||||
{
|
||||
const QString path = pluginDir.absoluteFilePath( fileName );
|
||||
paths << path;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
tDebug() << Q_FUNC_INFO << type << "plugin file paths:" << paths;
|
||||
|
||||
return paths.toList();
|
||||
}
|
||||
|
||||
|
||||
} // ns utils
|
||||
|
||||
} // ns Tomahawk
|
55
src/libtomahawk/utils/PluginLoader.h
Normal file
55
src/libtomahawk/utils/PluginLoader.h
Normal file
@@ -0,0 +1,55 @@
|
||||
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
|
||||
*
|
||||
* Copyright 2013, Dominik Schmidt <domme@tomahawk-player.org>
|
||||
*
|
||||
* Tomahawk is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Tomahawk is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef LIBTOMAHAWK_UTILS_PLUGINLOADER_H
|
||||
#define LIBTOMAHAWK_UTILS_PLUGINLOADER_H
|
||||
|
||||
#include <QStringList>
|
||||
|
||||
#include "DllMacro.h"
|
||||
|
||||
class QDir;
|
||||
class PluginLoaderPrivate;
|
||||
|
||||
namespace Tomahawk
|
||||
{
|
||||
|
||||
namespace Utils
|
||||
{
|
||||
|
||||
class DLLEXPORT PluginLoader
|
||||
{
|
||||
public:
|
||||
PluginLoader( const QString& type );
|
||||
virtual ~PluginLoader();
|
||||
|
||||
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;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
#endif // LIBTOMAHAWK_UTILS_PLUGINLOADER_H
|
41
src/libtomahawk/utils/PluginLoader_p.h
Normal file
41
src/libtomahawk/utils/PluginLoader_p.h
Normal file
@@ -0,0 +1,41 @@
|
||||
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
|
||||
*
|
||||
* Copyright 2013, Dominik Schmidt <domme@tomahawk-player.org>
|
||||
*
|
||||
* Tomahawk is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* Tomahawk is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <QString>
|
||||
|
||||
namespace Tomahawk
|
||||
{
|
||||
namespace Utils
|
||||
{
|
||||
class PluginLoader;
|
||||
}
|
||||
}
|
||||
|
||||
class PluginLoaderPrivate
|
||||
{
|
||||
public:
|
||||
PluginLoaderPrivate( Tomahawk::Utils::PluginLoader* q )
|
||||
: q_ptr ( q )
|
||||
{
|
||||
}
|
||||
|
||||
Tomahawk::Utils::PluginLoader* q_ptr;
|
||||
Q_DECLARE_PUBLIC ( Tomahawk::Utils::PluginLoader )
|
||||
|
||||
QString type;
|
||||
};
|
Reference in New Issue
Block a user