1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-03-21 16:29:43 +01:00

Change updaters to use QMultiHash instead of QHash

This commit is contained in:
Leo Franchi 2012-05-01 20:08:16 -04:00
parent 710780f3e9
commit 455d7816e5
3 changed files with 47 additions and 39 deletions

View File

@ -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 ) );
}

View File

@ -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();
}

View File

@ -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 );