PageRenderTime 182ms CodeModel.GetById 71ms app.highlight 15ms RepoModel.GetById 66ms app.codeStats 0ms

/src/libtomahawk/playlist/customplaylistview.cpp

http://github.com/tomahawk-player/tomahawk
C++ | 172 lines | 121 code | 32 blank | 19 comment | 13 complexity | 5673e0c83225c4810733a4e92aa68e9f MD5 | raw file
  1/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
  2 *
  3 *   Copyright 2010-2011, Leo Franchi <lfranchi@kde.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
 20#include "customplaylistview.h"
 21
 22#include "database/databasecommand_genericselect.h"
 23#include "database/database.h"
 24#include "utils/tomahawkutils.h"
 25#include "sourcelist.h"
 26#include "audio/audioengine.h"
 27
 28using namespace Tomahawk;
 29
 30CustomPlaylistView::CustomPlaylistView( CustomPlaylistView::PlaylistType type, const source_ptr& s, QWidget* parent )
 31    : PlaylistView ( parent )
 32    , m_type( type )
 33    , m_source( s )
 34    , m_model( new PlaylistModel( this ) )
 35{
 36    // Generate the tracks, add them to the playlist
 37    setFrameShape( QFrame::NoFrame );
 38    setAttribute( Qt::WA_MacShowFocusRect, 0 );
 39
 40    setPlaylistModel( m_model );
 41    generateTracks();
 42
 43    if ( m_type == SourceLovedTracks )
 44        connect( m_source.data(), SIGNAL( socialAttributesChanged( QString ) ), this, SLOT( socialAttributesChanged( QString ) ) );
 45    else if ( m_type == AllLovedTracks )
 46    {
 47        connect( SourceList::instance()->getLocal().data(), SIGNAL( socialAttributesChanged( QString ) ), this, SLOT( socialAttributesChanged( QString ) ) );
 48        foreach ( const source_ptr& s, SourceList::instance()->sources( true ) )
 49            connect( s.data(), SIGNAL( socialAttributesChanged( QString ) ), this, SLOT( socialAttributesChanged( QString ) ) );
 50
 51        connect( SourceList::instance(), SIGNAL( sourceAdded( Tomahawk::source_ptr ) ), this, SLOT( sourceAdded( Tomahawk::source_ptr ) ) );
 52    }
 53}
 54
 55
 56CustomPlaylistView::~CustomPlaylistView()
 57{
 58}
 59
 60
 61bool
 62CustomPlaylistView::isBeingPlayed() const
 63{
 64    return AudioEngine::instance()->currentTrackPlaylist() == playlistInterface();
 65}
 66
 67
 68bool
 69CustomPlaylistView::jumpToCurrentTrack()
 70{
 71    return PlaylistView::jumpToCurrentTrack();
 72}
 73
 74
 75void
 76CustomPlaylistView::generateTracks()
 77{
 78    QString sql;
 79    switch ( m_type )
 80    {
 81        // TODO
 82        case SourceLovedTracks:
 83            sql = QString( "SELECT track.name, artist.name, COUNT(*) as counter "
 84                           "FROM social_attributes, track, artist "
 85                           "WHERE social_attributes.id = track.id AND artist.id = track.artist AND social_attributes.k = 'Love' AND social_attributes.v = 'true' AND social_attributes.source %1 "
 86                           "GROUP BY track.id "
 87                           "ORDER BY counter DESC, social_attributes.timestamp DESC " ).arg( m_source->isLocal() ? "IS NULL" : QString( "= %1" ).arg( m_source->id() ) );
 88            break;
 89        case AllLovedTracks:
 90            sql = QString( "SELECT track.name, artist.name, source, COUNT(*) as counter "
 91                           "FROM social_attributes, track, artist "
 92                           "WHERE social_attributes.id = track.id AND artist.id = track.artist AND social_attributes.k = 'Love' AND social_attributes.v = 'true'"
 93                           "GROUP BY track.id "
 94                           "ORDER BY counter DESC, social_attributes.timestamp DESC " );
 95            break;
 96    }
 97
 98    DatabaseCommand_GenericSelect* cmd = new DatabaseCommand_GenericSelect( sql, DatabaseCommand_GenericSelect::Track, -1, 0 );
 99    connect( cmd, SIGNAL( tracks( QList<Tomahawk::query_ptr> ) ), this, SLOT( tracksGenerated( QList<Tomahawk::query_ptr> ) ) );
100    Database::instance()->enqueue( QSharedPointer<DatabaseCommand>( cmd ) );
101}
102
103
104void
105CustomPlaylistView::tracksGenerated( QList< query_ptr > tracks )
106{
107    QList< query_ptr > newTracks = TomahawkUtils::mergePlaylistChanges( m_model->queries(), tracks );
108
109    m_model->clear();
110    m_model->append( newTracks );
111}
112
113
114QString
115CustomPlaylistView::title() const
116{
117    if ( m_source.isNull() )
118        return tr( "Top Loved Tracks" );
119    else
120    {
121        if ( m_source->isLocal() )
122            return tr( "Your loved tracks" );
123        else
124            return tr( "%1's loved tracks" ).arg( m_source->friendlyName() );
125    }
126}
127
128
129QString
130CustomPlaylistView::description() const
131{
132    if ( m_source.isNull() )
133        return tr( "The most loved tracks from all your friends" );
134    else
135    {
136        if ( m_source->isLocal() )
137            return tr( "All of your loved tracks" );
138        else
139            return tr( "All of %1's loved tracks" ).arg( m_source->friendlyName() );
140    }
141}
142
143
144QString
145CustomPlaylistView::longDescription() const
146{
147    return QString();
148}
149
150
151QPixmap
152CustomPlaylistView::pixmap() const
153{
154    return QPixmap( RESPATH "images/loved_playlist.png" );
155}
156
157
158void
159CustomPlaylistView::socialAttributesChanged( const QString& action )
160{
161    if ( action == "Love" )
162    {
163        generateTracks();
164    }
165}
166
167
168void
169CustomPlaylistView::sourceAdded( const source_ptr& s )
170{
171    connect( s.data(), SIGNAL( socialAttributesChanged( QString ) ), this, SLOT( socialAttributesChanged( QString ) ) );
172}