mirror of
https://github.com/clementine-player/Clementine
synced 2025-02-01 11:56:45 +01:00
Adding GrooveShark playlists
This commit is contained in:
parent
42d228be80
commit
8e6608a29f
@ -146,24 +146,7 @@ void GrooveSharkService::SearchSongsFinished() {
|
||||
reply->deleteLater();
|
||||
|
||||
QVariantMap result = ExtractResult(reply);
|
||||
QVariantList result_songs = result["songs"].toList();
|
||||
SongList songs;
|
||||
for (int i=0; i<result_songs.size(); ++i) {
|
||||
QVariantMap result_song = result_songs[i].toMap();
|
||||
Song song;
|
||||
int song_id = result_song["SongID"].toInt();
|
||||
QString song_name = result_song["SongName"].toString();
|
||||
QString artist_name = result_song["ArtistName"].toString();
|
||||
QString album_name = result_song["AlbumName"].toString();
|
||||
song.Init(song_name, artist_name, album_name, 0);
|
||||
song.set_id(song_id);
|
||||
// Special kind of URL: because we need to request a stream key for each
|
||||
// play, we generate fake URL for now, and we will create a real streaming
|
||||
// URL when user will actually play the song.
|
||||
// TODO: Implement an UrlHandler
|
||||
song.set_url(QString("grooveshark://%1").arg(song_id));
|
||||
songs << song;
|
||||
}
|
||||
SongList songs = ExtractSongs(result);
|
||||
pending_search_playlist_->Clear();
|
||||
pending_search_playlist_->InsertSongs(songs);
|
||||
}
|
||||
@ -336,6 +319,7 @@ void GrooveSharkService::EnsureItemsCreated() {
|
||||
search_->setData(InternetModel::PlayBehaviour_DoubleClickAction,
|
||||
InternetModel::Role_PlayBehaviour);
|
||||
root_->appendRow(search_);
|
||||
RetrieveUserPlaylists();
|
||||
}
|
||||
}
|
||||
|
||||
@ -347,6 +331,70 @@ void GrooveSharkService::EnsureConnected() {
|
||||
}
|
||||
}
|
||||
|
||||
void GrooveSharkService::RetrieveUserPlaylists() {
|
||||
QNetworkReply *reply;
|
||||
|
||||
reply = CreateRequest("getUserPlaylists", QList<Param>(), true);
|
||||
|
||||
connect(reply, SIGNAL(finished()), SLOT(UserPlaylistsRetrieved()));
|
||||
}
|
||||
|
||||
void GrooveSharkService::UserPlaylistsRetrieved() {
|
||||
QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
|
||||
if (!reply)
|
||||
return;
|
||||
|
||||
reply->deleteLater();
|
||||
|
||||
QVariantMap result = ExtractResult(reply);
|
||||
QVariantList playlists = result["playlists"].toList();
|
||||
QVariantList::iterator it;
|
||||
for (it = playlists.begin(); it != playlists.end(); ++it) {
|
||||
// Get playlist info
|
||||
QVariantMap playlist = (*it).toMap();
|
||||
int playlist_id = playlist["PlaylistID"].toInt();
|
||||
QString playlist_name = playlist["PlaylistName"].toString();
|
||||
|
||||
// Request playlist's songs
|
||||
QNetworkReply *reply;
|
||||
QList<Param> parameters;
|
||||
parameters << Param("playlistID", playlist_id);
|
||||
reply = CreateRequest("getPlaylistSongs", parameters, true);
|
||||
connect(reply, SIGNAL(finished()), SLOT(PlaylistSongsRetrieved()));
|
||||
|
||||
// Keep in mind correspondance between reply object and playlist
|
||||
pending_retrieve_playlists_.insert(reply, PlaylistInfo(playlist_id, playlist_name));
|
||||
}
|
||||
}
|
||||
|
||||
void GrooveSharkService::PlaylistSongsRetrieved() {
|
||||
QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender());
|
||||
if (!reply)
|
||||
return;
|
||||
|
||||
reply->deleteLater();
|
||||
|
||||
// Find corresponding playlist info
|
||||
PlaylistInfo playlist_info = pending_retrieve_playlists_.take(reply);
|
||||
// Create playlist item
|
||||
QStandardItem* item = new QStandardItem(playlist_info.name_);
|
||||
item->setData(Type_UserPlaylist, InternetModel::Role_Type);
|
||||
item->setData(true, InternetModel::Role_CanLazyLoad);
|
||||
|
||||
QVariantMap result = ExtractResult(reply);
|
||||
SongList songs = ExtractSongs(result);
|
||||
foreach (const Song& song, songs) {
|
||||
QStandardItem* child = new QStandardItem(song.PrettyTitleWithArtist());
|
||||
child->setData(Type_Track, InternetModel::Role_Type);
|
||||
child->setData(QVariant::fromValue(song), InternetModel::Role_SongMetadata);
|
||||
child->setData(InternetModel::PlayBehaviour_SingleItem, InternetModel::Role_PlayBehaviour);
|
||||
child->setData(song.url(), InternetModel::Role_Url);
|
||||
|
||||
item->appendRow(child);
|
||||
}
|
||||
root_->appendRow(item);
|
||||
}
|
||||
|
||||
void GrooveSharkService::OpenSearchTab() {
|
||||
model()->player()->playlists()->New(tr("Search GrooveShark"), SongList(),
|
||||
GrooveSharkSearchPlaylistType::kName);
|
||||
@ -408,3 +456,24 @@ QVariantMap GrooveSharkService::ExtractResult(QNetworkReply* reply) {
|
||||
qLog(Debug) << result;
|
||||
return result["result"].toMap();
|
||||
}
|
||||
|
||||
SongList GrooveSharkService::ExtractSongs(const QVariantMap& result) {
|
||||
QVariantList result_songs = result["songs"].toList();
|
||||
SongList songs;
|
||||
for (int i=0; i<result_songs.size(); ++i) {
|
||||
QVariantMap result_song = result_songs[i].toMap();
|
||||
Song song;
|
||||
int song_id = result_song["SongID"].toInt();
|
||||
QString song_name = result_song["SongName"].toString();
|
||||
QString artist_name = result_song["ArtistName"].toString();
|
||||
QString album_name = result_song["AlbumName"].toString();
|
||||
song.Init(song_name, artist_name, album_name, 0);
|
||||
song.set_id(song_id);
|
||||
// Special kind of URL: because we need to request a stream key for each
|
||||
// play, we generate a fake URL for now, and we will create a real streaming
|
||||
// URL when user will actually play the song (through url handler)
|
||||
song.set_url(QString("grooveshark://%1").arg(song_id));
|
||||
songs << song;
|
||||
}
|
||||
return songs;
|
||||
}
|
||||
|
@ -35,7 +35,9 @@ class GrooveSharkService : public InternetService {
|
||||
~GrooveSharkService();
|
||||
|
||||
enum Type {
|
||||
Type_SearchResults = InternetModel::TypeCount
|
||||
Type_SearchResults = InternetModel::TypeCount,
|
||||
Type_UserPlaylist,
|
||||
Type_Track
|
||||
};
|
||||
|
||||
// Values are persisted - don't change.
|
||||
@ -59,6 +61,8 @@ class GrooveSharkService : public InternetService {
|
||||
void Login(const QString& username, const QString& password);
|
||||
void Logout();
|
||||
bool IsLoggedIn() { return !session_id_.isEmpty(); }
|
||||
void RetrieveUserPlaylists();
|
||||
|
||||
// Persisted in the settings and updated on each Login().
|
||||
LoginState login_state() const { return login_state_; }
|
||||
const QString& session_id() { return session_id_; }
|
||||
@ -80,6 +84,15 @@ class GrooveSharkService : public InternetService {
|
||||
protected:
|
||||
QModelIndex GetCurrentIndex();
|
||||
|
||||
struct PlaylistInfo {
|
||||
PlaylistInfo() {}
|
||||
PlaylistInfo(int id, QString name)
|
||||
: id_(id), name_(name) {}
|
||||
|
||||
int id_;
|
||||
QString name_;
|
||||
};
|
||||
|
||||
private slots:
|
||||
void UpdateTotalSongCount(int count);
|
||||
|
||||
@ -89,6 +102,8 @@ class GrooveSharkService : public InternetService {
|
||||
void DoSearch();
|
||||
void SearchSongsFinished();
|
||||
void Authenticated();
|
||||
void UserPlaylistsRetrieved();
|
||||
void PlaylistSongsRetrieved();
|
||||
|
||||
private:
|
||||
void EnsureMenuCreated();
|
||||
@ -106,13 +121,18 @@ class GrooveSharkService : public InternetService {
|
||||
bool use_https = false);
|
||||
// Convenient function for extracting result from reply
|
||||
QVariantMap ExtractResult(QNetworkReply* reply);
|
||||
// Convenient function for extracting songs from grooveshark result
|
||||
SongList ExtractSongs(const QVariantMap& result);
|
||||
void ResetSessionId();
|
||||
|
||||
|
||||
GrooveSharkUrlHandler* url_handler_;
|
||||
|
||||
QString pending_search_;
|
||||
Playlist* pending_search_playlist_;
|
||||
|
||||
QMap<QNetworkReply*, PlaylistInfo> pending_retrieve_playlists_;
|
||||
|
||||
QStandardItem* root_;
|
||||
QStandardItem* search_;
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user