/src/scrobbler.cpp

http://github.com/tomahawk-player/tomahawk · C++ · 174 lines · 114 code · 41 blank · 19 comment · 14 complexity · 9873a1650b7dfd39b60641defda28306 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-2011, 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 "Scrobbler.h"
  20. #include "Artist.h"
  21. #include "Album.h"
  22. #include "Source.h"
  23. #include "Typedefs.h"
  24. #include "TomahawkSettings.h"
  25. #include "audio/AudioEngine.h"
  26. #include "infosystem/InfoSystem.h"
  27. #include "utils/Logger.h"
  28. #include <QDir>
  29. #include <QSettings>
  30. #include <QCryptographicHash>
  31. static QString s_scInfoIdentifier = QString( "SCROBBLER" );
  32. Scrobbler::Scrobbler( QObject* parent )
  33. : QObject( parent )
  34. , m_reachedScrobblePoint( false )
  35. {
  36. connect( AudioEngine::instance(), SIGNAL( timerSeconds( unsigned int ) ),
  37. SLOT( engineTick( unsigned int ) ), Qt::QueuedConnection );
  38. connect( Tomahawk::InfoSystem::InfoSystem::instance(),
  39. SIGNAL( info( Tomahawk::InfoSystem::InfoRequestData, QVariant ) ),
  40. SLOT( infoSystemInfo( Tomahawk::InfoSystem::InfoRequestData, QVariant ) ) );
  41. connect( AudioEngine::instance(), SIGNAL( started( const Tomahawk::result_ptr& ) ),
  42. SLOT( trackStarted( const Tomahawk::result_ptr& ) ), Qt::QueuedConnection );
  43. connect( AudioEngine::instance(), SIGNAL( paused() ),
  44. SLOT( trackPaused() ), Qt::QueuedConnection );
  45. connect( AudioEngine::instance(), SIGNAL( resumed() ),
  46. SLOT( trackResumed() ), Qt::QueuedConnection );
  47. connect( AudioEngine::instance(), SIGNAL( stopped() ),
  48. SLOT( trackStopped() ), Qt::QueuedConnection );
  49. connect( Tomahawk::InfoSystem::InfoSystem::instance(), SIGNAL( finished( QString ) ), SLOT( infoSystemFinished( QString ) ) );
  50. }
  51. Scrobbler::~Scrobbler()
  52. {
  53. }
  54. void
  55. Scrobbler::trackStarted( const Tomahawk::result_ptr& track )
  56. {
  57. Q_ASSERT( QThread::currentThread() == thread() );
  58. if ( m_reachedScrobblePoint )
  59. {
  60. m_reachedScrobblePoint = false;
  61. scrobble();
  62. }
  63. Tomahawk::InfoSystem::InfoStringHash trackInfo;
  64. trackInfo["title"] = track->track();
  65. trackInfo["artist"] = track->artist()->name();
  66. trackInfo["album"] = track->album()->name();
  67. trackInfo["duration"] = QString::number( track->duration() );
  68. trackInfo["albumpos"] = QString::number( track->albumpos() );
  69. QVariantMap playInfo;
  70. playInfo["trackinfo"] = QVariant::fromValue< Tomahawk::InfoSystem::InfoStringHash >( trackInfo );
  71. playInfo["private"] = TomahawkSettings::instance()->privateListeningMode();
  72. Tomahawk::InfoSystem::InfoPushData pushData (
  73. s_scInfoIdentifier, Tomahawk::InfoSystem::InfoSubmitNowPlaying,
  74. playInfo,
  75. Tomahawk::InfoSystem::PushNoFlag );
  76. Tomahawk::InfoSystem::InfoSystem::instance()->pushInfo( pushData );
  77. // liblastfm forces 0-length tracks to scrobble after 4 minutes, stupid.
  78. if ( track->duration() == 0 )
  79. m_scrobblePoint = lastfm::ScrobblePoint( 30 );
  80. else
  81. m_scrobblePoint = lastfm::ScrobblePoint( track->duration() / 2 );
  82. }
  83. void
  84. Scrobbler::trackPaused()
  85. {
  86. Q_ASSERT( QThread::currentThread() == thread() );
  87. }
  88. void
  89. Scrobbler::trackResumed()
  90. {
  91. Q_ASSERT( QThread::currentThread() == thread() );
  92. }
  93. void
  94. Scrobbler::trackStopped()
  95. {
  96. Q_ASSERT( QThread::currentThread() == thread() );
  97. if ( m_reachedScrobblePoint )
  98. {
  99. m_reachedScrobblePoint = false;
  100. scrobble();
  101. }
  102. }
  103. void
  104. Scrobbler::engineTick( unsigned int secondsElapsed )
  105. {
  106. if ( secondsElapsed > m_scrobblePoint )
  107. m_reachedScrobblePoint = true;
  108. }
  109. void
  110. Scrobbler::scrobble()
  111. {
  112. Q_ASSERT( QThread::currentThread() == thread() );
  113. Tomahawk::InfoSystem::InfoPushData pushData (
  114. s_scInfoIdentifier, Tomahawk::InfoSystem::InfoSubmitScrobble,
  115. QVariant(), Tomahawk::InfoSystem::PushNoFlag );
  116. Tomahawk::InfoSystem::InfoSystem::instance()->pushInfo( pushData );
  117. }
  118. void
  119. Scrobbler::infoSystemInfo( Tomahawk::InfoSystem::InfoRequestData requestData, QVariant output )
  120. {
  121. Q_UNUSED( output );
  122. if ( requestData.caller == s_scInfoIdentifier )
  123. qDebug() << Q_FUNC_INFO;
  124. }
  125. void
  126. Scrobbler::infoSystemFinished( QString target )
  127. {
  128. if ( target == s_scInfoIdentifier )
  129. {
  130. qDebug() << Q_FUNC_INFO;
  131. qDebug() << "Scrobbler received done signal from InfoSystem";
  132. }
  133. }