Пример #1
0
Track
PlayerCommandParser::extractTrack( const QMap<QChar, QString>& args )
{
    MutableTrack track;
    track.setArtist( args['a'] );
    track.setTitle( args['t'] );
    track.setAlbum( args['b'] );
    track.setMbid( Mbid( args['m'] ) );
    track.setDuration( args['l'].toInt() );
    track.setUrl( QUrl::fromLocalFile( QUrl::fromPercentEncoding( args['p'].toUtf8() ) ) );
    track.setSource( Track::Player );
    track.setExtra( "playerId", args['c'] );

    //TODO should be done earlier, NOTE don't get the plugin to send a stamp 
    // time as this is prolly unecessary, and I bet you get new bugs!
    track.stamp();
    
    return track;
}
void
SpotifyListenerWin::loop()
{
    m_windowTitle.clear();

    EnumWindows( SpotifyListenerWin::callback, reinterpret_cast<LPARAM>(this) );

    // stopped =
    // paused = Spotify
    // playing = Spotify - Allo, Darlin' – Kiss Your Lips

    State playerState = Stopped;

    if ( m_windowTitle.startsWith( "Spotify" ) )
    {
        if ( !m_connection )
            emit newConnection( m_connection = new SpotifyConnection );

        QRegExp re( QString( "^Spotify - (.+) %1 (.+)").arg( QChar( 0x2013 )  ), Qt::CaseSensitive, QRegExp::RegExp2 );

        playerState = re.indexIn( m_windowTitle ) == 0 ? Playing : Paused;

        if ( m_lastPlayerState != playerState )
        {
            if ( playerState == Stopped )
            {
                m_connection->stop();
                m_lastTrack = Track();
            }
            else if ( playerState == Paused )
            {
                if ( m_lastPlayerState == Playing )
                    m_connection->pause();
            }
            else if ( playerState == Playing )
            {
                MutableTrack t;
                t.setTitle( re.capturedTexts().at( 2 ) );
                t.setArtist( re.capturedTexts().at( 1 ) );
                // we don't know the duration, but we don't display it so just guess
                t.setDuration( 320 );

                if ( m_lastPlayerState == Paused && t == m_lastTrack )
                    m_connection->resume();
                else
                    m_connection->start( t );

                m_lastTrack = t;
            }
        }
        else if ( playerState == Playing )
        {
            // when going from one song to the next we stay in the play state
            MutableTrack t;
            t.setTitle( re.capturedTexts().at( 2 ) );
            t.setArtist( re.capturedTexts().at( 1 ) );
            // we don't know the duration, but we don't display it so just guess
            t.setDuration( 320 );

            if ( t != m_lastTrack )
                m_connection->start( t );

            m_lastTrack = t;
        }
    }
    else
    {
        if ( m_lastPlayerState == Playing || m_lastPlayerState == Paused )
            m_connection->stop();

        delete m_connection;
    }

    m_lastPlayerState = playerState;
}