goldendict-ng/src/langcoder.cc

217 lines
4.5 KiB
C++
Raw Normal View History

2009-04-22 21:37:32 +00:00
#include "langcoder.hh"
2009-04-23 11:41:13 +00:00
#include "folding.hh"
#include "wstring_qt.hh"
2009-04-22 21:37:32 +00:00
#include <cctype>
LangCoder langCoder;
LangCoder::LangCoder()
{
LangStruct ls;
for (int i = 0; true; i++) {
const LangCode &lc = LangCodes[i];
if (lc.lang[0] == 0)
2009-04-22 21:37:32 +00:00
break;
codeMap[code2toInt(lc.code)] = i;
}
}
QString LangCoder::decode(quint32 code)
{
if (langCoder.codeMap.contains(code))
return LangCodes[langCoder.codeMap[code]].lang;
2009-04-22 21:37:32 +00:00
return QString();
}
QIcon LangCoder::icon(quint32 code)
{
if (langCoder.codeMap.contains(code))
{
const LangCode &lc = LangCodes[ langCoder.codeMap[ code ] ];
// QString flag_id( lc.code );
// if (flag_id == "en")
// flag_id = "gb";
// else
// if (flag_id == "uk")
// flag_id = "ua";
return QIcon( ":/flags/" + QString( lc.code) + ".png" );
}
return QIcon();
}
2009-04-22 21:37:32 +00:00
LangStruct LangCoder::langStruct(quint32 code)
{
LangStruct ls;
ls.code = code;
ls.order = -1;
if (codeMap.contains(code)) {
int order = codeMap[code];
const LangCode &lc = LangCodes[order];
ls.order = order;
ls.lang = lc.lang;
ls.icon = QIcon(":/flags/" + QString(lc.code) + ".png");
}
return ls;
}
quint32 LangCoder::code3toInt(const std::string& code3)
{
if (code3.length() < 2)
return 0;
// this is temporary
2009-04-23 10:29:49 +00:00
char code1 = tolower( code3.at(1) );
char code0 = tolower( code3.at(0) );
2009-04-22 21:37:32 +00:00
2009-04-23 10:29:49 +00:00
return ( ((quint32)code1) << 8 ) + (quint32)code0;
2009-04-22 21:37:32 +00:00
}
2009-04-23 11:41:13 +00:00
quint32 LangCoder::findIdForLanguage( gd::wstring const & lang )
{
gd::wstring langFolded = Folding::apply( lang );
for( LangCode const * lc = LangCodes; lc->code[ 0 ]; ++lc )
{
if ( langFolded == Folding::apply( gd::toWString( lc->lang ) ) )
2009-04-23 11:41:13 +00:00
{
// We've got a match
return code2toInt( lc->code );
}
}
return 0;
}
quint32 LangCoder::guessId( const QString & lang )
{
QString lstr = lang.simplified().toLower();
// too small to guess
if (lstr.size() < 2)
return 0;
// check if it could be the whole language name
if (lstr.size() >= 3)
{
for( LangCode const * lc = LangCodes; lc->code[ 0 ]; ++lc )
{
if ( lstr == QString( lc->lang ) )
{
// We've got a match
return code2toInt( lc->code );
}
}
}
// still not found - try to match by 2-symbol code
return code2toInt( lstr.left(2).toAscii().data() );
}
QPair<quint32,quint32> LangCoder::findIdsForFilename( QString const & name )
{
QString nameFolded = QFileInfo( name ).fileName().toCaseFolded();
QRegExp reg( "[-_.]([a-z]{2,3})-([a-z]{2,3})[-_.]" ); reg.setMinimal(true);
int off = 0;
while ( reg.indexIn( nameFolded, off ) >= 0 )
{
quint32 from = guessId( reg.cap(1) );
quint32 to = guessId( reg.cap(2) );
if (from && to)
return QPair<quint32,quint32>(from, to);
off += reg.matchedLength();
}
return QPair<quint32,quint32>(0, 0);
}
2009-04-22 21:37:32 +00:00
/*
LangStruct& LangCoder::CodeToLangStruct(const QString &code)
{
if (codeMap.contains(code)) {
LangStruct &ls = codeMap[code];
if (ls.icon.isNull() && *ls.icon_code) {
ls.icon = QIcon(":/Resources/flags/" + QString(ls.icon_code) + ".png");
}
return ls;
}
return dummyLS;
}
QString LangCoder::CodeToHtml(const QString &code)
{
if (codeMap.contains(code)) {
LangStruct &ls = codeMap[code];
if (*ls.icon_code) {
return "<img src=':/Resources/flags/" + QString(ls.icon_code) + ".png'>&nbsp;" + ls.lang;
}
return ls.lang;
}
return "";
}
bool LangCoder::CheckCode(QString &code)
{
code = code.toUpper();
if (codeMap.contains(code))
return true;
if (code == "DEU") {
code = "GER";
return true;
}
return false;
}
*/
/*
LangModel::LangModel(QObject * parent) : QAbstractItemModel(parent)
{
}
int LangModel::columnCount ( const QModelIndex & parent ) const
{
return 2;
}
int LangModel::rowCount ( const QModelIndex & parent ) const
{
return arraySize(LangCodes);
}
QVariant LangModel::data ( const QModelIndex & index, int role ) const
{
switch (role) {
case Qt::DisplayRole:
return LangCodes[index.row()].lang;
case LangCodeRole:
return LangCodes[index.row()].code;
default:;
}
return QVariant();
}
QModelIndex LangModel::index ( int row, int column, const QModelIndex & parent ) const
{
return createIndex(row, column);
}
QModelIndex LangModel::parent ( const QModelIndex & index ) const
{
return QModelIndex();
}
*/