1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-08-13 17:43:59 +02:00

Use a QHash to avoid a bunch of loops

This commit is contained in:
Leo Franchi
2012-07-17 18:48:35 -04:00
parent 74d6ccd47d
commit 6194517eab
2 changed files with 21 additions and 35 deletions

View File

@@ -553,15 +553,7 @@ SpotifyAccount::subscribeActionTriggered( bool )
return; return;
} }
SpotifyPlaylistInfo* info = 0; SpotifyPlaylistInfo* info = m_allSpotifyPlaylists.value( updater->spotifyId(), 0 );
foreach ( SpotifyPlaylistInfo* ifo, m_allSpotifyPlaylists )
{
if ( ifo->plid == updater->spotifyId() )
{
info = ifo;
break;
}
}
// When we unsubscribe, all playlists is resent // When we unsubscribe, all playlists is resent
// and we will could loose the SpotifyPlaylistInfo, but all we really need is the id // and we will could loose the SpotifyPlaylistInfo, but all we really need is the id
@@ -648,22 +640,14 @@ SpotifyAccount::syncActionTriggered( bool )
} }
else else
{ {
SpotifyPlaylistInfo* info = 0; SpotifyPlaylistInfo* info = m_allSpotifyPlaylists.value( updater->spotifyId(), 0 );
foreach ( SpotifyPlaylistInfo* ifo, m_allSpotifyPlaylists )
{
if ( ifo->plid == updater->spotifyId() )
{
info = ifo;
break;
}
}
Q_ASSERT( info ); Q_ASSERT( info );
if ( info ) if ( info )
info->sync = !updater->sync(); info->sync = !updater->sync();
if ( m_configWidget.data() ) if ( m_configWidget.data() )
m_configWidget.data()->setPlaylists( m_allSpotifyPlaylists ); m_configWidget.data()->setPlaylists( m_allSpotifyPlaylists.values() );
if ( !updater->sync() ) if ( !updater->sync() )
{ {
@@ -737,7 +721,7 @@ SpotifyAccount::resolverMessage( const QString &msgType, const QVariantMap &msg
else if ( msgType == "allPlaylists" ) else if ( msgType == "allPlaylists" )
{ {
const QVariantList playlists = msg.value( "playlists" ).toList(); const QVariantList playlists = msg.value( "playlists" ).toList();
qDeleteAll( m_allSpotifyPlaylists ); qDeleteAll( m_allSpotifyPlaylists.values() );
m_allSpotifyPlaylists.clear(); m_allSpotifyPlaylists.clear();
foreach ( const QVariant& playlist, playlists ) foreach ( const QVariant& playlist, playlists )
@@ -755,12 +739,12 @@ SpotifyAccount::resolverMessage( const QString &msgType, const QVariantMap &msg
continue; continue;
} }
m_allSpotifyPlaylists << new SpotifyPlaylistInfo( name, plid, revid, sync, subscribed ); registerPlaylistInfo( new SpotifyPlaylistInfo( name, plid, revid, sync, subscribed ) );
} }
if ( !m_configWidget.isNull() ) if ( !m_configWidget.isNull() )
{ {
m_configWidget.data()->setPlaylists( m_allSpotifyPlaylists ); m_configWidget.data()->setPlaylists( m_allSpotifyPlaylists.values() );
} }
} }
else if ( msgType == "tracksAdded" ) else if ( msgType == "tracksAdded" )
@@ -936,7 +920,7 @@ SpotifyAccount::clearUser( bool permanentlyDelete )
m_updaters.clear(); m_updaters.clear();
qDeleteAll( m_allSpotifyPlaylists ); qDeleteAll( m_allSpotifyPlaylists.values() );
m_allSpotifyPlaylists.clear(); m_allSpotifyPlaylists.clear();
m_qidToSlotMap.clear(); m_qidToSlotMap.clear();
@@ -964,7 +948,7 @@ SpotifyAccount::configurationWidget()
m_configWidget = QWeakPointer< SpotifyAccountConfig >( new SpotifyAccountConfig( this ) ); m_configWidget = QWeakPointer< SpotifyAccountConfig >( new SpotifyAccountConfig( this ) );
connect( m_configWidget.data(), SIGNAL( login( QString,QString ) ), this, SLOT( login( QString,QString ) ) ); connect( m_configWidget.data(), SIGNAL( login( QString,QString ) ), this, SLOT( login( QString,QString ) ) );
connect( m_configWidget.data(), SIGNAL( logout() ), this, SLOT( logout() ) ); connect( m_configWidget.data(), SIGNAL( logout() ), this, SLOT( logout() ) );
m_configWidget.data()->setPlaylists( m_allSpotifyPlaylists ); m_configWidget.data()->setPlaylists( m_allSpotifyPlaylists.values() );
} }
if ( m_spotifyResolver.isNull() || !m_spotifyResolver.data()->running() ) if ( m_spotifyResolver.isNull() || !m_spotifyResolver.data()->running() )
@@ -1021,7 +1005,7 @@ SpotifyAccount::saveConfig()
setConfiguration( config ); setConfiguration( config );
m_configWidget.data()->saveSettings(); m_configWidget.data()->saveSettings();
foreach ( SpotifyPlaylistInfo* pl, m_allSpotifyPlaylists ) foreach ( SpotifyPlaylistInfo* pl, m_allSpotifyPlaylists.values() )
{ {
// qDebug() << "Checking changed state:" << pl->changed << pl->name << pl->sync; // qDebug() << "Checking changed state:" << pl->changed << pl->name << pl->sync;
if ( pl->changed ) if ( pl->changed )
@@ -1224,14 +1208,16 @@ SpotifyAccount::registerUpdaterForPlaylist( const QString& plId, SpotifyPlaylist
void void
SpotifyAccount::registerPlaylistInfo( const QString& name, const QString& plid, const QString &revid, const bool sync, const bool subscribed ) SpotifyAccount::registerPlaylistInfo( const QString& name, const QString& plid, const QString &revid, const bool sync, const bool subscribed )
{ {
m_allSpotifyPlaylists << new SpotifyPlaylistInfo( name, plid, revid, sync, subscribed ); m_allSpotifyPlaylists[ plid ] = new SpotifyPlaylistInfo( name, plid, revid, sync, subscribed );
} }
void void
SpotifyAccount::registerPlaylistInfo( SpotifyPlaylistInfo* info ) SpotifyAccount::registerPlaylistInfo( SpotifyPlaylistInfo* info )
{ {
m_allSpotifyPlaylists << info; m_allSpotifyPlaylists[ info->plid ] = info;
} }
void void
SpotifyAccount::unregisterUpdater( const QString& plid ) SpotifyAccount::unregisterUpdater( const QString& plid )
{ {
@@ -1300,14 +1286,14 @@ SpotifyAccount::loadPlaylists()
void void
SpotifyAccount::setSyncForPlaylist( const QString& spotifyPlaylistId, bool sync ) SpotifyAccount::setSyncForPlaylist( const QString& spotifyPlaylistId, bool sync )
{ {
foreach ( SpotifyPlaylistInfo* info, m_allSpotifyPlaylists ) SpotifyPlaylistInfo* info = m_allSpotifyPlaylists.value( spotifyPlaylistId, 0 );
{
if ( info->plid == spotifyPlaylistId ) if ( info )
info->sync = sync; info->sync = sync;
}
if ( !m_configWidget.isNull() ) if ( !m_configWidget.isNull() )
m_configWidget.data()->setPlaylists( m_allSpotifyPlaylists ); m_configWidget.data()->setPlaylists( m_allSpotifyPlaylists.values() );
} }

View File

@@ -166,7 +166,7 @@ private:
QMap<QString, QVariant > m_qidToExtraData; QMap<QString, QVariant > m_qidToExtraData;
// List of synced spotify playlists in config UI // List of synced spotify playlists in config UI
QList< SpotifyPlaylistInfo* > m_allSpotifyPlaylists; QHash< QString, SpotifyPlaylistInfo* > m_allSpotifyPlaylists;
QHash< QString, SpotifyPlaylistUpdater* > m_updaters; QHash< QString, SpotifyPlaylistUpdater* > m_updaters;
QHash< QString, playlist_ptr > m_waitingForCreateReply; QHash< QString, playlist_ptr > m_waitingForCreateReply;