diff --git a/src/libtomahawk/globalactionmanager.cpp b/src/libtomahawk/globalactionmanager.cpp index 417730883..6c41b2560 100644 --- a/src/libtomahawk/globalactionmanager.cpp +++ b/src/libtomahawk/globalactionmanager.cpp @@ -24,12 +24,15 @@ #include #include #include +#include +#include #include "artist.h" #include "album.h" #include "sourcelist.h" #include "pipeline.h" #include "viewmanager.h" +#include "tomahawksettings.h" #include "audio/audioengine.h" #include "database/localcollection.h" #include "playlist/dynamic/GeneratorInterface.h" @@ -64,6 +67,8 @@ GlobalActionManager::instance() GlobalActionManager::GlobalActionManager( QObject* parent ) : QObject( parent ) { + newNam(); + connect( TomahawkSettings::instance(), SIGNAL( changed() ), SLOT( newNam() ) ); } GlobalActionManager::~GlobalActionManager() @@ -104,12 +109,23 @@ GlobalActionManager::openLink( const QString& title, const QString& artist, cons return link; } -QUrl -GlobalActionManager::openShortTomahawkLink( const QString& title, const QString& artist, const QString& album ) const +void +GlobalActionManager::shortenLink( const QUrl& url ) const { - QUrl longLink = openLink( title, artist, album ); + qDebug() << Q_FUNC_INFO; + QNetworkRequest request; + request.setUrl( url ); + if( m_nam.isNull() ) + { + emit shortLinkReady( QUrl( "" ), QUrl( "" ) ); + return; + } + QNetworkReply *reply = m_nam.data()->get( request ); + connect( reply, SIGNAL( finished() ), this, SLOT( shortenLinkRequestFinished() ) ); + connect( reply, SIGNAL( error( QNetworkReply::NetworkError ) ), + this, SLOT( shortenLinkRequestError( QNetworkReply::NetworkError ) ) ); } QString @@ -698,6 +714,37 @@ GlobalActionManager::playNow( const query_ptr& q ) connect( q.data(), SIGNAL( resolvingFinished( bool ) ), this, SLOT( waitingForResolved( bool ) ) ); } +void +GlobalActionManager::newNam() +{ + QNetworkAccessManager *oldNam = TomahawkUtils::nam(); + +// qDebug() << Q_FUNC_INFO << "No nam exists, or it's a different thread, creating a new one"; + QNetworkAccessManager* newNam; +#ifdef LIBLASTFM_FOUND + newNam = new lastfm::NetworkAccessManager( this ); +#else + newNam = new QNetworkAccessManager( this ); +#endif + if ( !m_nam.isNull() ) + delete m_nam.data(); + + if ( !oldNam ) + oldNam = new QNetworkAccessManager(); + + TomahawkUtils::NetworkProxyFactory* oldProxyFactory = TomahawkUtils::proxyFactory(); + if ( !oldProxyFactory ) + oldProxyFactory = new TomahawkUtils::NetworkProxyFactory(); + + newNam->setConfiguration( oldNam->configuration() ); + newNam->setNetworkAccessible( oldNam->networkAccessible() ); + TomahawkUtils::NetworkProxyFactory* newProxyFactory = new TomahawkUtils::NetworkProxyFactory(); + newProxyFactory->setNoProxyHosts( oldProxyFactory->noProxyHosts() ); + newProxyFactory->setProxy( oldProxyFactory->proxy() ); + newNam->setProxyFactory( newProxyFactory ); + m_nam = QWeakPointer< QNetworkAccessManager >( newNam ); +} + bool GlobalActionManager::playRdio( const QUrl& url ) { @@ -757,7 +804,53 @@ bool GlobalActionManager::handleBookmarkCommand(const QUrl& url) return false; } +void +GlobalActionManager::shortenLinkRequestFinished() +{ + qDebug() << Q_FUNC_INFO; + QNetworkReply *reply = qobject_cast( sender() ); + // NOTE: this should never happen + if( !reply ) + { + emit shortLinkReady( QUrl( "" ), QUrl( "" ) ); + reply->deleteLater(); + return; + } + + // Check for the redirect attribute, as this should be the shortened link + + QVariant urlVariant = reply->attribute( QNetworkRequest::RedirectionTargetAttribute ); + + // NOTE: this should never happen + if( urlVariant.isNull() || !urlVariant.isValid() ) + { + emit shortLinkReady( reply->request().url(), QUrl( "" ) ); + reply->deleteLater(); + return; + } + + QUrl shortUrl = urlVariant.toUrl(); + + // NOTE: this should never happen + if( !shortUrl.isValid() ) + { + emit shortLinkReady( reply->request().url(), QUrl( "" ) ); + reply->deleteLater(); + return; + } + + // Success! Here is the short link + + emit shortLinkReady( reply->request().url(), shortUrl ); + reply->deleteLater(); +} + +void +GlobalActionManager::shortenLinkRequestError( QNetworkReply::NetworkError ) +{ + qDebug() << Q_FUNC_INFO; +} void GlobalActionManager::bookmarkPlaylistCreated( const playlist_ptr& pl ) diff --git a/src/libtomahawk/globalactionmanager.h b/src/libtomahawk/globalactionmanager.h index 901b89b46..985d899bf 100644 --- a/src/libtomahawk/globalactionmanager.h +++ b/src/libtomahawk/globalactionmanager.h @@ -26,6 +26,7 @@ #include "dllmacro.h" #include +#include #include #include #include @@ -44,7 +45,7 @@ public: QUrl openLinkFromQuery( const Tomahawk::query_ptr& query ) const; QUrl openLink( const QString& title, const QString& artist, const QString& album ) const; - QUrl shortenLink( const QString& link ); + void shortenLink( const QUrl& url ) const; /// Takes a spotify link and performs the default open action on it bool openSpotifyLink( const QString& link ); @@ -65,15 +66,21 @@ public slots: void handleOpenTrack( const Tomahawk::query_ptr& qry ); signals: - void shortLinkReady( QUrl url ); + void shortLinkReady( QUrl longUrl, QUrl shortUrl ) const; private slots: + void shortenLinkRequestFinished(); + void shortenLinkRequestError( QNetworkReply::NetworkError ); + void bookmarkPlaylistCreated( const Tomahawk::playlist_ptr& pl ); void showPlaylist(); void xspfCreated( const QByteArray& xspf ); void playNow( const Tomahawk::query_ptr& ); + + /// Network Access Manager + void newNam(); private: explicit GlobalActionManager( QObject* parent = 0 ); void doBookmark( const Tomahawk::playlist_ptr& pl, const Tomahawk::query_ptr& q ); diff --git a/src/libtomahawk/infosystem/infoplugins/mac/adiumplugin.cpp b/src/libtomahawk/infosystem/infoplugins/mac/adiumplugin.cpp index d29e3484e..4e029921c 100644 --- a/src/libtomahawk/infosystem/infoplugins/mac/adiumplugin.cpp +++ b/src/libtomahawk/infosystem/infoplugins/mac/adiumplugin.cpp @@ -73,6 +73,9 @@ AdiumPlugin::AdiumPlugin() m_pauseTimer->setSingleShot( true ); connect( m_pauseTimer, SIGNAL( timeout() ), this, SLOT( clearStatus() ) ); + + connect( GlobalActionManager::instance(), SIGNAL( shortLinkReady( QUrl, QUrl ) ), + SLOT( shortLinkReady( QUrl, QUrl ) ) ); } @@ -83,6 +86,35 @@ AdiumPlugin::~AdiumPlugin() setStatus( "" ); } +void +AdiumPlugin::shortLinkReady( QUrl longUrl, QUrl shortUrl ) +{ + // The URL we received is either from a previous track, or not requested by us + if( longUrl != m_currentLongUrl ) + return; + + // Build the core of the now-playing string + QString nowPlaying = ""; + nowPlaying.append( m_currentArtist ); + nowPlaying.append(" - "); + nowPlaying.append( m_currentTitle ); + nowPlaying.replace( "\"", "\\\"" ); // Escape quotes, or Applescript gets confused + + // We failed to get the short URL, just update the status with the metadata + if( ( longUrl.toString() == "" ) || ( shortUrl.toString() == "" ) ) + { + qDebug() << "nowPlaying: " << nowPlaying; + setStatus( nowPlaying ); + return; + } + + // Add the short URL + nowPlaying.append( " " ); + nowPlaying.append( shortUrl.toEncoded() ); + qDebug() << "nowPlaying: " << nowPlaying; + setStatus( nowPlaying ); + +} void AdiumPlugin::clearStatus() @@ -133,13 +165,7 @@ AdiumPlugin::pushInfo( QString caller, Tomahawk::InfoSystem::InfoType type, QVar void AdiumPlugin::namChangedSlot( QNetworkAccessManager* nam ) { - m_nam = QWeakPointer( nam ); - - if( !m_nam.isNull() ) - { - connect( m_nam.data(), SIGNAL( finished( QNetworkReply* ) ), - this, SLOT( replyFinished( QNetworkReply* ) ) ); - } + Q_UNUSED( nam ) } /** Audio state slots */ @@ -155,16 +181,12 @@ AdiumPlugin::audioStarted( const QVariant &input ) if ( !hash.contains( "title" ) || !hash.contains( "artist" ) ) return; - QString nowPlaying = ""; - nowPlaying.append( hash["artist"] ); - nowPlaying.append(" - "); - nowPlaying.append( hash["title"] ); - nowPlaying.append( " " ); - // Escape quotes, or Applescript gets confused - nowPlaying.replace( "\"", "\\\"" ); - nowPlaying.append( openLinkFromHash( hash ).toEncoded() ); - qDebug() << "nowPlaying: " << nowPlaying; - setStatus( nowPlaying ); + m_currentTitle = hash["title"]; + m_currentArtist = hash["artist"]; + + // Request a short URL + m_currentLongUrl = openLinkFromHash( hash ).toEncoded(); + GlobalActionManager::instance()->shortenLink( m_currentLongUrl ); } QUrl @@ -209,3 +231,4 @@ AdiumPlugin::audioResumed( const QVariant &input ) qDebug() << Q_FUNC_INFO; audioStarted( input ); } + diff --git a/src/libtomahawk/infosystem/infoplugins/mac/adiumplugin.h b/src/libtomahawk/infosystem/infoplugins/mac/adiumplugin.h index 06b831018..bdd4b6e42 100644 --- a/src/libtomahawk/infosystem/infoplugins/mac/adiumplugin.h +++ b/src/libtomahawk/infosystem/infoplugins/mac/adiumplugin.h @@ -60,6 +60,8 @@ public slots: } private slots: + void shortLinkReady( QUrl longUrl, QUrl shortUrl ); + void clearStatus(); void settingsChanged(); @@ -76,6 +78,10 @@ private: QString m_beforeStatus; QString m_afterStatus; + QString m_currentTitle; + QString m_currentArtist; + QUrl m_currentLongUrl; + QTimer* m_pauseTimer; QWeakPointer m_nam;