示例#1
0
bool KEncodingDetector::setEncoding(const char *_encoding, EncodingChoiceSource type)
{
    QTextCodec *codec;
    QByteArray enc(_encoding);
    if(/*enc.isNull() || */enc.isEmpty())
    {
        if (type==DefaultEncoding)
            codec=d->m_defaultCodec;
        else
            return false;
    }
    else
    {
        //QString->QTextCodec

        enc = enc.toLower();
         // hebrew visually ordered
        if(enc=="visual")
            enc="iso8859-8";
        bool b;
        codec = KGlobal::charsets()->codecForName(QLatin1String(enc), b);
        if (!b)
            return false;
    }

    if (d->m_codec->mibEnum()==codec->mibEnum())
    {
        // We already have the codec, but we still want to re-set the type,
        // as we may have overwritten a default with a detected
        d->m_source = type;
        return true;
    }

    if ((type==EncodingFromMetaTag || type==EncodingFromXMLHeader) && is16Bit(codec))
    {
        //Sometimes the codec specified is absurd, i.e. UTF-16 despite
        //us decoding a meta tag as ASCII. In that case, ignore it.
        return false;
    }

    if (codec->mibEnum() == Mib8859_8)
    {
        //We do NOT want to use Qt's QHebrewCodec, since it tries to reorder itself.
        codec = QTextCodec::codecForName("iso8859-8-i");

        // visually ordered unless one of the following
        if(!(enc=="iso-8859-8-i"||enc=="iso_8859-8-i"||enc=="csiso88598i"||enc=="logical"))
            d->m_visualRTL = true;
    }

    d->m_codec = codec;
    d->m_source = type;
    delete d->m_decoder;
    d->m_decoder = d->m_codec->makeDecoder();
#ifdef DECODE_DEBUG
    kDebug(6005) << "KEncodingDetector::encoding used is" << d->m_codec->name();
#endif
    return true;
}
示例#2
0
int QLibInputKeyboard::keysymToQtKey(xkb_keysym_t keysym, Qt::KeyboardModifiers *modifiers, const QString &text) const
{
    int code = 0;
#ifndef QT_NO_TEXTCODEC
    QTextCodec *systemCodec = QTextCodec::codecForLocale();
#endif
    if (keysym < 128 || (keysym < 256
#ifndef QT_NO_TEXTCODEC
                         && systemCodec->mibEnum() == 4
#endif
                         )) {
        // upper-case key, if known
        code = isprint((int)keysym) ? toupper((int)keysym) : 0;
    } else if (keysym >= XKB_KEY_F1 && keysym <= XKB_KEY_F35) {
        // function keys
        code = Qt::Key_F1 + ((int)keysym - XKB_KEY_F1);
    } else if (keysym >= XKB_KEY_KP_Space && keysym <= XKB_KEY_KP_9) {
        if (keysym >= XKB_KEY_KP_0) {
            // numeric keypad keys
            code = Qt::Key_0 + ((int)keysym - XKB_KEY_KP_0);
        } else {
            code = keysymToQtKey(keysym);
        }
        *modifiers |= Qt::KeypadModifier;
    } else if (text.length() == 1 && text.unicode()->unicode() > 0x1f
                                  && text.unicode()->unicode() != 0x7f
                                  && !(keysym >= XKB_KEY_dead_grave && keysym <= XKB_KEY_dead_currency)) {
        code = text.unicode()->toUpper().unicode();
    } else {
        // any other keys
        code = keysymToQtKey(keysym);
    }
    return code;
}
示例#3
0
/*!
    \threadsafe
    Returns the QTextCodec which matches the
    \l{QTextCodec::mibEnum()}{MIBenum} \a mib.
*/
QTextCodec* QTextCodec::codecForMib(int mib)
{
    QMutexLocker locker(textCodecsMutex());

    QCoreGlobalData *globalData = QCoreGlobalData::instance();
    if (!globalData)
        return 0;
    if (globalData->allCodecs.isEmpty())
        setup();

    QByteArray key = "MIB: " + QByteArray::number(mib);

    QTextCodecCache *cache = &globalData->codecCache;
    QTextCodec *codec;
    if (cache) {
        codec = cache->value(key);
        if (codec)
            return codec;
    }

    for (TextCodecListConstIt it = globalData->allCodecs.constBegin(), cend = globalData->allCodecs.constEnd(); it != cend; ++it) {
        QTextCodec *cursor = *it;
        if (cursor->mibEnum() == mib) {
            if (cache)
                cache->insert(key, cursor);
            return cursor;
        }
    }

#ifdef QT_USE_ICU
    return QIcuCodec::codecForMibUnlocked(mib);
#else
    return 0;
#endif
}
示例#4
0
文件: qfont_x11.cpp 项目: Fale/qtmoko
/*!
  Internal function that initializes the font system.

  \internal
  The font cache and font dict do not alloc the keys. The key is a QString
  which is shared between QFontPrivate and QXFontName.
*/
void QFont::initialize()
{
    extern int qt_encoding_id_for_mib(int mib); // from qfontdatabase_x11.cpp
    QTextCodec *codec = QTextCodec::codecForLocale();
    // determine the default encoding id using the locale, otherwise
    // fallback to latin1 (mib == 4)
    int mib = codec ? codec->mibEnum() : 4;

    // for asian locales, use the mib for the font codec instead of the locale codec
    switch (mib) {
    case 38: // eucKR
        mib = 36;
        break;

    case 2025: // GB2312
        mib = 57;
        break;

    case 113: // GBK
        mib = -113;
        break;

    case 114: // GB18030
        mib = -114;
        break;

    case 2026: // Big5
        mib = -2026;
        break;

    case 2101: // Big5-HKSCS
        mib = -2101;
        break;

    case 16: // JIS7
        mib = 15;
        break;

    case 17: // SJIS
    case 18: // eucJP
        mib = 63;
        break;
    }

    // get the default encoding id for the locale encoding...
    QFontPrivate::defaultEncodingID = qt_encoding_id_for_mib(mib);
}
示例#5
0
/*!
  \reimp
*/
QByteArray QTextDrag::encodedData(const char* mime) const
{
    QCString r;
    if ( 0==qstrnicmp(mime,"text/",5) ) {
	QCString m(mime);
	m = m.lower();
	QTextCodec *codec = findcharset(m);
	if ( !codec )
	    return r;
	r = codec->fromUnicode(d->txt);
	if (!codec || codec->mibEnum() != 1000) {
	    // Don't include NUL in size (QCString::resize() adds NUL)
	    ((QByteArray&)r).resize(r.length());
	}
    }
    return r;
}
示例#6
0
/*!
    Returns the QTextCodec which matches the \link
    QTextCodec::mibEnum() MIBenum\endlink \a mib.
*/
QTextCodec* QTextCodec::codecForMib(int mib)
{
    setup();

    // Qt 3 used 1000 (mib for UCS2) as its identifier for the utf16 codec. Map
    // this correctly for compatibility.
    if (mib == 1000)
        mib = 1015;

    QList<QTextCodec*>::ConstIterator i;
    for (int i = 0; i < all->size(); ++i) {
        QTextCodec *cursor = all->at(i);
        if (cursor->mibEnum() == mib)
            return cursor;
    }

    return createForMib(mib);
}
示例#7
0
/*!
  Attempts to decode the dropped information in \a e
  into \a str, returning TRUE if successful.  If \a subtype is null,
  any text subtype is accepted, otherwise only that specified is
  accepted.  \a subtype is set to the accepted subtype.

  \sa canDecode()
*/
bool QTextDrag::decode( const QMimeSource* e, QString& str, QCString& subtype )
{
    const char* mime;
    for (int i=0; (mime = e->format(i)); i++) {
	if ( 0==qstrnicmp(mime,"text/",5) ) {
	    QCString m(mime);
	    m = m.lower();
	    int semi = m.find(';');
	    if ( semi < 0 )
		semi = m.length();
	    QCString foundst = m.mid(5,semi-5);
	    if ( subtype.isNull() || foundst == subtype ) {
		QTextCodec* codec = findcharset(m);
		if ( codec ) {
		    QByteArray payload;

		    payload = e->encodedData(mime);
		    if ( payload.size() ) {
			int l;
			if ( codec->mibEnum() != 1000) {
			    // length is at NUL or payload.size()
			    l = 0;
			    while ( l < (int)payload.size() && payload[l] )
				l++;
			} else {
			    l = payload.size();
			}

			str = codec->toUnicode(payload,l);

			if ( subtype.isNull() )
			    subtype = foundst;

			return TRUE;
		    }
		}
	    }
	}
    }
    return FALSE;
}
示例#8
0
static uint32_t translateKeysym(uint32_t sym, const QString &text) {
    int code = 0;

    QTextCodec *systemCodec = QTextCodec::codecForLocale();
    if (sym < 128 || (sym < 256 && systemCodec->mibEnum() == 4)) {
        // upper-case key, if known
        code = isprint((int)sym) ? toupper((int)sym) : 0;
    } else if (sym >= XKB_KEY_F1 && sym <= XKB_KEY_F35) {
        return Qt::Key_F1 + (int(sym) - XKB_KEY_F1);
    } else if (text.length() == 1 && text.unicode()->unicode() > 0x1f
               && text.unicode()->unicode() != 0x7f
               && !(sym >= XKB_KEY_dead_grave && sym <= XKB_KEY_dead_currency)) {
        code = text.unicode()->toUpper().unicode();
    } else {
        for (int i = 0; KeyTable[i]; i += 2)
            if (sym == KeyTable[i])
                code = KeyTable[i + 1];
    }

    return code;
}
示例#9
0
/*!
    Returns the QTextCodec which matches the \link
    QTextCodec::mibEnum() MIBenum\endlink \a mib.
*/
QTextCodec* QTextCodec::codecForMib(int mib)
{
    QMutexLocker locker(textCodecsMutex());

    setup();

    if (!validCodecs())
        return 0;

    QByteArray key = "MIB: " + QByteArray::number(mib);
    QTextCodecCache *cache = qTextCodecCache();
    QTextCodec *codec;
    if (cache) {
        codec = cache->value(key);
        if (codec)
            return codec;
    }

    QList<QTextCodec*>::ConstIterator i;
    for (int i = 0; i < all->size(); ++i) {
        QTextCodec *cursor = all->at(i);
        if (cursor->mibEnum() == mib) {
            if (cache)
                cache->insert(key, cursor);
            return cursor;
        }
    }

    codec = createForMib(mib);

    // Qt 3 used 1000 (mib for UCS2) as its identifier for the utf16 codec. Map
    // this correctly for compatibility.
    if (!codec && mib == 1000)
        return codecForMib(1015);

    if (codec && cache)
        cache->insert(key, codec);
    return codec;
}
示例#10
0
文件: qtextcodec.cpp 项目: cedrus/qt
/*!
    \threadsafe
    Returns the QTextCodec which matches the
    \l{QTextCodec::mibEnum()}{MIBenum} \a mib.
*/
QTextCodec* QTextCodec::codecForMib(int mib)
{
    QMutexLocker locker(textCodecsMutex());

    QCoreGlobalData *globalData = QCoreGlobalData::instance();
    if (!globalData)
        return 0;
    if (globalData->allCodecs.isEmpty())
        setup();

    QByteArray key = "MIB: " + QByteArray::number(mib);

    QTextCodecCache *cache = &globalData->codecCache;
    QTextCodec *codec;
    if (cache) {
        codec = cache->value(key);
        if (codec)
            return codec;
    }

    QList<QTextCodec*>::ConstIterator i;
    for (int i = 0; i < globalData->allCodecs.size(); ++i) {
        QTextCodec *cursor = globalData->allCodecs.at(i);
        if (cursor->mibEnum() == mib) {
            if (cache)
                cache->insert(key, cursor);
            return cursor;
        }
    }

#ifdef QT_USE_ICU
    return QIcuCodec::codecForMibUnlocked(mib);
#else
    return 0;
#endif
}
示例#11
0
bool LiteEditorFile::loadFileHelper(const QString &fileName, const QString &mimeType, bool bCheckCodec, QString &outText)
{
    QFile file(fileName);
    if (!file.open(QFile::ReadOnly)) {
        return false;
    }
    const QFileInfo fi(fileName);
    m_bReadOnly = !fi.isWritable();

    m_mimeType = mimeType;
    m_fileName = fileName;

    if (file.size() > (3 << 24)) {
        m_liteApp->appendLog("LiteEditor","Large file not open in the text editor! "+fileName,true);
        m_hasDecodingError = true;
        return false;
    }

    QByteArray buf = file.readAll();
    m_hasDecodingError = false;

    if (HasBinaryData(buf,32)) {
        m_liteApp->appendLog("LiteEditor","Binary file not open in the text editor! "+fileName,true);
        m_hasDecodingError = true;
        //outText = "error load binary file!!!";
        return false;
    }

    if (bCheckCodec) {
        m_codec = QTextCodec::codecForName("UTF-8");
        m_hasUtf8Bom = false;

        if (mimeType == "text/html" || mimeType == "text/xml") {
            m_codec = QTextCodec::codecForHtml(buf,QTextCodec::codecForName("utf-8"));
        } else {
            LiteApi::IMimeType *im = m_liteApp->mimeTypeManager()->findMimeType(mimeType);
            if (im) {
                QString codecName = im->codec();
                if (!codecName.isEmpty()) {
                    m_codec = QTextCodec::codecForName(codecName.toLatin1());
                }
            }
            int bytesRead = buf.size();
            QTextCodec *codec = m_codec;
            // code taken from qtextstream
            if (bytesRead >= 4 && ((uchar(buf[0]) == 0xff && uchar(buf[1]) == 0xfe && uchar(buf[2]) == 0 && uchar(buf[3]) == 0)
                                   || (uchar(buf[0]) == 0 && uchar(buf[1]) == 0 && uchar(buf[2]) == 0xfe && uchar(buf[3]) == 0xff))) {
                codec = QTextCodec::codecForName("UTF-32");
            } else if (bytesRead >= 2 && ((uchar(buf[0]) == 0xff && uchar(buf[1]) == 0xfe)
                                          || (uchar(buf[0]) == 0xfe && uchar(buf[1]) == 0xff))) {
                codec = QTextCodec::codecForName("UTF-16");
            } else if (bytesRead >= 3 && uchar(buf[0]) == 0xef && uchar(buf[1]) == 0xbb && uchar(buf[2])== 0xbf) {
                codec = QTextCodec::codecForName("UTF-8");
                buf.remove(0,3);
                m_hasUtf8Bom = true;
            } else if (!codec){
                codec = QTextCodec::codecForLocale();
            }
            // end code taken from qtextstream
            m_codec = codec;
        }
    }

    QTextCodec::ConverterState state;
    outText = m_codec->toUnicode(buf,buf.size(),&state);
    if (state.invalidChars > 0 || state.remainingChars > 0) {
         m_hasDecodingError = true;
    }
    if (m_hasDecodingError && bCheckCodec) {
        QByteArray testName = m_libucd.parse(buf);
        if (!testName.isEmpty()) {
            QTextCodec *c = QTextCodec::codecForName(testName);
            if (c && (c->mibEnum() != m_codec->mibEnum()) ) {
                QTextCodec::ConverterState testState;
                QString testText = c->toUnicode(buf,buf.size(),&testState);
                if (testState.invalidChars == 0 && testState.remainingChars == 0) {
                    m_hasDecodingError = false;
                    m_codec = c;
                    outText = testText;
                }
            }
        }
    }
/*
    QByteArray verifyBuf = m_codec->fromUnicode(text); // slow
    // the minSize trick lets us ignore unicode headers
    int minSize = qMin(verifyBuf.size(), buf.size());
    m_hasDecodingError = (minSize < buf.size()- 4
                          || memcmp(verifyBuf.constData() + verifyBuf.size() - minSize,
                                    buf.constData() + buf.size() - minSize, minSize));
*/
    /*
    if (text.length()*2+4 < buf.length()) {
        m_hasDecodingError = true;
    }
    */

    int lf = outText.indexOf('\n');
    if (lf < 0) {
        m_lineTerminatorMode = NativeLineTerminator;
    } else if (lf == 0) {
        m_lineTerminatorMode = LFLineTerminator;
    } else {
        lf = outText.indexOf(QRegExp("[^\r]\n"),lf-1);
        if (lf >= 0) {
            m_lineTerminatorMode = LFLineTerminator;
        } else {
            m_lineTerminatorMode = CRLFLineTerminator;
        }
    }

    if (m_lineTerminatorMode == CRLFLineTerminator) {
        outText.replace("\r\n","\n");
    }

    bool noprintCheck = m_liteApp->settings()->value(EDITOR_NOPRINTCHECK,true).toBool();
    if (noprintCheck && !LiteApi::mimeIsText(mimeType)) {
        for (int i = 0; i < outText.length(); i++) {
//            if (!outText[i].isPrint() && !outText[i].isSpace() && outText[i] != '\r' && outText[i] != '\n') {
//                outText[i] = '.';
//                m_hasDecodingError = true;
//            }
            if (IsBinaryCode(outText[i].unicode())) {
                outText[i] = '.';
                m_hasDecodingError = true;
            }
        }
    }

    if (m_hasDecodingError) {
        m_liteApp->appendLog("LiteEditor",QString("Decode file error! file:\"%1\" codec:%2").arg(fileName).arg(textCodec()),true);
    }

    return true;
}
示例#12
0
/*!
  Internal function that initializes the font system.

  \internal
  The font cache and font dict do not alloc the keys. The key is a QString
  which is shared between QFontPrivate and QXFontName.
*/
void QFont::initialize()
{
    // create global font cache
    if ( ! QFontCache::instance ) (void) new QFontCache;

#ifndef QT_NO_CODECS
#ifndef QT_NO_BIG_CODECS
    static bool codecs_once = FALSE;
    if ( ! codecs_once ) {
	(void) new QFontJis0201Codec;
	(void) new QFontJis0208Codec;
	(void) new QFontKsc5601Codec;
	(void) new QFontGb2312Codec;
	(void) new QFontGbkCodec;
	(void) new QFontGb18030_0Codec;
	(void) new QFontBig5Codec;
	(void) new QFontBig5hkscsCodec;
	(void) new QFontLaoCodec;
	codecs_once = TRUE;
    }
#endif // QT_NO_BIG_CODECS
#endif // QT_NO_CODECS

    extern int qt_encoding_id_for_mib( int mib ); // from qfontdatabase_x11.cpp
    QTextCodec *codec = QTextCodec::codecForLocale();
    // determine the default encoding id using the locale, otherwise
    // fallback to latin1 ( mib == 4 )
    int mib = codec ? codec->mibEnum() : 4;

    // for asian locales, use the mib for the font codec instead of the locale codec
    switch (mib) {
    case 38: // eucKR
	mib = 36;
	break;

    case 2025: // GB2312
	mib = 57;
	break;

    case 113: // GBK
	mib = -113;
	break;

    case 114: // GB18030
	mib = -114;
	break;

    case 2026: // Big5
	mib = -2026;
	break;

    case 2101: // Big5-HKSCS
	mib = -2101;
	break;

    case 16: // JIS7
	mib = 15;
	break;

    case 17: // SJIS
    case 18: // eucJP
	mib = 63;
	break;
    }

    // get the default encoding id for the locale encoding...
    QFontPrivate::defaultEncodingID = qt_encoding_id_for_mib( mib );

    // get some sample text based on the users locale. we use this to determine the
    // default script for the font system
    QCString oldlctime = setlocale(LC_TIME, 0);
    QCString lctime = setlocale(LC_TIME, "");

    time_t ttmp = time(0);
    struct tm *tt = 0;
    char samp[64];
    QString sample;

    if ( ttmp != -1 ) {
#if defined(QT_THREAD_SUPPORT) && defined(_POSIX_THREAD_SAFE_FUNCTIONS)
	// use the reentrant versions of localtime() where available
	tm res;
	tt = localtime_r( &ttmp, &res );
#else
	tt = localtime( &ttmp );
#endif // QT_THREAD_SUPPORT && _POSIX_THREAD_SAFE_FUNCTIONS

	if ( tt != 0 && strftime( samp, 64, "%A%B", tt ) > 0 )
	    if ( codec )
		sample = codec->toUnicode( samp );
    }

    if ( ! sample.isNull() && ! sample.isEmpty() ) {
	QFont::Script cs = QFont::NoScript, tmp;
	const QChar *uc = sample.unicode();
	QFontPrivate *priv = new QFontPrivate;

	for ( uint i = 0; i < sample.length(); i++ ) {
	    SCRIPT_FOR_CHAR( tmp, *uc );
	    uc++;
	    if ( tmp != cs && tmp != QFont::UnknownScript ) {
		cs = tmp;
		break;
	    }
	}
	delete priv;

	if ( cs != QFont::UnknownScript )
	    QFontPrivate::defaultScript = cs;
    }

    setlocale( LC_TIME, oldlctime.data() );
}