diff --git a/ext/libclementine-tagreader/tagreader.cpp b/ext/libclementine-tagreader/tagreader.cpp index b32d3ee66..490aa23c8 100644 --- a/ext/libclementine-tagreader/tagreader.cpp +++ b/ext/libclementine-tagreader/tagreader.cpp @@ -25,6 +25,7 @@ #include #include #include +#include #include #include @@ -893,36 +894,67 @@ void TagReader::SetTextFrame(const char* id, const QString& value, void TagReader::SetTextFrame(const char* id, const std::string& value, TagLib::ID3v2::Tag* tag) const { TagLib::ByteVector id_vector(id); + QVector frames_buffer; - // Remove the frame if it already exists + // Store and clear existing frames while (tag->frameListMap().contains(id_vector) && tag->frameListMap()[id_vector].size() != 0) { + frames_buffer.push_back(tag->frameListMap()[id_vector].front()->render()); tag->removeFrame(tag->frameListMap()[id_vector].front()); } - // Create and add a new frame - TagLib::ID3v2::TextIdentificationFrame* frame = - new TagLib::ID3v2::TextIdentificationFrame(id_vector, + // If no frames stored create empty frame + if (frames_buffer.isEmpty()) { + TagLib::ID3v2::TextIdentificationFrame frame(id_vector, TagLib::String::UTF8); - frame->setText(StdStringToTaglibString(value)); - tag->addFrame(frame); + frames_buffer.push_back(frame.render()); + } + + // Update and add the frames + for (int lyrics_index = 0; lyrics_index < frames_buffer.size(); + lyrics_index++) { + TagLib::ID3v2::TextIdentificationFrame* frame = + new TagLib::ID3v2::TextIdentificationFrame( + frames_buffer.at(lyrics_index)); + if (lyrics_index == 0) { + frame->setText(StdStringToTaglibString(value)); + } + // add frame takes ownership and clears the memory + tag->addFrame(frame); + } } void TagReader::SetUnsyncLyricsFrame(const std::string& value, TagLib::ID3v2::Tag* tag) const { TagLib::ByteVector id_vector("USLT"); + QVector frames_buffer; - // Remove the frame if it already exists + // Store and clear existing frames while (tag->frameListMap().contains(id_vector) && tag->frameListMap()[id_vector].size() != 0) { + frames_buffer.push_back(tag->frameListMap()[id_vector].front()->render()); tag->removeFrame(tag->frameListMap()[id_vector].front()); } - // Create and add a new frame - TagLib::ID3v2::UnsynchronizedLyricsFrame* frame = - new TagLib::ID3v2::UnsynchronizedLyricsFrame(TagLib::String::UTF8); - frame->setText(StdStringToTaglibString(value)); - tag->addFrame(frame); + // If no frames stored create empty frame + if (frames_buffer.isEmpty()) { + TagLib::ID3v2::UnsynchronizedLyricsFrame frame(TagLib::String::UTF8); + frame.setDescription("Clementine editor"); + frames_buffer.push_back(frame.render()); + } + + // Update and add the frames + for (int lyrics_index = 0; lyrics_index < frames_buffer.size(); + lyrics_index++) { + TagLib::ID3v2::UnsynchronizedLyricsFrame* frame = + new TagLib::ID3v2::UnsynchronizedLyricsFrame( + frames_buffer.at(lyrics_index)); + if (lyrics_index == 0) { + frame->setText(StdStringToTaglibString(value)); + } + // add frame takes ownership and clears the memory + tag->addFrame(frame); + } } bool TagReader::IsMediaFile(const QString& filename) const {