diff --git a/src/libtomahawk/CMakeLists.txt b/src/libtomahawk/CMakeLists.txt index b9d636d73..6727aa2da 100644 --- a/src/libtomahawk/CMakeLists.txt +++ b/src/libtomahawk/CMakeLists.txt @@ -77,6 +77,7 @@ set( libGuiSources playlist/artistview.cpp playlist/customplaylistview.cpp playlist/ViewHeader.cpp + playlist/RecentlyAddedModel.cpp playlist/RecentlyPlayedModel.cpp playlist/dynamic/DynamicPlaylist.cpp diff --git a/src/libtomahawk/playlist/RecentlyAddedModel.cpp b/src/libtomahawk/playlist/RecentlyAddedModel.cpp new file mode 100644 index 000000000..e5adacc67 --- /dev/null +++ b/src/libtomahawk/playlist/RecentlyAddedModel.cpp @@ -0,0 +1,102 @@ +/* === This file is part of Tomahawk Player - === + * + * Copyright 2010-2012, Christian Muehlhaeuser + * + * Tomahawk is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Tomahawk is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Tomahawk. If not, see . + */ + +#include "RecentlyAddedModel.h" + +#include +#include + +#include "source.h" +#include "sourcelist.h" +#include "database/database.h" +#include "database/databasecommand_alltracks.h" +#include "utils/tomahawkutils.h" +#include "utils/logger.h" + +#define LATEST_TRACK_ITEMS 250 + +using namespace Tomahawk; + + +RecentlyAddedModel::RecentlyAddedModel( const source_ptr& source, QObject* parent ) + : TrackModel( parent ) + , m_source( source ) + , m_limit( LATEST_TRACK_ITEMS ) +{ + if ( source.isNull() ) + { + connect( SourceList::instance(), SIGNAL( ready() ), SLOT( onSourcesReady() ) ); + connect( SourceList::instance(), SIGNAL( sourceAdded( Tomahawk::source_ptr ) ), SLOT( onSourceAdded( Tomahawk::source_ptr ) ) ); + } + else + { + onSourceAdded( source ); + loadHistory(); + } +} + + +RecentlyAddedModel::~RecentlyAddedModel() +{ +} + + +void +RecentlyAddedModel::loadHistory() +{ + if ( rowCount( QModelIndex() ) ) + { + clear(); + } + + DatabaseCommand_AllTracks* cmd = new DatabaseCommand_AllTracks( m_source->collection() ); + cmd->setLimit( m_limit ); + cmd->setSortOrder( DatabaseCommand_AllTracks::ModificationTime ); + cmd->setSortDescending( true ); + + connect( cmd, SIGNAL( tracks( QList, QVariant ) ), + SLOT( append( QList ) ), Qt::QueuedConnection ); + + Database::instance()->enqueue( QSharedPointer( cmd ) ); +} + + +void +RecentlyAddedModel::onSourcesReady() +{ + Q_ASSERT( m_source.isNull() ); + + loadHistory(); + + foreach ( const source_ptr& source, SourceList::instance()->sources() ) + onSourceAdded( source ); +} + + +void +RecentlyAddedModel::onSourceAdded( const Tomahawk::source_ptr& source ) +{ + connect( source->collection().data(), SIGNAL( changed() ), SLOT( loadHistory() ) ); +} + + +bool +RecentlyAddedModel::isTemporary() const +{ + return true; +} diff --git a/src/libtomahawk/playlist/RecentlyAddedModel.h b/src/libtomahawk/playlist/RecentlyAddedModel.h new file mode 100644 index 000000000..53d113f43 --- /dev/null +++ b/src/libtomahawk/playlist/RecentlyAddedModel.h @@ -0,0 +1,54 @@ +/* === This file is part of Tomahawk Player - === + * + * Copyright 2010-2011, Christian Muehlhaeuser + * + * Tomahawk is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Tomahawk is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Tomahawk. If not, see . + */ + +#ifndef RECENTLYADDEDMODEL_H +#define RECENTLYADDEDMODEL_H + +#include +#include + +#include "typedefs.h" +#include "trackmodel.h" + +#include "dllmacro.h" + +class DLLEXPORT RecentlyAddedModel : public TrackModel +{ +Q_OBJECT + +public: + explicit RecentlyAddedModel( const Tomahawk::source_ptr& source, QObject* parent = 0 ); + ~RecentlyAddedModel(); + + unsigned int limit() const { return m_limit; } + void setLimit( unsigned int limit ) { m_limit = limit; } + + bool isTemporary() const; + +private slots: + void onSourcesReady(); + void onSourceAdded( const Tomahawk::source_ptr& source ); + + void loadHistory(); + +private: + Tomahawk::source_ptr m_source; + unsigned int m_limit; +}; + +#endif // RECENTLYADDEDMODEL_H diff --git a/src/libtomahawk/playlist/collectionflatmodel.cpp b/src/libtomahawk/playlist/collectionflatmodel.cpp index 7d94ba023..7265cb27b 100644 --- a/src/libtomahawk/playlist/collectionflatmodel.cpp +++ b/src/libtomahawk/playlist/collectionflatmodel.cpp @@ -138,13 +138,3 @@ CollectionFlatModel::onTracksRemoved( const QList& tracks ) } } } - - -void -CollectionFlatModel::onDataChanged() -{ - TrackModelItem* p = (TrackModelItem*)sender(); - - if ( p ) - emit dataChanged( p->index, p->index.sibling( p->index.row(), columnCount( QModelIndex() ) - 1 ) ); -} diff --git a/src/libtomahawk/playlist/collectionflatmodel.h b/src/libtomahawk/playlist/collectionflatmodel.h index 0d2784b44..b5982974f 100644 --- a/src/libtomahawk/playlist/collectionflatmodel.h +++ b/src/libtomahawk/playlist/collectionflatmodel.h @@ -58,8 +58,6 @@ signals: void trackCountChanged( unsigned int tracks ); private slots: - void onDataChanged(); - void onTracksAdded( const QList& tracks ); void onTracksRemoved( const QList& tracks ); diff --git a/src/libtomahawk/playlist/playlistmodel.cpp b/src/libtomahawk/playlist/playlistmodel.cpp index 6d5d28392..080af747e 100644 --- a/src/libtomahawk/playlist/playlistmodel.cpp +++ b/src/libtomahawk/playlist/playlistmodel.cpp @@ -257,15 +257,6 @@ PlaylistModel::trackResolved( bool ) } -void -PlaylistModel::onDataChanged() -{ - TrackModelItem* p = (TrackModelItem*)sender(); - if ( p && p->index.isValid() ) - emit dataChanged( p->index, p->index.sibling( p->index.row(), columnCount() - 1 ) ); -} - - void PlaylistModel::onRevisionLoaded( Tomahawk::PlaylistRevision revision ) { diff --git a/src/libtomahawk/playlist/playlistmodel.h b/src/libtomahawk/playlist/playlistmodel.h index f5586e043..01c3ff3b3 100644 --- a/src/libtomahawk/playlist/playlistmodel.h +++ b/src/libtomahawk/playlist/playlistmodel.h @@ -84,7 +84,6 @@ protected: void removeFromWaitList( const QString& revisionguid ) { m_waitForRevision.removeAll( revisionguid ); } private slots: - void onDataChanged(); void onRevisionLoaded( Tomahawk::PlaylistRevision revision ); void parsedDroppedTracks( QList ); void trackResolved( bool ); diff --git a/src/libtomahawk/playlist/trackmodel.cpp b/src/libtomahawk/playlist/trackmodel.cpp index e31885612..9633398c0 100644 --- a/src/libtomahawk/playlist/trackmodel.cpp +++ b/src/libtomahawk/playlist/trackmodel.cpp @@ -584,3 +584,12 @@ TrackModel::columnAlignment( int column ) const return Qt::AlignLeft; } } + + +void +TrackModel::onDataChanged() +{ + TrackModelItem* p = (TrackModelItem*)sender(); + if ( p && p->index.isValid() ) + emit dataChanged( p->index, p->index.sibling( p->index.row(), columnCount() - 1 ) ); +} diff --git a/src/libtomahawk/playlist/trackmodel.h b/src/libtomahawk/playlist/trackmodel.h index a368c508b..206fe7573 100644 --- a/src/libtomahawk/playlist/trackmodel.h +++ b/src/libtomahawk/playlist/trackmodel.h @@ -134,6 +134,8 @@ protected: TrackModelItem* rootItem() const { return m_rootItem; } private slots: + void onDataChanged(); + void onPlaybackStarted( const Tomahawk::result_ptr& result ); void onPlaybackStopped(); diff --git a/src/libtomahawk/widgets/infowidgets/sourceinfowidget.cpp b/src/libtomahawk/widgets/infowidgets/sourceinfowidget.cpp index 9a67ce08d..4514bdd85 100644 --- a/src/libtomahawk/widgets/infowidgets/sourceinfowidget.cpp +++ b/src/libtomahawk/widgets/infowidgets/sourceinfowidget.cpp @@ -24,10 +24,10 @@ #include "playlist/albummodel.h" #include "playlist/collectionflatmodel.h" +#include "playlist/RecentlyAddedModel.h" #include "playlist/RecentlyPlayedModel.h" #include "database/database.h" -#include "database/databasecommand_alltracks.h" #include "database/databasecommand_allalbums.h" #include "utils/tomahawkutils.h" @@ -61,9 +61,9 @@ SourceInfoWidget::SourceInfoWidget( const Tomahawk::source_ptr& source, QWidget* ui->historyView->overlay()->setEnabled( false ); - m_recentCollectionModel = new CollectionFlatModel( ui->recentCollectionView ); - m_recentCollectionModel->setStyle( TrackModel::Short ); - ui->recentCollectionView->setTrackModel( m_recentCollectionModel ); + m_recentTracksModel = new RecentlyAddedModel( source, ui->recentCollectionView ); + m_recentTracksModel->setStyle( TrackModel::Short ); + ui->recentCollectionView->setTrackModel( m_recentTracksModel ); ui->recentCollectionView->sortByColumn( TrackModel::Age, Qt::DescendingOrder ); m_historyModel = new RecentlyPlayedModel( source, ui->historyView ); @@ -75,7 +75,6 @@ SourceInfoWidget::SourceInfoWidget( const Tomahawk::source_ptr& source, QWidget* ui->recentAlbumView->proxyModel()->sort( -1 ); onCollectionChanged(); - connect( source->collection().data(), SIGNAL( changed() ), SLOT( onCollectionChanged() ) ); m_title = tr( "New Additions" ); @@ -101,7 +100,6 @@ SourceInfoWidget::~SourceInfoWidget() void SourceInfoWidget::onCollectionChanged() { - loadTracks(); loadRecentAdditions(); } @@ -113,29 +111,6 @@ SourceInfoWidget::loadRecentAdditions() } -void -SourceInfoWidget::loadTracks() -{ - DatabaseCommand_AllTracks* cmd = new DatabaseCommand_AllTracks( m_source->collection() ); - cmd->setLimit( 250 ); - cmd->setSortOrder( DatabaseCommand_AllTracks::ModificationTime ); - cmd->setSortDescending( true ); - - connect( cmd, SIGNAL( tracks( QList, QVariant ) ), - SLOT( onLoadedTrackHistory( QList ) ), Qt::QueuedConnection ); - - Database::instance()->enqueue( QSharedPointer( cmd ) ); -} - - -void -SourceInfoWidget::onLoadedTrackHistory( const QList& queries ) -{ - m_recentCollectionModel->clear(); - m_recentCollectionModel->append( queries ); -} - - void SourceInfoWidget::changeEvent( QEvent* e ) { diff --git a/src/libtomahawk/widgets/infowidgets/sourceinfowidget.h b/src/libtomahawk/widgets/infowidgets/sourceinfowidget.h index 1c5f67da9..73b8fa9eb 100644 --- a/src/libtomahawk/widgets/infowidgets/sourceinfowidget.h +++ b/src/libtomahawk/widgets/infowidgets/sourceinfowidget.h @@ -28,6 +28,7 @@ class AlbumModel; class CollectionFlatModel; +class RecentlyAddedModel; class RecentlyPlayedModel; namespace Ui @@ -58,16 +59,14 @@ protected: void changeEvent( QEvent* e ); private slots: - void loadTracks(); void loadRecentAdditions(); void onCollectionChanged(); - void onLoadedTrackHistory( const QList& queries ); private: Ui::SourceInfoWidget *ui; - CollectionFlatModel* m_recentCollectionModel; + RecentlyAddedModel* m_recentTracksModel; RecentlyPlayedModel* m_historyModel; AlbumModel* m_recentAlbumModel; diff --git a/src/libtomahawk/widgets/welcomewidget.cpp b/src/libtomahawk/widgets/welcomewidget.cpp index 3235a2371..928524d86 100644 --- a/src/libtomahawk/widgets/welcomewidget.cpp +++ b/src/libtomahawk/widgets/welcomewidget.cpp @@ -95,7 +95,6 @@ WelcomeWidget::~WelcomeWidget() void WelcomeWidget::loadData() { - m_recentAlbumsModel->addFilteredCollection( collection_ptr(), 20, DatabaseCommand_AllAlbums::ModificationTime, true ); }