kmail

khtmlparthtmlwriter.cpp

Go to the documentation of this file.
00001 /*  -*- c++ -*-
00002     khtmlparthtmlwriter.cpp
00003 
00004     This file is part of KMail, the KDE mail client.
00005     Copyright (c) 2003 Marc Mutz <mutz@kde.org>
00006 
00007     KMail is free software; you can redistribute it and/or modify it
00008     under the terms of the GNU General Public License, version 2, as
00009     published by the Free Software Foundation.
00010 
00011     KMail is distributed in the hope that it will be useful, but
00012     WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014     General Public License for more details.
00015 
00016     You should have received a copy of the GNU General Public License
00017     along with this program; if not, write to the Free Software
00018     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00019 
00020     In addition, as a special exception, the copyright holders give
00021     permission to link the code of this program with any edition of
00022     the Qt library by Trolltech AS, Norway (or with modified versions
00023     of Qt that use the same license as Qt), and distribute linked
00024     combinations including the two.  You must obey the GNU General
00025     Public License in all respects for all of the code used other than
00026     Qt.  If you modify this file, you may extend this exception to
00027     your version of the file, but you are not obligated to do so.  If
00028     you do not wish to do so, delete this exception statement from
00029     your version.
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     // clear the widget:
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; // don't run into end()'s warning
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; // don't run into end()'s warning
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 } // namespace KMail
00153 
00154 #include "khtmlparthtmlwriter.moc"