1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-08-05 05:37:29 +02:00

* Use short-urls when copying track url to clipboard.

This commit is contained in:
Christian Muehlhaeuser
2011-09-10 12:00:30 +02:00
parent b0ae9fead5
commit 89f39f1a1f
2 changed files with 26 additions and 20 deletions

View File

@@ -207,12 +207,10 @@ GlobalActionManager::xspfCreated( const QByteArray& xspf )
void void
GlobalActionManager::copyToClipboard( const query_ptr& query ) const GlobalActionManager::copyToClipboard( const query_ptr& query )
{ {
QClipboard* cb = QApplication::clipboard(); m_clipboardLongUrl = openLinkFromQuery( query );
QByteArray data = openLinkFromQuery( query ).toEncoded(); GlobalActionManager::instance()->shortenLink( m_clipboardLongUrl );
data.replace( "'", "%27" ); // QUrl doesn't encode ', which it doesn't have to. Some apps don't like ' though, and want %27. Both are valid.
cb->setText( data );
} }
@@ -800,6 +798,7 @@ GlobalActionManager::shortenLinkRequestFinished()
{ {
qDebug() << Q_FUNC_INFO; qDebug() << Q_FUNC_INFO;
QNetworkReply *reply = qobject_cast<QNetworkReply*>( sender() ); QNetworkReply *reply = qobject_cast<QNetworkReply*>( sender() );
bool error = false;
// NOTE: this should never happen // NOTE: this should never happen
if( !reply ) if( !reply )
@@ -809,30 +808,38 @@ GlobalActionManager::shortenLinkRequestFinished()
} }
// Check for the redirect attribute, as this should be the shortened link // Check for the redirect attribute, as this should be the shortened link
QVariant urlVariant = reply->attribute( QNetworkRequest::RedirectionTargetAttribute ); QVariant urlVariant = reply->attribute( QNetworkRequest::RedirectionTargetAttribute );
// NOTE: this should never happen // NOTE: this should never happen
if( urlVariant.isNull() || !urlVariant.isValid() ) if( urlVariant.isNull() || !urlVariant.isValid() )
{ error = true;
emit shortLinkReady( reply->request().url(), QUrl( "" ) );
reply->deleteLater();
return;
}
QUrl longUrl = reply->request().url();
QUrl shortUrl = urlVariant.toUrl(); QUrl shortUrl = urlVariant.toUrl();
// NOTE: this should never happen // NOTE: this should never happen
if( !shortUrl.isValid() ) if( !shortUrl.isValid() )
{ error = true;
emit shortLinkReady( reply->request().url(), QUrl( "" ) );
reply->deleteLater();
return;
}
// Success! Here is the short link // Success! Here is the short link
if ( m_clipboardLongUrl == reply->request().url() )
{
QClipboard* cb = QApplication::clipboard();
QByteArray data = error ? longUrl.toEncoded() : shortUrl.toEncoded();
data.replace( "'", "%27" ); // QUrl doesn't encode ', which it doesn't have to. Some apps don't like ' though, and want %27. Both are valid.
cb->setText( data );
m_clipboardLongUrl.clear();
}
else
{
if ( !error )
emit shortLinkReady( longUrl, shortUrl );
else
emit shortLinkReady( longUrl, QUrl( "" ) );
}
emit shortLinkReady( reply->request().url(), shortUrl );
reply->deleteLater(); reply->deleteLater();
} }

View File

@@ -53,7 +53,7 @@ public:
/// Takes a spotify link and performs the default open action on it /// Takes a spotify link and performs the default open action on it
bool openRdioLink( const QString& link ); bool openRdioLink( const QString& link );
void copyToClipboard( const Tomahawk::query_ptr& query ) const; void copyToClipboard( const Tomahawk::query_ptr& query );
QString copyPlaylistToClipboard( const Tomahawk::dynplaylist_ptr& playlist ); QString copyPlaylistToClipboard( const Tomahawk::dynplaylist_ptr& playlist );
void savePlaylistToFile( const Tomahawk::playlist_ptr& playlist, const QString& filename ); void savePlaylistToFile( const Tomahawk::playlist_ptr& playlist, const QString& filename );
@@ -105,8 +105,7 @@ private:
Tomahawk::playlist_ptr m_toShow; Tomahawk::playlist_ptr m_toShow;
Tomahawk::query_ptr m_waitingToBookmark; Tomahawk::query_ptr m_waitingToBookmark;
Tomahawk::query_ptr m_waitingToPlay; Tomahawk::query_ptr m_waitingToPlay;
QUrl m_clipboardLongUrl;
QWeakPointer<QNetworkAccessManager> m_nam;
static GlobalActionManager* s_instance; static GlobalActionManager* s_instance;
}; };