From 781a0149e0bd36832e81a6d11b4bcaf48a9396a9 Mon Sep 17 00:00:00 2001 From: Teo Mrnjavac Date: Sun, 24 Feb 2013 22:57:20 +0100 Subject: [PATCH] Basic ScriptCollection filtering support. --- src/libtomahawk/collection/AlbumsRequest.cpp | 1 + src/libtomahawk/collection/AlbumsRequest.h | 2 + src/libtomahawk/collection/ArtistsRequest.cpp | 1 + src/libtomahawk/collection/ArtistsRequest.h | 2 + src/libtomahawk/playlist/TreeProxyModel.cpp | 37 ++++++++++--------- src/libtomahawk/playlist/TreeProxyModel.h | 5 +-- .../resolvers/ScriptCommand_AllAlbums.cpp | 23 +++++++++++- .../resolvers/ScriptCommand_AllAlbums.h | 3 ++ .../resolvers/ScriptCommand_AllArtists.cpp | 21 ++++++++++- .../resolvers/ScriptCommand_AllArtists.h | 3 ++ 10 files changed, 76 insertions(+), 22 deletions(-) diff --git a/src/libtomahawk/collection/AlbumsRequest.cpp b/src/libtomahawk/collection/AlbumsRequest.cpp index 8fc5d3dbd..1cac10fab 100644 --- a/src/libtomahawk/collection/AlbumsRequest.cpp +++ b/src/libtomahawk/collection/AlbumsRequest.cpp @@ -19,5 +19,6 @@ #include "AlbumsRequest.h" + Tomahawk::AlbumsRequest::~AlbumsRequest() {} diff --git a/src/libtomahawk/collection/AlbumsRequest.h b/src/libtomahawk/collection/AlbumsRequest.h index 0bb457867..ec826a263 100644 --- a/src/libtomahawk/collection/AlbumsRequest.h +++ b/src/libtomahawk/collection/AlbumsRequest.h @@ -34,6 +34,8 @@ public: virtual void enqueue() = 0; + virtual void setFilter( const QString& filter ) = 0; + protected: //signals virtual void albums( const QList< Tomahawk::album_ptr >& ) = 0; }; diff --git a/src/libtomahawk/collection/ArtistsRequest.cpp b/src/libtomahawk/collection/ArtistsRequest.cpp index 59d46c001..4a2d9744f 100644 --- a/src/libtomahawk/collection/ArtistsRequest.cpp +++ b/src/libtomahawk/collection/ArtistsRequest.cpp @@ -19,6 +19,7 @@ #include "ArtistsRequest.h" + Tomahawk::ArtistsRequest::~ArtistsRequest() {} diff --git a/src/libtomahawk/collection/ArtistsRequest.h b/src/libtomahawk/collection/ArtistsRequest.h index 49c2de750..ac83abf6a 100644 --- a/src/libtomahawk/collection/ArtistsRequest.h +++ b/src/libtomahawk/collection/ArtistsRequest.h @@ -34,6 +34,8 @@ public: virtual void enqueue() = 0; + virtual void setFilter( const QString& filter ) = 0; + protected: //signals virtual void artists( const QList< Tomahawk::artist_ptr >& ) = 0; }; diff --git a/src/libtomahawk/playlist/TreeProxyModel.cpp b/src/libtomahawk/playlist/TreeProxyModel.cpp index 45af5eda0..975e626c1 100644 --- a/src/libtomahawk/playlist/TreeProxyModel.cpp +++ b/src/libtomahawk/playlist/TreeProxyModel.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 @@ -24,7 +25,8 @@ #include "Query.h" #include "database/Database.h" #include "database/DatabaseImpl.h" -#include "database/DatabaseCommand_AllAlbums.h" +#include "collection/AlbumsRequest.h" +#include "collection/ArtistsRequest.h" #include "PlayableItem.h" #include "utils/Logger.h" @@ -71,14 +73,14 @@ TreeProxyModel::onRowsInserted( const QModelIndex& parent, int /* start */, int if ( pi->artist().isNull() ) return; - DatabaseCommand_AllAlbums* cmd = new DatabaseCommand_AllAlbums( m_model->collection() ); - cmd->setArtist( pi->artist() ); + Tomahawk::AlbumsRequest* cmd = m_model->collection()->requestAlbums( pi->artist() ); + cmd->setFilter( m_filter ); - connect( cmd, SIGNAL( albums( QList, QVariant ) ), - SLOT( onFilterAlbums( QList ) ) ); + connect( dynamic_cast< QObject* >( cmd ), SIGNAL( albums( QList ) ), + SLOT( onFilterAlbums( QList ) ) ); - Database::instance()->enqueue( QSharedPointer( cmd ) ); + cmd->enqueue(); } @@ -101,8 +103,8 @@ TreeProxyModel::setFilter( const QString& pattern ) if ( m_artistsFilterCmd ) { - disconnect( m_artistsFilterCmd, SIGNAL( artists( QList ) ), - this, SLOT( onFilterArtists( QList ) ) ); + disconnect( dynamic_cast< QObject* >( m_artistsFilterCmd ), SIGNAL( artists( QList ) ), + this, SLOT( onFilterArtists( QList ) ) ); m_artistsFilterCmd = 0; } @@ -113,14 +115,15 @@ TreeProxyModel::setFilter( const QString& pattern ) } else { - DatabaseCommand_AllArtists* cmd = new DatabaseCommand_AllArtists( m_model->collection() ); + Tomahawk::ArtistsRequest* cmd = m_model->collection()->requestArtists(); + cmd->setFilter( pattern ); m_artistsFilterCmd = cmd; - connect( cmd, SIGNAL( artists( QList ) ), - SLOT( onFilterArtists( QList ) ) ); + connect( dynamic_cast< QObject* >( cmd ), SIGNAL( artists( QList ) ), + SLOT( onFilterArtists( QList ) ) ); - Database::instance()->enqueue( QSharedPointer( cmd ) ); + cmd->enqueue(); } } @@ -146,14 +149,14 @@ TreeProxyModel::onFilterArtists( const QList& artists ) { finished = false; - DatabaseCommand_AllAlbums* cmd = new DatabaseCommand_AllAlbums( m_model->collection() ); - cmd->setArtist( artist ); + Tomahawk::AlbumsRequest* cmd = m_model->collection()->requestAlbums( artist ); + cmd->setFilter( m_filter ); - connect( cmd, SIGNAL( albums( QList, QVariant ) ), - SLOT( onFilterAlbums( QList ) ) ); + connect( dynamic_cast< QObject* >( cmd ), SIGNAL( albums( QList ) ), + SLOT( onFilterAlbums( QList ) ) ); - Database::instance()->enqueue( QSharedPointer( cmd ) ); + cmd->enqueue(); } } diff --git a/src/libtomahawk/playlist/TreeProxyModel.h b/src/libtomahawk/playlist/TreeProxyModel.h index 9d0e21839..3f93cfdea 100644 --- a/src/libtomahawk/playlist/TreeProxyModel.h +++ b/src/libtomahawk/playlist/TreeProxyModel.h @@ -26,10 +26,9 @@ #include "DllMacro.h" -class DatabaseCommand_AllArtists; - namespace Tomahawk { + class ArtistsRequest; class TreeProxyModelPlaylistInterface; } @@ -76,7 +75,7 @@ private: QList m_artistsFilter; QList m_albumsFilter; - DatabaseCommand_AllArtists* m_artistsFilterCmd; + Tomahawk::ArtistsRequest* m_artistsFilterCmd; QString m_filter; TreeModel* m_model; diff --git a/src/libtomahawk/resolvers/ScriptCommand_AllAlbums.cpp b/src/libtomahawk/resolvers/ScriptCommand_AllAlbums.cpp index 94fa3c0f8..498c2c7b5 100644 --- a/src/libtomahawk/resolvers/ScriptCommand_AllAlbums.cpp +++ b/src/libtomahawk/resolvers/ScriptCommand_AllAlbums.cpp @@ -20,6 +20,8 @@ #include "ExternalResolver.h" #include "ScriptCollection.h" +#include "Album.h" +#include "Artist.h" #include "utils/Logger.h" @@ -47,6 +49,13 @@ ScriptCommand_AllAlbums::enqueue() } +void +ScriptCommand_AllAlbums::setFilter( const QString& filter ) +{ + m_filter = filter; +} + + void ScriptCommand_AllAlbums::exec() { @@ -81,6 +90,18 @@ ScriptCommand_AllAlbums::reportFailure() void ScriptCommand_AllAlbums::onResolverDone( const QList< Tomahawk::album_ptr >& a ) { - emit albums( a ); + if ( m_filter.isEmpty() ) + emit albums( a ); + else + { + QList< Tomahawk::album_ptr > filtered; + foreach( const Tomahawk::album_ptr& album, a ) + { + if( album->name().toLower().contains( m_filter.toLower() ) || + album->artist()->name().toLower().contains( m_filter.toLower() ) ) + filtered.append( album ); + } + emit albums( filtered ); + } emit done(); } diff --git a/src/libtomahawk/resolvers/ScriptCommand_AllAlbums.h b/src/libtomahawk/resolvers/ScriptCommand_AllAlbums.h index d1e144552..a8b25a505 100644 --- a/src/libtomahawk/resolvers/ScriptCommand_AllAlbums.h +++ b/src/libtomahawk/resolvers/ScriptCommand_AllAlbums.h @@ -33,6 +33,8 @@ public: virtual ~ScriptCommand_AllAlbums() {} virtual void enqueue(); + + virtual void setFilter( const QString& filter ); signals: void albums( const QList< Tomahawk::album_ptr >& ); @@ -48,6 +50,7 @@ private slots: private: Tomahawk::collection_ptr m_collection; Tomahawk::artist_ptr m_artist; + QString m_filter; }; #endif // SCRIPTCOMMAND_ALLALBUMS_H diff --git a/src/libtomahawk/resolvers/ScriptCommand_AllArtists.cpp b/src/libtomahawk/resolvers/ScriptCommand_AllArtists.cpp index 6c9553040..3d4509f0f 100644 --- a/src/libtomahawk/resolvers/ScriptCommand_AllArtists.cpp +++ b/src/libtomahawk/resolvers/ScriptCommand_AllArtists.cpp @@ -18,6 +18,7 @@ #include "ScriptCommand_AllArtists.h" +#include "Artist.h" #include "ExternalResolver.h" #include "ScriptCollection.h" @@ -43,6 +44,13 @@ ScriptCommand_AllArtists::enqueue() } +void +ScriptCommand_AllArtists::setFilter( const QString& filter ) +{ + m_filter = filter; +} + + void ScriptCommand_AllArtists::exec() { @@ -70,6 +78,17 @@ ScriptCommand_AllArtists::reportFailure() void ScriptCommand_AllArtists::onResolverDone( const QList< Tomahawk::artist_ptr >& a ) { - emit artists( a ); + if ( m_filter.isEmpty() ) + emit artists( a ); + else + { + QList< Tomahawk::artist_ptr > filtered; + foreach( const Tomahawk::artist_ptr& artist, a ) + { + if ( artist->name().contains( m_filter ) ) + filtered.append( artist ); + } + emit artists( filtered ); + } emit done(); } diff --git a/src/libtomahawk/resolvers/ScriptCommand_AllArtists.h b/src/libtomahawk/resolvers/ScriptCommand_AllArtists.h index 1bfb0c83f..4962c3a00 100644 --- a/src/libtomahawk/resolvers/ScriptCommand_AllArtists.h +++ b/src/libtomahawk/resolvers/ScriptCommand_AllArtists.h @@ -32,6 +32,8 @@ public: virtual ~ScriptCommand_AllArtists() {} virtual void enqueue(); + + void setFilter( const QString& filter ); signals: void artists( const QList< Tomahawk::artist_ptr >& ); @@ -46,6 +48,7 @@ private slots: private: Tomahawk::collection_ptr m_collection; + QString m_filter; }; #endif // SCRIPTCOMMAND_ALLARTISTS_H