コード例 #1
0
ファイル: kconfigbase.cpp プロジェクト: serghei/kde3-kdelibs
QString KConfigBase::readEntry(const char *pKey, const QString &aDefault) const
{
    // we need to access _locale instead of the method locale()
    // because calling locale() will create a locale object if it
    // doesn't exist, which requires KConfig, which will create a infinite
    // loop, and nobody likes those.
    if(!bLocaleInitialized && KGlobal::_locale)
    {
        // get around const'ness.
        KConfigBase *that = const_cast< KConfigBase * >(this);
        that->setLocale();
    }

    QString aValue;

    bool expand = false;
    // construct a localized version of the key
    // try the localized key first
    KEntry aEntryData;
    KEntryKey entryKey(mGroup, 0);
    entryKey.c_key = pKey;
    entryKey.bDefault = readDefaults();
    entryKey.bLocal = true;
    aEntryData = lookupData(entryKey);
    if(!aEntryData.mValue.isNull())
    {
        // for GNOME .desktop
        aValue = KStringHandler::from8Bit(aEntryData.mValue.data());
        expand = aEntryData.bExpand;
    }
    else
    {
        entryKey.bLocal = false;
        aEntryData = lookupData(entryKey);
        if(!aEntryData.mValue.isNull())
        {
            aValue = QString::fromUtf8(aEntryData.mValue.data());
            if(aValue.isNull())
            {
                static const QString &emptyString = KGlobal::staticQString("");
                aValue = emptyString;
            }
            expand = aEntryData.bExpand;
        }
        else
        {
            aValue = aDefault;
        }
    }

    // only do dollar expansion if so desired
    if(expand || bExpand)
    {
        // check for environment variables and make necessary translations
        int nDollarPos = aValue.find('$');

        while(nDollarPos != -1 && nDollarPos + 1 < static_cast< int >(aValue.length()))
        {
            // there is at least one $
            if((aValue)[nDollarPos + 1] == '(')
            {
                uint nEndPos = nDollarPos + 1;
                // the next character is no $
                while((nEndPos <= aValue.length()) && (aValue[nEndPos] != ')'))
                    nEndPos++;
                nEndPos++;
                QString cmd = aValue.mid(nDollarPos + 2, nEndPos - nDollarPos - 3);

                QString result;
                FILE *fs = popen(QFile::encodeName(cmd).data(), "r");
                if(fs)
                {
                    {
                        QTextStream ts(fs, IO_ReadOnly);
                        result = ts.read().stripWhiteSpace();
                    }
                    pclose(fs);
                }
                aValue.replace(nDollarPos, nEndPos - nDollarPos, result);
            }
            else if((aValue)[nDollarPos + 1] != '$')
            {
                uint nEndPos = nDollarPos + 1;
                // the next character is no $
                QString aVarName;
                if(aValue[nEndPos] == '{')
                {
                    while((nEndPos <= aValue.length()) && (aValue[nEndPos] != '}'))
                        nEndPos++;
                    nEndPos++;
                    aVarName = aValue.mid(nDollarPos + 2, nEndPos - nDollarPos - 3);
                }
                else
                {
                    while(nEndPos <= aValue.length() && (aValue[nEndPos].isNumber() || aValue[nEndPos].isLetter() || aValue[nEndPos] == '_'))
                        nEndPos++;
                    aVarName = aValue.mid(nDollarPos + 1, nEndPos - nDollarPos - 1);
                }
                const char *pEnv = 0;
                if(!aVarName.isEmpty())
                    pEnv = getenv(aVarName.ascii());
                if(pEnv)
                {
                    // !!! Sergey A. Sukiyazov <*****@*****.**> !!!
                    // A environment variables may contain values in 8bit
                    // locale cpecified encoding or in UTF8 encoding.
                    aValue.replace(nDollarPos, nEndPos - nDollarPos, KStringHandler::from8Bit(pEnv));
                }
                else
                    aValue.remove(nDollarPos, nEndPos - nDollarPos);
            }
            else
            {
                // remove one of the dollar signs
                aValue.remove(nDollarPos, 1);
                nDollarPos++;
            }
            nDollarPos = aValue.find('$', nDollarPos);
        }
    }

    return aValue;
}