kilobyte is kB not kb
[kdepim.git] / messageviewer / webkitparthtmlwriter.cpp
blob602c4d7b931b20bb57116339d5ebcee5a10b5803
1 /* -*- c++ -*-
2 webkitparthtmlwriter.cpp
4 This file is part of KMail, the KDE mail client.
5 Copyright (c) 2003 Marc Mutz <mutz@kde.org>
6 Copyright (c) 2009 Torgny Nyblom <nyblom@kde.org>
8 KMail is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License, version 2, as
10 published by the Free Software Foundation.
12 KMail is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 In addition, as a special exception, the copyright holders give
22 permission to link the code of this program with any edition of
23 the Qt library by Trolltech AS, Norway (or with modified versions
24 of Qt that use the same license as Qt), and distribute linked
25 combinations including the two. You must obey the GNU General
26 Public License in all respects for all of the code used other than
27 Qt. If you modify this file, you may extend this exception to
28 your version of the file, but you are not obligated to do so. If
29 you do not wish to do so, delete this exception statement from
30 your version.
34 #include <config-messageviewer.h>
36 #include "webkitparthtmlwriter.h"
38 #include "mailwebview.h"
40 #include <KDebug>
41 #include <KUrl>
43 #include <cassert>
44 #include <QByteArray>
45 #include <QWebView>
46 #include <QWebPage>
47 #include <QWebFrame>
48 #include <QWebElement>
50 using namespace MessageViewer;
52 WebKitPartHtmlWriter::WebKitPartHtmlWriter( MailWebView * view, QObject * parent )
53 : QObject( parent ), HtmlWriter(),
54 mHtmlView( view ), mState( Ended )
56 assert( view );
59 WebKitPartHtmlWriter::~WebKitPartHtmlWriter() {
62 void WebKitPartHtmlWriter::begin( const QString & css ) {
63 // The stylesheet is now included CSSHelper::htmlHead()
64 Q_UNUSED( css );
65 if ( mState != Ended ) {
66 kWarning() << "begin() called on non-ended session!";
67 reset();
70 mEmbeddedPartMap.clear();
72 // clear the widget:
73 mHtmlView->setUpdatesEnabled( false );
74 mHtmlView->scrollUp( 10 );
75 #ifndef KDEPIM_NO_WEBKIT
76 // PENDING(marc) push into MailWebView
77 mHtmlView->load( QUrl() );
78 #endif
79 mState = Begun;
82 void WebKitPartHtmlWriter::end() {
83 if ( mState != Begun ) {
84 kWarning() << "Called on non-begun or queued session!";
86 mHtmlView->setHtml( mHtml, QUrl( "file:///" ) );
87 mHtmlView->show();
88 mHtml.clear();
90 resolveCidUrls();
92 mHtmlView->setUpdatesEnabled( true );
93 mHtmlView->update();
94 mState = Ended;
95 emit finished();
98 void WebKitPartHtmlWriter::reset() {
99 if ( mState != Ended ) {
100 mHtml.clear();
101 mState = Begun; // don't run into end()'s warning
102 end();
103 mState = Ended;
107 void WebKitPartHtmlWriter::write( const QString & str ) {
108 if ( mState != Begun ) {
109 kWarning() << "Called in Ended or Queued state!";
111 mHtml.append( str );
114 void WebKitPartHtmlWriter::queue( const QString & str ) {
115 write( str );
118 void WebKitPartHtmlWriter::flush() {
119 mState = Begun; // don't run into end()'s warning
120 end();
123 void WebKitPartHtmlWriter::embedPart( const QByteArray & contentId,
124 const QString & contentURL ) {
125 mEmbeddedPartMap[QString(contentId)] = contentURL;
128 void WebKitPartHtmlWriter::resolveCidUrls()
130 // FIXME: instead of patching around in the HTML source, this should
131 // be replaced by a custom QNetworkAccessManager (for QWebView), or
132 // virtual loadResource() (for QTextBrowser)
133 #ifndef KDEPIM_NO_WEBKIT
134 QWebElement root = mHtmlView->page()->mainFrame()->documentElement();
135 QWebElementCollection images = root.findAll( "img" );
136 QWebElementCollection::iterator end(images.end());
137 for( QWebElementCollection::iterator it = images.begin(); it != end; ++it )
139 KUrl url( (*it).attribute( "src" ) );
140 if ( url.protocol() == QLatin1String( "cid" ) )
142 EmbeddedPartMap::const_iterator cit = mEmbeddedPartMap.constFind( url.path() );
143 if ( cit != mEmbeddedPartMap.constEnd() ) {
144 kDebug() << "Replacing" << url.prettyUrl() << "by" << cit.value();
145 (*it).setAttribute( "src", cit.value() );
149 #endif
153 #include "webkitparthtmlwriter.moc"