~ubuntu-branches/ubuntu/trusty/tomahawk/trusty-proposed

« back to all changes in this revision

Viewing changes to src/libtomahawk/playlist/RecentlyPlayedModel.cpp

  • Committer: Package Import Robot
  • Author(s): Harald Sitter
  • Date: 2013-03-07 21:50:13 UTC
  • Revision ID: package-import@ubuntu.com-20130307215013-6gdjkdds7i9uenvs
Tags: upstream-0.6.0+dfsg
ImportĀ upstreamĀ versionĀ 0.6.0+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
 
2
 *
 
3
 *   Copyright 2010-2012, Christian Muehlhaeuser <muesli@tomahawk-player.org>
 
4
 *
 
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.
 
9
 *
 
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.
 
14
 *
 
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/>.
 
17
 */
 
18
 
 
19
#include "RecentlyPlayedModel.h"
 
20
 
 
21
#include <QMimeData>
 
22
#include <QTreeView>
 
23
 
 
24
#include "Source.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"
 
31
 
 
32
#define HISTORY_TRACK_ITEMS 25
 
33
 
 
34
using namespace Tomahawk;
 
35
 
 
36
 
 
37
RecentlyPlayedModel::RecentlyPlayedModel( QObject* parent )
 
38
    : PlaylistModel( parent )
 
39
    , m_limit( HISTORY_TRACK_ITEMS )
 
40
{
 
41
}
 
42
 
 
43
 
 
44
RecentlyPlayedModel::~RecentlyPlayedModel()
 
45
{
 
46
}
 
47
 
 
48
 
 
49
void
 
50
RecentlyPlayedModel::loadHistory()
 
51
{
 
52
    if ( rowCount( QModelIndex() ) )
 
53
    {
 
54
        clear();
 
55
    }
 
56
    startLoading();
 
57
 
 
58
    DatabaseCommand_PlaybackHistory* cmd = new DatabaseCommand_PlaybackHistory( m_source );
 
59
    cmd->setLimit( m_limit );
 
60
 
 
61
    connect( cmd, SIGNAL( tracks( QList<Tomahawk::query_ptr> ) ),
 
62
                    SLOT( appendQueries( QList<Tomahawk::query_ptr> ) ), Qt::QueuedConnection );
 
63
 
 
64
    Database::instance()->enqueue( QSharedPointer<DatabaseCommand>( cmd ) );
 
65
}
 
66
 
 
67
 
 
68
void
 
69
RecentlyPlayedModel::onSourcesReady()
 
70
{
 
71
    Q_ASSERT( m_source.isNull() );
 
72
 
 
73
    loadHistory();
 
74
 
 
75
    foreach ( const source_ptr& source, SourceList::instance()->sources() )
 
76
        onSourceAdded( source );
 
77
}
 
78
 
 
79
 
 
80
void
 
81
RecentlyPlayedModel::setSource( const Tomahawk::source_ptr& source )
 
82
{
 
83
    m_source = source;
 
84
    if ( source.isNull() )
 
85
    {
 
86
        if ( SourceList::instance()->isReady() )
 
87
            onSourcesReady();
 
88
        else
 
89
            connect( SourceList::instance(), SIGNAL( ready() ), SLOT( onSourcesReady() ) );
 
90
 
 
91
        connect( SourceList::instance(), SIGNAL( sourceAdded( Tomahawk::source_ptr ) ), SLOT( onSourceAdded( Tomahawk::source_ptr ) ) );
 
92
    }
 
93
    else
 
94
    {
 
95
        onSourceAdded( source );
 
96
        loadHistory();
 
97
    }
 
98
}
 
99
 
 
100
 
 
101
void
 
102
RecentlyPlayedModel::onSourceAdded( const Tomahawk::source_ptr& source )
 
103
{
 
104
    connect( source.data(), SIGNAL( playbackFinished( Tomahawk::query_ptr ) ), SLOT( onPlaybackFinished( Tomahawk::query_ptr ) ), Qt::UniqueConnection );
 
105
}
 
106
 
 
107
 
 
108
void
 
109
RecentlyPlayedModel::onPlaybackFinished( const Tomahawk::query_ptr& query )
 
110
{
 
111
    int count = trackCount();
 
112
    unsigned int playtime = query->playedBy().second;
 
113
 
 
114
    if ( count )
 
115
    {
 
116
        PlayableItem* oldestItem = itemFromIndex( index( count - 1, 0, QModelIndex() ) );
 
117
        if ( oldestItem->query()->playedBy().second >= playtime )
 
118
            return;
 
119
 
 
120
        PlayableItem* youngestItem = itemFromIndex( index( 0, 0, QModelIndex() ) );
 
121
        if ( youngestItem->query()->playedBy().second <= playtime )
 
122
            insertQuery( query, 0 );
 
123
        else
 
124
        {
 
125
            for ( int i = 0; i < count - 1; i++ )
 
126
            {
 
127
                PlayableItem* item1 = itemFromIndex( index( i, 0, QModelIndex() ) );
 
128
                PlayableItem* item2 = itemFromIndex( index( i + 1, 0, QModelIndex() ) );
 
129
 
 
130
                if ( item1->query()->playedBy().second >= playtime && item2->query()->playedBy().second <= playtime )
 
131
                {
 
132
                    insertQuery( query, i + 1 );
 
133
                    break;
 
134
                }
 
135
            }
 
136
        }
 
137
    }
 
138
    else
 
139
        insertQuery( query, 0 );
 
140
 
 
141
    if ( trackCount() > (int)m_limit )
 
142
        remove( m_limit );
 
143
 
 
144
    ensureResolved();
 
145
}
 
146
 
 
147
 
 
148
bool
 
149
RecentlyPlayedModel::isTemporary() const
 
150
{
 
151
    return true;
 
152
}