From 62e0d9fe64d53865afe175fb86462f85cf6370e2 Mon Sep 17 00:00:00 2001 From: Jonas Kvinge Date: Wed, 4 Nov 2020 18:16:23 +0100 Subject: [PATCH] Remove all uses of QTextCodec --- ext/libstrawberry-tagreader/tagreader.cpp | 110 ++++++++----------- ext/libstrawberry-tagreader/tagreader.h | 10 +- src/core/song.cpp | 9 -- src/core/song.h | 3 - src/covermanager/musixmatchcoverprovider.cpp | 10 +- src/lyrics/geniuslyricsprovider.cpp | 9 +- src/lyrics/musixmatchlyricsprovider.cpp | 9 +- src/playlistparsers/cueparser.cpp | 4 - 8 files changed, 53 insertions(+), 111 deletions(-) diff --git a/ext/libstrawberry-tagreader/tagreader.cpp b/ext/libstrawberry-tagreader/tagreader.cpp index 31309bbd..896f4f32 100644 --- a/ext/libstrawberry-tagreader/tagreader.cpp +++ b/ext/libstrawberry-tagreader/tagreader.cpp @@ -84,7 +84,6 @@ #include #include #include -#include #include #include @@ -207,10 +206,10 @@ void TagReader::ReadFile(const QString &filename, pb::tagreader::SongMetadata *s TagLib::Tag *tag = fileref->tag(); if (tag) { - Decode(tag->title(), nullptr, song->mutable_title()); - Decode(tag->artist(), nullptr, song->mutable_artist()); // TPE1 - Decode(tag->album(), nullptr, song->mutable_album()); - Decode(tag->genre(), nullptr, song->mutable_genre()); + Decode(tag->title(), song->mutable_title()); + Decode(tag->artist(), song->mutable_artist()); // TPE1 + Decode(tag->album(), song->mutable_album()); + Decode(tag->genre(), song->mutable_genre()); song->set_year(tag->year()); song->set_track(tag->track()); song->set_valid(true); @@ -223,7 +222,7 @@ void TagReader::ReadFile(const QString &filename, pb::tagreader::SongMetadata *s // Handle all the files which have VorbisComments (Ogg, OPUS, ...) in the same way; // apart, so we keep specific behavior for some formats by adding another "else if" block below. if (TagLib::Ogg::XiphComment *tag_ogg = dynamic_cast(fileref->file()->tag())) { - ParseOggTag(tag_ogg->fieldListMap(), nullptr, &disc, &compilation, song); + ParseOggTag(tag_ogg->fieldListMap(), &disc, &compilation, song); if (!tag_ogg->pictureList().isEmpty()) { song->set_art_automatic(kEmbeddedCover); } @@ -234,28 +233,28 @@ void TagReader::ReadFile(const QString &filename, pb::tagreader::SongMetadata *s song->set_bitdepth(file_flac->audioProperties()->bitsPerSample()); if (file_flac->xiphComment()) { - ParseOggTag(file_flac->xiphComment()->fieldListMap(), nullptr, &disc, &compilation, song); + ParseOggTag(file_flac->xiphComment()->fieldListMap(), &disc, &compilation, song); if (!file_flac->pictureList().isEmpty()) { song->set_art_automatic(kEmbeddedCover); } } - if (tag) Decode(tag->comment(), nullptr, song->mutable_comment()); + if (tag) Decode(tag->comment(), song->mutable_comment()); } else if (TagLib::WavPack::File *file_wavpack = dynamic_cast(fileref->file())) { song->set_bitdepth(file_wavpack->audioProperties()->bitsPerSample()); if (file_wavpack->APETag()) { - ParseAPETag(file_wavpack->APETag()->itemListMap(), nullptr, &disc, &compilation, song); + ParseAPETag(file_wavpack->APETag()->itemListMap(), &disc, &compilation, song); } - if (tag) Decode(tag->comment(), nullptr, song->mutable_comment()); + if (tag) Decode(tag->comment(), song->mutable_comment()); } else if (TagLib::APE::File *file_ape = dynamic_cast(fileref->file())) { if (file_ape->APETag()) { - ParseAPETag(file_ape->APETag()->itemListMap(), nullptr, &disc, &compilation, song); + ParseAPETag(file_ape->APETag()->itemListMap(), &disc, &compilation, song); } song->set_bitdepth(file_ape->audioProperties()->bitsPerSample()); - if (tag) Decode(tag->comment(), nullptr, song->mutable_comment()); + if (tag) Decode(tag->comment(), song->mutable_comment()); } else if (TagLib::MPEG::File *file_mpeg = dynamic_cast(fileref->file())) { @@ -264,22 +263,22 @@ void TagReader::ReadFile(const QString &filename, pb::tagreader::SongMetadata *s const TagLib::ID3v2::FrameListMap &map = file_mpeg->ID3v2Tag()->frameListMap(); if (!map["TPOS"].isEmpty()) disc = TStringToQString(map["TPOS"].front()->toString()).trimmed(); - if (!map["TCOM"].isEmpty()) Decode(map["TCOM"].front()->toString(), nullptr, song->mutable_composer()); + if (!map["TCOM"].isEmpty()) Decode(map["TCOM"].front()->toString(), song->mutable_composer()); // content group - if (!map["TIT1"].isEmpty()) Decode(map["TIT1"].front()->toString(), nullptr, song->mutable_grouping()); + if (!map["TIT1"].isEmpty()) Decode(map["TIT1"].front()->toString(), song->mutable_grouping()); // ID3v2: lead performer/soloist - if (!map["TPE1"].isEmpty()) Decode(map["TPE1"].front()->toString(), nullptr, song->mutable_performer()); + if (!map["TPE1"].isEmpty()) Decode(map["TPE1"].front()->toString(), song->mutable_performer()); // original artist/performer - if (!map["TOPE"].isEmpty()) Decode(map["TOPE"].front()->toString(), nullptr, song->mutable_performer()); + if (!map["TOPE"].isEmpty()) Decode(map["TOPE"].front()->toString(), song->mutable_performer()); // Skip TPE1 (which is the artist) here because we already fetched it // non-standard: Apple, Microsoft - if (!map["TPE2"].isEmpty()) Decode(map["TPE2"].front()->toString(), nullptr, song->mutable_albumartist()); + if (!map["TPE2"].isEmpty()) Decode(map["TPE2"].front()->toString(), song->mutable_albumartist()); if (!map["TCMP"].isEmpty()) compilation = TStringToQString(map["TCMP"].front()->toString()).trimmed(); @@ -289,10 +288,10 @@ void TagReader::ReadFile(const QString &filename, pb::tagreader::SongMetadata *s } if (!map["USLT"].isEmpty()) { - Decode(map["USLT"].front()->toString(), nullptr, song->mutable_lyrics()); + Decode(map["USLT"].front()->toString(), song->mutable_lyrics()); } else if (!map["SYLT"].isEmpty()) { - Decode(map["SYLT"].front()->toString(), nullptr, song->mutable_lyrics()); + Decode(map["SYLT"].front()->toString(), song->mutable_lyrics()); } if (!map["APIC"].isEmpty()) song->set_art_automatic(kEmbeddedCover); @@ -302,7 +301,7 @@ void TagReader::ReadFile(const QString &filename, pb::tagreader::SongMetadata *s const TagLib::ID3v2::CommentsFrame *frame = dynamic_cast(map["COMM"][i]); if (frame && TStringToQString(frame->description()) != "iTunNORM") { - Decode(frame->text(), nullptr, song->mutable_comment()); + Decode(frame->text(), song->mutable_comment()); break; } } @@ -321,7 +320,7 @@ void TagReader::ReadFile(const QString &filename, pb::tagreader::SongMetadata *s if (mp4_tag->item("aART").isValid()) { TagLib::StringList album_artists = mp4_tag->item("aART").toStringList(); if (!album_artists.isEmpty()) { - Decode(album_artists.front(), nullptr, song->mutable_albumartist()); + Decode(album_artists.front(), song->mutable_albumartist()); } } @@ -335,20 +334,20 @@ void TagReader::ReadFile(const QString &filename, pb::tagreader::SongMetadata *s } if (mp4_tag->item("\251wrt").isValid()) { - Decode(mp4_tag->item("\251wrt").toStringList().toString(", "), nullptr, song->mutable_composer()); + Decode(mp4_tag->item("\251wrt").toStringList().toString(", "), song->mutable_composer()); } if (mp4_tag->item("\251grp").isValid()) { - Decode(mp4_tag->item("\251grp").toStringList().toString(" "), nullptr, song->mutable_grouping()); + Decode(mp4_tag->item("\251grp").toStringList().toString(" "), song->mutable_grouping()); } if (mp4_tag->item("\251lyr").isValid()) { - Decode(mp4_tag->item("\251lyr").toStringList().toString(" "), nullptr, song->mutable_lyrics()); + Decode(mp4_tag->item("\251lyr").toStringList().toString(" "), song->mutable_lyrics()); } if (mp4_tag->item(kMP4_OriginalYear_ID).isValid()) { song->set_originalyear(TStringToQString(mp4_tag->item(kMP4_OriginalYear_ID).toStringList().toString('\n')).left(4).toInt()); } - Decode(mp4_tag->comment(), nullptr, song->mutable_comment()); + Decode(mp4_tag->comment(), song->mutable_comment()); } } @@ -357,7 +356,7 @@ void TagReader::ReadFile(const QString &filename, pb::tagreader::SongMetadata *s song->set_bitdepth(file_asf->audioProperties()->bitsPerSample()); if (file_asf->tag()) { - Decode(file_asf->tag()->comment(), nullptr, song->mutable_comment()); + Decode(file_asf->tag()->comment(), song->mutable_comment()); } const TagLib::ASF::AttributeListMap &attributes_map = file_asf->tag()->attributeListMap(); @@ -378,13 +377,13 @@ void TagReader::ReadFile(const QString &filename, pb::tagreader::SongMetadata *s else if (TagLib::MPC::File *file_mpc = dynamic_cast(fileref->file())) { if (file_mpc->APETag()) { - ParseAPETag(file_mpc->APETag()->itemListMap(), nullptr, &disc, &compilation, song); + ParseAPETag(file_mpc->APETag()->itemListMap(), &disc, &compilation, song); } - if (tag) Decode(tag->comment(), nullptr, song->mutable_comment()); + if (tag) Decode(tag->comment(), song->mutable_comment()); } else if (tag) { - Decode(tag->comment(), nullptr, song->mutable_comment()); + Decode(tag->comment(), song->mutable_comment()); } if (!disc.isEmpty()) { @@ -423,42 +422,27 @@ void TagReader::ReadFile(const QString &filename, pb::tagreader::SongMetadata *s } -void TagReader::Decode(const TagLib::String &tag, const QTextCodec *codec, std::string *output) { - - QString tmp; - - if (codec && tag.isLatin1()) { // Never override UTF-8. - const std::string fixed = QString::fromUtf8(tag.toCString(true)).toStdString(); - tmp = codec->toUnicode(fixed.c_str()).trimmed(); - } - else { - tmp = TStringToQString(tag).trimmed(); - } +void TagReader::Decode(const TagLib::String &tag, std::string *output) { + QString tmp = TStringToQString(tag).trimmed(); output->assign(DataCommaSizeFromQString(tmp)); } -void TagReader::Decode(const QString &tag, const QTextCodec *codec, std::string *output) { +void TagReader::Decode(const QString &tag, std::string *output) { - if (!codec) { - output->assign(DataCommaSizeFromQString(tag)); - } - else { - const QString decoded(codec->toUnicode(tag.toUtf8())); - output->assign(DataCommaSizeFromQString(decoded)); - } + output->assign(DataCommaSizeFromQString(tag)); } -void TagReader::ParseOggTag(const TagLib::Ogg::FieldListMap &map, const QTextCodec *codec, QString *disc, QString *compilation, pb::tagreader::SongMetadata *song) const { +void TagReader::ParseOggTag(const TagLib::Ogg::FieldListMap &map, QString *disc, QString *compilation, pb::tagreader::SongMetadata *song) const { - if (!map["COMPOSER"].isEmpty()) Decode(map["COMPOSER"].front(), codec, song->mutable_composer()); - if (!map["PERFORMER"].isEmpty()) Decode(map["PERFORMER"].front(), codec, song->mutable_performer()); - if (!map["CONTENT GROUP"].isEmpty()) Decode(map["CONTENT GROUP"].front(), codec, song->mutable_grouping()); + if (!map["COMPOSER"].isEmpty()) Decode(map["COMPOSER"].front(), song->mutable_composer()); + if (!map["PERFORMER"].isEmpty()) Decode(map["PERFORMER"].front(), song->mutable_performer()); + if (!map["CONTENT GROUP"].isEmpty()) Decode(map["CONTENT GROUP"].front(), song->mutable_grouping()); - if (!map["ALBUMARTIST"].isEmpty()) Decode(map["ALBUMARTIST"].front(), codec, song->mutable_albumartist()); - else if (!map["ALBUM ARTIST"].isEmpty()) Decode(map["ALBUM ARTIST"].front(), codec, song->mutable_albumartist()); + if (!map["ALBUMARTIST"].isEmpty()) Decode(map["ALBUMARTIST"].front(), song->mutable_albumartist()); + else if (!map["ALBUM ARTIST"].isEmpty()) Decode(map["ALBUM ARTIST"].front(), song->mutable_albumartist()); if (!map["ORIGINALDATE"].isEmpty()) song->set_originalyear(TStringToQString(map["ORIGINALDATE"].front()).left(4).toInt()); else if (!map["ORIGINALYEAR"].isEmpty()) song->set_originalyear(TStringToQString(map["ORIGINALYEAR"].front()).toInt()); @@ -470,20 +454,18 @@ void TagReader::ParseOggTag(const TagLib::Ogg::FieldListMap &map, const QTextCod if (!map["FMPS_PLAYCOUNT"].isEmpty() && song->playcount() <= 0) song->set_playcount(TStringToQString( map["FMPS_PLAYCOUNT"].front() ).trimmed().toFloat()); - if (!map["LYRICS"].isEmpty()) Decode(map["LYRICS"].front(), codec, song->mutable_lyrics()); - else if (!map["UNSYNCEDLYRICS"].isEmpty()) Decode(map["UNSYNCEDLYRICS"].front(), codec, song->mutable_lyrics()); + if (!map["LYRICS"].isEmpty()) Decode(map["LYRICS"].front(), song->mutable_lyrics()); + else if (!map["UNSYNCEDLYRICS"].isEmpty()) Decode(map["UNSYNCEDLYRICS"].front(), song->mutable_lyrics()); } -void TagReader::ParseAPETag(const TagLib::APE::ItemListMap &map, const QTextCodec *codec, QString *disc, QString *compilation, pb::tagreader::SongMetadata *song) const { - - Q_UNUSED(codec); +void TagReader::ParseAPETag(const TagLib::APE::ItemListMap &map, QString *disc, QString *compilation, pb::tagreader::SongMetadata *song) const { TagLib::APE::ItemListMap::ConstIterator it = map.find("ALBUM ARTIST"); if (it != map.end()) { TagLib::StringList album_artists = it->second.values(); if (!album_artists.isEmpty()) { - Decode(album_artists.front(), nullptr, song->mutable_albumartist()); + Decode(album_artists.front(), song->mutable_albumartist()); } } @@ -497,19 +479,19 @@ void TagReader::ParseAPETag(const TagLib::APE::ItemListMap &map, const QTextCode } if (map.contains("PERFORMER")) { - Decode(map["PERFORMER"].values().toString(", "), nullptr, song->mutable_performer()); + Decode(map["PERFORMER"].values().toString(", "), song->mutable_performer()); } if (map.contains("COMPOSER")) { - Decode(map["COMPOSER"].values().toString(", "), nullptr, song->mutable_composer()); + Decode(map["COMPOSER"].values().toString(", "), song->mutable_composer()); } if (map.contains("GROUPING")) { - Decode(map["GROUPING"].values().toString(" "), nullptr, song->mutable_grouping()); + Decode(map["GROUPING"].values().toString(" "), song->mutable_grouping()); } if (map.contains("LYRICS")) { - Decode(map["LYRICS"].toString(), nullptr, song->mutable_lyrics()); + Decode(map["LYRICS"].toString(), song->mutable_lyrics()); } if (map.contains("FMPS_PLAYCOUNT")) { diff --git a/ext/libstrawberry-tagreader/tagreader.h b/ext/libstrawberry-tagreader/tagreader.h index b2da388c..aea8d2d3 100644 --- a/ext/libstrawberry-tagreader/tagreader.h +++ b/ext/libstrawberry-tagreader/tagreader.h @@ -35,8 +35,6 @@ #include "tagreadermessages.pb.h" -class QTextCodec; - #ifndef USE_SYSTEM_TAGLIB using namespace Strawberry_TagLib; #endif @@ -61,11 +59,11 @@ class TagReader { QByteArray LoadEmbeddedArt(const QString &filename) const; QByteArray LoadEmbeddedAPEArt(const TagLib::APE::ItemListMap &map) const; - static void Decode(const TagLib::String& tag, const QTextCodec *codec, std::string *output); - static void Decode(const QString &tag, const QTextCodec *codec, std::string *output); + static void Decode(const TagLib::String &tag, std::string *output); + static void Decode(const QString &tag, std::string *output); - void ParseOggTag(const TagLib::Ogg::FieldListMap &map, const QTextCodec *codec, QString *disc, QString *compilation, pb::tagreader::SongMetadata *song) const; - void ParseAPETag(const TagLib::APE::ItemListMap &map, const QTextCodec *codec, QString *disc, QString *compilation, pb::tagreader::SongMetadata *song) const; + void ParseOggTag(const TagLib::Ogg::FieldListMap &map, QString *disc, QString *compilation, pb::tagreader::SongMetadata *song) const; + void ParseAPETag(const TagLib::APE::ItemListMap &map, QString *disc, QString *compilation, pb::tagreader::SongMetadata *song) const; void SetVorbisComments(TagLib::Ogg::XiphComment *vorbis_comments, const pb::tagreader::SongMetadata &song) const; void SaveAPETag(TagLib::APE::Tag *tag, const pb::tagreader::SongMetadata &song) const; diff --git a/src/core/song.cpp b/src/core/song.cpp index cf513e19..2d9a1718 100644 --- a/src/core/song.cpp +++ b/src/core/song.cpp @@ -50,7 +50,6 @@ #include #include #include -#include #include #include #include @@ -745,14 +744,6 @@ void Song::set_genre_id3(int id) { set_genre(TStringToQString(TagLib::ID3v1::genre(id))); } -QString Song::Decode(const QString &tag, const QTextCodec *codec) { - if (!codec) { - return tag; - } - - return codec->toUnicode(tag.toUtf8()); -} - void Song::InitFromProtobuf(const pb::tagreader::SongMetadata &pb) { if (d->source_ == Source_Unknown) d->source_ = Source_LocalFile; diff --git a/src/core/song.h b/src/core/song.h index a34adbf7..d474ffcf 100644 --- a/src/core/song.h +++ b/src/core/song.h @@ -38,7 +38,6 @@ #include #include -class QTextCodec; class QSqlQuery; namespace Engine { @@ -177,8 +176,6 @@ class Song { // Useful when you want updated tags from disk but you want to keep user stats. void MergeUserSetData(const Song &other); - static QString Decode(const QString &tag, const QTextCodec *codec = nullptr); - // Save void BindToQuery(QSqlQuery *query) const; void BindToFtsQuery(QSqlQuery *query) const; diff --git a/src/covermanager/musixmatchcoverprovider.cpp b/src/covermanager/musixmatchcoverprovider.cpp index 08da96af..6d05edd9 100644 --- a/src/covermanager/musixmatchcoverprovider.cpp +++ b/src/covermanager/musixmatchcoverprovider.cpp @@ -26,7 +26,6 @@ #include #include #include -#include #include #include #include @@ -122,14 +121,7 @@ void MusixmatchCoverProvider::HandleSearchReply(QNetworkReply *reply, const int emit SearchFinished(id, results); return; } - - QTextCodec *codec = QTextCodec::codecForName("utf-8"); - if (!codec) { - emit SearchFinished(id, results); - return; - } - QString content = codec->toUnicode(data); - + QString content = data; QString data_begin = "var __mxmState = "; QString data_end = ";"; int begin_idx = content.indexOf(data_begin); diff --git a/src/lyrics/geniuslyricsprovider.cpp b/src/lyrics/geniuslyricsprovider.cpp index a4a4cb78..2a8f98ff 100644 --- a/src/lyrics/geniuslyricsprovider.cpp +++ b/src/lyrics/geniuslyricsprovider.cpp @@ -33,7 +33,6 @@ #include #include #include -#include #include #include #include @@ -496,13 +495,7 @@ void GeniusLyricsProvider::HandleLyricReply(QNetworkReply *reply, const int sear EndSearch(search, lyric); return; } - - QTextCodec *codec = QTextCodec::codecForName("utf-8"); - if (!codec) { - EndSearch(search, lyric); - return; - } - QString content = codec->toUnicode(data); + QString content = data; // Extract the lyrics from HTML. diff --git a/src/lyrics/musixmatchlyricsprovider.cpp b/src/lyrics/musixmatchlyricsprovider.cpp index 5ade90f6..c75ec3fd 100644 --- a/src/lyrics/musixmatchlyricsprovider.cpp +++ b/src/lyrics/musixmatchlyricsprovider.cpp @@ -26,7 +26,6 @@ #include #include #include -#include #include #include #include @@ -122,13 +121,7 @@ void MusixmatchLyricsProvider::HandleSearchReply(QNetworkReply *reply, const qui return; } - QTextCodec *codec = QTextCodec::codecForName("utf-8"); - if (!codec) { - emit SearchFinished(id, results); - return; - } - QString content = codec->toUnicode(data); - + QString content = data; QString data_begin = "var __mxmState = "; QString data_end = ";"; int begin_idx = content.indexOf(data_begin); diff --git a/src/playlistparsers/cueparser.cpp b/src/playlistparsers/cueparser.cpp index 30459999..1e7dae42 100644 --- a/src/playlistparsers/cueparser.cpp +++ b/src/playlistparsers/cueparser.cpp @@ -29,7 +29,6 @@ #include #include #include -#include #include #include @@ -63,9 +62,6 @@ SongList CueParser::Load(QIODevice *device, const QString &playlist_path, const SongList ret; QTextStream text_stream(device); -#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0) - text_stream.setCodec(QTextCodec::codecForUtfText(device->peek(1024), QTextCodec::codecForName("UTF-8"))); -#endif QString dir_path = dir.absolutePath(); // read the first line already