-
Notifications
You must be signed in to change notification settings - Fork 364
Expand file tree
/
Copy pathLatchManager.cpp
More file actions
196 lines (153 loc) · 5.78 KB
/
LatchManager.cpp
File metadata and controls
196 lines (153 loc) · 5.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
*
* Copyright 2010-2011, Leo Franchi <[email protected]>
* Copyright 2010-2012, Jeff Mitchell <[email protected]>
*
* Tomahawk is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Tomahawk is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
*/
#include "LatchManager.h"
#include "ActionCollection.h"
#include "audio/AudioEngine.h"
#include "database/Database.h"
#include "SourceList.h"
#include "database/DatabaseCommand_SocialAction.h"
#include "SourcePlaylistInterface.h"
#include <QAction>
// Forward Declarations breaking QSharedPointer
#if QT_VERSION < QT_VERSION_CHECK( 5, 0, 0 )
#include "Result.h"
#endif
using namespace Tomahawk;
LatchManager* LatchManager::s_instance = 0;
LatchManager*
LatchManager::instance()
{
if ( !s_instance )
{
s_instance = new LatchManager();
}
return s_instance;
}
LatchManager::LatchManager( QObject* parent )
: QObject( parent )
, m_state( NotLatched )
{
connect( AudioEngine::instance(), SIGNAL( playlistChanged( Tomahawk::playlistinterface_ptr ) ), this, SLOT( playlistChanged( Tomahawk::playlistinterface_ptr ) ) );
connect( AudioEngine::instance(), SIGNAL( paused() ), SLOT( audioPaused() ) );
}
LatchManager::~LatchManager()
{
}
bool
LatchManager::isLatched( const source_ptr& src )
{
return m_state == Latched && m_latchedOnTo == src;
}
void
LatchManager::latchRequest( const source_ptr& source )
{
qDebug() << Q_FUNC_INFO;
if ( isLatched( source ) )
return;
m_state = Latching;
m_waitingForLatch = source;
AudioEngine::instance()->playItem( source->playlistInterface(), source->playlistInterface()->nextResult() );
}
void
LatchManager::playlistChanged( Tomahawk::playlistinterface_ptr )
{
// If we were latched on and changed, send the listening along stop
if ( m_latchedOnTo.isNull() )
{
if ( m_waitingForLatch.isNull() )
return; // Neither latched on nor waiting to be latched on, no-op
m_latchedOnTo = m_waitingForLatch;
m_latchedInterface = m_waitingForLatch->playlistInterface();
m_waitingForLatch.clear();
m_state = Latched;
DatabaseCommand_SocialAction* cmd = new DatabaseCommand_SocialAction();
cmd->setSource( SourceList::instance()->getLocal() );
cmd->setAction( "latchOn");
cmd->setComment( m_latchedOnTo->nodeId() );
cmd->setTimestamp( QDateTime::currentDateTime().toTime_t() );
Database::instance()->enqueue( Tomahawk::dbcmd_ptr( cmd ) );
QAction *latchOnAction = ActionCollection::instance()->getAction( "latchOn" );
latchOnAction->setText( tr( "&Catch Up" ) );
latchOnAction->setIcon( QIcon() );
// If not, then keep waiting
return;
}
// We're current latched, and the user changed playlist, so stop
SourcePlaylistInterface* origsourcepi = dynamic_cast< SourcePlaylistInterface* >( m_latchedInterface.data() );
Q_ASSERT( origsourcepi );
const source_ptr source = SourceList::instance()->get( origsourcepi->source()->id() );
DatabaseCommand_SocialAction* cmd = new DatabaseCommand_SocialAction();
cmd->setSource( SourceList::instance()->getLocal() );
cmd->setAction( "latchOff");
cmd->setComment( source->nodeId() );
cmd->setTimestamp( QDateTime::currentDateTime().toTime_t() );
Database::instance()->enqueue( Tomahawk::dbcmd_ptr( cmd ) );
if ( !m_waitingForLatch.isNull() &&
m_waitingForLatch != m_latchedOnTo )
{
// We are asked to latch on immediately to another source
m_latchedOnTo.clear();
m_latchedInterface.clear();
// call ourselves to hit the "create latch" condition
playlistChanged( Tomahawk::playlistinterface_ptr() );
return;
}
m_latchedOnTo.clear();
m_waitingForLatch.clear();
m_latchedInterface.clear();
m_state = NotLatched;
QAction *latchOnAction = ActionCollection::instance()->getAction( "latchOn" );
latchOnAction->setText( tr( "&Listen Along" ) );
latchOnAction->setIcon( QIcon( RESPATH "images/headphones-sidebar.png" ) );
}
void
LatchManager::audioPaused()
{
if ( !m_latchedOnTo.isNull() )
{
SourcePlaylistInterface* plInterface = qobject_cast< SourcePlaylistInterface* >( m_latchedOnTo->playlistInterface().data() );
Q_ASSERT( plInterface );
plInterface->audioPaused();
}
}
void
LatchManager::catchUpRequest()
{
//it's a catch-up -- logic in audioengine should take care of it
AudioEngine::instance()->next();
}
void
LatchManager::unlatchRequest( const source_ptr& source )
{
Q_UNUSED( source );
AudioEngine::instance()->stop();
AudioEngine::instance()->setPlaylist( Tomahawk::playlistinterface_ptr() );
QAction *latchOnAction = ActionCollection::instance()->getAction( "latchOn" );
latchOnAction->setText( tr( "&Listen Along" ) );
latchOnAction->setIcon( QIcon( RESPATH "images/headphones-sidebar.png" ) );
}
void
LatchManager::latchModeChangeRequest( const Tomahawk::source_ptr& source, bool realtime )
{
if ( !isLatched( source ) )
return;
source->playlistInterface()->setLatchMode( realtime ? Tomahawk::PlaylistModes::RealTime : Tomahawk::PlaylistModes::StayOnSong );
if ( realtime )
catchUpRequest();
}