diff --git a/src/libtomahawk/SourceList.cpp b/src/libtomahawk/SourceList.cpp index 19109df8e..141b76ac9 100644 --- a/src/libtomahawk/SourceList.cpp +++ b/src/libtomahawk/SourceList.cpp @@ -138,6 +138,7 @@ SourceList::add( const source_ptr& source ) connect( source.data(), SIGNAL( syncedWithDatabase() ), SLOT( sourceSynced() ) ); collection_ptr coll( new RemoteCollection( source ) ); + coll->setWeakRef( coll.toWeakRef() ); source->addCollection( coll ); connect( source.data(), SIGNAL( latchedOn( Tomahawk::source_ptr ) ), this, SLOT( latchedOn( Tomahawk::source_ptr ) ) ); diff --git a/src/libtomahawk/Typedefs.h b/src/libtomahawk/Typedefs.h index f4cbc3d31..60d409118 100644 --- a/src/libtomahawk/Typedefs.h +++ b/src/libtomahawk/Typedefs.h @@ -58,6 +58,7 @@ namespace Tomahawk class ScriptObject; typedef QSharedPointer collection_ptr; + typedef QWeakPointer collection_wptr; typedef QSharedPointer playlist_ptr; typedef QSharedPointer plentry_ptr; typedef QSharedPointer playlistinterface_ptr; diff --git a/src/libtomahawk/collection/Collection.cpp b/src/libtomahawk/collection/Collection.cpp index b230d7bd3..87802c151 100644 --- a/src/libtomahawk/collection/Collection.cpp +++ b/src/libtomahawk/collection/Collection.cpp @@ -53,6 +53,20 @@ Collection::~Collection() } +void +Collection::setWeakRef( const collection_wptr& weakRef ) +{ + m_ownRef = weakRef; +} + + +const collection_wptr +Collection::weakRef() const +{ + return m_ownRef; +} + + QString Collection::name() const { diff --git a/src/libtomahawk/collection/Collection.h b/src/libtomahawk/collection/Collection.h index 60ee3bab2..0fee6e62a 100644 --- a/src/libtomahawk/collection/Collection.h +++ b/src/libtomahawk/collection/Collection.h @@ -53,6 +53,9 @@ public: Collection( const source_ptr& source, const QString& name, QObject* parent = 0 ); virtual ~Collection(); + void setWeakRef( const collection_wptr& weakRef ); + const collection_wptr weakRef() const; + enum BackendType { NullCollectionType = 0, @@ -133,7 +136,9 @@ private slots: private: bool m_changed; + collection_wptr m_ownRef; source_ptr m_source; + QHash< QString, Tomahawk::playlist_ptr > m_playlists; QHash< QString, Tomahawk::dynplaylist_ptr > m_autoplaylists; QHash< QString, Tomahawk::dynplaylist_ptr > m_stations; diff --git a/src/libtomahawk/resolvers/JSResolver.cpp b/src/libtomahawk/resolvers/JSResolver.cpp index 59e819442..efea147fb 100644 --- a/src/libtomahawk/resolvers/JSResolver.cpp +++ b/src/libtomahawk/resolvers/JSResolver.cpp @@ -783,6 +783,9 @@ JSResolver::loadCollections() m_collections.clear(); // at this point we assume that all the tracks browsable through a resolver belong to the local source Tomahawk::ScriptCollection* sc = new Tomahawk::ScriptCollection( collectionInfo[ "id" ].toString(), SourceList::instance()->getLocal(), this ); + Tomahawk::collection_ptr collection( sc ); + collection->setWeakRef( collection.toWeakRef() ); + sc->setServiceName( prettyname ); sc->setDescription( desc ); @@ -805,8 +808,6 @@ JSResolver::loadCollections() sc->setIcon( QIcon( iconPixmap ) ); } - Tomahawk::collection_ptr collection( sc ); - m_collections.insert( collection->name(), collection ); emit collectionAdded( collection ); diff --git a/src/libtomahawk/resolvers/ScriptCollection.cpp b/src/libtomahawk/resolvers/ScriptCollection.cpp index f19e4d814..c9cc57f49 100644 --- a/src/libtomahawk/resolvers/ScriptCollection.cpp +++ b/src/libtomahawk/resolvers/ScriptCollection.cpp @@ -145,11 +145,7 @@ ScriptCollection::description() const Tomahawk::ArtistsRequest* ScriptCollection::requestArtists() { - Tomahawk::collection_ptr thisCollection = m_resolver->collections().value( name() ); - if ( thisCollection->name() != name() ) - return 0; - - Tomahawk::ArtistsRequest* cmd = new ScriptCommand_AllArtists( thisCollection ); + Tomahawk::ArtistsRequest* cmd = new ScriptCommand_AllArtists( weakRef().toStrongRef() ); return cmd; } @@ -158,11 +154,7 @@ ScriptCollection::requestArtists() Tomahawk::AlbumsRequest* ScriptCollection::requestAlbums( const Tomahawk::artist_ptr& artist ) { - Tomahawk::collection_ptr thisCollection = m_resolver->collections().value( name() ); - if ( thisCollection->name() != name() ) - return 0; - - Tomahawk::AlbumsRequest* cmd = new ScriptCommand_AllAlbums( thisCollection, artist ); + Tomahawk::AlbumsRequest* cmd = new ScriptCommand_AllAlbums( weakRef().toStrongRef(), artist ); return cmd; } @@ -171,11 +163,7 @@ ScriptCollection::requestAlbums( const Tomahawk::artist_ptr& artist ) Tomahawk::TracksRequest* ScriptCollection::requestTracks( const Tomahawk::album_ptr& album ) { - Tomahawk::collection_ptr thisCollection = m_resolver->collections().value( name() ); - if ( thisCollection->name() != name() ) - return 0; - - Tomahawk::TracksRequest* cmd = new ScriptCommand_AllTracks( thisCollection, album ); + Tomahawk::TracksRequest* cmd = new ScriptCommand_AllTracks( weakRef().toStrongRef(), album ); return cmd; } diff --git a/src/tomahawk/TomahawkApp.cpp b/src/tomahawk/TomahawkApp.cpp index 3adbba434..e6272f95c 100644 --- a/src/tomahawk/TomahawkApp.cpp +++ b/src/tomahawk/TomahawkApp.cpp @@ -516,6 +516,7 @@ TomahawkApp::initLocalCollection() source_ptr src( new Source( 0, Database::instance()->impl()->dbid() ) ); src->setFriendlyName( tr( "You" ) ); collection_ptr coll( new LocalCollection( src ) ); + coll->setWeakRef( coll.toWeakRef() ); src->addCollection( coll ); SourceList::instance()->setLocal( src );