From eebf5dfe0703f92e1da896634d5a350cb1555b90 Mon Sep 17 00:00:00 2001 From: Jeff Mitchell Date: Wed, 28 Mar 2012 14:17:20 -0400 Subject: [PATCH] Dynamically enable/disable the web API, and change the wording for the option to make it more enticing --- src/stackedsettingsdialog.ui | 2 +- src/tomahawkapp.cpp | 48 ++++++++++++++++++++++++++++++------ src/tomahawkapp.h | 7 +++--- 3 files changed, 45 insertions(+), 12 deletions(-) diff --git a/src/stackedsettingsdialog.ui b/src/stackedsettingsdialog.ui index 6d08a1a2b..7b241b440 100644 --- a/src/stackedsettingsdialog.ui +++ b/src/stackedsettingsdialog.ui @@ -393,7 +393,7 @@ Qt::RightToLeft - Playdar HTTP API + Allow web browsers to detect Tomahawk true diff --git a/src/tomahawkapp.cpp b/src/tomahawkapp.cpp index 09576f369..e7f3869b1 100644 --- a/src/tomahawkapp.cpp +++ b/src/tomahawkapp.cpp @@ -268,6 +268,7 @@ TomahawkApp::init() { initHTTP(); } + connect( TomahawkSettings::instance(), SIGNAL( changed() ), SLOT( initHTTP() ) ); #ifndef ENABLE_HEADLESS if ( !s->hasScannerPaths() ) @@ -309,6 +310,11 @@ TomahawkApp::~TomahawkApp() { tLog() << "Shutting down Tomahawk..."; + if ( !m_session.isNull() ) + delete m_session.data(); + if ( !m_connector.isNull() ) + delete m_connector.data(); + Pipeline::instance()->stop(); if ( !m_servent.isNull() ) @@ -470,15 +476,43 @@ TomahawkApp::initDatabase() void TomahawkApp::initHTTP() { - m_session.setPort( 60210 ); //TODO config - m_session.setListenInterface( QHostAddress::LocalHost ); - m_session.setConnector( &m_connector ); + if ( !TomahawkSettings::instance()->httpEnabled() ) + { + tLog() << "Stopping HTTPd, not enabled"; + if ( !m_session.isNull() ) + delete m_session.data(); + if ( !m_connector.isNull() ) + delete m_connector.data(); + return; + } - Api_v1* api = new Api_v1( &m_session ); - m_session.setStaticContentService( api ); + if ( m_session ) + { + tLog() << "HTTPd session already exists, returning"; + return; + } + + m_session = QWeakPointer< QxtHttpSessionManager >( new QxtHttpSessionManager() ); + m_connector = QWeakPointer< QxtHttpServerConnector >( new QxtHttpServerConnector ); + if ( m_session.isNull() || m_connector.isNull() ) + { + if ( !m_session.isNull() ) + delete m_session.data(); + if ( !m_connector.isNull() ) + delete m_connector.data(); + tLog() << "Failed to start HTTPd, could not create object"; + return; + } + + m_session.data()->setPort( 60210 ); //TODO config + m_session.data()->setListenInterface( QHostAddress::LocalHost ); + m_session.data()->setConnector( m_connector.data() ); - tLog() << "Starting HTTPd on" << m_session.listenInterface().toString() << m_session.port(); - m_session.start(); + Api_v1* api = new Api_v1( m_session.data() ); + m_session.data()->setStaticContentService( api ); + + tLog() << "Starting HTTPd on" << m_session.data()->listenInterface().toString() << m_session.data()->port(); + m_session.data()->start(); } diff --git a/src/tomahawkapp.h b/src/tomahawkapp.h index a8d2a0c45..ade0e72d8 100644 --- a/src/tomahawkapp.h +++ b/src/tomahawkapp.h @@ -108,6 +108,7 @@ public slots: private slots: void initServent(); void initSIP(); + void initHTTP(); void spotifyApiCheckFinished(); @@ -121,8 +122,6 @@ private: void initLocalCollection(); void initPipeline(); - void initHTTP(); - QWeakPointer m_database; QWeakPointer m_scanManager; QWeakPointer m_audioEngine; @@ -143,8 +142,8 @@ private: bool m_headless, m_loaded; - QxtHttpServerConnector m_connector; - QxtHttpSessionManager m_session; + QWeakPointer< QxtHttpServerConnector > m_connector; + QWeakPointer< QxtHttpSessionManager > m_session; }; Q_DECLARE_METATYPE( QPersistentModelIndex )