From bee99b75544414e9c23f43c3a50871db4ae24271 Mon Sep 17 00:00:00 2001 From: Arnaud Bienner Date: Tue, 27 Mar 2012 00:43:47 +0200 Subject: [PATCH] Ability to get a URL to share GS playlists --- src/internet/groovesharkservice.cpp | 48 +++++++++++++++++++++++++++-- src/internet/groovesharkservice.h | 8 +++++ 2 files changed, 54 insertions(+), 2 deletions(-) diff --git a/src/internet/groovesharkservice.cpp b/src/internet/groovesharkservice.cpp index fd9dc7d9f..522265e1f 100644 --- a/src/internet/groovesharkservice.cpp +++ b/src/internet/groovesharkservice.cpp @@ -102,6 +102,7 @@ GroovesharkService::GroovesharkService(Application* app, InternetModel *parent) remove_from_playlist_(NULL), remove_from_favorites_(NULL), get_url_to_share_song_(NULL), + get_url_to_share_playlist_(NULL), search_delay_(new QTimer(this)), last_search_reply_(NULL), api_key_(QByteArray::fromBase64(kApiSecret)), @@ -442,7 +443,8 @@ void GroovesharkService::ShowContextMenu(const QPoint& global_pos) { bool display_delete_playlist_action = false, display_remove_from_playlist_action = false, display_remove_from_favorites_action = false, - display_share_song_url = false; + display_share_song_url = false, + display_share_playlist_url = false; QModelIndex index(model()->current_index()); @@ -466,12 +468,26 @@ void GroovesharkService::ShowContextMenu(const QPoint& global_pos) { remove_from_playlist_->setVisible(display_remove_from_playlist_action); remove_from_favorites_->setVisible(display_remove_from_favorites_action); + // Check if we can display actions to get URL for sharing songs/playlists: + // - share song if (index.data(InternetModel::Role_Type).toInt() == Type_Track) { display_share_song_url = true; current_song_id_ = ExtractSongId(index.data(InternetModel::Role_Url).toUrl()); } get_url_to_share_song_->setVisible(display_share_song_url); + // - share playlist + if (index.data(InternetModel::Role_Type).toInt() == InternetModel::Type_UserPlaylist + && index.data(Role_UserPlaylistId).isValid()) { + display_share_playlist_url = true; + current_playlist_id_ = index.data(Role_UserPlaylistId).toInt(); + } else if (parent_type == InternetModel::Type_UserPlaylist + && index.parent().data(Role_UserPlaylistId).isValid()) { + display_share_playlist_url = true; + current_playlist_id_ = index.parent().data(Role_UserPlaylistId).toInt(); + } + get_url_to_share_playlist_->setVisible(display_share_playlist_url); + context_menu_->popup(global_pos); } @@ -501,6 +517,9 @@ void GroovesharkService::EnsureMenuCreated() { get_url_to_share_song_ = context_menu_->addAction( tr("Get an URL to share this Grooveshark song"), this, SLOT(GetCurrentSongUrlToShare())); + get_url_to_share_playlist_ = context_menu_->addAction( + tr("Get an URL to share this Grooveshark playlist"), + this, SLOT(GetCurrentPlaylistUrlToShare())); context_menu_->addSeparator(); context_menu_->addAction(IconLoader::Load("edit-find"), tr("Search Grooveshark (opens a new tab)") + "...", @@ -1071,9 +1090,34 @@ void GroovesharkService::SongUrlToShareReceived(QNetworkReply* reply) { if (!result["url"].isValid()) return; QString url = result["url"].toString(); + ShowUrlBox(tr("Grooveshark song's URL"), url); +} +void GroovesharkService::GetCurrentPlaylistUrlToShare() { + GetPlaylistUrlToShare(current_playlist_id_); +} + +void GroovesharkService::GetPlaylistUrlToShare(int playlist_id) { + QList parameters; + parameters << Param("playlistID", playlist_id); + QNetworkReply* reply = CreateRequest("getPlaylistURLFromPlaylistID", parameters); + + NewClosure(reply, SIGNAL(finished()), this, + SLOT(PlaylistUrlToShareReceived(QNetworkReply*)), reply); +} + +void GroovesharkService::PlaylistUrlToShareReceived(QNetworkReply* reply) { + reply->deleteLater(); + QVariantMap result = ExtractResult(reply); + if (!result["url"].isValid()) + return; + QString url = result["url"].toString(); + ShowUrlBox(tr("Grooveshark playlist's URL"), url); +} + +void GroovesharkService::ShowUrlBox(const QString& title, const QString& url) { QMessageBox url_box; - url_box.setWindowTitle(tr("Grooveshark song's URL")); + url_box.setWindowTitle(title); url_box.setWindowIcon(QIcon(":/icon.png")); url_box.setText(url); url_box.setStandardButtons(QMessageBox::Ok); diff --git a/src/internet/groovesharkservice.h b/src/internet/groovesharkservice.h index 1dfafb896..92d451878 100644 --- a/src/internet/groovesharkservice.h +++ b/src/internet/groovesharkservice.h @@ -98,6 +98,7 @@ class GroovesharkService : public InternetService { void AddUserFavoriteSong(int song_id); void RemoveFromFavorites(int song_id); void GetSongUrlToShare(int song_id); + void GetPlaylistUrlToShare(int playlist_id); // Start autoplay for the given tag_id, fill the autoplay_state, returns a // first song to play Song StartAutoplayTag(int tag_id, QVariantMap& autoplay_state); @@ -172,6 +173,8 @@ class GroovesharkService : public InternetService { void UserFavoriteSongAdded(QNetworkReply* reply, int task_id); void GetCurrentSongUrlToShare(); void SongUrlToShareReceived(QNetworkReply* reply); + void GetCurrentPlaylistUrlToShare(); + void PlaylistUrlToShareReceived(QNetworkReply* reply); void RemoveCurrentFromPlaylist(); void RemoveCurrentFromFavorites(); void SongRemovedFromFavorites(QNetworkReply* reply, int task_id); @@ -205,6 +208,7 @@ class GroovesharkService : public InternetService { // Convenient function which block until 'reply' replies, or timeout after 10 // seconds. Returns false if reply has timeouted bool WaitForReply(QNetworkReply* reply); + void ShowUrlBox(const QString& title, const QString& url); // Convenient function for extracting result from reply QVariantMap ExtractResult(QNetworkReply* reply); // Convenient function for extracting songs from grooveshark result. result @@ -247,7 +251,10 @@ class GroovesharkService : public InternetService { NetworkAccessManager* network_; QMenu* context_menu_; + // IDs kept when showing menu, to know what the user has clicked on, to be + // able to perform actions on corresponding items int current_song_id_; + int current_playlist_id_; QAction* create_playlist_; QAction* delete_playlist_; @@ -255,6 +262,7 @@ class GroovesharkService : public InternetService { QAction* remove_from_playlist_; QAction* remove_from_favorites_; QAction* get_url_to_share_song_; + QAction* get_url_to_share_playlist_; QList playlistitem_actions_; QTimer* search_delay_;