kmail

tdehtmlparthtmlwriter.cpp
1/*
2 tdehtmlparthtmlwriter.cpp
3
4 This file is part of KMail, the KDE mail client.
5 Copyright (c) 2003 Marc Mutz <mutz@kde.org>
6
7 KMail is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License, version 2, as
9 published by the Free Software Foundation.
10
11 KMail is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
20 In addition, as a special exception, the copyright holders give
21 permission to link the code of this program with any edition of
22 the TQt library by Trolltech AS, Norway (or with modified versions
23 of TQt that use the same license as TQt), and distribute linked
24 combinations including the two. You must obey the GNU General
25 Public License in all respects for all of the code used other than
26 TQt. If you modify this file, you may extend this exception to
27 your version of the file, but you are not obligated to do so. If
28 you do not wish to do so, delete this exception statement from
29 your version.
30*/
31
32#include <config.h>
33
34#include "tdehtmlparthtmlwriter.h"
35
36#include <kdebug.h>
37#include <tdehtml_part.h>
38#include <tdehtmlview.h>
39
40#include <dom/dom_string.h>
41#include <dom/html_document.h>
42#include <dom/html_image.h>
43#include <dom/html_misc.h>
44
45#include <cassert>
46
47namespace KMail {
48
49 KHtmlPartHtmlWriter::KHtmlPartHtmlWriter( TDEHTMLPart * part,
50 TQObject * parent, const char * name )
51 : TQObject( parent, name ), HtmlWriter(),
52 mHtmlPart( part ), mHtmlTimer( 0, "mHtmlTimer" ), mState( Ended )
53 {
54 assert( part );
55 connect( &mHtmlTimer, TQ_SIGNAL(timeout()), TQ_SLOT(slotWriteNextHtmlChunk()) );
56 }
57
58 KHtmlPartHtmlWriter::~KHtmlPartHtmlWriter() {
59
60 }
61
62 void KHtmlPartHtmlWriter::begin( const TQString & css ) {
63 if ( mState != Ended ) {
64 kdWarning( 5006 ) << "KHtmlPartHtmlWriter: begin() called on non-ended session!" << endl;
65 reset();
66 }
67
68 mEmbeddedPartMap.clear();
69
70 // clear the widget:
71 mHtmlPart->view()->setUpdatesEnabled( false );
72 mHtmlPart->view()->viewport()->setUpdatesEnabled( false );
73 static_cast<TQScrollView *>(mHtmlPart->widget())->ensureVisible( 0, 0 );
74
75 mHtmlPart->begin( KURL( "file:/" ) );
76 if ( !css.isEmpty() )
77 mHtmlPart->setUserStyleSheet( css );
78 mState = Begun;
79 }
80
81 void KHtmlPartHtmlWriter::end() {
82 kdWarning( mState != Begun, 5006 ) << "KHtmlPartHtmlWriter: end() called on non-begun or queued session!" << endl;
83 mHtmlPart->end();
84
85 resolveCidUrls();
86
87 mHtmlPart->view()->viewport()->setUpdatesEnabled( true );
88 mHtmlPart->view()->setUpdatesEnabled( true );
89 mHtmlPart->view()->viewport()->repaint( false );
90 mState = Ended;
91 }
92
93 void KHtmlPartHtmlWriter::reset() {
94 if ( mState != Ended ) {
95 mHtmlTimer.stop();
96 mHtmlQueue.clear();
97 mState = Begun; // don't run into end()'s warning
98 end();
99 }
100 mState = Ended;
101 }
102
103 void KHtmlPartHtmlWriter::write( const TQString & str ) {
104 kdWarning( mState != Begun, 5006 ) << "KHtmlPartHtmlWriter: write() called in Ended or Queued state!" << endl;
105 mHtmlPart->write( str );
106 }
107
108 void KHtmlPartHtmlWriter::queue( const TQString & str ) {
109 static const uint chunksize = 16384;
110 for ( uint pos = 0 ; pos < str.length() ; pos += chunksize )
111 mHtmlQueue.push_back( str.mid( pos, chunksize ) );
112 mState = Queued;
113 }
114
115 void KHtmlPartHtmlWriter::flush() {
116 slotWriteNextHtmlChunk();
117 }
118
119 void KHtmlPartHtmlWriter::slotWriteNextHtmlChunk() {
120 if ( mHtmlQueue.empty() ) {
121 mState = Begun; // don't run into end()'s warning
122 end();
123 } else {
124 mHtmlPart->write( mHtmlQueue.front() );
125 mHtmlQueue.pop_front();
126 mHtmlTimer.start( 0, true );
127 }
128 }
129
130 void KHtmlPartHtmlWriter::embedPart( const TQCString & contentId,
131 const TQString & contentURL ) {
132 mEmbeddedPartMap[TQString(contentId)] = contentURL;
133 }
134
135 void KHtmlPartHtmlWriter::resolveCidUrls()
136 {
137 DOM::HTMLDocument document = mHtmlPart->htmlDocument();
138 DOM::HTMLCollection images = document.images();
139 for ( DOM::Node node = images.firstItem(); !node.isNull(); node = images.nextItem() ) {
140 DOM::HTMLImageElement image( node );
141 KURL url( image.src().string() );
142 if ( url.protocol() == "cid" ) {
143 EmbeddedPartMap::const_iterator it = mEmbeddedPartMap.find( url.path() );
144 if ( it != mEmbeddedPartMap.end() ) {
145 kdDebug(5006) << "Replacing " << url.prettyURL() << " by " << it.data() << endl;
146 image.setSrc( it.data() );
147 }
148 }
149 }
150 }
151
152} // namespace KMail
153
154#include "tdehtmlparthtmlwriter.moc"
folderdiaquotatab.h
Definition: aboutdata.cpp:40