diff --git a/src/tomahawk/web/Api_v2.cpp b/src/tomahawk/web/Api_v2.cpp index 497faf49f..e5a317cb8 100644 --- a/src/tomahawk/web/Api_v2.cpp +++ b/src/tomahawk/web/Api_v2.cpp @@ -26,15 +26,66 @@ Api_v2::Api_v2( QxtAbstractWebSessionManager* sm, QObject* parent ) { } + void -Api_v2::api( QxtWebRequestEvent *event, const QString &version, const QString &method ) +Api_v2::sendJsonOk( QxtWebRequestEvent* event ) +{ + QxtWebPageEvent * e = new QxtWebPageEvent( event->sessionID, event->requestID, "{ result: \"ok\" }" ); + e->contentType = "application/json"; + postEvent( e ); +} + + +void +Api_v2::sendJsonError( QxtWebRequestEvent* event, const QString& message ) +{ + QxtWebPageEvent * e = new QxtWebPageEvent( event->sessionID, event->requestID, QString( "{ result: \"error\", error: \"%1\" }" ).arg( message ).toUtf8().constData() ); + e->contentType = "application/json"; + e->status = 500; + e->statusMessage = "Method call failed."; + postEvent( e ); +} + + +void +Api_v2::apiCallFailed( QxtWebRequestEvent* event, const QString& method ) +{ + sendPlain404( event, QString( "Method \"%1\" for API 2.0 not found" ).arg( method ), "Method in API 2.0 not found" ); +} + + +void +Api_v2::api( QxtWebRequestEvent* event, const QString& version, const QString& method, const QString& arg1, const QString& arg2, const QString& arg3 ) { if ( version == "2.0" ) { - if ( !QMetaObject::invokeMethod( &m_apiv2_0, method.toAscii().constData(), Q_ARG( QxtWebRequestEvent*, event ) ) ) + if ( !arg3.isEmpty() ) { - // Method call failed, report failure - sendPlain404( event, QString( "Method \"%1\" for API 2.0 not found" ).arg( method ), "Method in API 2.0 not found" ); + if ( !QMetaObject::invokeMethod( &m_apiv2_0, method.toAscii().constData(), Q_ARG( QxtWebRequestEvent*, event ), Q_ARG( QString, arg1 ), Q_ARG( QString, arg2 ), Q_ARG( QString, arg3 ) ) ) + { + apiCallFailed(event, method); + } + } + else if ( !arg2.isEmpty() ) + { + if ( !QMetaObject::invokeMethod( &m_apiv2_0, method.toAscii().constData(), Q_ARG( QxtWebRequestEvent*, event ), Q_ARG( QString, arg1 ), Q_ARG( QString, arg2 ) ) ) + { + apiCallFailed(event, method); + } + } + else if ( !arg1.isEmpty() ) + { + if ( !QMetaObject::invokeMethod( &m_apiv2_0, method.toAscii().constData(), Q_ARG( QxtWebRequestEvent*, event ), Q_ARG( QString, arg1 ) ) ) + { + apiCallFailed(event, method); + } + } + else + { + if ( !QMetaObject::invokeMethod( &m_apiv2_0, method.toAscii().constData(), Q_ARG( QxtWebRequestEvent*, event ) ) ) + { + apiCallFailed(event, method); + } } } @@ -44,8 +95,9 @@ Api_v2::api( QxtWebRequestEvent *event, const QString &version, const QString &m } } + void -Api_v2::sendPlain404( QxtWebRequestEvent *event, const QString &message, const QString &statusmessage ) +Api_v2::sendPlain404( QxtWebRequestEvent* event, const QString& message, const QString& statusmessage ) { QxtWebPageEvent * e = new QxtWebPageEvent( event->sessionID, event->requestID, message.toUtf8() ); e->contentType = "text/plain"; diff --git a/src/tomahawk/web/Api_v2.h b/src/tomahawk/web/Api_v2.h index 55bf06949..5a106d1d0 100644 --- a/src/tomahawk/web/Api_v2.h +++ b/src/tomahawk/web/Api_v2.h @@ -30,16 +30,24 @@ class Api_v2 : public QxtWebSlotService Q_OBJECT public: Api_v2( QxtAbstractWebSessionManager* sm, QObject* parent = 0 ); - + + void sendJsonOk( QxtWebRequestEvent* event ); + void sendJsonError( QxtWebRequestEvent* event, const QString& message ); + void sendPlain404( QxtWebRequestEvent* event, const QString& message, const QString& statusmessage ); + signals: public slots: /** * All api (non-UI) calls with go to /api//method */ - void api( QxtWebRequestEvent* event, const QString& version, const QString& method ); + void api( QxtWebRequestEvent* event, const QString& version, const QString& method, const QString& arg1 = QString(), const QString& arg2 = QString(), const QString& arg3 = QString() ); + private: - void sendPlain404( QxtWebRequestEvent* event, const QString& message, const QString& statusmessage ); + /** + * Method call failed, report failure. + */ + void apiCallFailed( QxtWebRequestEvent* event, const QString& method); Api_v2_0 m_apiv2_0; }; diff --git a/src/tomahawk/web/Api_v2_0.cpp b/src/tomahawk/web/Api_v2_0.cpp index 222bd1dd9..610accd34 100644 --- a/src/tomahawk/web/Api_v2_0.cpp +++ b/src/tomahawk/web/Api_v2_0.cpp @@ -18,19 +18,120 @@ #include "Api_v2_0.h" +#include "audio/AudioEngine.h" +#include "utils/Logger.h" + +#include "Api_v2.h" +#include "Query.h" + #include #include -Api_v2_0::Api_v2_0( QxtWebSlotService *parent ) +#define JSON_ERROR( event, message ) { tLog( LOGVERBOSE ) << Q_FUNC_INFO << message; m_service->sendJsonError( event, message ); } + +Api_v2_0::Api_v2_0( Api_v2* parent ) : QObject( parent ) , m_service( parent ) { } void -Api_v2_0::ping( QxtWebRequestEvent *event ) +Api_v2_0::ping( QxtWebRequestEvent* event ) { QxtWebPageEvent * e = new QxtWebPageEvent( event->sessionID, event->requestID, "pong" ); e->contentType = "text/plain"; m_service->postEvent( e ); } + +void +Api_v2_0::playback( QxtWebRequestEvent* event, const QString& command ) +{ + if ( command == "next ") + { + if ( QMetaObject::invokeMethod( AudioEngine::instance(), "next", Qt::QueuedConnection ) ) + { + m_service->sendJsonOk( event ); + } + else + { + JSON_ERROR( event, "Skipping to the next track failed." ); + } + } + else if ( command == "previous" ) + { + if ( QMetaObject::invokeMethod( AudioEngine::instance(), "previous", Qt::QueuedConnection ) ) + { + m_service->sendJsonOk( event ); + } + else + { + JSON_ERROR( event, "Rewinding to the previous track failed." ); + } + } + else if ( command == "playpause" ) + { + if ( QMetaObject::invokeMethod( AudioEngine::instance(), "playpause", Qt::QueuedConnection ) ) + { + m_service->sendJsonOk( event ); + } + else + { + JSON_ERROR( event, "Play/Pause failed." ); + } + } + else if ( command == "play" ) + { + if ( QMetaObject::invokeMethod( AudioEngine::instance(), "play", Qt::QueuedConnection ) ) + { + m_service->sendJsonOk( event ); + } + else + { + JSON_ERROR( event, "Skipping to the next track failed." ); + } + } + else if ( command == "pause" ) + { + if ( QMetaObject::invokeMethod( AudioEngine::instance(), "pause", Qt::QueuedConnection ) ) + { + m_service->sendJsonOk( event ); + } + else + { + JSON_ERROR( event, "Skipping to the next track failed." ); + } + } + else if ( command == "stop" ) + { + if ( QMetaObject::invokeMethod( AudioEngine::instance(), "stop", Qt::QueuedConnection ) ) + { + m_service->sendJsonOk( event ); + } + else + { + JSON_ERROR( event, "Skipping to the next track failed." ); + } + } + else if ( command == "lowervolume" ) + { + if ( QMetaObject::invokeMethod( AudioEngine::instance(), "lowerVolume", Qt::QueuedConnection ) ) + { + m_service->sendJsonOk( event ); + } + else + { + JSON_ERROR( event, "Skipping to the next track failed." ); + } + } + else if ( command == "raisevolume" ) + { + if ( QMetaObject::invokeMethod( AudioEngine::instance(), "raiseVolume", Qt::QueuedConnection ) ) + { + m_service->sendJsonOk( event ); + } + else + { + JSON_ERROR( event, "Skipping to the next track failed." ); + } + } +} diff --git a/src/tomahawk/web/Api_v2_0.h b/src/tomahawk/web/Api_v2_0.h index e649cf212..53e6da851 100644 --- a/src/tomahawk/web/Api_v2_0.h +++ b/src/tomahawk/web/Api_v2_0.h @@ -21,6 +21,7 @@ #include +class Api_v2; class QxtWebRequestEvent; class QxtWebSlotService; @@ -28,7 +29,7 @@ class Api_v2_0 : public QObject { Q_OBJECT public: - Api_v2_0( QxtWebSlotService *parent = 0 ); + Api_v2_0( Api_v2* parent = 0 ); signals: @@ -39,8 +40,15 @@ public slots: * This call needs no authentication. */ void ping( QxtWebRequestEvent* event ); + + /** + * Control playback. + * + * This call needs to be authenticated. + */ + void playback( QxtWebRequestEvent* event, const QString& command ); private: - QxtWebSlotService* m_service; + Api_v2* m_service; }; #endif // API_V2_0_H