• 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
  • viewer
htmlquotecolorer.cpp
Go to the documentation of this file.
1 /* Copyright 2009 Thomas McGuire <mcguire@kde.org>
2  2009 Torgny Nyblom <nyblom@kde.org>
3 
4  This program is free software; you can redistribute it and/or
5  modify it under the terms of the GNU General Public License as
6  published by the Free Software Foundation; either version 2 of
7  the License or (at your option) version 3 or any later version
8  accepted by the membership of KDE e.V. (or its successor approved
9  by the membership of KDE e.V.), which shall act as a proxy
10  defined in Section 14 of version 3 of the license.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU 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, see <http://www.gnu.org/licenses/>.
19 */
20 
21 #include "htmlquotecolorer.h"
22 #include "settings/globalsettings.h"
23 
24 #include <KDebug>
25 
26 #ifndef KDEPIM_NO_WEBKIT
27 #include <QWebPage>
28 #include <QWebFrame>
29 #include <QWebElement>
30 #endif
31 
32 using namespace MessageViewer;
33 
34 HTMLQuoteColorer::HTMLQuoteColorer()
35  : mEnableHtmlQuoteColorer(true)
36 {
37 }
38 
39 void HTMLQuoteColorer::setEnableHtmlQuoteColorer(bool enabled)
40 {
41  mEnableHtmlQuoteColorer = enabled;
42 }
43 
44 QString HTMLQuoteColorer::process( const QString &htmlSource, QString&extraHead )
45 {
46 #ifndef KDEPIM_NO_WEBKIT
47  // Create a DOM Document from the HTML source
48  QWebPage page(0);
49  page.settings()->setAttribute( QWebSettings::JavascriptEnabled, false );
50  page.settings()->setAttribute( QWebSettings::JavaEnabled, false );
51  page.settings()->setAttribute( QWebSettings::PluginsEnabled, false );
52 
53  page.settings()->setAttribute( QWebSettings::AutoLoadImages, false );
54 
55  QWebFrame *frame = page.mainFrame();
56  frame->setHtml( htmlSource );
57 
58  QString script(QLatin1String(
59  "mIsQuotedLine = false;\n"
60  "mIsFirstTextNodeInLine = true;\n"
61  "mQuoteColors = new Array();\n"
62  "mQuoteColors[0] = \"") + mQuoteColors[0].name() + QLatin1String("\";\n"
63  "mQuoteColors[1] = \"") + mQuoteColors[1].name() + QLatin1String("\";\n"
64  "mQuoteColors[2] = \"") + mQuoteColors[2].name() + QLatin1String("\";\n"
65 
66  "processNode( document.documentElement );\n"
67 
68  "function processNode( node ) {\n"
69  // Below, we determine if the current text node should be quote colored by keeping track of"
70  // linebreaks and whether this text node is the first one."
71  " var textContent = node.textContent;\n"
72  " var isTextNode = !textContent.length == 0 && !node.hasChildNodes();\n"
73  " if ( isTextNode ) {\n"
74  " if ( mIsFirstTextNodeInLine ) {\n"
75  " if ( textContent.charAt( 0 ) == '>' || textContent.charAt( 0 ) == '|' ) {\n"
76  " mIsQuotedLine = true;\n"
77  " currentQuoteLength = quoteLength( textContent ) - 1;\n"
78  " }\n"
79  " else {\n"
80  " mIsQuotedLine = false;\n"
81  " }\n"
82  " }\n"
83 
84  // All subsequent text nodes are not the first ones anymore"
85  " mIsFirstTextNodeInLine = false;\n"
86  " }\n"
87 
88  " var nodeName = node.nodeName.toLowerCase();\n"
89  " var lineBreakNodes = new Array();\n"
90  " lineBreakNodes[0] = \"br\"\n"
91  " lineBreakNodes[1] = \"p\"\n"
92  " lineBreakNodes[2] = \"div\"\n"
93  " lineBreakNodes[3] = \"ul\"\n"
94  " lineBreakNodes[4] = \"ol\"\n"
95  " lineBreakNodes[5] = \"li\"\n"
96 
97  " for( i = 0; i < lineBreakNodes.length; i++) {\n"
98  " if ( lineBreakNodes[i] == nodeName ) {\n"
99  " mIsFirstTextNodeInLine = true;\n"
100  " break;\n"
101  " }\n"
102  " }\n"
103 
104  " var returnNode = node;\n"
105  " var fontTagAdded = false;\n"
106  " if ( mIsQuotedLine && isTextNode ) {\n"
107 
108  // Ok, we are in a line that should be quoted, so create a font node for the color and replace"
109  // the current node with it."
110  " var font = node.ownerDocument.createElement( \"font\" );\n"
111  " font.setAttribute( \"color\", mQuoteColors[ currentQuoteLength ] );\n"
112  " node.parentNode.replaceChild( font, node );\n"
113  " font.appendChild( node );\n"
114  " returnNode = font;\n"
115  " fontTagAdded = true;\n"
116  " }\n"
117 
118  // Process all child nodes, but only if we are didn't add those child nodes itself, as otherwise"
119  // we'll go into an infinite recursion."
120  " if ( !fontTagAdded ) {\n"
121  " var childNode = node.firstChild;\n"
122  " while ( childNode ) {\n"
123  " childNode = processNode( childNode );\n"
124  " childNode = childNode.nextSibling;\n"
125  " }\n"
126  " }\n"
127  " return returnNode;\n"
128  "}\n"
129 
130  "function quoteLength( line )\n"
131  "{\n"
132  " line = line.replace( / /g, \"\" ).replace( '|', '>' );\n"
133  " if ( line.substr( 0, 3 ) == \">>>\" ) return 3;\n"
134  " if ( line.substr( 0, 2 ) == \">>\" ) return 2;\n"
135  " if ( line.substr( 0, 1 ) == '>' ) return 1;\n"
136  " return 0;\n"
137  "}\n"));
138 
139  if (mEnableHtmlQuoteColorer) {
140  page.settings()->setAttribute( QWebSettings::JavascriptEnabled, true );
141  frame->evaluateJavaScript( script );
142  page.settings()->setAttribute( QWebSettings::JavascriptEnabled, false );
143  }
144  const QWebElement body = frame->documentElement().findFirst(QLatin1String("body"));
145  const QWebElement header = frame->documentElement().findFirst(QLatin1String("head"));
146 
147  extraHead = header.toInnerXml();
148  return body.toInnerXml();
149 #else
150  return htmlSource;
151 #endif
152 }
153 
154 void HTMLQuoteColorer::setQuoteColor( unsigned int level, const QColor& color )
155 {
156  if ( level < 3 )
157  mQuoteColors[level] = color;
158 }
159 
globalsettings.h
QWebFrame
QWebFrame::documentElement
QWebElement documentElement() const
QWebElement::toInnerXml
QString toInnerXml() const
MessageViewer::HTMLQuoteColorer::process
QString process(const QString &htmlSource, QString &extraHead)
Do the work and add nice colors to the HTML.
Definition: htmlquotecolorer.cpp:44
QWebFrame::setHtml
void setHtml(const QString &html, const QUrl &baseUrl)
QWebElement
MessageViewer::HTMLQuoteColorer::setEnableHtmlQuoteColorer
void setEnableHtmlQuoteColorer(bool enabled)
Definition: htmlquotecolorer.cpp:39
QWebPage::mainFrame
QWebFrame * mainFrame() const
QWebPage
QString
QWebFrame::evaluateJavaScript
QVariant evaluateJavaScript(const QString &scriptSource)
QColor
MessageViewer::HTMLQuoteColorer::setQuoteColor
void setQuoteColor(unsigned int level, const QColor &color)
Sets the quote color of the specific leve.
Definition: htmlquotecolorer.cpp:154
QWebSettings::setAttribute
void setAttribute(WebAttribute attribute, bool on)
QWebPage::settings
QWebSettings * settings() const
QWebElement::findFirst
QWebElement findFirst(const QString &selectorQuery) const
MessageViewer::HTMLQuoteColorer::HTMLQuoteColorer
HTMLQuoteColorer()
Definition: htmlquotecolorer.cpp:34
htmlquotecolorer.h
QLatin1String
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