From b385722d31f2e9d390661de9d9d6accb865d33c0 Mon Sep 17 00:00:00 2001 From: "Uwe L. Korn" Date: Tue, 28 Jan 2014 22:40:48 +0000 Subject: [PATCH] Add remote controlling of playback --- src/libtomahawk-playdarapi/Api_v1_5.cpp | 62 +++++++++++++++++++++++++ src/libtomahawk-playdarapi/Api_v1_5.h | 8 ++++ 2 files changed, 70 insertions(+) diff --git a/src/libtomahawk-playdarapi/Api_v1_5.cpp b/src/libtomahawk-playdarapi/Api_v1_5.cpp index ac3864b1e..21c7c26ea 100644 --- a/src/libtomahawk-playdarapi/Api_v1_5.cpp +++ b/src/libtomahawk-playdarapi/Api_v1_5.cpp @@ -20,6 +20,13 @@ #include "Api_v1.h" +#include "audio/AudioEngine.h" +#include "utils/Logger.h" + +// Assumptions: QxtWebRequestEvent instance is called event and result is true on success +#define JSON_REPLY( result, message ) jsonReply( event, Q_FUNC_INFO, message, !result ) + + Api_v1_5::Api_v1_5( Api_v1* parent ) : QObject( parent ) , m_service( parent ) @@ -35,3 +42,58 @@ Api_v1_5::ping( QxtWebRequestEvent* event ) m_service->postEvent( e ); } +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 + { + m_service->sendJsonError( event, "No such playback command." ); + } +} + +void +Api_v1_5::jsonReply( QxtWebRequestEvent* event, const char* funcInfo, const QString& errorMessage, bool isError ) +{ + if ( isError ) + { + tLog( LOGVERBOSE ) << funcInfo << errorMessage; + m_service->sendJsonError( event, errorMessage ); + } + else + { + m_service->sendJsonOk( event ); + } +} + diff --git a/src/libtomahawk-playdarapi/Api_v1_5.h b/src/libtomahawk-playdarapi/Api_v1_5.h index 6a0c5edb5..bab5af035 100644 --- a/src/libtomahawk-playdarapi/Api_v1_5.h +++ b/src/libtomahawk-playdarapi/Api_v1_5.h @@ -40,6 +40,14 @@ public slots: */ void ping( QxtWebRequestEvent* event ); + /** + * Control playback. + */ + void playback( QxtWebRequestEvent* event, const QString& command ); + +protected: + void jsonReply( QxtWebRequestEvent* event, const char* funcInfo, const QString& errorMessage, bool isError ); + private: Api_v1* m_service; };