31 #include <QtCore/QDir> 32 #include <QtCore/QRegExp> 33 #include <QtCore/QCharRef> 34 #include <QtCore/QMutableStringListIterator> 35 #include <QtCore/QTextCodec> 157 I18N_NOOP2(
"@item Text character set",
"Western European")
"\0" 163 I18N_NOOP2(
"@item Text character set",
"Central European")
"\0" 166 I18N_NOOP2(
"@item Text character set",
"Baltic")
"\0" 169 I18N_NOOP2(
"@item Text character set",
"South-Eastern Europe")
"\0" 172 I18N_NOOP2(
"@item Text character set",
"Turkish")
"\0" 175 I18N_NOOP2(
"@item Text character set",
"Cyrillic")
"\0" 181 I18N_NOOP2(
"@item Text character set",
"Chinese Traditional")
"\0" 184 I18N_NOOP2(
"@item Text character set",
"Chinese Simplified")
"\0" 188 I18N_NOOP2(
"@item Text character set",
"Korean")
"\0" 191 I18N_NOOP2(
"@item Text character set",
"Japanese")
"\0" 195 I18N_NOOP2(
"@item Text character set",
"Greek")
"\0" 198 I18N_NOOP2(
"@item Text character set",
"Arabic")
"\0" 201 I18N_NOOP2(
"@item Text character set",
"Hebrew")
"\0" 206 I18N_NOOP2(
"@item Text character set",
"Thai")
"\0" 209 I18N_NOOP2(
"@item Text character set",
"Unicode")
"\0" 215 I18N_NOOP2(
"@item Text character set",
"Northern Saami")
"\0" 217 I18N_NOOP2(
"@item Text character set",
"Other")
"\0" 223 0, 11, 28, 11, 40, 11, 52, 11,
224 60, 11, 67, 78, 95, 78, 106, 117,
225 124, 117, 136, 148, 169, 78, 177, 185,
226 193, 117, 201, 208, 217, 208, 228, 208,
227 236, 208, 243, 208, 250, 255, 275, 255,
228 286, 294, 313, 294, 317, 294, 324, 331,
229 338, 331, 345, 350, 359, 350, 364, 350,
230 371, 382, 388, 382, 396, 407, 414, 407,
231 422, 433, 440, 433, 453, 433, 461, 185,
232 472, 479, 484, 479, 496, 502, 510, 502,
233 517, 502, 522, 502, 527, 502, 543, 552,
234 567, 580, 586, 580, 593, 580, -1
347 "unicode-1-1-utf-7\0" 402 0, 11, 18, 11, 30, 39, 50, 39,
403 58, 39, 64, 82, 88, 93, 109, 93,
404 120, 135, 143, 135, 158, 164, 168, 164,
405 175, 164, 189, 196, 201, 208, 214, 220,
406 227, 234, 240, 234, 256, 234, 272, 234,
407 288, 234, 304, 234, 320, 234, 336, 234,
408 352, 362, 367, 362, 362, 362, 377, 389,
409 394, 405, 412, 423, 430, 405, 442, 423,
410 454, 463, 475, 487, 499, 511, 518, 511,
411 529, 511, 536, 208, 551, 208, 566, 463,
412 576, 463, 586, 463, 590, 377, -1
452 0, 7, 18, 25, 36, 18, 43, 50,
461 for (
int i = 0; indices[i] != -1; i += 2)
462 if (qstrcmp(start + indices[i], entry) == 0)
463 return start + indices[i + 1];
468 class KCharsetsPrivate
474 codecForNameDict.reserve( 43 );
487 :d(new KCharsetsPrivate(this))
498 QChar res = QChar::Null;
504 if(str[pos] == QLatin1Char(
'&')) pos++;
507 if (str[pos] == QLatin1Char(
'#') && str.length()-pos > 1) {
510 if (str[pos] == QLatin1Char(
'x') || str[pos] == QLatin1Char(
'X')) {
513 const QString tmp( str.mid( pos ) );
514 res = tmp.toInt(&ok, 16);
517 const QString tmp( str.mid( pos ) );
518 res = tmp.toInt(&ok, 10);
526 const QByteArray raw ( str.toLatin1() );
527 const entity *e = Perfect_Hash::kde_findEntity( raw, raw.length() );
536 return QChar(e->code);
548 if( res != QChar::Null )
return res;
558 ent.sprintf(
"�x%x;", ch.unicode());
565 const QChar *p = text.unicode();
566 const QChar *end = p + text.length();
567 const QChar *ampersand = 0;
568 bool scanForSemicolon =
false;
570 for ( ; p < end; ++p ) {
573 if ( ch == QLatin1Char(
'&') ) {
575 scanForSemicolon =
true;
579 if ( ch != QLatin1Char(
';') || scanForSemicolon ==
false )
584 scanForSemicolon =
false;
586 const QChar *entityBegin = ampersand + 1;
588 const uint entityLength = p - entityBegin;
589 if ( entityLength == 0 )
593 if ( entityValue.isNull() )
596 const uint ampersandPos = ampersand - text.unicode();
598 text[ (int)ampersandPos ] = entityValue;
599 text.remove( ampersandPos + 1, entityLength + 1 );
600 p = text.unicode() + ampersandPos;
601 end = text.unicode() + text.length();
617 #ifndef KDE_NO_DEPRECATED 622 encoding.toUtf8().constData() );
624 return i18nc(
"@item Text character set", lang );
626 return i18nc(
"@item Text character set",
"Other" );
636 return i18nc(
"@item %1 character set, %2 encoding",
"%1 ( %2 )",
637 i18nc(
"@item Text character set", lang ), encoding );
639 return i18nc(
"@item",
"Other encoding (%1)", encoding );
644 const int left = descriptiveName.lastIndexOf( QLatin1Char(
'(') );
647 return descriptiveName.trimmed();
649 QString name(descriptiveName.mid(left+1));
651 const int right = name.lastIndexOf( QLatin1Char(
')') );
656 return name.left(right).trimmed();
665 encodings.append(
i18nc(
"@item Text encoding: %1 character set, %2 encoding",
"%1 ( %2 )",
666 description, name ) );
674 if (!d->encodingsByScript.isEmpty())
675 return d->encodingsByScript;
681 for (i=0; i<d->encodingsByScript.size(); ++i) {
682 if (d->encodingsByScript.at(i).at(0) == description) {
683 d->encodingsByScript[i].append(name);
688 if (i==d->encodingsByScript.size()) {
689 d->encodingsByScript.append(
QStringList() << description << name);
693 return d->encodingsByScript;
698 if ( n == QLatin1String(
"gb2312") || n == QLatin1String(
"gbk") )
699 return QTextCodec::codecForName(
"gb18030" );
700 const QByteArray name( n.toLatin1() );
701 QTextCodec* codec = codecForNameOrNull( name );
705 return QTextCodec::codecForName(
"iso-8859-1" );
710 if (n == QLatin1String(
"gb2312") || n == QLatin1String(
"gbk")) {
712 return QTextCodec::codecForName(
"gb18030" );
714 const QByteArray name( n.toLatin1() );
715 QTextCodec* codec = codecForNameOrNull( name );
724 return QTextCodec::codecForName(
"iso-8859-1" );
728 QTextCodec *KCharsets::codecForNameOrNull(
const QByteArray& n )
const 730 QTextCodec* codec = 0;
734 const QByteArray
locale =
"->locale<-";
735 if ( d->codecForNameDict.contains( locale ) )
736 return d->codecForNameDict.value( locale );
738 d->codecForNameDict.insert(
"->locale<-", codec);
742 else if ( d->codecForNameDict.contains( n ) ) {
743 return d->codecForNameDict.value( n );
748 codec = QTextCodec::codecForName( n );
750 d->codecForNameDict.insert( n, codec );
756 QByteArray name = n.toLower();
757 bool changed =
false;
758 if (name.endsWith(
"_charset")) {
762 if ( name.startsWith(
"x-" ) ) {
767 if (name.isEmpty()) {
774 codec = QTextCodec::codecForName(name);
776 d->codecForNameDict.insert( n, codec );
787 codec = QTextCodec::codecForName(cname);
791 d->codecForNameDict.insert( n, codec );
799 if (!cname.isEmpty()) {
800 codec = QTextCodec::codecForName(cname);
802 d->codecForNameDict.insert( n, codec );
static const char builtin_string[]
Charset font and encoder/decoder handling.
#define I18N_NOOP2(comment, x)
If the string is too ambiguous to be translated well to a non-english language, use this instead of I...
KCharsets()
Protected constructor.
virtual ~KCharsets()
Destructor.
static const int language_for_encoding_indices[]
QString i18nc(const char *ctxt, const char *text)
Returns a localized version of a string and a context.
static const int builtin_indices[]
QStringList descriptiveEncodingNames() const
Lists the available encoding names together with a more descriptive language.
QString languageForEncoding(const QString &encoding) const
Returns the language the encoding is used for.
static QString toEntity(const QChar &ch)
Converts a QChar to an entity.
static const int conversion_hints_indices[]
QString encodingForName(const QString &descriptiveName) const
Returns the encoding for a string obtained with descriptiveEncodingNames().
QList< QStringList > encodingsByScript() const
Lists the available encoding names grouped by script (or language that uses them).
QTextCodec * codecForName(const QString &name) const
Provided for compatibility.
KLocale * locale()
Returns the global locale object.
static QString resolveEntities(const QString &text)
Scans the given string for entities (like &) and resolves them using fromEntity.
static QChar fromEntity(const QString &str)
Converts an entity to a character.
static const char * kcharsets_array_search(const char *start, const int *indices, const char *entry)
QTextCodec * codecForEncoding() const
Returns the user's preferred encoding.
static const char language_for_encoding_string[]
QStringList availableEncodingNames() const
Lists all available encodings as names.
static const char conversion_hints_string[]
QString descriptionForEncoding(const QString &encoding) const
Returns a long description for an encoding name.