From 7284a53d0f6554ecd787e1d7d6dcc117b110a8fa Mon Sep 17 00:00:00 2001 From: Jeff Mitchell Date: Wed, 11 Apr 2012 08:19:10 -0400 Subject: [PATCH] Move Jreen bits in xmpp info plugin to the SIP side, allowing the plugin to run in the correct thread. --- src/accounts/xmpp/XmppInfoPlugin.cpp | 73 +++++++++------------------- src/accounts/xmpp/XmppInfoPlugin.h | 11 ++--- src/accounts/xmpp/sip/xmppsip.cpp | 59 +++++++++++++++++++++- src/accounts/xmpp/sip/xmppsip.h | 3 ++ 4 files changed, 87 insertions(+), 59 deletions(-) diff --git a/src/accounts/xmpp/XmppInfoPlugin.cpp b/src/accounts/xmpp/XmppInfoPlugin.cpp index cb7352a9c..b1d822a68 100644 --- a/src/accounts/xmpp/XmppInfoPlugin.cpp +++ b/src/accounts/xmpp/XmppInfoPlugin.cpp @@ -24,31 +24,18 @@ #include "sip/xmppsip.h" #include "utils/logger.h" -#include -#include -#include - -#include // remove now playing status after PAUSE_TIMEOUT seconds -static const int PAUSE_TIMEOUT = 60; +static const int PAUSE_TIMEOUT = 10; Tomahawk::InfoSystem::XmppInfoPlugin::XmppInfoPlugin( XmppSipPlugin* sipPlugin ) : m_sipPlugin( sipPlugin ) - , m_pubSubManager( 0 ) , m_pauseTimer( this ) { Q_ASSERT( sipPlugin->m_client ); m_supportedPushTypes << InfoNowPlaying << InfoNowPaused << InfoNowResumed << InfoNowStopped; - m_pubSubManager = new Jreen::PubSub::Manager( sipPlugin->m_client ); - m_pubSubManager->addEntityType< Jreen::Tune >(); - - // Clear status - Jreen::Tune::Ptr tune( new Jreen::Tune() ); - m_pubSubManager->publishItems(QList() << tune, Jreen::JID()); - m_pauseTimer.setSingleShot( true ); connect( &m_pauseTimer, SIGNAL( timeout() ), this, SLOT( audioStopped() ) ); @@ -57,24 +44,28 @@ Tomahawk::InfoSystem::XmppInfoPlugin::XmppInfoPlugin( XmppSipPlugin* sipPlugin ) Tomahawk::InfoSystem::XmppInfoPlugin::~XmppInfoPlugin() { - //Note: the next two lines don't currently work, because the deletion wipes out internally posted events, need to talk to euro about a fix - Jreen::Tune::Ptr tune( new Jreen::Tune() ); - m_pubSubManager->publishItems(QList() << tune, Jreen::JID()); - m_pubSubManager->deleteLater(); +} + + +void +Tomahawk::InfoSystem::XmppInfoPlugin::init() +{ + if ( QThread::currentThread() != Tomahawk::InfoSystem::InfoSystem::instance()->workerThread().data() ) + { + QMetaObject::invokeMethod( this, "init", Qt::QueuedConnection ); + return; + } + + if ( m_sipPlugin.isNull() ) + return; + + connect( this, SIGNAL( publishTune( QUrl, Tomahawk::InfoSystem::InfoStringHash ) ), m_sipPlugin.data(), SLOT( publishTune( QUrl, Tomahawk::InfoSystem::InfoStringHash ) ), Qt::QueuedConnection ); } void Tomahawk::InfoSystem::XmppInfoPlugin::pushInfo( Tomahawk::InfoSystem::InfoPushData pushData ) { - tDebug() << Q_FUNC_INFO << m_sipPlugin.data()->m_client->jid().full(); - - if( m_sipPlugin.data()->m_account->configuration().value("publishtracks").toBool() == false ) - { - tDebug() << Q_FUNC_INFO << m_sipPlugin.data()->m_client->jid().full() << "Not publishing now playing info (disabled in account config)"; - return; - } - switch ( pushData.type ) { case InfoNowPlaying: @@ -109,8 +100,7 @@ Tomahawk::InfoSystem::XmppInfoPlugin::audioStarted( const Tomahawk::InfoSystem:: QVariantMap map = pushInfoPair.second.toMap(); if ( map.contains( "private" ) && map[ "private" ] == TomahawkSettings::FullyPrivate ) { - Jreen::Tune::Ptr tune( new Jreen::Tune() ); - m_pubSubManager->publishItems( QList() << tune, Jreen::JID() ); + emit publishTune( QUrl(), Tomahawk::InfoSystem::InfoStringHash() ); return; } @@ -121,42 +111,25 @@ Tomahawk::InfoSystem::XmppInfoPlugin::audioStarted( const Tomahawk::InfoSystem:: } Tomahawk::InfoSystem::InfoStringHash info = map[ "trackinfo" ].value< Tomahawk::InfoSystem::InfoStringHash >(); - tDebug() << Q_FUNC_INFO << m_sipPlugin.data()->m_client->jid().full() << info; - - Jreen::Tune::Ptr tune( new Jreen::Tune() ); - tune->setTitle( info.value( "title" ) ); - tune->setArtist( info.value( "artist" ) ); - tune->setLength( info.value("duration").toInt() ); - tune->setTrack( info.value("albumpos") ); + QUrl url; if ( pushInfoPair.first.contains( "shorturl" ) ) - tune->setUri( pushInfoPair.first[ "shorturl" ].toUrl() ); + url = pushInfoPair.first[ "shorturl" ].toUrl(); else - tune->setUri( GlobalActionManager::instance()->openLink( info.value( "title" ), info.value( "artist" ), info.value( "album" ) ) ); + url = GlobalActionManager::instance()->openLink( info.value( "title" ), info.value( "artist" ), info.value( "album" ) ); - tDebug() << Q_FUNC_INFO << "Setting URI of " << tune->uri().toString(); - //TODO: provide a rating once available in Tomahawk - tune->setRating( 10 ); - - //TODO: it would be nice to set Spotify, Dilandau etc here, but not the jabber ids of friends - tune->setSource( "Tomahawk" ); - - m_pubSubManager->publishItems( QList() << tune, Jreen::JID() ); + emit publishTune( url, info ); } void Tomahawk::InfoSystem::XmppInfoPlugin::audioPaused() { - tDebug() << Q_FUNC_INFO << m_sipPlugin.data()->m_client->jid().full(); } void Tomahawk::InfoSystem::XmppInfoPlugin::audioStopped() { - tDebug() << Q_FUNC_INFO << m_sipPlugin.data()->m_client->jid().full(); - - Jreen::Tune::Ptr tune( new Jreen::Tune() ); - m_pubSubManager->publishItems(QList() << tune, Jreen::JID()); + emit publishTune( QUrl(), Tomahawk::InfoSystem::InfoStringHash() ); } diff --git a/src/accounts/xmpp/XmppInfoPlugin.h b/src/accounts/xmpp/XmppInfoPlugin.h index c9ff63b91..c15f1571f 100644 --- a/src/accounts/xmpp/XmppInfoPlugin.h +++ b/src/accounts/xmpp/XmppInfoPlugin.h @@ -24,12 +24,6 @@ #include -namespace Jreen { - namespace PubSub { - class Manager; - } -} - class XmppSipPlugin; namespace Tomahawk { @@ -44,7 +38,11 @@ namespace Tomahawk { XmppInfoPlugin(XmppSipPlugin* parent); virtual ~XmppInfoPlugin(); + signals: + void publishTune( QUrl url, Tomahawk::InfoSystem::InfoStringHash trackInfo ); + public slots: + void init(); void notInCacheSlot( const Tomahawk::InfoSystem::InfoStringHash criteria, Tomahawk::InfoSystem::InfoRequestData requestData ); protected slots: @@ -58,7 +56,6 @@ namespace Tomahawk { private: QWeakPointer< XmppSipPlugin > m_sipPlugin; - Jreen::PubSub::Manager* m_pubSubManager; QTimer m_pauseTimer; }; diff --git a/src/accounts/xmpp/sip/xmppsip.cpp b/src/accounts/xmpp/sip/xmppsip.cpp index 45e81d369..a36d3fb1e 100644 --- a/src/accounts/xmpp/sip/xmppsip.cpp +++ b/src/accounts/xmpp/sip/xmppsip.cpp @@ -35,6 +35,7 @@ #include #include #include +#include #include #include @@ -90,6 +91,7 @@ XmppSipPlugin::XmppSipPlugin( Account *account ) #ifndef ENABLE_HEADLESS , m_menu( 0 ) , m_xmlConsole( 0 ) + , m_pubSubManager( 0 ) #endif { Jreen::Logger::addHandler( JreenMessageHandler ); @@ -160,10 +162,22 @@ XmppSipPlugin::XmppSipPlugin( Account *account ) #ifndef ENABLE_HEADLESS connect(m_avatarManager, SIGNAL(newAvatar(QString)), SLOT(onNewAvatar(QString))); #endif + + m_pubSubManager = new Jreen::PubSub::Manager( m_client ); + m_pubSubManager->addEntityType< Jreen::Tune >(); + + // Clear status + Jreen::Tune::Ptr tune( new Jreen::Tune() ); + m_pubSubManager->publishItems(QList() << tune, Jreen::JID()); + } XmppSipPlugin::~XmppSipPlugin() { + //Note: the next two lines don't currently work, because the deletion wipes out internally posted events, need to talk to euro about a fix + Jreen::Tune::Ptr tune( new Jreen::Tune() ); + m_pubSubManager->publishItems(QList() << tune, Jreen::JID()); + delete m_pubSubManager; delete m_avatarManager; delete m_roster; #ifndef ENABLE_HEADLESS @@ -242,6 +256,8 @@ XmppSipPlugin::disconnectPlugin() m_peers.clear(); + publishTune( QUrl(), Tomahawk::InfoSystem::InfoStringHash() ); + m_client->disconnectFromServer( true ); m_state = Account::Disconnecting; emit stateChanged( m_state ); @@ -273,8 +289,12 @@ XmppSipPlugin::onConnect() m_roster->load(); // load XmppInfoPlugin - if( infoPlugin() ) - InfoSystem::InfoSystem::instance()->addInfoPlugin( infoPlugin() ); + if ( infoPlugin() && Tomahawk::InfoSystem::InfoSystem::instance()->workerThread() ) + { + infoPlugin().data()->moveToThread( Tomahawk::InfoSystem::InfoSystem::instance()->workerThread().data() ); + Tomahawk::InfoSystem::InfoSystem::instance()->addInfoPlugin( infoPlugin() ); + QMetaObject::invokeMethod( infoPlugin().data(), "init", Qt::QueuedConnection ); + } //FIXME: this implementation is totally broken atm, so it's disabled to avoid harm :P // join MUC with bare jid as nickname @@ -510,6 +530,41 @@ XmppSipPlugin::showAddFriendDialog() } +void +XmppSipPlugin::publishTune( const QUrl& url, const InfoSystem::InfoStringHash& trackInfo ) +{ + if( m_account->configuration().value("publishtracks").toBool() == false ) + { + tDebug() << Q_FUNC_INFO << m_client->jid().full() << "Not publishing now playing info (disabled in account config)"; + return; + } + + if ( trackInfo.isEmpty() ) + { + Jreen::Tune::Ptr tune( new Jreen::Tune() ); + m_pubSubManager->publishItems(QList() << tune, Jreen::JID()); + } + + Jreen::Tune::Ptr tune( new Jreen::Tune() ); + + tune->setTitle( trackInfo.value( "title" ) ); + tune->setArtist( trackInfo.value( "artist" ) ); + tune->setLength( trackInfo.value("duration").toInt() ); + tune->setTrack( trackInfo.value("albumpos") ); + + //TODO: provide a rating once available in Tomahawk + tune->setRating( 10 ); + + //TODO: it would be nice to set Spotify, Dilandau etc here, but not the jabber ids of friends + tune->setSource( "Tomahawk" ); + + tune->setUri( url ); + tDebug() << Q_FUNC_INFO << "Setting URI of " << tune->uri().toString(); + + m_pubSubManager->publishItems( QList() << tune, Jreen::JID() ); +} + + QString XmppSipPlugin::defaultSuffix() const { diff --git a/src/accounts/xmpp/sip/xmppsip.h b/src/accounts/xmpp/sip/xmppsip.h index 0a56ad9fa..1e570928d 100644 --- a/src/accounts/xmpp/sip/xmppsip.h +++ b/src/accounts/xmpp/sip/xmppsip.h @@ -42,6 +42,7 @@ #include #include #include +#include #ifndef ENABLE_HEADLESS #include @@ -92,6 +93,7 @@ public slots: void broadcastMsg( const QString &msg ); virtual void addContact( const QString &jid, const QString& msg = QString() ); void showAddFriendDialog(); + void publishTune( const QUrl &url, const Tomahawk::InfoSystem::InfoStringHash &trackInfo ); protected: virtual QString defaultSuffix() const; @@ -147,6 +149,7 @@ private: #endif enum IqContext { NoContext, RequestDisco, RequestedDisco, SipMessageSent, RequestedVCard, RequestVersion, RequestedVersion }; AvatarManager *m_avatarManager; + Jreen::PubSub::Manager* m_pubSubManager; }; #endif