diff --git a/src/libtomahawk/playlist/PlaylistUpdaterInterface.cpp b/src/libtomahawk/playlist/PlaylistUpdaterInterface.cpp index c057ceb59..6102538c1 100644 --- a/src/libtomahawk/playlist/PlaylistUpdaterInterface.cpp +++ b/src/libtomahawk/playlist/PlaylistUpdaterInterface.cpp @@ -18,10 +18,19 @@ #include "PlaylistUpdaterInterface.h" #include "tomahawksettings.h" -#include "XspfUpdater.h" using namespace Tomahawk; +QMap< QString, PlaylistUpdaterFactory* > PlaylistUpdaterInterface::s_factories = QMap< QString, PlaylistUpdaterFactory* >(); + +void +PlaylistUpdaterInterface::registerUpdaterFactory( PlaylistUpdaterFactory* f ) +{ + s_factories[ f->type() ] = f; +} + + + PlaylistUpdaterInterface* 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 const QString type = s->value( QString( "%1/type" ).arg( key ) ).toString(); 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 ) { Q_ASSERT( false ); diff --git a/src/libtomahawk/playlist/PlaylistUpdaterInterface.h b/src/libtomahawk/playlist/PlaylistUpdaterInterface.h index fbbc40cfe..c4d053fef 100644 --- a/src/libtomahawk/playlist/PlaylistUpdaterInterface.h +++ b/src/libtomahawk/playlist/PlaylistUpdaterInterface.h @@ -22,7 +22,9 @@ #include "dllmacro.h" #include "typedefs.h" #include "playlist.h" + #include +#include namespace Tomahawk { @@ -32,6 +34,8 @@ namespace Tomahawk * Default is auto-updating. */ +class PlaylistUpdaterFactory; + class DLLEXPORT PlaylistUpdaterInterface : public QObject { Q_OBJECT @@ -58,6 +62,7 @@ public: /// updater if one was saved static PlaylistUpdaterInterface* loadForPlaylist( const playlist_ptr& pl ); + static void registerUpdaterFactory( PlaylistUpdaterFactory* f ); public slots: virtual void updateNow() {} @@ -73,6 +78,19 @@ private: QTimer* m_timer; bool m_autoUpdate; 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; }; } diff --git a/src/libtomahawk/playlist/XspfUpdater.h b/src/libtomahawk/playlist/XspfUpdater.h index c5254d79a..5851cd894 100644 --- a/src/libtomahawk/playlist/XspfUpdater.h +++ b/src/libtomahawk/playlist/XspfUpdater.h @@ -20,13 +20,15 @@ #define XSPFUPDATER_H #include "PlaylistUpdaterInterface.h" +#include "dllmacro.h" class QTimer; namespace Tomahawk { -class XspfUpdater : public PlaylistUpdaterInterface + +class DLLEXPORT XspfUpdater : public PlaylistUpdaterInterface { Q_OBJECT public: @@ -52,6 +54,15 @@ private: 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 diff --git a/src/tomahawkapp.cpp b/src/tomahawkapp.cpp index 8504370b3..15f93115e 100644 --- a/src/tomahawkapp.cpp +++ b/src/tomahawkapp.cpp @@ -41,6 +41,7 @@ #include "playlist/dynamic/GeneratorFactory.h" #include "playlist/dynamic/echonest/EchonestGenerator.h" #include "playlist/dynamic/database/DatabaseGenerator.h" +#include "playlist/XspfUpdater.h" #include "network/servent.h" #include "web/api_v1.h" #include "sourcelist.h" @@ -288,6 +289,9 @@ TomahawkApp::init() // Set up echonest catalog synchronizer Tomahawk::EchonestCatalogSynchronizer::instance(); + PlaylistUpdaterInterface::registerUpdaterFactory( new XspfUpdaterFactory ); + PlaylistUpdaterInterface::registerUpdaterFactory( new SpotifyUpdaterFactory ); + #ifndef ENABLE_HEADLESS // Make sure to init GAM in the gui thread GlobalActionManager::instance();