PageRenderTime 23ms CodeModel.GetById 1ms app.highlight 18ms RepoModel.GetById 1ms app.codeStats 0ms

/src/libtomahawk/SourcePlaylistInterface.cpp

http://github.com/tomahawk-player/tomahawk
C++ | 209 lines | 146 code | 45 blank | 18 comment | 20 complexity | d2afe0ee75eec468911512f4d87bb588 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-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 "SourcePlaylistInterface.h"
 21
 22#include "audio/AudioEngine.h"
 23#include "utils/Logger.h"
 24
 25#include "Pipeline.h"
 26#include "Result.h"
 27#include "Source.h"
 28
 29using namespace Tomahawk;
 30
 31
 32SourcePlaylistInterface::SourcePlaylistInterface( Tomahawk::Source* source, Tomahawk::PlaylistModes::LatchMode latchMode )
 33    : PlaylistInterface()
 34    , m_source( source )
 35    , m_currentItem( 0 )
 36    , m_gotNextItem( false )
 37{
 38    setLatchMode( latchMode );
 39
 40    if ( !m_source.isNull() )
 41        connect( m_source.data(), SIGNAL( playbackStarted( const Tomahawk::track_ptr& ) ), SLOT( onSourcePlaybackStarted( const Tomahawk::track_ptr& ) ) );
 42}
 43
 44
 45SourcePlaylistInterface::~SourcePlaylistInterface()
 46{
 47    m_source = 0;
 48}
 49
 50
 51void
 52SourcePlaylistInterface::setCurrentIndex( qint64 index )
 53{
 54    if ( index == 1 )
 55        m_gotNextItem = false;
 56}
 57
 58
 59qint64
 60SourcePlaylistInterface::indexOfResult( const Tomahawk::result_ptr& result ) const
 61{
 62    if ( nextResult() == result )
 63        return 1;
 64    else
 65        return -1;
 66}
 67
 68
 69qint64
 70SourcePlaylistInterface::siblingIndex( int itemsAway, qint64 rootIndex ) const
 71{
 72    Q_UNUSED( itemsAway );
 73    Q_UNUSED( rootIndex );
 74
 75    if ( nextResult() )
 76        return 1;
 77    else
 78        return -1;
 79}
 80
 81
 82Tomahawk::result_ptr
 83SourcePlaylistInterface::nextResult() const
 84{
 85    if ( !sourceValid() )
 86    {
 87        tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "Source no longer valid";
 88        m_currentItem = Tomahawk::result_ptr();
 89        return m_currentItem;
 90    }
 91    else if ( !hasNextResult() )
 92    {
 93        tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "This song was already fetched or the source isn't playing anything";
 94        return Tomahawk::result_ptr();
 95    }
 96
 97    if ( m_source.data()->currentTrack()->numResults() )
 98    {
 99        m_currentItem = m_source.data()->currentTrack()->results().first();
100    }
101    else
102        m_currentItem = result_ptr();
103
104    return m_currentItem;
105}
106
107
108result_ptr
109SourcePlaylistInterface::currentItem() const
110{
111    return m_currentItem;
112}
113
114
115bool
116SourcePlaylistInterface::sourceValid() const
117{
118    tDebug( LOGEXTRA ) << Q_FUNC_INFO;
119    if ( m_source.isNull() || m_source.data()->currentTrack().isNull() )
120        return false;
121
122    return true;
123}
124
125
126bool
127SourcePlaylistInterface::hasNextResult() const
128{
129    if ( !sourceValid() )
130        return false;
131
132    return m_gotNextItem;
133}
134
135
136QList<Tomahawk::query_ptr>
137SourcePlaylistInterface::tracks() const
138{
139    QList<Tomahawk::query_ptr> tracks;
140    if ( nextResult() )
141        tracks << nextResult()->toQuery();
142
143    return tracks;
144}
145
146
147QPointer< Tomahawk::Source >
148SourcePlaylistInterface::source() const
149{
150    return m_source;
151}
152
153
154void
155SourcePlaylistInterface::reset()
156{
157    if ( m_currentItem.isNull() )
158        m_gotNextItem = false;
159    else
160        m_gotNextItem = true;
161}
162
163
164void
165SourcePlaylistInterface::onSourcePlaybackStarted( const Tomahawk::track_ptr& track )
166{
167    tDebug( LOGVERBOSE ) << Q_FUNC_INFO;
168
169    query_ptr query = track->toQuery();
170    connect( query.data(), SIGNAL( resolvingFinished( bool ) ), SLOT( resolvingFinished( bool ) ) );
171    Pipeline::instance()->resolve( query );
172    m_gotNextItem = false;
173}
174
175
176void
177SourcePlaylistInterface::resolvingFinished( bool hasResults )
178{
179    tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "Has results?" << ( hasResults ? "true" : "false" );
180    if ( hasResults )
181    {
182        m_gotNextItem = true;
183    }
184
185    emit nextTrackAvailable( hasResults );
186}
187
188
189Tomahawk::query_ptr
190SourcePlaylistInterface::queryAt( qint64 index ) const
191{
192    if ( index == 1 )
193    {
194        Tomahawk::result_ptr res = nextResult();
195        return res->toQuery();
196    }
197    else
198        return Tomahawk::query_ptr();
199}
200
201
202Tomahawk::result_ptr
203SourcePlaylistInterface::resultAt( qint64 index ) const
204{
205    if ( index == 1 )
206        return nextResult();
207    else
208        return Tomahawk::result_ptr();
209}