diff --git a/src/libtomahawk/CMakeLists.txt b/src/libtomahawk/CMakeLists.txt index 21777479a..158b9dbf6 100644 --- a/src/libtomahawk/CMakeLists.txt +++ b/src/libtomahawk/CMakeLists.txt @@ -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 diff --git a/src/libtomahawk/accounts/AccountManager.cpp b/src/libtomahawk/accounts/AccountManager.cpp index ffd8e8513..2654f0b19 100644 --- a/src/libtomahawk/accounts/AccountManager.cpp +++ b/src/libtomahawk/accounts/AccountManager.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; diff --git a/src/libtomahawk/accounts/AccountManager.h b/src/libtomahawk/accounts/AccountManager.h index 4a39323a3..1e24fbc93 100644 --- a/src/libtomahawk/accounts/AccountManager.h +++ b/src/libtomahawk/accounts/AccountManager.h @@ -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; diff --git a/src/libtomahawk/infosystem/InfoSystemWorker.cpp b/src/libtomahawk/infosystem/InfoSystemWorker.cpp index fbe974f2e..2c0967457 100644 --- a/src/libtomahawk/infosystem/InfoSystemWorker.cpp +++ b/src/libtomahawk/infosystem/InfoSystemWorker.cpp @@ -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 ) { diff --git a/src/libtomahawk/infosystem/InfoSystemWorker.h b/src/libtomahawk/infosystem/InfoSystemWorker.h index 96a0ddb8f..65cfbd4ed 100644 --- a/src/libtomahawk/infosystem/InfoSystemWorker.h +++ b/src/libtomahawk/infosystem/InfoSystemWorker.h @@ -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 ); diff --git a/src/libtomahawk/utils/PluginLoader.cpp b/src/libtomahawk/utils/PluginLoader.cpp new file mode 100644 index 000000000..db66d4583 --- /dev/null +++ b/src/libtomahawk/utils/PluginLoader.cpp @@ -0,0 +1,136 @@ +/* === This file is part of Tomahawk Player - === + * + * Copyright 2010-2011, Christian Muehlhaeuser + * Copyright 2010-2012, Leo Franchi + * Copyright 2010-2011, Jeff Mitchell + * Copyright 2013, Teo Mrnjavac + * Copyright 2013, Dominik Schmidt + * + * 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 . + */ + +#include "PluginLoader.h" +#include "PluginLoader_p.h" + +#include "config.h" + +#include "utils/Logger.h" + +#include +#include + +#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 diff --git a/src/libtomahawk/utils/PluginLoader.h b/src/libtomahawk/utils/PluginLoader.h new file mode 100644 index 000000000..484137fae --- /dev/null +++ b/src/libtomahawk/utils/PluginLoader.h @@ -0,0 +1,55 @@ +/* === This file is part of Tomahawk Player - === + * + * Copyright 2013, Dominik Schmidt + * + * 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 . + */ + +#ifndef LIBTOMAHAWK_UTILS_PLUGINLOADER_H +#define LIBTOMAHAWK_UTILS_PLUGINLOADER_H + +#include + +#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 diff --git a/src/libtomahawk/utils/PluginLoader_p.h b/src/libtomahawk/utils/PluginLoader_p.h new file mode 100644 index 000000000..ed34790f7 --- /dev/null +++ b/src/libtomahawk/utils/PluginLoader_p.h @@ -0,0 +1,41 @@ +/* === This file is part of Tomahawk Player - === + * + * Copyright 2013, Dominik Schmidt + * + * 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 . + */ + +#include + +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; +};