1
0
mirror of https://github.com/clementine-player/Clementine synced 2024-12-16 19:31:02 +01:00

Start of (still disabled) ID3v1 encoding detector.

Comes with const_cast hackiness \o/
Updates issue #254
This commit is contained in:
John Maguire 2010-05-10 13:46:06 +00:00
parent 41f306fd2a
commit 37960cb5df
3 changed files with 61 additions and 0 deletions

View File

@ -390,6 +390,7 @@ target_link_libraries(clementine_lib
qtsingleapplication
qtiocompressor
lastfm
chardet
${GOBJECT_LIBRARIES}
${GLIB_LIBRARIES}
${TAGLIB_LIBRARIES}

View File

@ -44,6 +44,7 @@
#include <QFileInfo>
#include <QTime>
#include <QSqlQuery>
#include <QTextCodec>
#include <QVariant>
#include <boost/scoped_ptr.hpp>
@ -87,6 +88,46 @@ static TagLib::String QStringToTaglibString(const QString& s);
TagLibFileRefFactory Song::kDefaultFactory;
UniversalEncodingHandler::UniversalEncodingHandler()
: nsUniversalDetector(NS_FILTER_ALL),
current_codec_(NULL) {
}
TagLib::String UniversalEncodingHandler::parse(const TagLib::ByteVector& data) const {
const_cast<UniversalEncodingHandler*>(this)->Reset();
const_cast<UniversalEncodingHandler*>(this)->HandleData(data.data(), data.size());
const_cast<UniversalEncodingHandler*>(this)->DataEnd();
if (!current_codec_) {
return TagLib::String(data); // Latin-1
} else {
// Detected codec -> QString (UTF-16) -> UTF8 -> UTF16-BE (TagLib::String)
// That's probably expensive.
QString unicode = current_codec_->toUnicode(data.data(), data.size());
qDebug() << "Decoded to:" << unicode;
return TagLib::String(unicode.toUtf8().constData(), TagLib::String::UTF8);
}
}
TagLib::ByteVector UniversalEncodingHandler::render(const TagLib::String& s) const {
// TODO: what should we do here?
// 1. Coerce to ASCII
// 2. Just write UTF8
// 3. Write what we read
// 4. Nothing and rewrite the tag as ID3v2 & UTF8
return TagLib::ByteVector();
}
void UniversalEncodingHandler::Report(const char* charset) {
qDebug() << "Detected as" << charset;
QTextCodec* codec = QTextCodec::codecForName(charset);
if (!codec) {
qWarning() << "Could not identify encoding in ID3v1 tag. Assuming ASCII.";
}
current_codec_ = codec;
}
Song::Private::Private()
: valid_(false),
id_(-1),

View File

@ -27,6 +27,9 @@
#include "engines/engine_fwd.h"
#include <taglib/id3v1tag.h>
#include "nsUniversalDetector.h"
namespace lastfm {
class Track;
}
@ -50,6 +53,22 @@ class TagLibFileRefFactory : public FileRefFactory {
virtual TagLib::FileRef* GetFileRef(const QString& filename);
};
class UniversalEncodingHandler : public TagLib::ID3v1::StringHandler,
nsUniversalDetector {
public:
UniversalEncodingHandler();
// TagLib::ID3v1::StringHandler
virtual TagLib::String parse(const TagLib::ByteVector& data) const;
virtual TagLib::ByteVector render(const TagLib::String& s) const;
private:
// nsUniversalDetector
virtual void Report(const char* charset);
QTextCodec* current_codec_;
};
class Song {
public:
Song();