~ubuntu-branches/ubuntu/trusty/tomahawk/trusty-proposed

« back to all changes in this revision

Viewing changes to src/libtomahawk/LatchManager.cpp

  • Committer: Package Import Robot
  • Author(s): Harald Sitter
  • Date: 2013-03-07 21:50:13 UTC
  • Revision ID: package-import@ubuntu.com-20130307215013-6gdjkdds7i9uenvs
Tags: upstream-0.6.0+dfsg
ImportĀ upstreamĀ versionĀ 0.6.0+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 <QtGui/QAction>
 
27
#include "SourceList.h"
 
28
#include "database/DatabaseCommand_SocialAction.h"
 
29
#include "SourcePlaylistInterface.h"
 
30
 
 
31
using namespace Tomahawk;
 
32
 
 
33
LatchManager::LatchManager( QObject* parent )
 
34
    : QObject( parent )
 
35
    , m_state( NotLatched )
 
36
{
 
37
    connect( AudioEngine::instance(), SIGNAL( playlistChanged( Tomahawk::playlistinterface_ptr ) ), this, SLOT( playlistChanged( Tomahawk::playlistinterface_ptr ) ) );
 
38
}
 
39
 
 
40
LatchManager::~LatchManager()
 
41
{
 
42
 
 
43
}
 
44
 
 
45
 
 
46
bool
 
47
LatchManager::isLatched( const source_ptr& src )
 
48
{
 
49
    return m_state == Latched && m_latchedOnTo == src;
 
50
}
 
51
 
 
52
 
 
53
void
 
54
LatchManager::latchRequest( const source_ptr& source )
 
55
{
 
56
    qDebug() << Q_FUNC_INFO;
 
57
    if ( isLatched( source ) )
 
58
        return;
 
59
 
 
60
    m_state = Latching;
 
61
    m_waitingForLatch = source;
 
62
    AudioEngine::instance()->playItem( source->playlistInterface(), source->playlistInterface()->nextResult() );
 
63
}
 
64
 
 
65
 
 
66
void
 
67
LatchManager::playlistChanged( Tomahawk::playlistinterface_ptr )
 
68
{
 
69
    // If we were latched on and changed, send the listening along stop
 
70
    if ( m_latchedOnTo.isNull() )
 
71
    {
 
72
        if ( m_waitingForLatch.isNull() )
 
73
            return; // Neither latched on nor waiting to be latched on, no-op
 
74
 
 
75
        m_latchedOnTo = m_waitingForLatch;
 
76
        m_latchedInterface = m_waitingForLatch->playlistInterface();
 
77
        m_waitingForLatch.clear();
 
78
        m_state = Latched;
 
79
 
 
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 ) );
 
86
 
 
87
        QAction *latchOnAction = ActionCollection::instance()->getAction( "latchOn" );
 
88
        latchOnAction->setText( tr( "&Catch Up" ) );
 
89
        latchOnAction->setIcon( QIcon() );
 
90
 
 
91
        // If not, then keep waiting
 
92
        return;
 
93
    }
 
94
 
 
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();
 
99
 
 
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 ) );
 
106
 
 
107
    if ( !m_waitingForLatch.isNull() &&
 
108
          m_waitingForLatch != m_latchedOnTo )
 
109
    {
 
110
        // We are asked to latch on immediately to another source
 
111
        m_latchedOnTo.clear();
 
112
        m_latchedInterface.clear();
 
113
 
 
114
        // call ourselves to hit the "create latch" condition
 
115
        playlistChanged( Tomahawk::playlistinterface_ptr() );
 
116
        return;
 
117
    }
 
118
    m_latchedOnTo.clear();
 
119
    m_waitingForLatch.clear();
 
120
    m_latchedInterface.clear();
 
121
 
 
122
    m_state = NotLatched;
 
123
 
 
124
    QAction *latchOnAction = ActionCollection::instance()->getAction( "latchOn" );
 
125
    latchOnAction->setText( tr( "&Listen Along" ) );
 
126
    latchOnAction->setIcon( QIcon( RESPATH "images/headphones-sidebar.png" ) );
 
127
}
 
128
 
 
129
 
 
130
void
 
131
LatchManager::catchUpRequest()
 
132
{
 
133
    //it's a catch-up -- logic in audioengine should take care of it
 
134
    AudioEngine::instance()->next();
 
135
}
 
136
 
 
137
 
 
138
void
 
139
LatchManager::unlatchRequest( const source_ptr& source )
 
140
{
 
141
    Q_UNUSED( source );
 
142
    AudioEngine::instance()->stop();
 
143
    AudioEngine::instance()->setPlaylist( Tomahawk::playlistinterface_ptr() );
 
144
 
 
145
    QAction *latchOnAction = ActionCollection::instance()->getAction( "latchOn" );
 
146
    latchOnAction->setText( tr( "&Listen Along" ) );
 
147
    latchOnAction->setIcon( QIcon( RESPATH "images/headphones-sidebar.png" ) );
 
148
}
 
149
 
 
150
 
 
151
void
 
152
LatchManager::latchModeChangeRequest( const Tomahawk::source_ptr& source, bool realtime )
 
153
{
 
154
    if ( !isLatched( source ) )
 
155
        return;
 
156
 
 
157
    source->playlistInterface()->setLatchMode( realtime ? Tomahawk::PlaylistModes::RealTime : Tomahawk::PlaylistModes::StayOnSong );
 
158
    if ( realtime )
 
159
        catchUpRequest();
 
160
}