1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-03-21 16:29:43 +01: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
GlobalActionManager::copyToClipboard( const query_ptr& query ) const
GlobalActionManager::copyToClipboard( const query_ptr& query )
{
QClipboard* cb = QApplication::clipboard();
QByteArray data = openLinkFromQuery( query ).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 = openLinkFromQuery( query );
GlobalActionManager::instance()->shortenLink( m_clipboardLongUrl );
}
@ -800,6 +798,7 @@ GlobalActionManager::shortenLinkRequestFinished()
{
qDebug() << Q_FUNC_INFO;
QNetworkReply *reply = qobject_cast<QNetworkReply*>( sender() );
bool error = false;
// NOTE: this should never happen
if( !reply )
@ -809,30 +808,38 @@ GlobalActionManager::shortenLinkRequestFinished()
}
// 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;
}
error = true;
QUrl longUrl = reply->request().url();
QUrl shortUrl = urlVariant.toUrl();
// NOTE: this should never happen
if( !shortUrl.isValid() )
{
emit shortLinkReady( reply->request().url(), QUrl( "" ) );
reply->deleteLater();
return;
}
error = true;
// 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();
}

View File

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