Getting the document from the cache

Andras Mantia amantia at kde.org
Mon Jan 31 09:01:34 GMT 2005


Andras Mantia wrote:

> 2) use the KHTMLPart::encoding() and give back the source in QString, but
> using this encoding information. I must verify this, maybe it's not
> possible to directly use QTextStream::setEncoding(encoding()) but I should
> use QTextStream::setCodec(QTextCodec::codecForName(encoding()))

I've tested the below variant with UTF8 and ISO-8859-2 and seems to work.

Andras
 
QString KHTMLPart::documentSource() const
{
  QString sourceStr;
  if ( !( m_url.isLocalFile() ) &&
KHTMLPageCache::self()->isComplete( d->m_cacheId ) )
  {
     KTempFile sourceFile(QString::null, QString::fromLatin1(".html"));
     if (sourceFile.status() == 0)
     {
       KHTMLPageCache::self()->saveData(d->m_cacheId,
sourceFile.dataStream());
       sourceFile.close();
       QFile f( sourceFile.name() );
       if ( f.open( IO_ReadOnly ) )
       {
         QTextStream stream( &f );
         stream.setCodec(QTextCodec::codecForName(encoding().latin1()));
         sourceStr = stream.read();
         f.close();
       }
     }
     sourceFile.unlink();
  } else
  {
    QString tmpFile;
    if( KIO::NetAccess::download( m_url, tmpFile, NULL ) )
    {
      QFile f( tmpFile );
      if ( f.open( IO_ReadOnly ) )
      {
        QTextStream stream( &f );
        stream.setCodec(QTextCodec::codecForName(encoding().latin1()));
        sourceStr = stream.read();
        f.close();
      }
      KIO::NetAccess::removeTempFile( tmpFile );
    }
  }

  return sourceStr;
}

-- 
Quanta Plus developer - http://quanta.sourceforge.net
K Desktop Environment - http://www.kde.org





More information about the kfm-devel mailing list