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