1
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
3
* Copyright 2010-2011, Leo Franchi <lfranchi@kde.org>
4
* Copyright 2010-2012, Jeff Mitchell <jeff@tomahawk-player.org>
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.
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.
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/>.
20
#include "LatchManager.h"
22
#include "ActionCollection.h"
23
#include "audio/AudioEngine.h"
24
#include "database/Database.h"
26
#include <QtGui/QAction>
27
#include "SourceList.h"
28
#include "database/DatabaseCommand_SocialAction.h"
29
#include "SourcePlaylistInterface.h"
31
using namespace Tomahawk;
33
LatchManager::LatchManager( QObject* parent )
35
, m_state( NotLatched )
37
connect( AudioEngine::instance(), SIGNAL( playlistChanged( Tomahawk::playlistinterface_ptr ) ), this, SLOT( playlistChanged( Tomahawk::playlistinterface_ptr ) ) );
40
LatchManager::~LatchManager()
47
LatchManager::isLatched( const source_ptr& src )
49
return m_state == Latched && m_latchedOnTo == src;
54
LatchManager::latchRequest( const source_ptr& source )
56
qDebug() << Q_FUNC_INFO;
57
if ( isLatched( source ) )
61
m_waitingForLatch = source;
62
AudioEngine::instance()->playItem( source->playlistInterface(), source->playlistInterface()->nextResult() );
67
LatchManager::playlistChanged( Tomahawk::playlistinterface_ptr )
69
// If we were latched on and changed, send the listening along stop
70
if ( m_latchedOnTo.isNull() )
72
if ( m_waitingForLatch.isNull() )
73
return; // Neither latched on nor waiting to be latched on, no-op
75
m_latchedOnTo = m_waitingForLatch;
76
m_latchedInterface = m_waitingForLatch->playlistInterface();
77
m_waitingForLatch.clear();
80
DatabaseCommand_SocialAction* cmd = new DatabaseCommand_SocialAction();
81
cmd->setSource( SourceList::instance()->getLocal() );
82
cmd->setAction( "latchOn");
83
cmd->setComment( m_latchedOnTo->userName() );
84
cmd->setTimestamp( QDateTime::currentDateTime().toTime_t() );
85
Database::instance()->enqueue( QSharedPointer< DatabaseCommand >( cmd ) );
87
QAction *latchOnAction = ActionCollection::instance()->getAction( "latchOn" );
88
latchOnAction->setText( tr( "&Catch Up" ) );
89
latchOnAction->setIcon( QIcon() );
91
// If not, then keep waiting
95
// We're current latched, and the user changed playlist, so stop
96
SourcePlaylistInterface* origsourcepi = dynamic_cast< SourcePlaylistInterface* >( m_latchedInterface.data() );
97
Q_ASSERT( origsourcepi );
98
const source_ptr source = origsourcepi->source();
100
DatabaseCommand_SocialAction* cmd = new DatabaseCommand_SocialAction();
101
cmd->setSource( SourceList::instance()->getLocal() );
102
cmd->setAction( "latchOff");
103
cmd->setComment( source->userName() );
104
cmd->setTimestamp( QDateTime::currentDateTime().toTime_t() );
105
Database::instance()->enqueue( QSharedPointer< DatabaseCommand >( cmd ) );
107
if ( !m_waitingForLatch.isNull() &&
108
m_waitingForLatch != m_latchedOnTo )
110
// We are asked to latch on immediately to another source
111
m_latchedOnTo.clear();
112
m_latchedInterface.clear();
114
// call ourselves to hit the "create latch" condition
115
playlistChanged( Tomahawk::playlistinterface_ptr() );
118
m_latchedOnTo.clear();
119
m_waitingForLatch.clear();
120
m_latchedInterface.clear();
122
m_state = NotLatched;
124
QAction *latchOnAction = ActionCollection::instance()->getAction( "latchOn" );
125
latchOnAction->setText( tr( "&Listen Along" ) );
126
latchOnAction->setIcon( QIcon( RESPATH "images/headphones-sidebar.png" ) );
131
LatchManager::catchUpRequest()
133
//it's a catch-up -- logic in audioengine should take care of it
134
AudioEngine::instance()->next();
139
LatchManager::unlatchRequest( const source_ptr& source )
142
AudioEngine::instance()->stop();
143
AudioEngine::instance()->setPlaylist( Tomahawk::playlistinterface_ptr() );
145
QAction *latchOnAction = ActionCollection::instance()->getAction( "latchOn" );
146
latchOnAction->setText( tr( "&Listen Along" ) );
147
latchOnAction->setIcon( QIcon( RESPATH "images/headphones-sidebar.png" ) );
152
LatchManager::latchModeChangeRequest( const Tomahawk::source_ptr& source, bool realtime )
154
if ( !isLatched( source ) )
157
source->playlistInterface()->setLatchMode( realtime ? Tomahawk::PlaylistModes::RealTime : Tomahawk::PlaylistModes::StayOnSong );