diff --git a/src/libtomahawk/TomahawkSettings.cpp b/src/libtomahawk/TomahawkSettings.cpp index 48a662765..248b80915 100644 --- a/src/libtomahawk/TomahawkSettings.cpp +++ b/src/libtomahawk/TomahawkSettings.cpp @@ -42,11 +42,14 @@ operator<<(QDataStream& out, const PlaylistUpdaterInterface::SerializedUpdaters& { out << TOMAHAWK_SETTINGS_VERSION; out << (quint32)updaters.count(); - foreach( const QString& key, updaters.keys() ) + PlaylistUpdaterInterface::SerializedUpdaters::const_iterator iter = updaters.begin(); + int count = 0; + for ( ; iter != updaters.end(); ++iter ) { - PlaylistUpdaterInterface::SerializedUpdater updater = updaters[ key ]; - out << key << updater.type << updater.customData; + out << iter.key() << iter->type << iter->customData; + count++; } + Q_ASSERT( count == updaters.count() ); return out; } @@ -61,13 +64,11 @@ operator>>(QDataStream& in, PlaylistUpdaterInterface::SerializedUpdaters& update for ( uint i = 0; i < count; i++ ) { QString key, type; - bool sync; QVariantHash customData; - qint32 state, userRating; in >> key; in >> type; in >> customData; - updaters[ key ] = PlaylistUpdaterInterface::SerializedUpdater( type, customData ); + updaters.insert( key, PlaylistUpdaterInterface::SerializedUpdater( type, customData ) ); } return in; @@ -504,7 +505,7 @@ TomahawkSettings::doUpgrade( int oldVersion, int newVersion ) extraData[ key ] = value( key ); } - updaters[ playlist ] = PlaylistUpdaterInterface::SerializedUpdater( type, extraData ); + updaters.insert( playlist, PlaylistUpdaterInterface::SerializedUpdater( type, extraData ) ); endGroup(); } @@ -1250,14 +1251,14 @@ TomahawkSettings::setImportXspfPath( const QString& path ) PlaylistUpdaterInterface::SerializedUpdaters TomahawkSettings::playlistUpdaters() const { - return value( "playlist/updaters" ).value< PlaylistUpdaterInterface::SerializedUpdaters >(); + return value( "playlists/updaters" ).value< PlaylistUpdaterInterface::SerializedUpdaters >(); } void TomahawkSettings::setPlaylistUpdaters( const PlaylistUpdaterInterface::SerializedUpdaters& updaters ) { - setValue( "playlist/updaters", QVariant::fromValue< PlaylistUpdaterInterface::SerializedUpdaters >( updaters ) ); + setValue( "playlists/updaters", QVariant::fromValue< PlaylistUpdaterInterface::SerializedUpdaters >( updaters ) ); } diff --git a/src/libtomahawk/playlist/PlaylistUpdaterInterface.cpp b/src/libtomahawk/playlist/PlaylistUpdaterInterface.cpp index e5eb35d6d..2e88c8992 100644 --- a/src/libtomahawk/playlist/PlaylistUpdaterInterface.cpp +++ b/src/libtomahawk/playlist/PlaylistUpdaterInterface.cpp @@ -23,6 +23,14 @@ using namespace Tomahawk; QMap< QString, PlaylistUpdaterFactory* > PlaylistUpdaterInterface::s_factories = QMap< QString, PlaylistUpdaterFactory* >(); + +bool +operator==( const Tomahawk::PlaylistUpdaterInterface::SerializedUpdater& one, const Tomahawk::PlaylistUpdaterInterface::SerializedUpdater& two ) +{ + return one.type == two.type; +} + + void PlaylistUpdaterInterface::registerUpdaterFactory( PlaylistUpdaterFactory* f ) { @@ -31,30 +39,29 @@ PlaylistUpdaterInterface::registerUpdaterFactory( PlaylistUpdaterFactory* f ) -PlaylistUpdaterInterface* +void PlaylistUpdaterInterface::loadForPlaylist( const playlist_ptr& pl ) { TomahawkSettings* s = TomahawkSettings::instance(); - const SerializedUpdaters updaters = s->playlistUpdaters(); - if ( updaters.contains( pl->guid() ) ) + const SerializedUpdaters allUpdaters = s->playlistUpdaters(); + if ( allUpdaters.contains( pl->guid() ) ) { - // Ok, we have one we can try to load - PlaylistUpdaterInterface* updater = 0; - const SerializedUpdater info = updaters[ pl->guid() ]; - - if ( !s_factories.contains( info.type ) ) + // Ok, we have some we can try to load + const SerializedUpdaterList updaters = allUpdaters.values( pl->guid() ); + foreach ( const SerializedUpdater& info, updaters ) { - Q_ASSERT( false ); - // You forgot to register your new updater type with the factory.... - return 0; + if ( !s_factories.contains( info.type ) ) + { + Q_ASSERT( false ); + // You forgot to register your new updater type with the factory.... + continue; + } + + // Updaters register themselves in their constructor + s_factories[ info.type ]->create( pl, info.customData ); } - - updater = s_factories[ info.type ]->create( pl, info.customData ); - return updater; } - - return 0; } @@ -85,14 +92,15 @@ PlaylistUpdaterInterface::save() TomahawkSettings* s = TomahawkSettings::instance(); - SerializedUpdaters updaters = s->playlistUpdaters(); + SerializedUpdaters allUpdaters = s->playlistUpdaters(); + if ( allUpdaters.contains( m_playlist->guid(), SerializedUpdater( type() ) ) ) + allUpdaters.remove( m_playlist->guid(), SerializedUpdater( type() ) ); - SerializedUpdater updater = updaters.value( m_playlist ->guid() ); + SerializedUpdater updater; updater.type = type(); updater.customData = m_extraData; - updaters[ m_playlist->guid() ] = updater; - - s->setPlaylistUpdaters( updaters ); + allUpdaters.insert( m_playlist->guid(), updater ); + s->setPlaylistUpdaters( allUpdaters ); } void @@ -102,13 +110,12 @@ PlaylistUpdaterInterface::remove() return; TomahawkSettings* s = TomahawkSettings::instance(); + SerializedUpdaters allUpdaters = s->playlistUpdaters(); - SerializedUpdaters updaters = s->playlistUpdaters(); - if ( updaters.remove( m_playlist->guid() ) ) - s->setPlaylistUpdaters( updaters ); + if ( allUpdaters.remove( m_playlist->guid(), SerializedUpdater( type() ) ) ) + s->setPlaylistUpdaters( allUpdaters ); aboutToDelete(); - deleteLater(); } diff --git a/src/libtomahawk/playlist/PlaylistUpdaterInterface.h b/src/libtomahawk/playlist/PlaylistUpdaterInterface.h index 3cdac7397..12b59cfa6 100644 --- a/src/libtomahawk/playlist/PlaylistUpdaterInterface.h +++ b/src/libtomahawk/playlist/PlaylistUpdaterInterface.h @@ -48,11 +48,12 @@ public: QString type; QVariantHash customData; - SerializedUpdater( const QString& t, const QVariantHash cd ) : type( t ), customData( cd ) {} + SerializedUpdater( const QString& t, const QVariantHash cd = QVariantHash() ) : type( t ), customData( cd ) {} SerializedUpdater() {} }; - typedef QHash< QString, SerializedUpdater > SerializedUpdaters; + typedef QMultiHash< QString, SerializedUpdater > SerializedUpdaters; + typedef QList< SerializedUpdater > SerializedUpdaterList; explicit PlaylistUpdaterInterface( const playlist_ptr& pl ); @@ -74,9 +75,8 @@ public: playlist_ptr playlist() const { return m_playlist; } - /// If you want to try to load a updater from the settings. Returns a valid - /// updater if one was saved - static PlaylistUpdaterInterface* loadForPlaylist( const playlist_ptr& pl ); + /// If you want to try to load updaters for a playlist + static void loadForPlaylist( const playlist_ptr& pl ); static void registerUpdaterFactory( PlaylistUpdaterFactory* f );