From 67951f64a5291307434dafcc4e2f626254a1a064 Mon Sep 17 00:00:00 2001 From: Arnaud Bienner Date: Wed, 16 Apr 2014 23:09:48 +0200 Subject: [PATCH] SoundCloud: retrieve user's playlists --- src/internet/soundcloudservice.cpp | 34 ++++++++++++++++++++++++++++++ src/internet/soundcloudservice.h | 3 +++ 2 files changed, 37 insertions(+) diff --git a/src/internet/soundcloudservice.cpp b/src/internet/soundcloudservice.cpp index 76220f066..b8ac9b69c 100644 --- a/src/internet/soundcloudservice.cpp +++ b/src/internet/soundcloudservice.cpp @@ -67,6 +67,7 @@ SoundCloudService::SoundCloudService(Application* app, InternetModel* parent) root_(nullptr), search_(nullptr), user_tracks_(nullptr), + user_playlists_(nullptr), user_activities_(nullptr), network_(new NetworkAccessManager(this)), context_menu_(nullptr), @@ -130,6 +131,10 @@ void SoundCloudService::EnsureItemsCreated() { user_activities_->setData(InternetModel::PlayBehaviour_MultipleItems, InternetModel::Role_PlayBehaviour); root_->appendRow(user_activities_); + + user_playlists_ = + new QStandardItem(tr("Playlists")); + root_->appendRow(user_playlists_); RetrieveUserData(); // at least, try to (this will do nothing if user isn't logged) } } @@ -198,14 +203,18 @@ void SoundCloudService::Logout() { root_->removeRow(user_activities_->row()); if (user_tracks_) root_->removeRow(user_tracks_->row()); + if (user_playlists_) + root_->removeRow(user_playlists_->row()); user_activities_ = nullptr; user_tracks_ = nullptr; + user_playlists_ = nullptr; } void SoundCloudService::RetrieveUserData() { LoadAccessTokenIfEmpty(); RetrieveUserTracks(); RetrieveUserActivities(); + RetrieveUserPlaylists(); } void SoundCloudService::RetrieveUserTracks() { @@ -245,6 +254,31 @@ void SoundCloudService::UserActivitiesRetrieved(QNetworkReply* reply) { } } +void SoundCloudService::RetrieveUserPlaylists() { + QList parameters; + parameters << Param("oauth_token", access_token_); + QNetworkReply* reply = CreateRequest("me/playlists", parameters); + NewClosure(reply, SIGNAL(finished()), this, + SLOT(UserPlaylistsRetrieved(QNetworkReply*)), reply); + +} + +void SoundCloudService::UserPlaylistsRetrieved(QNetworkReply* reply) { + reply->deleteLater(); + + QList playlists = ExtractResult(reply).toList(); + for (const QVariant& playlist : playlists) { + QMap playlist_map = playlist.toMap(); + + QStandardItem* playlist_item = CreatePlaylistItem(playlist_map["title"].toString()); + SongList songs = ExtractSongs(playlist_map["tracks"]); + for (const Song& song : songs) { + playlist_item->appendRow(CreateSongItem(song)); + } + user_playlists_->appendRow(playlist_item); + } +} + void SoundCloudService::Search(const QString& text, bool now) { pending_search_ = text; diff --git a/src/internet/soundcloudservice.h b/src/internet/soundcloudservice.h index c8ab76c1d..48fec7ec0 100644 --- a/src/internet/soundcloudservice.h +++ b/src/internet/soundcloudservice.h @@ -63,6 +63,7 @@ class SoundCloudService : public InternetService { void ConnectFinished(OAuthenticator* oauth); void UserTracksRetrieved(QNetworkReply* reply); void UserActivitiesRetrieved(QNetworkReply* reply); + void UserPlaylistsRetrieved(QNetworkReply* reply); void PlaylistRetrieved(QNetworkReply* reply, int request_id); void Search(const QString& text, bool now = false); void DoSearch(); @@ -87,6 +88,7 @@ class SoundCloudService : public InternetService { void RetrieveUserData(); void RetrieveUserTracks(); void RetrieveUserActivities(); + void RetrieveUserPlaylists(); void RetrievePlaylist(int playlist_id, QStandardItem* playlist_item); void ClearSearchResults(); void EnsureItemsCreated(); @@ -106,6 +108,7 @@ class SoundCloudService : public InternetService { QStandardItem* root_; QStandardItem* search_; QStandardItem* user_tracks_; + QStandardItem* user_playlists_; QStandardItem* user_activities_; NetworkAccessManager* network_;