kmail Library API Documentation

khtmlparthtmlwriter.cpp

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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  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 
00041 #include <cassert>
00042 
00043 namespace KMail {
00044 
00045   KHtmlPartHtmlWriter::KHtmlPartHtmlWriter( KHTMLPart * part,
00046                         QObject * parent, const char * name )
00047     : QObject( parent, name ), HtmlWriter(),
00048       mHtmlPart( part ), mState( Ended )
00049   {
00050     assert( part );
00051     connect( &mHtmlTimer, SIGNAL(timeout()), SLOT(slotWriteNextHtmlChunk()) );
00052   }
00053 
00054   KHtmlPartHtmlWriter::~KHtmlPartHtmlWriter() {
00055 
00056   }
00057 
00058   void KHtmlPartHtmlWriter::begin( const QString & css ) {
00059     if ( mState != Ended ) {
00060       kdWarning( 5006 ) << "KHtmlPartHtmlWriter: begin() called on non-ended session!" << endl;
00061       reset();
00062     }
00063 
00064     // clear the widget:
00065     mHtmlPart->view()->setUpdatesEnabled( false );
00066     mHtmlPart->view()->viewport()->setUpdatesEnabled( false );
00067     static_cast<QScrollView *>(mHtmlPart->widget())->ensureVisible( 0, 0 );
00068 
00069     mHtmlPart->begin( KURL( "file:/" ) );
00070     if ( !css.isEmpty() )
00071       mHtmlPart->setUserStyleSheet( css );
00072     mState = Begun;
00073   }
00074 
00075   void KHtmlPartHtmlWriter::end() {
00076     kdWarning( mState != Begun, 5006 ) << "KHtmlPartHtmlWriter: end() called on non-begun or queued session!" << endl;
00077     mHtmlPart->end();
00078     mHtmlPart->view()->viewport()->setUpdatesEnabled( true );
00079     mHtmlPart->view()->setUpdatesEnabled( true );
00080     mHtmlPart->view()->viewport()->repaint( false );
00081     mState = Ended;
00082   }
00083 
00084   void KHtmlPartHtmlWriter::reset() {
00085     if ( mState != Ended ) {
00086       mHtmlTimer.stop();
00087       mHtmlQueue.clear();
00088       mState = Begun; // don't run into end()'s warning
00089       end();
00090     }
00091     mState = Ended;
00092   }
00093 
00094   void KHtmlPartHtmlWriter::write( const QString & str ) {
00095     kdWarning( mState != Begun, 5006 ) << "KHtmlPartHtmlWriter: write() called in Ended or Queued state!" << endl;
00096     mHtmlPart->write( str );
00097   }
00098 
00099   void KHtmlPartHtmlWriter::queue( const QString & str ) {
00100     static const uint chunksize = 16384;
00101     for ( uint pos = 0 ; pos < str.length() ; pos += chunksize )
00102       mHtmlQueue.push_back( str.mid( pos, chunksize ) );
00103     mState = Queued;
00104   }
00105 
00106   void KHtmlPartHtmlWriter::flush() {
00107     slotWriteNextHtmlChunk();
00108   }
00109 
00110   void KHtmlPartHtmlWriter::slotWriteNextHtmlChunk() {
00111     if ( mHtmlQueue.empty() ) {
00112       mState = Begun; // don't run into end()'s warning
00113       end();
00114     } else {
00115       mHtmlPart->write( mHtmlQueue.front() );
00116       mHtmlQueue.pop_front();
00117       mHtmlTimer.start( 0, true );
00118     }
00119   }
00120 
00121   
00122 
00123 } // namespace KMail
00124 
00125 #include "khtmlparthtmlwriter.moc"
KDE Logo
This file is part of the documentation for kmail Library Version 3.2.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sat May 1 11:37:19 2004 by doxygen 1.2.15 written by Dimitri van Heesch, © 1997-2003