PageRenderTime 93ms CodeModel.GetById 40ms app.highlight 19ms RepoModel.GetById 31ms 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
  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
 20#include "LatchManager.h"
 21
 22#include "ActionCollection.h"
 23#include "audio/AudioEngine.h"
 24#include "database/Database.h"
 25
 26#include "SourceList.h"
 27#include "database/DatabaseCommand_SocialAction.h"
 28#include "SourcePlaylistInterface.h"
 29
 30#include <QAction>
 31
 32
 33using namespace Tomahawk;
 34
 35LatchManager::LatchManager( QObject* parent )
 36    : QObject( parent )
 37    , m_state( NotLatched )
 38{
 39    connect( AudioEngine::instance(), SIGNAL( playlistChanged( Tomahawk::playlistinterface_ptr ) ), this, SLOT( playlistChanged( Tomahawk::playlistinterface_ptr ) ) );
 40    connect( AudioEngine::instance(), SIGNAL( paused() ), SLOT( audioPaused() ) );
 41}
 42
 43LatchManager::~LatchManager()
 44{
 45
 46}
 47
 48
 49bool
 50LatchManager::isLatched( const source_ptr& src )
 51{
 52    return m_state == Latched && m_latchedOnTo == src;
 53}
 54
 55
 56void
 57LatchManager::latchRequest( const source_ptr& source )
 58{
 59    qDebug() << Q_FUNC_INFO;
 60    if ( isLatched( source ) )
 61        return;
 62
 63    m_state = Latching;
 64    m_waitingForLatch = source;
 65    AudioEngine::instance()->playItem( source->playlistInterface(), source->playlistInterface()->nextResult() );
 66}
 67
 68
 69void
 70LatchManager::playlistChanged( Tomahawk::playlistinterface_ptr )
 71{
 72    // If we were latched on and changed, send the listening along stop
 73    if ( m_latchedOnTo.isNull() )
 74    {
 75        if ( m_waitingForLatch.isNull() )
 76            return; // Neither latched on nor waiting to be latched on, no-op
 77
 78        m_latchedOnTo = m_waitingForLatch;
 79        m_latchedInterface = m_waitingForLatch->playlistInterface();
 80        m_waitingForLatch.clear();
 81        m_state = Latched;
 82
 83        DatabaseCommand_SocialAction* cmd = new DatabaseCommand_SocialAction();
 84        cmd->setSource( SourceList::instance()->getLocal() );
 85        cmd->setAction( "latchOn");
 86        cmd->setComment( m_latchedOnTo->nodeId() );
 87        cmd->setTimestamp( QDateTime::currentDateTime().toTime_t() );
 88        Database::instance()->enqueue( Tomahawk::dbcmd_ptr( cmd ) );
 89
 90        QAction *latchOnAction = ActionCollection::instance()->getAction( "latchOn" );
 91        latchOnAction->setText( tr( "&Catch Up" ) );
 92        latchOnAction->setIcon( QIcon() );
 93
 94        // If not, then keep waiting
 95        return;
 96    }
 97
 98    // We're current latched, and the user changed playlist, so stop
 99    SourcePlaylistInterface* origsourcepi = dynamic_cast< SourcePlaylistInterface* >( m_latchedInterface.data() );
100    Q_ASSERT( origsourcepi );
101    const source_ptr source = SourceList::instance()->get( origsourcepi->source()->id() );
102
103    DatabaseCommand_SocialAction* cmd = new DatabaseCommand_SocialAction();
104    cmd->setSource( SourceList::instance()->getLocal() );
105    cmd->setAction( "latchOff");
106    cmd->setComment( source->nodeId() );
107    cmd->setTimestamp( QDateTime::currentDateTime().toTime_t() );
108    Database::instance()->enqueue( Tomahawk::dbcmd_ptr( cmd ) );
109
110    if ( !m_waitingForLatch.isNull() &&
111          m_waitingForLatch != m_latchedOnTo )
112    {
113        // We are asked to latch on immediately to another source
114        m_latchedOnTo.clear();
115        m_latchedInterface.clear();
116
117        // call ourselves to hit the "create latch" condition
118        playlistChanged( Tomahawk::playlistinterface_ptr() );
119        return;
120    }
121    m_latchedOnTo.clear();
122    m_waitingForLatch.clear();
123    m_latchedInterface.clear();
124
125    m_state = NotLatched;
126
127    QAction *latchOnAction = ActionCollection::instance()->getAction( "latchOn" );
128    latchOnAction->setText( tr( "&Listen Along" ) );
129    latchOnAction->setIcon( QIcon( RESPATH "images/headphones-sidebar.png" ) );
130}
131
132
133void
134LatchManager::audioPaused()
135{
136    if ( !m_latchedOnTo.isNull() )
137    {
138        SourcePlaylistInterface* plInterface = qobject_cast< SourcePlaylistInterface* >( m_latchedOnTo->playlistInterface().data() );
139        Q_ASSERT( plInterface );
140        plInterface->audioPaused();
141    }
142}
143
144
145void
146LatchManager::catchUpRequest()
147{
148    //it's a catch-up -- logic in audioengine should take care of it
149    AudioEngine::instance()->next();
150}
151
152
153void
154LatchManager::unlatchRequest( const source_ptr& source )
155{
156    Q_UNUSED( source );
157    AudioEngine::instance()->stop();
158    AudioEngine::instance()->setPlaylist( Tomahawk::playlistinterface_ptr() );
159
160    QAction *latchOnAction = ActionCollection::instance()->getAction( "latchOn" );
161    latchOnAction->setText( tr( "&Listen Along" ) );
162    latchOnAction->setIcon( QIcon( RESPATH "images/headphones-sidebar.png" ) );
163}
164
165
166void
167LatchManager::latchModeChangeRequest( const Tomahawk::source_ptr& source, bool realtime )
168{
169    if ( !isLatched( source ) )
170        return;
171
172    source->playlistInterface()->setLatchMode( realtime ? Tomahawk::PlaylistModes::RealTime : Tomahawk::PlaylistModes::StayOnSong );
173    if ( realtime )
174        catchUpRequest();
175}