From 63aa26f607087b6d2afd4ae94604ea53086f68f1 Mon Sep 17 00:00:00 2001 From: Christian Muehlhaeuser Date: Sun, 17 Jul 2011 01:02:13 +0200 Subject: [PATCH] * Fixed init procedure: don't setup Servent before database and sources are loaded. --- src/libtomahawk/sip/SipHandler.h | 1 - src/libtomahawk/source.cpp | 6 ++- src/libtomahawk/sourcelist.cpp | 8 +++- src/libtomahawk/sourcelist.h | 3 ++ src/tomahawkapp.cpp | 63 ++++++++++++++++---------------- src/tomahawkapp.h | 13 ++++--- 6 files changed, 54 insertions(+), 40 deletions(-) diff --git a/src/libtomahawk/sip/SipHandler.h b/src/libtomahawk/sip/SipHandler.h index 3a898e5e5..74a2e5df5 100644 --- a/src/libtomahawk/sip/SipHandler.h +++ b/src/libtomahawk/sip/SipHandler.h @@ -104,7 +104,6 @@ private slots: // set data for other sources void onAvatarReceived( const QString& from, const QPixmap& avatar ); - private: static SipHandler *s_instance; diff --git a/src/libtomahawk/source.cpp b/src/libtomahawk/source.cpp index eeedee104..e0eaddc88 100644 --- a/src/libtomahawk/source.cpp +++ b/src/libtomahawk/source.cpp @@ -168,6 +168,7 @@ Source::removeCollection( const collection_ptr& c ) void Source::setOffline() { + qDebug() << Q_FUNC_INFO << friendlyName(); if ( !m_online ) return; @@ -183,17 +184,18 @@ Source::setOffline() void Source::setOnline() { + qDebug() << Q_FUNC_INFO << friendlyName(); if ( m_online ) return; + m_online = true; + emit online(); // ensure username is in the database DatabaseCommand_addSource* cmd = new DatabaseCommand_addSource( m_username, m_friendlyname ); connect( cmd, SIGNAL( done( unsigned int, QString ) ), SLOT( dbLoaded( unsigned int, const QString& ) ) ); Database::instance()->enqueue( QSharedPointer(cmd) ); - - emit online(); } diff --git a/src/libtomahawk/sourcelist.cpp b/src/libtomahawk/sourcelist.cpp index cb78d51cb..aad583902 100644 --- a/src/libtomahawk/sourcelist.cpp +++ b/src/libtomahawk/sourcelist.cpp @@ -44,6 +44,7 @@ SourceList::instance() SourceList::SourceList( QObject* parent ) : QObject( parent ) + , m_isReady( false ) { } @@ -85,6 +86,7 @@ SourceList::setSources( const QList& sources ) { QMutexLocker lock( &m_mut ); + m_isReady = true; foreach( const source_ptr& src, sources ) { add( src ); @@ -116,6 +118,8 @@ SourceList::setLocal( const Tomahawk::source_ptr& localSrc ) void SourceList::add( const source_ptr& source ) { + Q_ASSERT( m_isReady ); + qDebug() << "Adding to sources:" << source->userName() << source->id(); m_sources.insert( source->userName(), source ); @@ -125,7 +129,6 @@ SourceList::add( const source_ptr& source ) collection_ptr coll( new RemoteCollection( source ) ); source->addCollection( coll ); -// source->collection()->tracks(); emit sourceAdded( source ); } @@ -134,8 +137,11 @@ SourceList::add( const source_ptr& source ) void SourceList::removeAllRemote() { + Q_ASSERT( m_isReady ); + foreach( const source_ptr& s, m_sources ) { + qDebug() << "Disconnecting" << s->friendlyName() << s->isLocal() << s->controlConnection() << s->isOnline(); if ( !s->isLocal() && s->controlConnection() ) { s->controlConnection()->shutdown( true ); diff --git a/src/libtomahawk/sourcelist.h b/src/libtomahawk/sourcelist.h index d4757095b..4297c7cbb 100644 --- a/src/libtomahawk/sourcelist.h +++ b/src/libtomahawk/sourcelist.h @@ -37,6 +37,8 @@ public: explicit SourceList( QObject* parent = 0 ); + bool isReady() const { return m_isReady; } + const Tomahawk::source_ptr& getLocal(); void setLocal( const Tomahawk::source_ptr& localSrc ); @@ -68,6 +70,7 @@ private: QMap< QString, Tomahawk::source_ptr > m_sources; QMap< int, QString > m_sources_id2name; + bool m_isReady; Tomahawk::source_ptr m_local; Tomahawk::source_ptr m_dummy; mutable QMutex m_mut; // mutable so const methods can use a lock diff --git a/src/tomahawkapp.cpp b/src/tomahawkapp.cpp index 76b27e6ac..fe9883838 100644 --- a/src/tomahawkapp.cpp +++ b/src/tomahawkapp.cpp @@ -211,11 +211,10 @@ TomahawkApp::init() qDebug() << "Setting NAM."; TomahawkUtils::setNam( new QNetworkAccessManager() ); #endif - + TomahawkUtils::NetworkProxyFactory* proxyFactory = new TomahawkUtils::NetworkProxyFactory(); - if( s->proxyType() != QNetworkProxy::NoProxy && - !s->proxyHost().isEmpty() ) + if ( s->proxyType() != QNetworkProxy::NoProxy && !s->proxyHost().isEmpty() ) { qDebug() << "Setting proxy to saved values"; QNetworkProxy proxy( static_cast( s->proxyType() ), s->proxyHost(), s->proxyPort(), s->proxyUsername(), s->proxyPassword() ); @@ -226,9 +225,9 @@ TomahawkApp::init() if ( !s->proxyNoProxyHosts().isEmpty() ) proxyFactory->setNoProxyHosts( s->proxyNoProxyHosts().split( ',', QString::SkipEmptyParts ) ); - + TomahawkUtils::setProxyFactory( proxyFactory ); - + Echonest::Config::instance()->setAPIKey( "JRIHWEP6GPOER2QQ6" ); m_audioEngine = QWeakPointer( new AudioEngine ); @@ -236,10 +235,10 @@ TomahawkApp::init() new Pipeline( this ); m_servent = QWeakPointer( new Servent( this ) ); - connect( m_servent.data(), SIGNAL( ready() ), SLOT( setupSIP() ) ); + connect( m_servent.data(), SIGNAL( ready() ), SLOT( initSIP() ) ); qDebug() << "Init Database."; - setupDatabase(); + initDatabase(); qDebug() << "Init Echonest Factory."; GeneratorFactory::registerFactory( "echonest", new EchonestFactory ); @@ -272,8 +271,6 @@ TomahawkApp::init() Echonest::Config::instance()->setAPIKey( "JRIHWEP6GPOER2QQ6" ); Echonest::Config::instance()->setNetworkAccessManager( TomahawkUtils::nam() ); - qDebug() << "Init SIP system."; - #ifndef TOMAHAWK_HEADLESS if ( !m_headless ) { @@ -287,14 +284,12 @@ TomahawkApp::init() qDebug() << "Init Local Collection."; initLocalCollection(); qDebug() << "Init Pipeline."; - setupPipeline(); - qDebug() << "Init Servent."; - startServent(); + initPipeline(); - if( arguments().contains( "--http" ) || TomahawkSettings::instance()->value( "network/http", true ).toBool() ) + if ( arguments().contains( "--http" ) || TomahawkSettings::instance()->value( "network/http", true ).toBool() ) { qDebug() << "Init HTTP Server."; - startHTTP(); + initHTTP(); } #ifndef TOMAHAWK_HEADLESS @@ -322,26 +317,28 @@ TomahawkApp::~TomahawkApp() } m_scriptResolvers.clear(); - if( !m_servent.isNull() ) + if ( !m_servent.isNull() ) delete m_servent.data(); - if( !m_scanManager.isNull() ) + if ( !m_scanManager.isNull() ) delete m_scanManager.data(); + #ifndef TOMAHAWK_HEADLESS delete m_mainwindow; #endif - if( !m_audioEngine.isNull() ) + + if ( !m_audioEngine.isNull() ) delete m_audioEngine.data(); - if( !m_infoSystem.isNull() ) + if ( !m_infoSystem.isNull() ) delete m_infoSystem.data(); //FIXME: delete GeneratorFactory::registerFactory( "echonest", new EchonestFactory ); ? delete SipHandler::instance(); - if( !m_scanManager.isNull() ) + if ( !m_scanManager.isNull() ) delete m_scanManager.data(); - if( !m_database.isNull() ) + if ( !m_database.isNull() ) delete m_database.data(); Pipeline::instance()->stop(); @@ -428,10 +425,10 @@ TomahawkApp::registerMetaTypes() void -TomahawkApp::setupDatabase() +TomahawkApp::initDatabase() { QString dbpath; - if( arguments().contains( "--testdb" ) ) + if ( arguments().contains( "--testdb" ) ) { dbpath = QDir::currentPath() + "/test.db"; } @@ -447,7 +444,7 @@ TomahawkApp::setupDatabase() void -TomahawkApp::startHTTP() +TomahawkApp::initHTTP() { m_session.setPort( 60210 ); //TODO config m_session.setListenInterface( QHostAddress::LocalHost ); @@ -463,7 +460,7 @@ TomahawkApp::startHTTP() void -TomahawkApp::setupPipeline() +TomahawkApp::initPipeline() { // setup resolvers for local content, and (cached) remote collection content Pipeline::instance()->addResolver( new DatabaseResolver( 100 ) ); @@ -488,7 +485,7 @@ TomahawkApp::enableScriptResolver( const QString& path ) void TomahawkApp::disableScriptResolver( const QString& path ) { - if( m_scriptResolvers.contains( path ) ) + if ( m_scriptResolvers.contains( path ) ) { Tomahawk::ExternalResolver* r = m_scriptResolvers.take( path ); @@ -509,6 +506,8 @@ TomahawkApp::resolverForPath( const QString& scriptPath ) void TomahawkApp::initLocalCollection() { + connect( SourceList::instance(), SIGNAL( ready() ), SLOT( initServent() ) ); + source_ptr src( new Source( 0, "My Collection" ) ); collection_ptr coll( new LocalCollection( src ) ); @@ -534,8 +533,10 @@ TomahawkApp::initLocalCollection() void -TomahawkApp::startServent() +TomahawkApp::initServent() { + qDebug() << "Init Servent."; + bool upnp = !arguments().contains( "--noupnp" ) && TomahawkSettings::instance()->value( "network/upnp", true ).toBool() && !TomahawkSettings::instance()->preferStaticHostPort(); int port = TomahawkSettings::instance()->externalPort(); if ( !Servent::instance()->startListening( QHostAddress( QHostAddress::Any ), upnp, port ) ) @@ -547,12 +548,12 @@ TomahawkApp::startServent() void -TomahawkApp::setupSIP() +TomahawkApp::initSIP() { qDebug() << Q_FUNC_INFO; //FIXME: jabber autoconnect is really more, now that there is sip -- should be renamed and/or split out of jabber-specific settings - if( !arguments().contains( "--nosip" ) ) + if ( !arguments().contains( "--nosip" ) ) { #ifdef GLOOX_FOUND m_xmppBot = QWeakPointer( new XMPPBot( this ) ); @@ -577,13 +578,13 @@ TomahawkApp::activate() bool TomahawkApp::loadUrl( const QString& url ) { - if( url.startsWith( "tomahawk://" ) ) + if ( url.startsWith( "tomahawk://" ) ) return GlobalActionManager::instance()->parseTomahawkLink( url ); else { QFile f( url ); QFileInfo info( f ); - if( f.exists() && info.suffix() == "xspf" ) { + if ( f.exists() && info.suffix() == "xspf" ) { XSPFLoader* l = new XSPFLoader( true, this ); qDebug() << "Loading spiff:" << url; l->load( QUrl::fromUserInput( url ) ); @@ -601,7 +602,7 @@ TomahawkApp::instanceStarted( KDSingleApplicationGuard::Instance instance ) { qDebug() << "INSTANCE STARTED!" << instance.pid << instance.arguments; - if( instance.arguments.size() < 2 ) + if ( instance.arguments.size() < 2 ) { return; } diff --git a/src/tomahawkapp.h b/src/tomahawkapp.h index 48790f151..69b78fe3e 100644 --- a/src/tomahawkapp.h +++ b/src/tomahawkapp.h @@ -102,17 +102,20 @@ public slots: void instanceStarted( KDSingleApplicationGuard::Instance ); private slots: - void setupSIP(); + void initServent(); + void initSIP(); private: void registerMetaTypes(); + // Start-up order: database, collection, pipeline, servent, http + void initDatabase(); void initLocalCollection(); + void initPipeline(); + + void initHTTP(); + void loadPlugins(); - void startServent(); - void setupDatabase(); - void setupPipeline(); - void startHTTP(); QList m_collections; QHash m_scriptResolvers;