Esempio n. 1
0
void
Api_v1_5::playback( QxtWebRequestEvent* event, const QString& command )
{
    if ( command == "next")
    {
        JSON_REPLY( QMetaObject::invokeMethod( AudioEngine::instance(), "next", Qt::QueuedConnection ) , "Skipping to the next track failed." );
    }
    else if ( command == "previous" )
    {
        JSON_REPLY( QMetaObject::invokeMethod( AudioEngine::instance(), "previous", Qt::QueuedConnection ), "Rewinding to the previous track failed." );
    }
    else if ( command == "playpause" )
    {
        JSON_REPLY( QMetaObject::invokeMethod( AudioEngine::instance(), "playPause", Qt::QueuedConnection ), "Play/Pause failed." );
    }
    else if ( command == "play" )
    {
        JSON_REPLY( QMetaObject::invokeMethod( AudioEngine::instance(), "play", Qt::QueuedConnection ), "Starting the playback failed." );
    }
    else if ( command == "pause" )
    {
        JSON_REPLY( QMetaObject::invokeMethod( AudioEngine::instance(), "pause", Qt::QueuedConnection ), "Pausing the current track failed." );
    }
    else if ( command == "stop" )
    {
        JSON_REPLY( QMetaObject::invokeMethod( AudioEngine::instance(), "stop", Qt::QueuedConnection ), "Stopping the current track failed." );
    }
    else if ( command == "lowervolume" )
    {
        JSON_REPLY( QMetaObject::invokeMethod( AudioEngine::instance(), "lowerVolume", Qt::QueuedConnection ), "Lowering volume failed." );
    }
    else if ( command == "raisevolume" )
    {
        JSON_REPLY( QMetaObject::invokeMethod( AudioEngine::instance(), "raiseVolume", Qt::QueuedConnection ), "Raising volume failed." );
    }
    else if ( command == "currenttrack" )
    {
        QByteArray json;
        Tomahawk::result_ptr currentTrack =  AudioEngine::instance()->currentTrack();

        if ( currentTrack.isNull() )
        {
            json = "{ \"playing\": false }";
        }
        else
        {
            QVariantMap trackInfo;
            trackInfo.insert( "playing", true );
            trackInfo.insert( "paused", AudioEngine::instance()->isPaused() );
            trackInfo.insert( "position", AudioEngine::instance()->currentTime() / 1000 );
            trackInfo.insert( "bitrate", currentTrack->bitrate() );
            if ( currentTrack->resolvedBy() ) {
                QString resolverName = currentTrack->resolvedBy()->name();
                trackInfo.insert( "resolvedBy", resolverName );
            } else {
                trackInfo.insert( "resolvedBy", "<unknown resolver>" );
            }
            trackInfo.insert( "score", currentTrack->score() );
            trackInfo.insert( "album", currentTrack->track()->album() );
            trackInfo.insert( "albumpos", currentTrack->track()->albumpos() );
            trackInfo.insert( "artist", currentTrack->track()->artist() );
            trackInfo.insert( "duration", currentTrack->track()->duration() );
            trackInfo.insert( "track", currentTrack->track()->track() );

            bool ok;
            json = TomahawkUtils::toJson( trackInfo, &ok );
            Q_ASSERT( ok );
        }

        QxtWebPageEvent * e = new QxtWebPageEvent( event->sessionID, event->requestID, json );
        e->headers.insert( "Access-Control-Allow-Origin", "*" );
        e->contentType = "application/json";
        m_service->postEvent( e );
    }
    else if ( command == "volume" )
    {
        QByteArray json = QString( "{ \"result\": \"ok\", \"volume\": %1}" ).arg( AudioEngine::instance()->volume() ).toUtf8();
        QxtWebPageEvent * e = new QxtWebPageEvent( event->sessionID, event->requestID, json );
        e->headers.insert( "Access-Control-Allow-Origin", "*" );
        e->contentType = "application/json";
        m_service->postEvent( e );
    }
    else
    {
        m_service->sendJsonError( event, "No such playback command." );
    }
}