From 92637cb7d16b454bf0b61c72ba30257d0d646322 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Przemys=C5=82aw=20D=C4=85bek?= Date: Wed, 21 Nov 2012 16:06:19 +0100 Subject: [PATCH 1/2] improved removing duplicated songs --- src/core/song.cpp | 6 ++++++ src/core/song.h | 1 + src/playlist/playlist.cpp | 29 +++++++++++++++++++++++------ 3 files changed, 30 insertions(+), 6 deletions(-) diff --git a/src/core/song.cpp b/src/core/song.cpp index 8a60602d6..ac83a2aad 100644 --- a/src/core/song.cpp +++ b/src/core/song.cpp @@ -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(); diff --git a/src/core/song.h b/src/core/song.h index 0fda6a1df..1a4d6439a 100644 --- a/src/core/song.h +++ b/src/core/song.h @@ -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; diff --git a/src/playlist/playlist.cpp b/src/playlist/playlist.cpp index f61d85391..1640bf561 100644 --- a/src/playlist/playlist.cpp +++ b/src/playlist/playlist.cpp @@ -1921,17 +1921,34 @@ void Playlist::RemoveDeletedSongs() { void Playlist::RemoveDuplicateSongs() { QList rows_to_remove; - QSet filenames; + QHash 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 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); From a241774fb571ac1c52bbe14001f6adc897c6fa43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Przemys=C5=82aw=20D=C4=85bek?= Date: Wed, 21 Nov 2012 18:46:42 +0100 Subject: [PATCH 2/2] more consistent whitespaces --- src/playlist/playlist.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/playlist/playlist.cpp b/src/playlist/playlist.cpp index 1640bf561..4fd678fd1 100644 --- a/src/playlist/playlist.cpp +++ b/src/playlist/playlist.cpp @@ -1923,7 +1923,7 @@ void Playlist::RemoveDuplicateSongs() { QList rows_to_remove; QHash 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(); @@ -1934,8 +1934,8 @@ void Playlist::RemoveDuplicateSongs() { iterator.next(); Song uniq_song = iterator.key(); - if(song.IsDuplicate(uniq_song)){ - if(song.bitrate() > uniq_song.bitrate()) { + 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); @@ -1947,7 +1947,7 @@ void Playlist::RemoveDuplicateSongs() { } } - if(!found_duplicate) + if (!found_duplicate) unique_songs.insert(song, row); }