improved removing duplicated songs

This commit is contained in:
Przemysław Dąbek 2012-11-21 16:06:19 +01:00
parent 6cf9d17f7b
commit 92637cb7d1
3 changed files with 30 additions and 6 deletions

View File

@ -1114,6 +1114,12 @@ bool Song::IsMetadataEqual(const Song& other) const {
d->cue_path_ == other.d->cue_path_;
}
bool Song::IsDuplicate(const Song& other) const {
return url() == other.url() ||
(title().toLower() == other.title().toLower() &&
artist().toLower() == other.artist().toLower());
}
bool Song::IsEditable() const {
return d->valid_ && !d->url_.isEmpty() && !is_stream() &&
d->filetype_ != Type_Unknown && !has_cue();

View File

@ -262,6 +262,7 @@ class Song {
// Comparison functions
bool IsMetadataEqual(const Song& other) const;
bool IsOnSameAlbum(const Song& other) const;
bool IsDuplicate(const Song& other) const;
bool operator==(const Song& other) const;

View File

@ -1921,17 +1921,34 @@ void Playlist::RemoveDeletedSongs() {
void Playlist::RemoveDuplicateSongs() {
QList<int> rows_to_remove;
QSet<QUrl> filenames;
QHash<Song, int> unique_songs;
for (int row = 0; row < items_.count(); ++row) {
for(int row = 0; row < items_.count(); ++row) {
PlaylistItemPtr item = items_[row];
Song song = item->Metadata();
if (filenames.contains(song.url())) {
rows_to_remove.append(row);
} else {
filenames.insert(song.url());
bool found_duplicate = false;
QHashIterator<Song, int> iterator(unique_songs);
while (iterator.hasNext() && !found_duplicate) {
iterator.next();
Song uniq_song = iterator.key();
if(song.IsDuplicate(uniq_song)){
if(song.bitrate() > uniq_song.bitrate()) {
rows_to_remove.append(unique_songs[uniq_song]);
unique_songs.remove(uniq_song);
unique_songs.insert(song, row);
}
else {
rows_to_remove.append(row);
}
found_duplicate = true;
}
}
if(!found_duplicate)
unique_songs.insert(song, row);
}
removeRows(rows_to_remove);