PageRenderTime 213ms CodeModel.GetById 100ms app.highlight 16ms RepoModel.GetById 95ms app.codeStats 0ms

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