1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-08-16 19:14:06 +02:00

factory-ize the playlist updater loading

This commit is contained in:
Leo Franchi
2012-03-18 15:45:32 -04:00
parent b2612f9189
commit 6de0656794
4 changed files with 54 additions and 5 deletions

View File

@@ -18,10 +18,19 @@
#include "PlaylistUpdaterInterface.h" #include "PlaylistUpdaterInterface.h"
#include "tomahawksettings.h" #include "tomahawksettings.h"
#include "XspfUpdater.h"
using namespace Tomahawk; using namespace Tomahawk;
QMap< QString, PlaylistUpdaterFactory* > PlaylistUpdaterInterface::s_factories = QMap< QString, PlaylistUpdaterFactory* >();
void
PlaylistUpdaterInterface::registerUpdaterFactory( PlaylistUpdaterFactory* f )
{
s_factories[ f->type() ] = f;
}
PlaylistUpdaterInterface* PlaylistUpdaterInterface*
PlaylistUpdaterInterface::loadForPlaylist( const playlist_ptr& pl ) PlaylistUpdaterInterface::loadForPlaylist( const playlist_ptr& pl )
{ {
@@ -32,10 +41,17 @@ PlaylistUpdaterInterface::loadForPlaylist( const playlist_ptr& pl )
// Ok, we have one we can try to load // Ok, we have one we can try to load
const QString type = s->value( QString( "%1/type" ).arg( key ) ).toString(); const QString type = s->value( QString( "%1/type" ).arg( key ) ).toString();
PlaylistUpdaterInterface* updater = 0; PlaylistUpdaterInterface* updater = 0;
if ( type == "xspf" )
updater = new XspfUpdater( pl );
// You forgot to register your new updater type with the factory above. 00ps. if ( !s_factories.contains( type ) )
{
Q_ASSERT( false );
// You forgot to register your new updater type with the factory....
return 0;
}
updater = s_factories[ type ]->create( pl );
if ( !updater ) if ( !updater )
{ {
Q_ASSERT( false ); Q_ASSERT( false );

View File

@@ -22,7 +22,9 @@
#include "dllmacro.h" #include "dllmacro.h"
#include "typedefs.h" #include "typedefs.h"
#include "playlist.h" #include "playlist.h"
#include <QTimer> #include <QTimer>
#include <QMutex>
namespace Tomahawk namespace Tomahawk
{ {
@@ -32,6 +34,8 @@ namespace Tomahawk
* Default is auto-updating. * Default is auto-updating.
*/ */
class PlaylistUpdaterFactory;
class DLLEXPORT PlaylistUpdaterInterface : public QObject class DLLEXPORT PlaylistUpdaterInterface : public QObject
{ {
Q_OBJECT Q_OBJECT
@@ -58,6 +62,7 @@ public:
/// updater if one was saved /// updater if one was saved
static PlaylistUpdaterInterface* loadForPlaylist( const playlist_ptr& pl ); static PlaylistUpdaterInterface* loadForPlaylist( const playlist_ptr& pl );
static void registerUpdaterFactory( PlaylistUpdaterFactory* f );
public slots: public slots:
virtual void updateNow() {} virtual void updateNow() {}
@@ -73,6 +78,19 @@ private:
QTimer* m_timer; QTimer* m_timer;
bool m_autoUpdate; bool m_autoUpdate;
playlist_ptr m_playlist; playlist_ptr m_playlist;
static QMap< QString, PlaylistUpdaterFactory* > s_factories;
};
class DLLEXPORT PlaylistUpdaterFactory
{
public:
PlaylistUpdaterFactory() {}
virtual ~PlaylistUpdaterFactory() {}
virtual QString type() const = 0;
virtual PlaylistUpdaterInterface* create( const playlist_ptr& ) = 0;
}; };
} }

View File

@@ -20,13 +20,15 @@
#define XSPFUPDATER_H #define XSPFUPDATER_H
#include "PlaylistUpdaterInterface.h" #include "PlaylistUpdaterInterface.h"
#include "dllmacro.h"
class QTimer; class QTimer;
namespace Tomahawk namespace Tomahawk
{ {
class XspfUpdater : public PlaylistUpdaterInterface
class DLLEXPORT XspfUpdater : public PlaylistUpdaterInterface
{ {
Q_OBJECT Q_OBJECT
public: public:
@@ -52,6 +54,15 @@ private:
QString m_url; QString m_url;
}; };
class DLLEXPORT XspfUpdaterFactory : public PlaylistUpdaterFactory
{
public:
XspfUpdaterFactory() {}
virtual QString type() const { return "type"; }
virtual PlaylistUpdaterInterface* create( const playlist_ptr& pl ) { return new XspfUpdater( pl ); }
};
} }
#endif // XSPFUPDATER_H #endif // XSPFUPDATER_H

View File

@@ -41,6 +41,7 @@
#include "playlist/dynamic/GeneratorFactory.h" #include "playlist/dynamic/GeneratorFactory.h"
#include "playlist/dynamic/echonest/EchonestGenerator.h" #include "playlist/dynamic/echonest/EchonestGenerator.h"
#include "playlist/dynamic/database/DatabaseGenerator.h" #include "playlist/dynamic/database/DatabaseGenerator.h"
#include "playlist/XspfUpdater.h"
#include "network/servent.h" #include "network/servent.h"
#include "web/api_v1.h" #include "web/api_v1.h"
#include "sourcelist.h" #include "sourcelist.h"
@@ -288,6 +289,9 @@ TomahawkApp::init()
// Set up echonest catalog synchronizer // Set up echonest catalog synchronizer
Tomahawk::EchonestCatalogSynchronizer::instance(); Tomahawk::EchonestCatalogSynchronizer::instance();
PlaylistUpdaterInterface::registerUpdaterFactory( new XspfUpdaterFactory );
PlaylistUpdaterInterface::registerUpdaterFactory( new SpotifyUpdaterFactory );
#ifndef ENABLE_HEADLESS #ifndef ENABLE_HEADLESS
// Make sure to init GAM in the gui thread // Make sure to init GAM in the gui thread
GlobalActionManager::instance(); GlobalActionManager::instance();