diff --git a/src/libtomahawk-playdarapi/Api_v1.cpp b/src/libtomahawk-playdarapi/Api_v1.cpp index 28622e777..ee8d310d2 100644 --- a/src/libtomahawk-playdarapi/Api_v1.cpp +++ b/src/libtomahawk-playdarapi/Api_v1.cpp @@ -39,11 +39,61 @@ using namespace Tomahawk; using namespace TomahawkUtils; -Api_v1::Api_v1(QxtAbstractWebSessionManager* sm, QObject* parent) +QPointer< Api_v1 > Api_v1::s_instance = NULL; +QPointer< QxtHttpServerConnector > Api_v1::s_connector; +QPointer< QxtHttpSessionManager > Api_v1::s_session; + + +Api_v1::Api_v1( QxtAbstractWebSessionManager* sm, QObject* parent ) : QxtWebSlotService(sm, parent) { } +Api_v1* +Api_v1::startInstance( QHostAddress interface, qint16 port ) +{ + if ( !s_session.isNull() ) + { + tLog() << "HTTPd session already exists, returning"; + return NULL; + } + + s_session = QPointer< QxtHttpSessionManager >( new QxtHttpSessionManager() ); + s_connector = QPointer< QxtHttpServerConnector >( new QxtHttpServerConnector ); + if ( s_session.isNull() || s_connector.isNull() ) + { + if ( !s_session.isNull() ) + delete s_session.data(); + if ( !s_connector.isNull() ) + delete s_connector.data(); + tLog() << "Failed to start HTTPd, could not create object"; + return NULL; + } + + s_session->setListenInterface( interface ); + s_session->setPort( port ); + s_session->setConnector( s_connector.data() ); + + s_instance = new Api_v1( s_session.data() ); + s_session->setStaticContentService( s_instance ); + + tLog() << "Starting HTTPd on" << s_session->listenInterface().toString() << s_session->port(); + s_session->start(); + return s_instance; +} + + +void +Api_v1::stopInstance() +{ + if ( !s_connector.isNull() ) + delete s_connector.data(); + if ( !s_session.isNull() ) + delete s_session.data(); + if ( !s_instance.isNull() ) + delete s_instance; +} + void Api_v1::auth_1( QxtWebRequestEvent* event, QString arg ) diff --git a/src/libtomahawk-playdarapi/Api_v1.h b/src/libtomahawk-playdarapi/Api_v1.h index 87f3d2989..06b5b2ce0 100644 --- a/src/libtomahawk-playdarapi/Api_v1.h +++ b/src/libtomahawk-playdarapi/Api_v1.h @@ -50,9 +50,26 @@ class TOMAHAWK_PLAYDARAPI_EXPORT Api_v1 : public QxtWebSlotService Q_OBJECT public: - Api_v1( QxtAbstractWebSessionManager* sm, QObject* parent = 0 ); + /** + * Get the current running instance instaniated by the singleton helper + * functions. + */ + static Api_v1* instance(); + + /** + * Start a singleton instance of the playdar API. Though you are not + * limited to use one instance of this class, this helper function takes + * care of all related Qxt classes that need to be instantiated to run the API. + */ + static Api_v1* startInstance( QHostAddress interface, qint16 port ); + + /** + * Stop the singleton instance and release its resources. + */ + static void stopInstance(); + public slots: // authenticating uses /auth_1 // we redirect to /auth_2 for the callback @@ -83,6 +100,12 @@ private: QxtWebRequestEvent* m_storedEvent; QSharedPointer< QIODevice > m_ioDevice; + + // Static variables for the singleton instance. + static QPointer< Api_v1 > s_instance; + static QPointer< QxtHttpServerConnector > s_connector; + static QPointer< QxtHttpSessionManager > s_session; + }; #endif diff --git a/src/tomahawk/CMakeLists.txt b/src/tomahawk/CMakeLists.txt index 1256db3b8..3791bfed1 100644 --- a/src/tomahawk/CMakeLists.txt +++ b/src/tomahawk/CMakeLists.txt @@ -184,7 +184,6 @@ TARGET_LINK_LIBRARIES( tomahawk_bin ${QT_LIBRARIES} ${MAC_EXTRA_LIBS} ${ECHONEST_LIBRARIES} - ${QXTWEB_LIBRARIES} ${QJSON_LIBRARIES} ${TAGLIB_LIBRARIES} ) diff --git a/src/tomahawk/TomahawkApp.cpp b/src/tomahawk/TomahawkApp.cpp index a351f7922..be10d9997 100644 --- a/src/tomahawk/TomahawkApp.cpp +++ b/src/tomahawk/TomahawkApp.cpp @@ -285,11 +285,6 @@ TomahawkApp::~TomahawkApp() { tDebug( LOGVERBOSE ) << "Shutting down Tomahawk..."; - if ( !m_httpv1_session.isNull() ) - delete m_httpv1_session.data(); - if ( !m_httpv1_connector.isNull() ) - delete m_httpv1_connector.data(); - if ( Pipeline::instance() ) Pipeline::instance()->stop(); @@ -482,43 +477,14 @@ TomahawkApp::initDatabase() void TomahawkApp::initHTTP() { - if ( !TomahawkSettings::instance()->httpEnabled() ) + if ( TomahawkSettings::instance()->httpEnabled() ) { - tLog() << "Stopping HTTPd, not enabled"; - if ( !m_httpv1_session.isNull() ) - delete m_httpv1_session.data(); - if ( !m_httpv1_connector.isNull() ) - delete m_httpv1_connector.data(); - return; + Api_v1::startInstance( QHostAddress::LocalHost, 60210 ); // TODO: Config } - - if ( m_httpv1_session ) + else { - tLog() << "HTTPd session already exists, returning"; - return; + Api_v1::stopInstance(); } - - m_httpv1_session = QPointer< QxtHttpSessionManager >( new QxtHttpSessionManager() ); - m_httpv1_connector = QPointer< QxtHttpServerConnector >( new QxtHttpServerConnector ); - if ( m_httpv1_session.isNull() || m_httpv1_connector.isNull() ) - { - if ( !m_httpv1_session.isNull() ) - delete m_httpv1_session.data(); - if ( !m_httpv1_connector.isNull() ) - delete m_httpv1_connector.data(); - tLog() << "Failed to start HTTPd, could not create object"; - return; - } - - m_httpv1_session->setPort( 60210 ); //TODO config - m_httpv1_session->setListenInterface( QHostAddress::LocalHost ); - m_httpv1_session->setConnector( m_httpv1_connector.data() ); - - Api_v1* api = new Api_v1( m_httpv1_session.data() ); - m_httpv1_session->setStaticContentService( api ); - - tLog() << "Starting HTTPd on" << m_httpv1_session->listenInterface().toString() << m_httpv1_session->port(); - m_httpv1_session->start(); } diff --git a/src/tomahawk/TomahawkApp.h b/src/tomahawk/TomahawkApp.h index 2d9ab6f2d..2a9ae0897 100644 --- a/src/tomahawk/TomahawkApp.h +++ b/src/tomahawk/TomahawkApp.h @@ -31,9 +31,6 @@ #include "HeadlessCheck.h" #include "config.h" -#include -#include - #include #include #include @@ -147,9 +144,6 @@ private: #endif bool m_headless; - - QPointer< QxtHttpServerConnector > m_httpv1_connector; - QPointer< QxtHttpSessionManager > m_httpv1_session; }; Q_DECLARE_METATYPE( PairList )