From fddb87ba635ca42a7f55c289a01c9bbc75177d12 Mon Sep 17 00:00:00 2001 From: Arnaud Bienner Date: Fri, 21 Mar 2014 00:52:12 +0100 Subject: [PATCH] Add an option to sort Grooveshark playlist alphabetically. Fixes issue #3480. Did it the lazy way for now, but it would have better to refactor Grooveshark code, to add a proper sort model, etc. --- src/internet/groovesharkservice.cpp | 22 ++++++++++++++--- src/internet/groovesharkservice.h | 8 ++++-- src/internet/groovesharksettingspage.cpp | 8 ++++++ src/internet/groovesharksettingspage.ui | 31 +++++++++++++++++++++++- 4 files changed, 63 insertions(+), 6 deletions(-) diff --git a/src/internet/groovesharkservice.cpp b/src/internet/groovesharkservice.cpp index f00fb096a..b8efd766a 100644 --- a/src/internet/groovesharkservice.cpp +++ b/src/internet/groovesharkservice.cpp @@ -723,7 +723,7 @@ void GroovesharkService::PlaylistSongsRetrieved(QNetworkReply* reply, QVariantMap result = ExtractResult(reply); SongList songs = ExtractSongs(result); - Song::SortSongsListAlphabetically(&songs); + SortSongsAlphabeticallyIfNeeded(&songs); for (const Song& song : songs) { QStandardItem* child = CreateSongItem(song); @@ -764,7 +764,7 @@ void GroovesharkService::UserFavoritesRetrieved(QNetworkReply* reply, QVariantMap result = ExtractResult(reply); SongList songs = ExtractSongs(result); - Song::SortSongsListAlphabetically(&songs); + SortSongsAlphabeticallyIfNeeded(&songs); for (const Song& song : songs) { QStandardItem* child = CreateSongItem(song); @@ -798,7 +798,7 @@ void GroovesharkService::UserLibrarySongsRetrieved(QNetworkReply* reply, QVariantMap result = ExtractResult(reply); SongList songs = ExtractSongs(result); - Song::SortSongsListAlphabetically(&songs); + SortSongsAlphabeticallyIfNeeded(&songs); for (const Song& song : songs) { QStandardItem* child = CreateSongItem(song); @@ -1704,8 +1704,15 @@ QVariantMap GroovesharkService::ExtractResult(QNetworkReply* reply) { return result["result"].toMap(); } +namespace { +bool CompareSongs(const QVariant& song1, const QVariant& song2) { + return song1.toMap()["Sort"].toInt() < song1.toMap()["Sort"].toInt(); +} +} + SongList GroovesharkService::ExtractSongs(const QVariantMap& result) { QVariantList result_songs = result["songs"].toList(); + qStableSort(result_songs.begin(), result_songs.end(), CompareSongs); SongList songs; for (int i = 0; i < result_songs.size(); ++i) { QVariantMap result_song = result_songs[i].toMap(); @@ -1805,3 +1812,12 @@ QList GroovesharkService::ExtractPlaylistInfo( return playlists; } + +void GroovesharkService::SortSongsAlphabeticallyIfNeeded(SongList* songs) const { + QSettings s; + s.beginGroup(GroovesharkService::kSettingsGroup); + const bool sort_songs_alphabetically = s.value("sort_alphabetically").toBool(); + if (sort_songs_alphabetically) { + Song::SortSongsListAlphabetically(songs); + } +} diff --git a/src/internet/groovesharkservice.h b/src/internet/groovesharkservice.h index d2e0c60cb..f657ad4da 100644 --- a/src/internet/groovesharkservice.h +++ b/src/internet/groovesharkservice.h @@ -135,6 +135,8 @@ signals: public slots: void Search(const QString& text, bool now = false); void ShowConfig(); + // Refresh all Grooveshark's items, and re-fill them + void RefreshItems(); protected: struct PlaylistInfo { @@ -200,8 +202,6 @@ signals: void RequestSslErrors(const QList& errors); void Homepage(); - // Refresh all Grooveshark's items, and re-fill them - void RefreshItems(); private: void EnsureMenuCreated(); @@ -248,6 +248,10 @@ signals: void ResetSessionId(); + // Sort songs alphabetically only if the "sort_alphabetically" option has been + // checked in the preferences settings. + void SortSongsAlphabeticallyIfNeeded(SongList* songs) const; + GroovesharkUrlHandler* url_handler_; QString pending_search_; diff --git a/src/internet/groovesharksettingspage.cpp b/src/internet/groovesharksettingspage.cpp index 71bd461a3..ca198f5b3 100644 --- a/src/internet/groovesharksettingspage.cpp +++ b/src/internet/groovesharksettingspage.cpp @@ -70,6 +70,8 @@ void GroovesharkSettingsPage::Load() { validated_ = false; UpdateLoginState(); + + ui_->sort_alphabetically->setChecked(s.value("sort_alphabetically").toBool()); } void GroovesharkSettingsPage::Save() { @@ -78,6 +80,12 @@ void GroovesharkSettingsPage::Save() { s.setValue("username", ui_->username->text()); s.setValue("sessionid", service_->session_id()); + const bool old_sort_value = s.value("sort_alphabetically").toBool(); + const bool new_sort_value = ui_->sort_alphabetically->isChecked(); + if (old_sort_value != new_sort_value) { + s.setValue("sort_alphabetically", new_sort_value); + service_->RefreshItems(); + } } void GroovesharkSettingsPage::LoginFinished(bool success) { diff --git a/src/internet/groovesharksettingspage.ui b/src/internet/groovesharksettingspage.ui index 158af7799..f79278f6e 100644 --- a/src/internet/groovesharksettingspage.ui +++ b/src/internet/groovesharksettingspage.ui @@ -7,12 +7,15 @@ 0 0 480 - 141 + 184 Grooveshark + + By default, Grooveshark sorts songs on date added + @@ -69,6 +72,32 @@ + + + + Preferences + + + + + + + + true + + + Sort playlists songs alphabetically + + + false + + + + + + + +