diff --git a/src/libtomahawk/query.cpp b/src/libtomahawk/query.cpp index 9a40151be..1cd8c78a8 100644 --- a/src/libtomahawk/query.cpp +++ b/src/libtomahawk/query.cpp @@ -36,6 +36,9 @@ using namespace Tomahawk; +static QHash< QString, QWeakPointer< Query > > s_queries; +static QMutex s_mutex; + query_ptr Query::get( const QString& artist, const QString& track, const QString& album, const QID& qid, bool autoResolve ) @@ -44,6 +47,8 @@ Query::get( const QString& artist, const QString& track, const QString& album, c autoResolve = false; query_ptr q = query_ptr( new Query( artist, track, album, qid, autoResolve ) ); + QMutexLocker lock( &s_mutex ); + s_queries.insert( q->id(), q ); if ( autoResolve ) Pipeline::instance()->resolve( q ); @@ -97,7 +102,16 @@ Query::Query( const QString& query, const QID& qid ) Query::~Query() { - qDebug() << Q_FUNC_INFO << toString(); + tDebug() << Q_FUNC_INFO << toString(); + + if ( !id().isEmpty() ) + { + QMutexLocker lock( &s_mutex ); + if ( s_queries.contains( id() ) ) + { + s_queries.remove( id() ); + } + } } @@ -166,8 +180,10 @@ Query::addResults( const QList< Tomahawk::result_ptr >& newresults ) void Query::refreshResults() { - setResolveFinished( false ); - Pipeline::instance()->resolve( id() ); + tDebug() << "Re-resolving query:" << toString() << s_queries.contains( id() ); + + m_resolveFinished = false; + Pipeline::instance()->resolve( s_queries.value( id() ) ); } @@ -215,7 +231,7 @@ Query::onResolvingFinished() void Query::onResolverAdded() { - if ( !solved() ) + if ( m_resolveFinished && !solved() ) { refreshResults(); } @@ -225,7 +241,7 @@ Query::onResolverAdded() void Query::onResolverRemoved() { - if ( !solved() ) + if ( m_resolveFinished && !solved() ) { refreshResults(); } @@ -323,49 +339,46 @@ Query::clearResults() void Query::checkResults() { - bool becameSolved = false; - bool becameUnsolved = true; + bool playable = false; + bool solved = false; + { QMutexLocker lock( &m_mutex ); - m_playable = false; - // hook up signals, and check solved status foreach( const result_ptr& rp, m_results ) { if ( rp->score() > 0.0 && rp->collection().isNull() ) { - m_playable = true; + playable = true; } - if ( !rp->collection().isNull() && rp->collection()->source()->isOnline() ) + else if ( !rp->collection().isNull() && rp->collection()->source()->isOnline() ) { - m_playable = true; + playable = true; + } - if ( rp->score() > 0.99 ) - { - becameUnsolved = false; - - if ( !m_solved ) - { - m_solved = true; - becameSolved = true; - } - } + if ( rp->score() > 0.99 ) + { + solved = true; } } } - if ( m_solved && becameUnsolved ) + if ( m_playable && !playable ) { - m_solved = false; - m_resolveFinished = false; - emit solvedStateChanged( false ); - refreshResults(); } - if ( becameSolved ) - emit solvedStateChanged( true ); + if ( m_playable != playable ) + { + m_playable = playable; + emit playableStateChanged( m_playable ); + } + if ( m_solved != solved ) + { + m_solved = solved; + emit solvedStateChanged( m_solved ); + } } diff --git a/src/libtomahawk/query.h b/src/libtomahawk/query.h index 9f754cd74..ebba460b6 100644 --- a/src/libtomahawk/query.h +++ b/src/libtomahawk/query.h @@ -108,6 +108,7 @@ signals: void resultsChanged(); void solvedStateChanged( bool state ); + void playableStateChanged( bool state ); void resolvingFinished( bool hasResults ); public slots: diff --git a/src/libtomahawk/result.cpp b/src/libtomahawk/result.cpp index 0c5e05f13..1d41c7efa 100644 --- a/src/libtomahawk/result.cpp +++ b/src/libtomahawk/result.cpp @@ -31,13 +31,13 @@ using namespace Tomahawk; +static QHash< QString, QWeakPointer< Result > > s_results; +static QMutex s_mutex; + Tomahawk::result_ptr Result::get( const QString& url ) { - static QHash< QString, result_ptr > s_results; - static QMutex s_mutex; - QMutexLocker lock( &s_mutex ); if ( s_results.contains( url ) ) { @@ -68,6 +68,11 @@ Result::Result( const QString& url ) Result::~Result() { + QMutexLocker lock( &s_mutex ); + if ( s_results.contains( m_url ) ) + { + s_results.remove( m_url ); + } }