1
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
3
* Copyright 2010-2012, Christian Muehlhaeuser <muesli@tomahawk-player.org>
5
* Tomahawk is free software: you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation, either version 3 of the License, or
8
* (at your option) any later version.
10
* Tomahawk is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
19
#include "RecentlyPlayedModel.h"
25
#include "SourceList.h"
26
#include "database/Database.h"
27
#include "database/DatabaseCommand_PlaybackHistory.h"
28
#include "PlayableItem.h"
29
#include "utils/TomahawkUtils.h"
30
#include "utils/Logger.h"
32
#define HISTORY_TRACK_ITEMS 25
34
using namespace Tomahawk;
37
RecentlyPlayedModel::RecentlyPlayedModel( QObject* parent )
38
: PlaylistModel( parent )
39
, m_limit( HISTORY_TRACK_ITEMS )
44
RecentlyPlayedModel::~RecentlyPlayedModel()
50
RecentlyPlayedModel::loadHistory()
52
if ( rowCount( QModelIndex() ) )
58
DatabaseCommand_PlaybackHistory* cmd = new DatabaseCommand_PlaybackHistory( m_source );
59
cmd->setLimit( m_limit );
61
connect( cmd, SIGNAL( tracks( QList<Tomahawk::query_ptr> ) ),
62
SLOT( appendQueries( QList<Tomahawk::query_ptr> ) ), Qt::QueuedConnection );
64
Database::instance()->enqueue( QSharedPointer<DatabaseCommand>( cmd ) );
69
RecentlyPlayedModel::onSourcesReady()
71
Q_ASSERT( m_source.isNull() );
75
foreach ( const source_ptr& source, SourceList::instance()->sources() )
76
onSourceAdded( source );
81
RecentlyPlayedModel::setSource( const Tomahawk::source_ptr& source )
84
if ( source.isNull() )
86
if ( SourceList::instance()->isReady() )
89
connect( SourceList::instance(), SIGNAL( ready() ), SLOT( onSourcesReady() ) );
91
connect( SourceList::instance(), SIGNAL( sourceAdded( Tomahawk::source_ptr ) ), SLOT( onSourceAdded( Tomahawk::source_ptr ) ) );
95
onSourceAdded( source );
102
RecentlyPlayedModel::onSourceAdded( const Tomahawk::source_ptr& source )
104
connect( source.data(), SIGNAL( playbackFinished( Tomahawk::query_ptr ) ), SLOT( onPlaybackFinished( Tomahawk::query_ptr ) ), Qt::UniqueConnection );
109
RecentlyPlayedModel::onPlaybackFinished( const Tomahawk::query_ptr& query )
111
int count = trackCount();
112
unsigned int playtime = query->playedBy().second;
116
PlayableItem* oldestItem = itemFromIndex( index( count - 1, 0, QModelIndex() ) );
117
if ( oldestItem->query()->playedBy().second >= playtime )
120
PlayableItem* youngestItem = itemFromIndex( index( 0, 0, QModelIndex() ) );
121
if ( youngestItem->query()->playedBy().second <= playtime )
122
insertQuery( query, 0 );
125
for ( int i = 0; i < count - 1; i++ )
127
PlayableItem* item1 = itemFromIndex( index( i, 0, QModelIndex() ) );
128
PlayableItem* item2 = itemFromIndex( index( i + 1, 0, QModelIndex() ) );
130
if ( item1->query()->playedBy().second >= playtime && item2->query()->playedBy().second <= playtime )
132
insertQuery( query, i + 1 );
139
insertQuery( query, 0 );
141
if ( trackCount() > (int)m_limit )
149
RecentlyPlayedModel::isTemporary() const