QString SessionPrivate::readString(const QByteArray& data) const
 {
     QByteArray enc = encoding;
     if (enc.isNull())
         enc = detectEncoding(data);
     QTextCodec *codec = QTextCodec::codecForName(enc);
     if (!codec)
         codec = QTextCodec::codecForLocale();
     return codec->toUnicode(data);
 }
Exemple #2
0
QString Util::convertEncoding(const QByteArray &data)
{
    QTextCodec * codec = detectEncoding( data );

    if ( !codec )
    {
        qDebug("Util: failed to detect the data encoding");
        return "";
    }

    qDebug("Util: automatically detected encoding %s", codec->name().constData() );
    return codec->toUnicode( data );
}
void CustomScriptSearchProvider::parseAsHtml(QNetworkReply* pReply)
{
    if (m_pEngine)
    {
        QString contentType = pReply->header(QNetworkRequest::ContentTypeHeader).toString();
        QString encoding = detectEncoding(contentType);
        QTextCodec* codec = QTextCodec::codecForName(encoding.toLocal8Bit());
        QString data = codec->makeDecoder()->toUnicode(pReply->readAll());
        QSgml* sgml = new QSgml(data);
        QScriptValue htmlResult = m_pEngine->newQObject(sgml);
        QScriptValue thisObj = m_scriptVal;

        if (thisObj.isObject() && thisObj.isValid())
        {
            thisObj.setProperty("htmlResult", htmlResult);
            emit HtmlResultReady();
        }
    }
}
Exemple #4
0
void LyricsParser_KFN::parse(QIODevice * file, LyricsLoader::Container& output, LyricsLoader::Properties &properties)
{
    // Parse the song.ini and fill up the sync and text arrays
    QByteArrayList texts;
    QList< int > syncs;

    // Analyze each line
    int idx_text = 0, idx_sync = 0;

    QByteArrayList lines = load( file ).split( '\n' );
    bool inGeneral = false;

    for ( int i = 0; i < lines.size(); i++ )
    {
        QByteArray line = lines[i];

        // Section detector
        if ( line.startsWith( '[' ) && line.endsWith( ']') )
        {
            inGeneral = (line == "[General]");
            continue;
        }

        if ( inGeneral )
        {
            if ( line.startsWith( "Title=" ) )
            {
                QString v = QString::fromUtf8( line.mid(6) ).trimmed();

                if ( !v.isEmpty() )
                    properties[ LyricsLoader::PROP_TITLE ] = v;
            }

            if ( line.startsWith( "Artist=" ) )
            {
                QString v = QString::fromUtf8( line.mid(7) ).trimmed();

                if ( !v.isEmpty() )
                    properties[ LyricsLoader::PROP_ARTIST ] = v;
            }
        }

        // Try to match the sync first
        char matchbuf[128];
        sprintf( matchbuf, "Sync%d=", idx_sync );

        if ( line.startsWith( matchbuf ) )
        {
            idx_sync++;

            // Syncs are split by comma
            QByteArrayList values = line.mid( strlen(matchbuf) ).split( ',' );

            for ( int v = 0; v < values.size(); v++ )
                syncs.push_back( values[v].toInt() );
        }

        // Now the text
        sprintf( matchbuf, "Text%d=", idx_text );

        if ( line.startsWith( matchbuf ) )
        {
            idx_text++;
            QByteArray textvalue = line.mid( strlen(matchbuf) );

            if ( !textvalue.isEmpty() )
            {
                // Text is split by word and optionally by the slash
                QByteArrayList values = textvalue.split(' ');

                for ( int v = 0; v < values.size(); v++ )
                {
                    QByteArrayList morevalues = values[v].split( '/' );

                    for ( int vv = 0; vv < morevalues.size(); vv++ )
                        texts.push_back( morevalues[vv] );

                    // We split by space, so add it at the end of each word
                    texts.last() = texts.last() + " ";
                }
            }

            // Line matched, so make it a line
            if ( texts.size() > 2 && texts[ texts.size() - 2 ] != "\n" )
                texts.push_back( "\n" );
        }
    }

    int curr_sync = 0;
    bool has_linefeed = false;
    int lines_no_block = 0;
    int lastsync = -1;

    // The original timing marks are not necessarily sorted, so we add them into a map
    // and then output them from that map
    QMap< int, QByteArray > sortedLyrics;

    for ( int i = 0; i < texts.size(); i++ )
    {
        if ( texts[i] == "\n" )
        {
            if ( lastsync == -1 )
                continue;

            if ( has_linefeed )
                lines_no_block = 0;
            else if ( ++lines_no_block > 6 )
            {
                lines_no_block = 0;
                sortedLyrics[ lastsync ] += "\n";
            }

            has_linefeed = true;
            sortedLyrics[ lastsync ] += "\n";
            continue;
        }
        else
            has_linefeed = false;

        // Get the time if we have it
        if ( curr_sync >= syncs.size() )
            continue;

        lastsync = syncs[ curr_sync++ ];
        sortedLyrics.insert( lastsync, texts[i] );
    }

    // Generate the content for encoding detection
    QByteArray lyricsForEncoding;

    for ( QMap< int, QByteArray >::const_iterator it = sortedLyrics.begin(); it != sortedLyrics.end(); ++it )
        lyricsForEncoding.append( it.value() );

    // Detect the encoding
    QTextCodec * codec = detectEncoding( lyricsForEncoding, properties );

    for ( QMap< int, QByteArray >::const_iterator it = sortedLyrics.begin(); it != sortedLyrics.end(); ++it )
    {
        qint64 timing = it.key() * 10;
        Lyric lyr( timing, codec->toUnicode( it.value() ) );

        // Last line?
        if ( lyr.text.endsWith( '\n') )
        {
            // Remove the \n
            lyr.text.chop( 1 );
            output.push_back( lyr );

            // and add an empty lyric
            output.push_back( Lyric( timing ) );
        }
        else
        {
            output.push_back( lyr );
        }
    }
}