• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepim API Reference
  • KDE Home
  • Contact Us
 

messageviewer

  • sources
  • kde-4.14
  • kdepim
  • messageviewer
  • htmlwriter
webkitparthtmlwriter.cpp
Go to the documentation of this file.
1 /* -*- c++ -*-
2  webkitparthtmlwriter.cpp
3 
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>
7 
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.
11 
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.
16 
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
20 
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.
31 */
32 
33 
34 
35 
36 #include "webkitparthtmlwriter.h"
37 
38 #include "viewer/mailwebview.h"
39 
40 #include <KDebug>
41 #include <KUrl>
42 
43 #include <cassert>
44 #include <QByteArray>
45 #include <QWebView>
46 #include <QWebPage>
47 #include <QWebFrame>
48 #include <QWebElement>
49 
50 using namespace MessageViewer;
51 
52 WebKitPartHtmlWriter::WebKitPartHtmlWriter( MailWebView * view, QObject * parent )
53  : QObject( parent ), HtmlWriter(),
54  mHtmlView( view ), mState( Ended )
55 {
56  assert( view );
57 }
58 
59 WebKitPartHtmlWriter::~WebKitPartHtmlWriter() {
60 }
61 
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();
68  }
69 
70  mEmbeddedPartMap.clear();
71 
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;
80 }
81 
82 void WebKitPartHtmlWriter::end() {
83  if ( mState != Begun ) {
84  kWarning() << "Called on non-begun or queued session!";
85  }
86  if(!mExtraHead.isEmpty()) {
87  insertExtraHead();
88  mExtraHead.clear();
89  }
90  mHtmlView->setHtml( mHtml, QUrl( QLatin1String("file:///") ) );
91  mHtmlView->show();
92  mHtml.clear();
93 
94 #ifndef KDEPIM_NO_WEBKIT
95  resolveCidUrls();
96  mHtmlView->scamCheck();
97 #endif
98  mHtmlView->setUpdatesEnabled( true );
99  mHtmlView->update();
100  mState = Ended;
101  emit finished();
102 }
103 
104 void WebKitPartHtmlWriter::reset() {
105  if ( mState != Ended ) {
106  mHtml.clear();
107  mState = Begun; // don't run into end()'s warning
108  end();
109  mState = Ended;
110  }
111 }
112 
113 void WebKitPartHtmlWriter::write( const QString & str ) {
114  if ( mState != Begun ) {
115  kWarning() << "Called in Ended or Queued state!";
116  }
117  mHtml.append( str );
118 }
119 
120 void WebKitPartHtmlWriter::queue( const QString & str ) {
121  write( str );
122 }
123 
124 void WebKitPartHtmlWriter::flush() {
125  mState = Begun; // don't run into end()'s warning
126  end();
127 }
128 
129 void WebKitPartHtmlWriter::embedPart( const QByteArray & contentId,
130  const QString & contentURL ) {
131  mEmbeddedPartMap[QLatin1String(contentId)] = contentURL;
132 }
133 
134 void WebKitPartHtmlWriter::resolveCidUrls()
135 {
136  // FIXME: instead of patching around in the HTML source, this should
137  // be replaced by a custom QNetworkAccessManager (for QWebView), or
138  // virtual loadResource() (for QTextBrowser)
139 #ifndef KDEPIM_NO_WEBKIT
140  QWebElement root = mHtmlView->page()->mainFrame()->documentElement();
141  QWebElementCollection images = root.findAll( QLatin1String("img") );
142  QWebElementCollection::iterator end(images.end());
143  for( QWebElementCollection::iterator it = images.begin(); it != end; ++it ) {
144  KUrl url( (*it).attribute( QLatin1String("src") ) );
145  if ( url.protocol() == QLatin1String( "cid" ) ) {
146  EmbeddedPartMap::const_iterator cit = mEmbeddedPartMap.constFind( url.path() );
147  if ( cit != mEmbeddedPartMap.constEnd() ) {
148  kDebug() << "Replacing" << url.prettyUrl() << "by" << cit.value();
149  (*it).setAttribute( QLatin1String("src"), cit.value() );
150  }
151  }
152  }
153 #endif
154 }
155 
156 void WebKitPartHtmlWriter::insertExtraHead()
157 {
158  const QString headTag(QLatin1String("<head>"));
159  const int index = mHtml.indexOf(headTag);
160  if(index!=-1) {
161  mHtml.insert(index+headTag.length(),mExtraHead);
162  }
163 }
164 
165 void WebKitPartHtmlWriter::extraHead( const QString& str )
166 {
167  mExtraHead = str;
168 }
169 
QString::indexOf
int indexOf(QChar ch, int from, Qt::CaseSensitivity cs) const
QString::append
QString & append(QChar ch)
MessageViewer::WebKitPartHtmlWriter::WebKitPartHtmlWriter
WebKitPartHtmlWriter(MailWebView *view, QObject *parent=0)
Definition: webkitparthtmlwriter.cpp:52
MessageViewer::WebKitPartHtmlWriter::reset
void reset()
Stop all possibly pending processing in order to be able to call begin() again.
Definition: webkitparthtmlwriter.cpp:104
QByteArray
webkitparthtmlwriter.h
QMap::constFind
const_iterator constFind(const Key &key) const
MessageViewer::WebKitPartHtmlWriter::end
void end()
Signal the end of stuff to write.
Definition: webkitparthtmlwriter.cpp:82
QMap::clear
void clear()
MessageViewer::MailWebView::scrollUp
void scrollUp(int pixels)
Definition: mailwebview_textbrowser.cpp:80
MessageViewer::WebKitPartHtmlWriter::queue
void queue(const QString &str)
Definition: webkitparthtmlwriter.cpp:120
QString::clear
void clear()
MessageViewer::WebKitPartHtmlWriter::finished
void finished()
MessageViewer::MailWebView::setHtml
void setHtml(const QString &html, const QUrl &baseUrl)
Definition: mailwebview_textbrowser.cpp:189
QString::insert
QString & insert(int position, QChar ch)
MessageViewer::MailWebView
MailWebView extends KWebView so that it can emit the popupMenu() signal.
Definition: mailwebview.h:47
QObject
QWebElementCollection::begin
const_iterator begin() const
QWebElement
QWebElementCollection::end
const_iterator end() const
QString::isEmpty
bool isEmpty() const
QMap::constEnd
const_iterator constEnd() const
MessageViewer::MailWebView::scamCheck
void scamCheck()
Definition: mailwebview_webkit.cpp:611
MessageViewer::WebKitPartHtmlWriter::flush
void flush()
(Start) flushing internal buffers, if any.
Definition: webkitparthtmlwriter.cpp:124
QString
MessageViewer::WebKitPartHtmlWriter::~WebKitPartHtmlWriter
~WebKitPartHtmlWriter()
Definition: webkitparthtmlwriter.cpp:59
QUrl
QLatin1String
mailwebview.h
MessageViewer::WebKitPartHtmlWriter::embedPart
void embedPart(const QByteArray &contentId, const QString &url)
Embed a part with Content-ID contentId, using url url.
Definition: webkitparthtmlwriter.cpp:129
MessageViewer::WebKitPartHtmlWriter::extraHead
void extraHead(const QString &str)
Definition: webkitparthtmlwriter.cpp:165
QWebElement::findAll
QWebElementCollection findAll(const QString &selectorQuery) const
MessageViewer::HtmlWriter
An interface to HTML sinks.
Definition: htmlwriter.h:98
QWebElementCollection::iterator
MessageViewer::WebKitPartHtmlWriter::begin
void begin(const QString &cssDefs)
Signal the begin of stuff to write, and give the CSS definitions.
Definition: webkitparthtmlwriter.cpp:62
QWebElementCollection
MessageViewer::WebKitPartHtmlWriter::write
void write(const QString &str)
Write out a chunk of text.
Definition: webkitparthtmlwriter.cpp:113
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:32:45 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

messageviewer

Skip menu "messageviewer"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer
  • pimprint

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal