// g++ `taglib-config --libs --cflags` -I${QTDIR}/include -L${QTDIR}/lib -Wl,-R${QTDIR}/lib -lqt-mt utf8.cpp -o utf8

/*
 * Ok, here's a small Qt-based program that shows how to override the default
 * string encoding.  This one just uses utf8, but I think it's clear how one
 * would proceed based on this.
 *
 * I would also recommend by default (i.e. when ISO-8859-1 is selected) not
 * using an external string handler at all since they extra lookups will
 * likely slow things down.
 */

#include <tstring.h>
#include <mpegfile.h>
#include <id3v1tag.h>

#include <qstring.h>
#include <qtextcodec.h>

using namespace TagLib;

class LocaleStringHandler : public ID3v1::StringHandler
{
public:
  LocaleStringHandler() : m_codec(QTextCodec::codecForName("utf8")) {}

  virtual String parse(const ByteVector &data) const
  {
    return QStringToTString(m_codec->toUnicode(data.data(), data.size()));
  }

private:
  QTextCodec *m_codec;
};

int main(int argc, char *argv[])
{
  ID3v1::Tag::setStringHandler(new LocaleStringHandler);

  for(int i = 1; i < argc; i++) {

    TagLib::MPEG::File f(argv[i], false);

    if(f.isValid() && f.ID3v1Tag(false)) {

      Tag *tag = f.ID3v1Tag(false);

      cout << "title   - \"" << tag->title()   << "\"" << endl;
      cout << "artist  - \"" << tag->artist()  << "\"" << endl;
      cout << "album   - \"" << tag->album()   << "\"" << endl;
      cout << "year    - \"" << tag->year()    << "\"" << endl;
      cout << "comment - \"" << tag->comment() << "\"" << endl;
      cout << "track   - \"" << tag->track()   << "\"" << endl;
      cout << "genre   - \"" << tag->genre()   << "\"" << endl;
    }
  }
}

