diff --git a/src/radio/icecastservice.cpp b/src/radio/icecastservice.cpp index ef5c1f686..cdc4cd163 100644 --- a/src/radio/icecastservice.cpp +++ b/src/radio/icecastservice.cpp @@ -74,11 +74,18 @@ struct GenreSorter { template struct StationSorter { - bool operator() (T a, T b) const { + bool operator() (const T& a, const T& b) const { return a.name.compare(b.name, Qt::CaseInsensitive) < 0; } }; +template +struct StationSorter { + bool operator() (const T* a, const T* b) const { + return a->name.compare(b->name, Qt::CaseInsensitive) < 0; + } +}; + template struct StationEquality { bool operator() (T a, T b) const { @@ -132,9 +139,32 @@ void IcecastService::ParseDirectoryFinished() { } } - // Sort genres by station count. + QSet genre_set = genres.keys().toSet(); + + // Merge genres with only 1 or 2 stations into "Other". + foreach (const QString& genre, genre_set) { + if (genres.count(genre) < 3) { + const QList& small_genre = genres.values(genre); + foreach (const Station* s, small_genre) { + genres.insert("other", s); + } + genres.remove(genre); + } + } + // Re-sort "Other" genre. + QList other_genre = genres.values("other"); + sort(other_genre.begin(), other_genre.end(), StationSorter()); + genres.remove("other"); + QListIterator other_genre_it(other_genre); + other_genre_it.toBack(); + while (other_genre_it.hasPrevious()) { + const Station* s = other_genre_it.previous(); + genres.insert("other", s); + } + // HACK: De-dupe keys. QList genre_names = genres.keys().toSet().toList(); + // Sort genres by station count. sort(genre_names.begin(), genre_names.end(), GenreSorter(genres)); foreach (const QString& genre, genre_names) {