PageRenderTime 26ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/src/libtomahawk/LatchManager.cpp

http://github.com/tomahawk-player/tomahawk
C++ | 175 lines | 115 code | 36 blank | 24 comment | 11 complexity | 8b159f2e72c21aaa3b7aeb05f8b84599 MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause, GPL-3.0, GPL-2.0
  1. /* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
  2. *
  3. * Copyright 2010-2011, Leo Franchi <lfranchi@kde.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 "LatchManager.h"
  20. #include "ActionCollection.h"
  21. #include "audio/AudioEngine.h"
  22. #include "database/Database.h"
  23. #include "SourceList.h"
  24. #include "database/DatabaseCommand_SocialAction.h"
  25. #include "SourcePlaylistInterface.h"
  26. #include <QAction>
  27. using namespace Tomahawk;
  28. LatchManager::LatchManager( QObject* parent )
  29. : QObject( parent )
  30. , m_state( NotLatched )
  31. {
  32. connect( AudioEngine::instance(), SIGNAL( playlistChanged( Tomahawk::playlistinterface_ptr ) ), this, SLOT( playlistChanged( Tomahawk::playlistinterface_ptr ) ) );
  33. connect( AudioEngine::instance(), SIGNAL( paused() ), SLOT( audioPaused() ) );
  34. }
  35. LatchManager::~LatchManager()
  36. {
  37. }
  38. bool
  39. LatchManager::isLatched( const source_ptr& src )
  40. {
  41. return m_state == Latched && m_latchedOnTo == src;
  42. }
  43. void
  44. LatchManager::latchRequest( const source_ptr& source )
  45. {
  46. qDebug() << Q_FUNC_INFO;
  47. if ( isLatched( source ) )
  48. return;
  49. m_state = Latching;
  50. m_waitingForLatch = source;
  51. AudioEngine::instance()->playItem( source->playlistInterface(), source->playlistInterface()->nextResult() );
  52. }
  53. void
  54. LatchManager::playlistChanged( Tomahawk::playlistinterface_ptr )
  55. {
  56. // If we were latched on and changed, send the listening along stop
  57. if ( m_latchedOnTo.isNull() )
  58. {
  59. if ( m_waitingForLatch.isNull() )
  60. return; // Neither latched on nor waiting to be latched on, no-op
  61. m_latchedOnTo = m_waitingForLatch;
  62. m_latchedInterface = m_waitingForLatch->playlistInterface();
  63. m_waitingForLatch.clear();
  64. m_state = Latched;
  65. DatabaseCommand_SocialAction* cmd = new DatabaseCommand_SocialAction();
  66. cmd->setSource( SourceList::instance()->getLocal() );
  67. cmd->setAction( "latchOn");
  68. cmd->setComment( m_latchedOnTo->nodeId() );
  69. cmd->setTimestamp( QDateTime::currentDateTime().toTime_t() );
  70. Database::instance()->enqueue( Tomahawk::dbcmd_ptr( cmd ) );
  71. QAction *latchOnAction = ActionCollection::instance()->getAction( "latchOn" );
  72. latchOnAction->setText( tr( "&Catch Up" ) );
  73. latchOnAction->setIcon( QIcon() );
  74. // If not, then keep waiting
  75. return;
  76. }
  77. // We're current latched, and the user changed playlist, so stop
  78. SourcePlaylistInterface* origsourcepi = dynamic_cast< SourcePlaylistInterface* >( m_latchedInterface.data() );
  79. Q_ASSERT( origsourcepi );
  80. const source_ptr source = SourceList::instance()->get( origsourcepi->source()->id() );
  81. DatabaseCommand_SocialAction* cmd = new DatabaseCommand_SocialAction();
  82. cmd->setSource( SourceList::instance()->getLocal() );
  83. cmd->setAction( "latchOff");
  84. cmd->setComment( source->nodeId() );
  85. cmd->setTimestamp( QDateTime::currentDateTime().toTime_t() );
  86. Database::instance()->enqueue( Tomahawk::dbcmd_ptr( cmd ) );
  87. if ( !m_waitingForLatch.isNull() &&
  88. m_waitingForLatch != m_latchedOnTo )
  89. {
  90. // We are asked to latch on immediately to another source
  91. m_latchedOnTo.clear();
  92. m_latchedInterface.clear();
  93. // call ourselves to hit the "create latch" condition
  94. playlistChanged( Tomahawk::playlistinterface_ptr() );
  95. return;
  96. }
  97. m_latchedOnTo.clear();
  98. m_waitingForLatch.clear();
  99. m_latchedInterface.clear();
  100. m_state = NotLatched;
  101. QAction *latchOnAction = ActionCollection::instance()->getAction( "latchOn" );
  102. latchOnAction->setText( tr( "&Listen Along" ) );
  103. latchOnAction->setIcon( QIcon( RESPATH "images/headphones-sidebar.png" ) );
  104. }
  105. void
  106. LatchManager::audioPaused()
  107. {
  108. if ( !m_latchedOnTo.isNull() )
  109. {
  110. SourcePlaylistInterface* plInterface = qobject_cast< SourcePlaylistInterface* >( m_latchedOnTo->playlistInterface().data() );
  111. Q_ASSERT( plInterface );
  112. plInterface->audioPaused();
  113. }
  114. }
  115. void
  116. LatchManager::catchUpRequest()
  117. {
  118. //it's a catch-up -- logic in audioengine should take care of it
  119. AudioEngine::instance()->next();
  120. }
  121. void
  122. LatchManager::unlatchRequest( const source_ptr& source )
  123. {
  124. Q_UNUSED( source );
  125. AudioEngine::instance()->stop();
  126. AudioEngine::instance()->setPlaylist( Tomahawk::playlistinterface_ptr() );
  127. QAction *latchOnAction = ActionCollection::instance()->getAction( "latchOn" );
  128. latchOnAction->setText( tr( "&Listen Along" ) );
  129. latchOnAction->setIcon( QIcon( RESPATH "images/headphones-sidebar.png" ) );
  130. }
  131. void
  132. LatchManager::latchModeChangeRequest( const Tomahawk::source_ptr& source, bool realtime )
  133. {
  134. if ( !isLatched( source ) )
  135. return;
  136. source->playlistInterface()->setLatchMode( realtime ? Tomahawk::PlaylistModes::RealTime : Tomahawk::PlaylistModes::StayOnSong );
  137. if ( realtime )
  138. catchUpRequest();
  139. }