mirror of
https://github.com/tomahawk-player/tomahawk.git
synced 2025-03-21 16:29:43 +01:00
Add support for getting short links, make Adium use it by default.
This commit is contained in:
parent
df13198c5c
commit
b250d13362
@ -24,12 +24,15 @@
|
||||
#include <QUrl>
|
||||
#include <QtNetwork/QNetworkAccessManager>
|
||||
#include <QtNetwork/QNetworkReply>
|
||||
#include <QtNetwork/QNetworkConfiguration>
|
||||
#include <QtNetwork/QNetworkProxy>
|
||||
|
||||
#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<QNetworkReply*>( 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 )
|
||||
|
@ -26,6 +26,7 @@
|
||||
#include "dllmacro.h"
|
||||
|
||||
#include <QNetworkAccessManager>
|
||||
#include <QNetworkReply>
|
||||
#include <QObject>
|
||||
#include <QUrl>
|
||||
#include <QWeakPointer>
|
||||
@ -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 );
|
||||
|
@ -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<QNetworkAccessManager>( 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 );
|
||||
}
|
||||
|
||||
|
@ -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<QNetworkAccessManager> m_nam;
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user