From 65741c365881f4032d5c9ace4cea1c6ca179bbb3 Mon Sep 17 00:00:00 2001 From: Teo Mrnjavac Date: Thu, 24 Jan 2013 18:29:17 +0100 Subject: [PATCH] Route (almost) all artists/albums/tracks requests through Collections. --- src/libtomahawk/AlbumPlaylistInterface.cpp | 48 +++++++---- src/libtomahawk/Artist.cpp | 25 ++++-- src/libtomahawk/Artist.h | 3 +- src/libtomahawk/Collection.h | 8 +- .../database/DatabaseCollection.cpp | 80 +++++++++++++++++++ src/libtomahawk/database/DatabaseCollection.h | 9 +++ src/libtomahawk/playlist/TreeModel.cpp | 48 ++++++----- src/libtomahawk/playlist/TreeModel.h | 3 +- .../resolvers/ScriptCollection.cpp | 22 +++++ src/libtomahawk/resolvers/ScriptCollection.h | 4 + 10 files changed, 199 insertions(+), 51 deletions(-) diff --git a/src/libtomahawk/AlbumPlaylistInterface.cpp b/src/libtomahawk/AlbumPlaylistInterface.cpp index 2ecfd8175..8cc30aca7 100644 --- a/src/libtomahawk/AlbumPlaylistInterface.cpp +++ b/src/libtomahawk/AlbumPlaylistInterface.cpp @@ -2,6 +2,7 @@ * * Copyright 2010-2011, Christian Muehlhaeuser * Copyright 2010-2012, Jeff Mitchell + * Copyright 2013, Teo Mrnjavac * * Tomahawk is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -132,14 +133,23 @@ AlbumPlaylistInterface::tracks() const } else if ( m_mode == DatabaseMode && !m_databaseLoaded ) { - DatabaseCommand_AllTracks* cmd = new DatabaseCommand_AllTracks( m_collection ); - cmd->setAlbum( m_album->weakRef() ); - cmd->setSortOrder( DatabaseCommand_AllTracks::AlbumPosition ); + if ( m_collection.isNull() ) //we do a dbcmd directly, for the SuperCollection I guess? + { + DatabaseCommand_AllTracks* cmd = new DatabaseCommand_AllTracks( m_collection ); + cmd->setAlbum( m_album->weakRef() ); + cmd->setSortOrder( DatabaseCommand_AllTracks::AlbumPosition ); + connect( cmd, SIGNAL( tracks( QList, QVariant ) ), + SLOT( onTracksLoaded( QList ) ) ); + Database::instance()->enqueue( QSharedPointer( cmd ) ); + } + else + { + connect( m_collection.data(), SIGNAL( tracksResult( QList< Tomahawk::query_ptr > ) ), + SLOT( onTracksLoaded( QList< Tomahawk::query_ptr > ) ), Qt::UniqueConnection ); - connect( cmd, SIGNAL( tracks( QList, QVariant ) ), - SLOT( onTracksLoaded( QList ) ) ); - - Database::instance()->enqueue( QSharedPointer( cmd ) ); + Tomahawk::album_ptr ap = Album::get( m_album->id(), m_album->name(), m_album->artist() ); + m_collection->tracks( ap ); + } } } @@ -218,15 +228,23 @@ AlbumPlaylistInterface::infoSystemFinished( const QString& infoId ) if ( m_queries.isEmpty() && m_mode == Mixed ) { - DatabaseCommand_AllTracks* cmd = new DatabaseCommand_AllTracks( m_collection ); - cmd->setAlbum( m_album->weakRef() ); - //this takes discnumber into account as well - cmd->setSortOrder( DatabaseCommand_AllTracks::AlbumPosition ); + if ( m_collection.isNull() ) //we do a dbcmd directly, for the SuperCollection I guess? + { + DatabaseCommand_AllTracks* cmd = new DatabaseCommand_AllTracks( m_collection ); + cmd->setAlbum( m_album->weakRef() ); + cmd->setSortOrder( DatabaseCommand_AllTracks::AlbumPosition ); + connect( cmd, SIGNAL( tracks( QList, QVariant ) ), + SLOT( onTracksLoaded( QList ) ) ); + Database::instance()->enqueue( QSharedPointer( cmd ) ); + } + else + { + connect( m_collection.data(), SIGNAL( tracksResult( QList< Tomahawk::query_ptr > ) ), + SLOT( onTracksLoaded( QList< Tomahawk::query_ptr > ) ), Qt::UniqueConnection ); - connect( cmd, SIGNAL( tracks( QList, QVariant ) ), - SLOT( onTracksLoaded( QList ) ) ); - - Database::instance()->enqueue( QSharedPointer( cmd ) ); + Tomahawk::album_ptr ap = Album::get( m_album->id(), m_album->name(), m_album->artist() ); + m_collection->tracks( ap ); + } } else { diff --git a/src/libtomahawk/Artist.cpp b/src/libtomahawk/Artist.cpp index 18306f054..5f8518e3c 100644 --- a/src/libtomahawk/Artist.cpp +++ b/src/libtomahawk/Artist.cpp @@ -2,6 +2,7 @@ * * Copyright 2010-2011, Christian Muehlhaeuser * Copyright 2010-2012, Jeff Mitchell + * Copyright 2013, Teo Mrnjavac * * Tomahawk is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -165,13 +166,23 @@ Artist::albums( ModelMode mode, const Tomahawk::collection_ptr& collection ) con if ( ( mode == DatabaseMode || mode == Mixed ) && !dbLoaded ) { - DatabaseCommand_AllAlbums* cmd = new DatabaseCommand_AllAlbums( collection, artist ); - cmd->setData( QVariant( collection.isNull() ) ); + if ( collection.isNull() ) + { + DatabaseCommand_AllAlbums* cmd = new DatabaseCommand_AllAlbums( collection, artist ); + cmd->setData( QVariant( collection.isNull() ) ); - connect( cmd, SIGNAL( albums( QList, QVariant ) ), - SLOT( onAlbumsFound( QList, QVariant ) ) ); + connect( cmd, SIGNAL( albums( QList, QVariant ) ), + SLOT( onAlbumsFound( QList, QVariant ) ) ); - Database::instance()->enqueue( QSharedPointer( cmd ) ); + Database::instance()->enqueue( QSharedPointer( cmd ) ); + } + else + { + //collection is *surely* not null + connect( collection.data(), SIGNAL( albumsResult( QList< Tomahawk::album_ptr > ) ), + SLOT( onAlbumsFound( QList ) ), Qt::UniqueConnection ); + collection->albums( artist ); + } } if ( ( mode == InfoSystemMode || mode == Mixed ) && !infoLoaded ) @@ -376,9 +387,9 @@ Artist::playbackCount( const source_ptr& source ) void -Artist::onAlbumsFound( const QList< album_ptr >& albums, const QVariant& data ) +Artist::onAlbumsFound( const QList< album_ptr >& albums, const QVariant& collectionIsNull ) { - if ( data.toBool() ) + if ( collectionIsNull.toBool() ) { m_databaseAlbums << albums; m_albumsLoaded.insert( DatabaseMode, true ); diff --git a/src/libtomahawk/Artist.h b/src/libtomahawk/Artist.h index ab7c0741b..03c2ae1c5 100644 --- a/src/libtomahawk/Artist.h +++ b/src/libtomahawk/Artist.h @@ -89,8 +89,7 @@ signals: void statsLoaded(); private slots: - void onTracksLoaded( Tomahawk::ModelMode mode, const Tomahawk::collection_ptr& collection ); - void onAlbumsFound( const QList& albums, const QVariant& data ); + void onAlbumsFound( const QList& albums, const QVariant& collectionIsNull = QVariant( false ) ); void infoSystemInfo( Tomahawk::InfoSystem::InfoRequestData requestData, QVariant output ); void infoSystemFinished( QString target ); diff --git a/src/libtomahawk/Collection.h b/src/libtomahawk/Collection.h index 88e576512..b041540c6 100644 --- a/src/libtomahawk/Collection.h +++ b/src/libtomahawk/Collection.h @@ -1,6 +1,7 @@ /* === This file is part of Tomahawk Player - === * * Copyright 2010-2011, Christian Muehlhaeuser + * Copyright 2013, Teo Mrnjavac * * Tomahawk is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -75,14 +76,19 @@ public: virtual QList< Tomahawk::dynplaylist_ptr > autoPlaylists() { return m_autoplaylists.values(); } virtual QList< Tomahawk::dynplaylist_ptr > stations() { return m_stations.values(); } + // Async requests. Emit artists/albums/tracksResult in subclasses when finished. virtual void artists() = 0; virtual void albums( const Tomahawk::artist_ptr& artist ) = 0; - virtual void tracks( const Tomahawk::artist_ptr& artist, const Tomahawk::album_ptr& album ) = 0; + virtual void tracks( const Tomahawk::album_ptr& album ) = 0; const source_ptr& source() const; unsigned int lastmodified() const { return m_lastmodified; } signals: + void artistsResult( const QList< Tomahawk::artist_ptr >& artists ); + void albumsResult( const QList< Tomahawk::album_ptr >& albums ); + void tracksResult( const QList< Tomahawk::query_ptr >& queries ); + void tracksAdded( const QList& fileids ); void tracksRemoved( const QList& fileids ); diff --git a/src/libtomahawk/database/DatabaseCollection.cpp b/src/libtomahawk/database/DatabaseCollection.cpp index 5f2624ffe..1bdc518d8 100644 --- a/src/libtomahawk/database/DatabaseCollection.cpp +++ b/src/libtomahawk/database/DatabaseCollection.cpp @@ -2,6 +2,7 @@ * * Copyright 2010-2011, Christian Muehlhaeuser * Copyright 2010-2011, Leo Franchi + * Copyright 2013, Teo Mrnjavac * * Tomahawk is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -20,6 +21,8 @@ #include "DatabaseCollection.h" #include "database/Database.h" +#include "DatabaseCommand_AllArtists.h" +#include "DatabaseCommand_AllAlbums.h" #include "DatabaseCommand_AllTracks.h" #include "DatabaseCommand_AddFiles.h" #include "DatabaseCommand_DeleteFiles.h" @@ -130,6 +133,83 @@ DatabaseCollection::stations() } +void +DatabaseCollection::artists() +{ + //FIXME: assuming there's only one dbcollection per source, and that this is the one + Tomahawk::collection_ptr thisCollection = source()->dbCollection(); + if ( thisCollection->name() != this->name() ) + return; + + DatabaseCommand_AllArtists* cmd = new DatabaseCommand_AllArtists( thisCollection ); + + connect( cmd, SIGNAL( artists( QList< Tomahawk::artist_ptr > ) ), + SLOT( onArtistsFetched( QList< Tomahawk::artist_ptr > ) ) ); + + Database::instance()->enqueue( QSharedPointer( cmd ) ); +} + + +void +DatabaseCollection::onArtistsFetched( const QList< Tomahawk::artist_ptr >& artists ) +{ + emit artistsResult( artists ); +} + + +void +DatabaseCollection::albums( const Tomahawk::artist_ptr& artist ) +{ + //FIXME: assuming there's only one dbcollection per source, and that this is the one + Tomahawk::collection_ptr thisCollection = source()->dbCollection(); + if ( thisCollection->name() != this->name() ) + return; + + DatabaseCommand_AllAlbums* cmd = new DatabaseCommand_AllAlbums( thisCollection, artist ); + + // The QVariant might carry a bool that says whether the dbcmd was executed for a null collection + // but here we know for a fact that the collection is not null, so we'll happily ignore it + connect( cmd, SIGNAL( albums( QList, QVariant ) ), + SLOT( onAlbumsFetched( QList, QVariant ) ) ); + + Database::instance()->enqueue( QSharedPointer( cmd ) ); +} + + +void +DatabaseCollection::onAlbumsFetched( const QList< album_ptr >& albums, const QVariant& data ) +{ + Q_UNUSED( data ); + emit albumsResult( albums ); +} + + +void +DatabaseCollection::tracks( const Tomahawk::album_ptr& album ) +{ + //FIXME: assuming there's only one dbcollection per source, and that this is the one + Tomahawk::collection_ptr thisCollection = source()->dbCollection(); + if ( thisCollection->name() != this->name() ) + return; + + DatabaseCommand_AllTracks* cmd = new DatabaseCommand_AllTracks( thisCollection ); + cmd->setAlbum( album->weakRef() ); + cmd->setSortOrder( DatabaseCommand_AllTracks::AlbumPosition ); + + connect( cmd, SIGNAL( tracks( QList, QVariant ) ), + SLOT( onTracksFetched( QList ) ) ); + + Database::instance()->enqueue( QSharedPointer( cmd ) ); +} + + +void +DatabaseCollection::onTracksFetched( const QList< query_ptr >& tracks ) +{ + emit tracksResult( tracks ); +} + + void DatabaseCollection::autoPlaylistCreated( const source_ptr& source, const QVariantList& data ) { diff --git a/src/libtomahawk/database/DatabaseCollection.h b/src/libtomahawk/database/DatabaseCollection.h index 9325b122d..9f8d6a688 100644 --- a/src/libtomahawk/database/DatabaseCollection.h +++ b/src/libtomahawk/database/DatabaseCollection.h @@ -2,6 +2,7 @@ * * Copyright 2010-2011, Christian Muehlhaeuser * Copyright 2010-2011, Leo Franchi + * Copyright 2013, Teo Mrnjavac * * Tomahawk is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -49,11 +50,19 @@ public: virtual QList< Tomahawk::dynplaylist_ptr > autoPlaylists(); virtual QList< Tomahawk::dynplaylist_ptr > stations(); + virtual void artists(); + virtual void albums( const Tomahawk::artist_ptr& artist ); + virtual void tracks( const Tomahawk::album_ptr& album ); + public slots: virtual void addTracks( const QList& newitems ); virtual void removeTracks( const QDir& dir ); private slots: + void onArtistsFetched( const QList< Tomahawk::artist_ptr >& artists ); + void onAlbumsFetched( const QList< Tomahawk::album_ptr >& albums, const QVariant& data ); + void onTracksFetched( const QList< Tomahawk::query_ptr >& tracks ); + void stationCreated( const Tomahawk::source_ptr& source, const QVariantList& data ); void autoPlaylistCreated( const Tomahawk::source_ptr& source, const QVariantList& data ); }; diff --git a/src/libtomahawk/playlist/TreeModel.cpp b/src/libtomahawk/playlist/TreeModel.cpp index 24da2b6a5..d8e0a5192 100644 --- a/src/libtomahawk/playlist/TreeModel.cpp +++ b/src/libtomahawk/playlist/TreeModel.cpp @@ -3,6 +3,7 @@ * Copyright 2010-2011, Christian Muehlhaeuser * Copyright 2010-2011, Jeff Mitchell * Copyright 2012, Leo Franchi + * Copyright 2013, Teo Mrnjavac * * Tomahawk is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -251,12 +252,10 @@ TreeModel::addCollection( const collection_ptr& collection ) startLoading(); m_collection = collection; - DatabaseCommand_AllArtists* cmd = new DatabaseCommand_AllArtists( collection ); - connect( cmd, SIGNAL( artists( QList ) ), - SLOT( onArtistsAdded( QList ) ) ); - - Database::instance()->enqueue( QSharedPointer( cmd ) ); + connect( m_collection.data(), SIGNAL( artistsResult( QList ) ), + SLOT( onArtistsAdded( QList ) ), Qt::UniqueConnection ); + m_collection->artists(); connect( collection.data(), SIGNAL( changed() ), SLOT( onCollectionChanged() ), Qt::UniqueConnection ); @@ -270,29 +269,28 @@ TreeModel::addCollection( const collection_ptr& collection ) } -void -TreeModel::addFilteredCollection( const collection_ptr& collection, unsigned int amount, DatabaseCommand_AllArtists::SortOrder order ) -{ - qDebug() << Q_FUNC_INFO << collection->name() - << collection->source()->id() - << collection->source()->nodeId() - << amount << order; +//void +//TreeModel::addFilteredCollection( const collection_ptr& collection, unsigned int amount, DatabaseCommand_AllArtists::SortOrder order ) +//{ +// qDebug() << Q_FUNC_INFO << collection->name() +// << collection->source()->id() +// << collection->source()->nodeId() +// << amount << order; +// DatabaseCommand_AllArtists* cmd = new DatabaseCommand_AllArtists( collection ); +// cmd->setLimit( amount ); +// cmd->setSortOrder( order ); +// cmd->setSortDescending( true ); - DatabaseCommand_AllArtists* cmd = new DatabaseCommand_AllArtists( collection ); - cmd->setLimit( amount ); - cmd->setSortOrder( order ); - cmd->setSortDescending( true ); +// connect( cmd, SIGNAL( artists( QList, Tomahawk::collection_ptr ) ), +// SLOT( onArtistsAdded( QList, Tomahawk::collection_ptr ) ) ); - connect( cmd, SIGNAL( artists( QList, Tomahawk::collection_ptr ) ), - SLOT( onArtistsAdded( QList, Tomahawk::collection_ptr ) ) ); +// Database::instance()->enqueue( QSharedPointer( cmd ) ); - Database::instance()->enqueue( QSharedPointer( cmd ) ); - - if ( collection->source()->isLocal() ) - setTitle( tr( "My Collection" ) ); - else - setTitle( tr( "Collection of %1" ).arg( collection->source()->friendlyName() ) ); -} +// if ( collection->source()->isLocal() ) +// setTitle( tr( "My Collection" ) ); +// else +// setTitle( tr( "Collection of %1" ).arg( collection->source()->friendlyName() ) ); +//} void diff --git a/src/libtomahawk/playlist/TreeModel.h b/src/libtomahawk/playlist/TreeModel.h index d477370e3..591e908dc 100644 --- a/src/libtomahawk/playlist/TreeModel.h +++ b/src/libtomahawk/playlist/TreeModel.h @@ -53,7 +53,8 @@ public: void addAllCollections(); void addCollection( const Tomahawk::collection_ptr& collection ); - void addFilteredCollection( const Tomahawk::collection_ptr& collection, unsigned int amount, DatabaseCommand_AllArtists::SortOrder order ); + //TODO: Unused, but will be useful for supporting filtered queries. - Teo 1/2013 + //void addFilteredCollection( const Tomahawk::collection_ptr& collection, unsigned int amount, DatabaseCommand_AllArtists::SortOrder order ); void addArtists( const Tomahawk::artist_ptr& artist ); void addTracks( const Tomahawk::album_ptr& album, const QModelIndex& parent, bool autoRefetch = false ); diff --git a/src/libtomahawk/resolvers/ScriptCollection.cpp b/src/libtomahawk/resolvers/ScriptCollection.cpp index a1a4abbcf..ad5283565 100644 --- a/src/libtomahawk/resolvers/ScriptCollection.cpp +++ b/src/libtomahawk/resolvers/ScriptCollection.cpp @@ -64,3 +64,25 @@ ScriptCollection::icon() const } return QIcon(); } + + +void +ScriptCollection::artists() +{ + //TODO: implement! + emit artistsResult( QList< Tomahawk::artist_ptr >() ); +} + + +void +ScriptCollection::albums( const Tomahawk::artist_ptr& artist ) +{ + emit albumsResult( QList< Tomahawk::album_ptr >() ); +} + + +void +ScriptCollection::tracks( const Tomahawk::album_ptr& album ) +{ + emit tracksResult( QList< Tomahawk::query_ptr >() ); +} diff --git a/src/libtomahawk/resolvers/ScriptCollection.h b/src/libtomahawk/resolvers/ScriptCollection.h index 389b6d7a1..e8f846837 100644 --- a/src/libtomahawk/resolvers/ScriptCollection.h +++ b/src/libtomahawk/resolvers/ScriptCollection.h @@ -46,6 +46,10 @@ public: virtual ExternalResolver* resolver() { return m_resolver; } + virtual void artists(); + virtual void albums( const Tomahawk::artist_ptr& artist ); + virtual void tracks( const Tomahawk::album_ptr& album ); + private: ExternalResolver* m_resolver;