Sort retrieved GS songs only in some cases.

Indeed, even it makes sense sometimes (e.g. for playlists) it doesn't make sense at all for search results...
Make the sorting function part of Song.
This commit is contained in:
Arnaud Bienner 2012-06-27 01:55:51 +02:00
parent ac936c5374
commit b49c3db6e0
3 changed files with 18 additions and 5 deletions

View File

@ -331,6 +331,15 @@ QString Song::TextForFiletype(FileType type) {
}
}
int CompareSongsName(const Song& song1, const Song& song2) {
return song1.PrettyTitleWithArtist().localeAwareCompare(song2.PrettyTitleWithArtist()) < 0;
}
void Song::SortSongsListAlphabetically(SongList* songs) {
Q_ASSERT(songs);
qSort(songs->begin(), songs->end(), CompareSongsName);
}
void Song::Init(const QString& title, const QString& artist,
const QString& album, qint64 length_nanosec) {
d->valid_ = true;

View File

@ -95,6 +95,9 @@ class Song {
static QString TextForFiletype(FileType type);
QString TextForFiletype() const { return TextForFiletype(filetype()); }
// Sort songs alphabetically using their pretty title
static void SortSongsListAlphabetically(QList<Song>* songs);
// Constructors
void Init(const QString& title, const QString& artist, const QString& album, qint64 length_nanosec);
void Init(const QString& title, const QString& artist, const QString& album, qint64 beginning, qint64 end);

View File

@ -711,6 +711,8 @@ void GroovesharkService::PlaylistSongsRetrieved() {
QVariantMap result = ExtractResult(reply);
SongList songs = ExtractSongs(result);
Song::SortSongsListAlphabetically(&songs);
foreach (const Song& song, songs) {
QStandardItem* child = new QStandardItem(song.PrettyTitleWithArtist());
child->setData(Type_Track, InternetModel::Role_Type);
@ -752,6 +754,8 @@ void GroovesharkService::UserFavoritesRetrieved(QNetworkReply* reply, int task_i
QVariantMap result = ExtractResult(reply);
SongList songs = ExtractSongs(result);
Song::SortSongsListAlphabetically(&songs);
foreach (const Song& song, songs) {
QStandardItem* child = new QStandardItem(song.PrettyTitleWithArtist());
child->setData(Type_Track, InternetModel::Role_Type);
@ -784,6 +788,7 @@ void GroovesharkService::PopularSongsMonthRetrieved(QNetworkReply* reply) {
reply->deleteLater();
QVariantMap result = ExtractResult(reply);
SongList songs = ExtractSongs(result);
Song::SortSongsListAlphabetically(&songs);
app_->task_manager()->IncreaseTaskProgress(task_popular_id_, 50, 100);
if (app_->task_manager()->GetTaskProgress(task_popular_id_) >= 100) {
@ -816,6 +821,7 @@ void GroovesharkService::PopularSongsTodayRetrieved(QNetworkReply* reply) {
reply->deleteLater();
QVariantMap result = ExtractResult(reply);
SongList songs = ExtractSongs(result);
Song::SortSongsListAlphabetically(&songs);
app_->task_manager()->IncreaseTaskProgress(task_popular_id_, 50, 100);
if (app_->task_manager()->GetTaskProgress(task_popular_id_) >= 100) {
@ -1561,10 +1567,6 @@ QVariantMap GroovesharkService::ExtractResult(QNetworkReply* reply) {
return result["result"].toMap();
}
int CompareSongsName(const Song& song1, const Song& song2) {
return song1.PrettyTitleWithArtist().localeAwareCompare(song2.PrettyTitleWithArtist()) < 0;
}
SongList GroovesharkService::ExtractSongs(const QVariantMap& result) {
QVariantList result_songs = result["songs"].toList();
SongList songs;
@ -1572,7 +1574,6 @@ SongList GroovesharkService::ExtractSongs(const QVariantMap& result) {
QVariantMap result_song = result_songs[i].toMap();
songs << ExtractSong(result_song);
}
qSort(songs.begin(), songs.end(), CompareSongsName);
return songs;
}