From 1e0a00dbe27e7bfdc40a4cedbfd75d439aa927ae Mon Sep 17 00:00:00 2001
From: Leo Franchi <lfranchi@kde.org>
Date: Mon, 18 Jun 2012 20:08:50 +0200
Subject: [PATCH] Encode spaces as + and unify encoding logic in
 GlobalActionManager

---
 src/libtomahawk/GlobalActionManager.cpp | 33 ++++++++++++++++---------
 src/libtomahawk/GlobalActionManager.h   |  2 ++
 2 files changed, 23 insertions(+), 12 deletions(-)

diff --git a/src/libtomahawk/GlobalActionManager.cpp b/src/libtomahawk/GlobalActionManager.cpp
index 551f4bf42..a7553202c 100644
--- a/src/libtomahawk/GlobalActionManager.cpp
+++ b/src/libtomahawk/GlobalActionManager.cpp
@@ -103,8 +103,7 @@ GlobalActionManager::copyOpenLink( const artist_ptr& artist ) const
     const QUrl link( QString( "%1/artist/%2" ).arg( hostname() ).arg( artist->name() ) );
 
     QClipboard* cb = QApplication::clipboard();
-    QByteArray data = link.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.
+    QByteArray data = percentEncode( link );
     cb->setText( data );
 
     return link;
@@ -114,11 +113,11 @@ GlobalActionManager::copyOpenLink( const artist_ptr& artist ) const
 QUrl
 GlobalActionManager::copyOpenLink( const album_ptr& album ) const
 {
-    const QUrl link( QString( "%1/album/%2/%3" ).arg( hostname() ).arg( album->artist().isNull() ? QString() : album->artist()->name() ).arg( album->name()) );
+    const QUrl link = QUrl::fromUserInput( QString( "%1/album/%2/%3" ).arg( hostname() ).arg( album->artist().isNull() ? QString() : album->artist()->name() ).arg( album->name() ) );
 
     QClipboard* cb = QApplication::clipboard();
-    QByteArray data = link.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.
+    QByteArray data = percentEncode( link );
+
     cb->setText( data );
 
     return link;
@@ -255,8 +254,7 @@ GlobalActionManager::copyPlaylistToClipboard( const dynplaylist_ptr& playlist )
     }
 
     QClipboard* cb = QApplication::clipboard();
-    QByteArray data = link.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.
+    QByteArray data = percentEncode( link );
     cb->setText( data );
 
     return link.toString();
@@ -1217,8 +1215,7 @@ GlobalActionManager::shortenLinkRequestFinished()
     {
         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.
+        QByteArray data = percentEncode( error ? longUrl : shortUrl );
         cb->setText( data );
 
         m_clipboardLongUrl.clear();
@@ -1250,8 +1247,7 @@ GlobalActionManager::postShortenFinished()
     qDebug() << "GOT POSTED SHORT URL:" << url.toString();
     QClipboard* cb = QApplication::clipboard();
 
-    QByteArray data = url.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.
+    const QByteArray data = percentEncode( url );
     cb->setText( data );
 
     reply->deleteLater();
@@ -1387,5 +1383,18 @@ GlobalActionManager::openRdioLink( const QString& link )
 QString
 GlobalActionManager::hostname() const
 {
-    return QString( "http://toma.hk" );
+    return QString( "http://stage.toma.hk" );
+}
+
+
+QByteArray
+GlobalActionManager::percentEncode (const QUrl &url ) const
+{
+    QByteArray data = url.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.
+    data.replace( "%20", "+" );
+    data.replace( "&", "%26" );
+
+    return data;
 }
diff --git a/src/libtomahawk/GlobalActionManager.h b/src/libtomahawk/GlobalActionManager.h
index 8223ccafe..c04290d15 100644
--- a/src/libtomahawk/GlobalActionManager.h
+++ b/src/libtomahawk/GlobalActionManager.h
@@ -131,6 +131,8 @@ private:
 
     QString hostname() const;
 
+    inline QByteArray percentEncode( const QUrl& url ) const;
+
     Tomahawk::playlist_ptr m_toShow;
     Tomahawk::query_ptr m_waitingToBookmark;
     Tomahawk::query_ptr m_waitingToPlay;