mirror of
https://github.com/clementine-player/Clementine
synced 2024-12-18 12:28:31 +01:00
Code review comments for r1023
This commit is contained in:
parent
407bc7a41e
commit
2ad15ffec6
@ -14,6 +14,8 @@
|
||||
along with Clementine. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
#include "song.h"
|
||||
#include "utilities.h"
|
||||
|
||||
@ -161,24 +163,39 @@ QTextCodec* UniversalEncodingHandler::Guess(const char* data) {
|
||||
return current_codec_;
|
||||
}
|
||||
|
||||
QTextCodec* UniversalEncodingHandler::Guess(const TagLib::Tag& tag) {
|
||||
QTextCodec* UniversalEncodingHandler::Guess(const TagLib::FileRef& fileref) {
|
||||
const TagLib::Tag& tag = *fileref.tag();
|
||||
QHash<QTextCodec*, int> usages;
|
||||
Guess(tag.title(), &usages);
|
||||
Guess(tag.artist(), &usages);
|
||||
Guess(tag.album(), &usages);
|
||||
Guess(tag.comment(), &usages);
|
||||
Guess(tag.genre(), &usages);
|
||||
if (TagLib::MPEG::File* file = dynamic_cast<TagLib::MPEG::File*>(fileref.file())) {
|
||||
if (file->ID3v2Tag()) {
|
||||
if (!file->ID3v2Tag()->frameListMap()["TCOM"].isEmpty())
|
||||
Guess(file->ID3v2Tag()->frameListMap()["TCOM"].front()->toString(), &usages);
|
||||
|
||||
if (!file->ID3v2Tag()->frameListMap()["TPE2"].isEmpty()) // non-standard: Apple, Microsoft
|
||||
Guess(file->ID3v2Tag()->frameListMap()["TPE2"].front()->toString(), &usages);
|
||||
}
|
||||
} else if (TagLib::Ogg::Vorbis::File* file = dynamic_cast<TagLib::Ogg::Vorbis::File*>(fileref.file())) {
|
||||
if (file->tag()) {
|
||||
if (!file->tag()->fieldListMap()["COMPOSER"].isEmpty() )
|
||||
Guess(file->tag()->fieldListMap()["COMPOSER"].front(), &usages);
|
||||
}
|
||||
} else if (TagLib::FLAC::File* file = dynamic_cast<TagLib::FLAC::File*>(fileref.file())) {
|
||||
if (file->xiphComment()) {
|
||||
if (!file->xiphComment()->fieldListMap()["COMPOSER"].isEmpty())
|
||||
Guess(file->xiphComment()->fieldListMap()["COMPOSER"].front(), &usages);
|
||||
}
|
||||
}
|
||||
|
||||
if (usages.isEmpty()) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
QHash<QTextCodec*, int>::const_iterator max = usages.begin();
|
||||
for (QHash<QTextCodec*, int>::const_iterator it = usages.begin(); it != usages.end(); ++it) {
|
||||
if (it.value() > max.value()) {
|
||||
max = it;
|
||||
}
|
||||
}
|
||||
QHash<QTextCodec*, int>::const_iterator max = std::max_element(usages.begin(), usages.end());
|
||||
return max.key();
|
||||
}
|
||||
|
||||
@ -287,7 +304,7 @@ void Song::Init(const QString& title, const QString& artist, const QString& albu
|
||||
d->length_ = length;
|
||||
}
|
||||
|
||||
QString Song::Decode(const TagLib::String tag, const QTextCodec* codec) const {
|
||||
QString Song::Decode(const TagLib::String& tag, const QTextCodec* codec) const {
|
||||
if (codec) {
|
||||
const std::string fixed = QString::fromUtf8(tag.toCString(true)).toStdString();
|
||||
return codec->toUnicode(fixed.c_str()).trimmed();
|
||||
@ -317,7 +334,7 @@ void Song::InitFromFile(const QString& filename, int directory_id) {
|
||||
TagLib::Tag* tag = fileref->tag();
|
||||
QTextCodec* codec = NULL;
|
||||
if (tag) {
|
||||
codec = detector.Guess(*tag);
|
||||
codec = detector.Guess(*fileref);
|
||||
d->title_ = Decode(tag->title(), codec);
|
||||
d->artist_ = Decode(tag->artist(), codec);
|
||||
d->album_ = Decode(tag->album(), codec);
|
||||
|
@ -64,7 +64,7 @@ class UniversalEncodingHandler : public TagLib::ID3v1::StringHandler,
|
||||
virtual TagLib::String parse(const TagLib::ByteVector& data) const;
|
||||
|
||||
QTextCodec* Guess(const char* data);
|
||||
QTextCodec* Guess(const TagLib::Tag& tag);
|
||||
QTextCodec* Guess(const TagLib::FileRef& file);
|
||||
QTextCodec* Guess(const TagLib::String& input);
|
||||
|
||||
QString FixEncoding(const TagLib::String& input);
|
||||
@ -116,7 +116,7 @@ class Song {
|
||||
void InitFromLastFM(const lastfm::Track& track);
|
||||
void MergeFromSimpleMetaBundle(const Engine::SimpleMetaBundle& bundle);
|
||||
|
||||
QString Decode(const TagLib::String tag, const QTextCodec* codec) const;
|
||||
QString Decode(const TagLib::String& tag, const QTextCodec* codec) const;
|
||||
|
||||
// Save
|
||||
void BindToQuery(QSqlQuery* query) const;
|
||||
|
@ -143,13 +143,17 @@ TEST_F(SongTest, TakesMajorityVote) {
|
||||
const char w1251[] = { 0xca, 0xe8, 0xed, 0xee, '\0' }; // Кино
|
||||
// Actually windows-1251 but gets detected as windows-1252.
|
||||
const char w1252[] = { 0xcf, '.', 0xc7, '.', '\0' }; // П.Э.
|
||||
TagLib::ID3v2::Tag tag;
|
||||
tag.setTitle(w1251);
|
||||
tag.setArtist(w1251);
|
||||
tag.setAlbum(w1252);
|
||||
TagLib::ID3v2::Tag* tag = new TagLib::ID3v2::Tag;
|
||||
tag->setTitle(w1251);
|
||||
tag->setArtist(w1251);
|
||||
tag->setAlbum(w1252);
|
||||
|
||||
UniversalEncodingHandler handler(NS_FILTER_NON_CJK);
|
||||
EXPECT_EQ(QTextCodec::codecForName("windows-1251"), handler.Guess(tag));
|
||||
QTemporaryFile temp;
|
||||
temp.open();
|
||||
MockFile* file = new MockFile(tag, temp.fileName());
|
||||
TagLib::FileRef ref(file);
|
||||
EXPECT_EQ(QTextCodec::codecForName("windows-1251"), handler.Guess(ref));
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
Loading…
Reference in New Issue
Block a user