From 14fd0424a1a8ae35f44b99479d107c22a6eeea18 Mon Sep 17 00:00:00 2001 From: Jeff Mitchell Date: Sun, 15 Jan 2012 14:51:16 -0500 Subject: [PATCH] Working query-stores-its-own-pointer behavior --- src/libtomahawk/query.cpp | 26 +++++++++----------------- src/libtomahawk/query.h | 10 +++++++--- 2 files changed, 16 insertions(+), 20 deletions(-) diff --git a/src/libtomahawk/query.cpp b/src/libtomahawk/query.cpp index ed3852eca..04daebaaa 100644 --- a/src/libtomahawk/query.cpp +++ b/src/libtomahawk/query.cpp @@ -36,10 +36,6 @@ 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 ) { @@ -47,11 +43,11 @@ 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 ); + q->setWeakRef( q.toWeakRef() ); if ( autoResolve ) Pipeline::instance()->resolve( q ); + return q; } @@ -59,10 +55,13 @@ Query::get( const QString& artist, const QString& track, const QString& album, c query_ptr Query::get( const QString& query, const QID& qid ) { + query_ptr q = query_ptr( new Query( query, qid ) ); + q->setWeakRef( q.toWeakRef() ); if ( !qid.isEmpty() ) Pipeline::instance()->resolve( q ); + return q; } @@ -102,14 +101,7 @@ Query::Query( const QString& query, const QID& qid ) Query::~Query() { - QMutexLocker lock( &s_mutex ); - if ( !id().isEmpty() ) - { - if ( s_queries.contains( id() ) ) - { - s_queries.remove( id() ); - } - } + m_ownRef.clear(); } @@ -206,9 +198,9 @@ Query::refreshResults() if ( m_resolveFinished ) { m_resolveFinished = false; - QMutexLocker lock( &s_mutex ); - if ( s_queries.contains( id() ) && !s_queries[ id() ].isNull() ) - Pipeline::instance()->resolve( s_queries.value( id() ) ); + query_ptr q = m_ownRef.toStrongRef(); + if ( q ) + Pipeline::instance()->resolve( q ); } } diff --git a/src/libtomahawk/query.h b/src/libtomahawk/query.h index 610c28d26..57c5b1d22 100644 --- a/src/libtomahawk/query.h +++ b/src/libtomahawk/query.h @@ -51,9 +51,6 @@ public: static query_ptr get( const QString& artist, const QString& track, const QString& album, const QID& qid = QString(), bool autoResolve = true ); static query_ptr get( const QString& query, const QID& qid ); - explicit Query( const QString& artist, const QString& track, const QString& album, const QID& qid, bool autoResolve ); - explicit Query( const QString& query, const QID& qid ); - virtual ~Query(); /// returns list of all results so far @@ -105,6 +102,9 @@ public: void setResolveFinished( bool resolved ) { m_resolveFinished = resolved; } void setPlayedBy( const Tomahawk::source_ptr& source, unsigned int playtime ); + QWeakPointer< Tomahawk::Query > weakRef() { return m_ownRef; } + void setWeakRef( QWeakPointer< Tomahawk::Query > weakRef ) { m_ownRef = weakRef; } + signals: void resultsAdded( const QList& ); void resultsRemoved( const Tomahawk::result_ptr& ); @@ -137,6 +137,8 @@ private slots: private: Query(); + explicit Query( const QString& artist, const QString& track, const QString& album, const QID& qid, bool autoResolve ); + explicit Query( const QString& query, const QID& qid ); void init(); @@ -172,6 +174,8 @@ private: QList< QWeakPointer< Tomahawk::Resolver > > m_resolvers; mutable QMutex m_mutex; + + QWeakPointer< Tomahawk::Query > m_ownRef; }; }; //ns