/src/libtomahawk/SourcePlaylistInterface.cpp

http://github.com/tomahawk-player/tomahawk · C++ · 209 lines · 146 code · 45 blank · 18 comment · 20 complexity · d2afe0ee75eec468911512f4d87bb588 MD5 · raw file

  1. /* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
  2. *
  3. * Copyright 2010-2011, Christian Muehlhaeuser <muesli@tomahawk-player.org>
  4. * Copyright 2010-2012, Jeff Mitchell <jeff@tomahawk-player.org>
  5. *
  6. * Tomahawk is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Tomahawk is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "SourcePlaylistInterface.h"
  20. #include "audio/AudioEngine.h"
  21. #include "utils/Logger.h"
  22. #include "Pipeline.h"
  23. #include "Result.h"
  24. #include "Source.h"
  25. using namespace Tomahawk;
  26. SourcePlaylistInterface::SourcePlaylistInterface( Tomahawk::Source* source, Tomahawk::PlaylistModes::LatchMode latchMode )
  27. : PlaylistInterface()
  28. , m_source( source )
  29. , m_currentItem( 0 )
  30. , m_gotNextItem( false )
  31. {
  32. setLatchMode( latchMode );
  33. if ( !m_source.isNull() )
  34. connect( m_source.data(), SIGNAL( playbackStarted( const Tomahawk::track_ptr& ) ), SLOT( onSourcePlaybackStarted( const Tomahawk::track_ptr& ) ) );
  35. }
  36. SourcePlaylistInterface::~SourcePlaylistInterface()
  37. {
  38. m_source = 0;
  39. }
  40. void
  41. SourcePlaylistInterface::setCurrentIndex( qint64 index )
  42. {
  43. if ( index == 1 )
  44. m_gotNextItem = false;
  45. }
  46. qint64
  47. SourcePlaylistInterface::indexOfResult( const Tomahawk::result_ptr& result ) const
  48. {
  49. if ( nextResult() == result )
  50. return 1;
  51. else
  52. return -1;
  53. }
  54. qint64
  55. SourcePlaylistInterface::siblingIndex( int itemsAway, qint64 rootIndex ) const
  56. {
  57. Q_UNUSED( itemsAway );
  58. Q_UNUSED( rootIndex );
  59. if ( nextResult() )
  60. return 1;
  61. else
  62. return -1;
  63. }
  64. Tomahawk::result_ptr
  65. SourcePlaylistInterface::nextResult() const
  66. {
  67. if ( !sourceValid() )
  68. {
  69. tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "Source no longer valid";
  70. m_currentItem = Tomahawk::result_ptr();
  71. return m_currentItem;
  72. }
  73. else if ( !hasNextResult() )
  74. {
  75. tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "This song was already fetched or the source isn't playing anything";
  76. return Tomahawk::result_ptr();
  77. }
  78. if ( m_source.data()->currentTrack()->numResults() )
  79. {
  80. m_currentItem = m_source.data()->currentTrack()->results().first();
  81. }
  82. else
  83. m_currentItem = result_ptr();
  84. return m_currentItem;
  85. }
  86. result_ptr
  87. SourcePlaylistInterface::currentItem() const
  88. {
  89. return m_currentItem;
  90. }
  91. bool
  92. SourcePlaylistInterface::sourceValid() const
  93. {
  94. tDebug( LOGEXTRA ) << Q_FUNC_INFO;
  95. if ( m_source.isNull() || m_source.data()->currentTrack().isNull() )
  96. return false;
  97. return true;
  98. }
  99. bool
  100. SourcePlaylistInterface::hasNextResult() const
  101. {
  102. if ( !sourceValid() )
  103. return false;
  104. return m_gotNextItem;
  105. }
  106. QList<Tomahawk::query_ptr>
  107. SourcePlaylistInterface::tracks() const
  108. {
  109. QList<Tomahawk::query_ptr> tracks;
  110. if ( nextResult() )
  111. tracks << nextResult()->toQuery();
  112. return tracks;
  113. }
  114. QPointer< Tomahawk::Source >
  115. SourcePlaylistInterface::source() const
  116. {
  117. return m_source;
  118. }
  119. void
  120. SourcePlaylistInterface::reset()
  121. {
  122. if ( m_currentItem.isNull() )
  123. m_gotNextItem = false;
  124. else
  125. m_gotNextItem = true;
  126. }
  127. void
  128. SourcePlaylistInterface::onSourcePlaybackStarted( const Tomahawk::track_ptr& track )
  129. {
  130. tDebug( LOGVERBOSE ) << Q_FUNC_INFO;
  131. query_ptr query = track->toQuery();
  132. connect( query.data(), SIGNAL( resolvingFinished( bool ) ), SLOT( resolvingFinished( bool ) ) );
  133. Pipeline::instance()->resolve( query );
  134. m_gotNextItem = false;
  135. }
  136. void
  137. SourcePlaylistInterface::resolvingFinished( bool hasResults )
  138. {
  139. tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "Has results?" << ( hasResults ? "true" : "false" );
  140. if ( hasResults )
  141. {
  142. m_gotNextItem = true;
  143. }
  144. emit nextTrackAvailable( hasResults );
  145. }
  146. Tomahawk::query_ptr
  147. SourcePlaylistInterface::queryAt( qint64 index ) const
  148. {
  149. if ( index == 1 )
  150. {
  151. Tomahawk::result_ptr res = nextResult();
  152. return res->toQuery();
  153. }
  154. else
  155. return Tomahawk::query_ptr();
  156. }
  157. Tomahawk::result_ptr
  158. SourcePlaylistInterface::resultAt( qint64 index ) const
  159. {
  160. if ( index == 1 )
  161. return nextResult();
  162. else
  163. return Tomahawk::result_ptr();
  164. }