kmail
khtmlparthtmlwriter.cppGo to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #include <config.h>
00033
00034 #include "khtmlparthtmlwriter.h"
00035
00036 #include <kdebug.h>
00037 #include <khtml_part.h>
00038 #include <khtmlview.h>
00039
00040 #include <dom/dom_string.h>
00041 #include <dom/html_document.h>
00042 #include <dom/html_image.h>
00043 #include <dom/html_misc.h>
00044
00045 #include <cassert>
00046
00047 namespace KMail {
00048
00049 KHtmlPartHtmlWriter::KHtmlPartHtmlWriter( KHTMLPart * part,
00050 QObject * parent, const char * name )
00051 : QObject( parent, name ), HtmlWriter(),
00052 mHtmlPart( part ), mState( Ended ), mHtmlTimer( 0, "mHtmlTimer" )
00053 {
00054 assert( part );
00055 connect( &mHtmlTimer, SIGNAL(timeout()), SLOT(slotWriteNextHtmlChunk()) );
00056 }
00057
00058 KHtmlPartHtmlWriter::~KHtmlPartHtmlWriter() {
00059
00060 }
00061
00062 void KHtmlPartHtmlWriter::begin( const QString & css ) {
00063 if ( mState != Ended ) {
00064 kdWarning( 5006 ) << "KHtmlPartHtmlWriter: begin() called on non-ended session!" << endl;
00065 reset();
00066 }
00067
00068 mEmbeddedPartMap.clear();
00069
00070
00071 mHtmlPart->view()->setUpdatesEnabled( false );
00072 mHtmlPart->view()->viewport()->setUpdatesEnabled( false );
00073 static_cast<QScrollView *>(mHtmlPart->widget())->ensureVisible( 0, 0 );
00074
00075 mHtmlPart->begin( KURL( "file:/" ) );
00076 if ( !css.isEmpty() )
00077 mHtmlPart->setUserStyleSheet( css );
00078 mState = Begun;
00079 }
00080
00081 void KHtmlPartHtmlWriter::end() {
00082 kdWarning( mState != Begun, 5006 ) << "KHtmlPartHtmlWriter: end() called on non-begun or queued session!" << endl;
00083 mHtmlPart->end();
00084
00085 resolveCidUrls();
00086
00087 mHtmlPart->view()->viewport()->setUpdatesEnabled( true );
00088 mHtmlPart->view()->setUpdatesEnabled( true );
00089 mHtmlPart->view()->viewport()->repaint( false );
00090 mState = Ended;
00091 }
00092
00093 void KHtmlPartHtmlWriter::reset() {
00094 if ( mState != Ended ) {
00095 mHtmlTimer.stop();
00096 mHtmlQueue.clear();
00097 mState = Begun;
00098 end();
00099 }
00100 mState = Ended;
00101 }
00102
00103 void KHtmlPartHtmlWriter::write( const QString & str ) {
00104 kdWarning( mState != Begun, 5006 ) << "KHtmlPartHtmlWriter: write() called in Ended or Queued state!" << endl;
00105 mHtmlPart->write( str );
00106 }
00107
00108 void KHtmlPartHtmlWriter::queue( const QString & str ) {
00109 static const uint chunksize = 16384;
00110 for ( uint pos = 0 ; pos < str.length() ; pos += chunksize )
00111 mHtmlQueue.push_back( str.mid( pos, chunksize ) );
00112 mState = Queued;
00113 }
00114
00115 void KHtmlPartHtmlWriter::flush() {
00116 slotWriteNextHtmlChunk();
00117 }
00118
00119 void KHtmlPartHtmlWriter::slotWriteNextHtmlChunk() {
00120 if ( mHtmlQueue.empty() ) {
00121 mState = Begun;
00122 end();
00123 } else {
00124 mHtmlPart->write( mHtmlQueue.front() );
00125 mHtmlQueue.pop_front();
00126 mHtmlTimer.start( 0, true );
00127 }
00128 }
00129
00130 void KHtmlPartHtmlWriter::embedPart( const QCString & contentId,
00131 const QString & contentURL ) {
00132 mEmbeddedPartMap[QString(contentId)] = contentURL;
00133 }
00134
00135 void KHtmlPartHtmlWriter::resolveCidUrls()
00136 {
00137 DOM::HTMLDocument document = mHtmlPart->htmlDocument();
00138 DOM::HTMLCollection images = document.images();
00139 for ( DOM::Node node = images.firstItem(); !node.isNull(); node = images.nextItem() ) {
00140 DOM::HTMLImageElement image( node );
00141 KURL url( image.src().string() );
00142 if ( url.protocol() == "cid" ) {
00143 EmbeddedPartMap::const_iterator it = mEmbeddedPartMap.find( url.path() );
00144 if ( it != mEmbeddedPartMap.end() ) {
00145 kdDebug(5006) << "Replacing " << url.prettyURL() << " by " << it.data() << endl;
00146 image.setSrc( it.data() );
00147 }
00148 }
00149 }
00150 }
00151
00152 }
00153
00154 #include "khtmlparthtmlwriter.moc"
|