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

messageviewer

  • sources
  • kde-4.12
  • 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 
23 #include <KDebug>
24 
25 #ifndef KDEPIM_NO_WEBKIT
26 #include <QWebPage>
27 #include <QWebFrame>
28 #include <QWebElement>
29 #endif
30 
31 using namespace MessageViewer;
32 
33 HTMLQuoteColorer::HTMLQuoteColorer()
34 {
35 }
36 
37 QString HTMLQuoteColorer::process( const QString &htmlSource, QString&extraHead )
38 {
39 #ifndef KDEPIM_NO_WEBKIT
40  // Create a DOM Document from the HTML source
41  QWebPage page(0);
42  page.settings()->setAttribute( QWebSettings::JavascriptEnabled, false );
43  page.settings()->setAttribute( QWebSettings::JavaEnabled, false );
44  page.settings()->setAttribute( QWebSettings::PluginsEnabled, false );
45 
46  page.settings()->setAttribute( QWebSettings::AutoLoadImages, false );
47 
48  QWebFrame *frame = page.mainFrame();
49  frame->setHtml( htmlSource );
50 
51  QString script(QLatin1String(
52  "mIsQuotedLine = false;\n"
53  "mIsFirstTextNodeInLine = true;\n"
54  "mQuoteColors = new Array();\n"
55  "mQuoteColors[0] = \"") + mQuoteColors[0].name() + QLatin1String("\";\n"
56  "mQuoteColors[1] = \"") + mQuoteColors[1].name() + QLatin1String("\";\n"
57  "mQuoteColors[2] = \"") + mQuoteColors[2].name() + QLatin1String("\";\n"
58 
59  "processNode( document.documentElement );\n"
60 
61  "function processNode( node ) {\n"
62  // Below, we determine if the current text node should be quote colored by keeping track of"
63  // linebreaks and whether this text node is the first one."
64  " var textContent = node.textContent;\n"
65  " var isTextNode = !textContent.length == 0 && !node.hasChildNodes();\n"
66  " if ( isTextNode ) {\n"
67  " if ( mIsFirstTextNodeInLine ) {\n"
68  " if ( textContent.charAt( 0 ) == '>' || textContent.charAt( 0 ) == '|' ) {\n"
69  " mIsQuotedLine = true;\n"
70  " currentQuoteLength = quoteLength( textContent ) - 1;\n"
71  " }\n"
72  " else {\n"
73  " mIsQuotedLine = false;\n"
74  " }\n"
75  " }\n"
76 
77  // All subsequent text nodes are not the first ones anymore"
78  " mIsFirstTextNodeInLine = false;\n"
79  " }\n"
80 
81  " var nodeName = node.nodeName.toLowerCase();\n"
82  " var lineBreakNodes = new Array();\n"
83  " lineBreakNodes[0] = \"br\"\n"
84  " lineBreakNodes[1] = \"p\"\n"
85  " lineBreakNodes[2] = \"div\"\n"
86  " lineBreakNodes[3] = \"ul\"\n"
87  " lineBreakNodes[4] = \"ol\"\n"
88  " lineBreakNodes[5] = \"li\"\n"
89 
90  " for( i = 0; i < lineBreakNodes.length; i++) {\n"
91  " if ( lineBreakNodes[i] == nodeName ) {\n"
92  " mIsFirstTextNodeInLine = true;\n"
93  " break;\n"
94  " }\n"
95  " }\n"
96 
97  " var returnNode = node;\n"
98  " var fontTagAdded = false;\n"
99  " if ( mIsQuotedLine && isTextNode ) {\n"
100 
101  // Ok, we are in a line that should be quoted, so create a font node for the color and replace"
102  // the current node with it."
103  " var font = node.ownerDocument.createElement( \"font\" );\n"
104  " font.setAttribute( \"color\", mQuoteColors[ currentQuoteLength ] );\n"
105  " node.parentNode.replaceChild( font, node );\n"
106  " font.appendChild( node );\n"
107  " returnNode = font;\n"
108  " fontTagAdded = true;\n"
109  " }\n"
110 
111  // Process all child nodes, but only if we are didn't add those child nodes itself, as otherwise"
112  // we'll go into an infinite recursion."
113  " if ( !fontTagAdded ) {\n"
114  " var childNode = node.firstChild;\n"
115  " while ( childNode ) {\n"
116  " childNode = processNode( childNode );\n"
117  " childNode = childNode.nextSibling;\n"
118  " }\n"
119  " }\n"
120  " return returnNode;\n"
121  "}\n"
122 
123  "function quoteLength( line )\n"
124  "{\n"
125  " line = line.replace( / /g, \"\" ).replace( '|', '>' );\n"
126  " if ( line.substr( 0, 3 ) == \">>>\" ) return 3;\n"
127  " if ( line.substr( 0, 2 ) == \">>\" ) return 2;\n"
128  " if ( line.substr( 0, 1 ) == '>' ) return 1;\n"
129  " return 0;\n"
130  "}\n"));
131 
132  page.settings()->setAttribute( QWebSettings::JavascriptEnabled, true );
133  frame->evaluateJavaScript( script );
134  page.settings()->setAttribute( QWebSettings::JavascriptEnabled, false );
135 
136  const QWebElement body = frame->documentElement().findFirst(QLatin1String("body"));
137  const QWebElement header = frame->documentElement().findFirst(QLatin1String("head"));
138 
139  extraHead = header.toInnerXml();
140  return body.toInnerXml();
141 #else
142  return htmlSource;
143 #endif
144 }
145 
146 void HTMLQuoteColorer::setQuoteColor( unsigned int level, const QColor& color )
147 {
148  if ( level < 3 )
149  mQuoteColors[level] = color;
150 }
151 
MessageViewer::HTMLQuoteColorer::process
QString process(const QString &htmlSource, QString &extraHead)
Do the work and add nice colors to the HTML.
Definition: htmlquotecolorer.cpp:37
MessageViewer::HTMLQuoteColorer::setQuoteColor
void setQuoteColor(unsigned int level, const QColor &color)
Sets the quote color of the specific leve.
Definition: htmlquotecolorer.cpp:146
MessageViewer::HTMLQuoteColorer::HTMLQuoteColorer
HTMLQuoteColorer()
Definition: htmlquotecolorer.cpp:33
htmlquotecolorer.h
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:55:57 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

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