25 #ifndef KDEPIM_NO_WEBKIT
28 #include <QWebElement>
31 using namespace MessageViewer;
39 #ifndef KDEPIM_NO_WEBKIT
42 page.settings()->setAttribute( QWebSettings::JavascriptEnabled,
false );
43 page.settings()->setAttribute( QWebSettings::JavaEnabled,
false );
44 page.settings()->setAttribute( QWebSettings::PluginsEnabled,
false );
46 page.settings()->setAttribute( QWebSettings::AutoLoadImages,
false );
48 QWebFrame *frame = page.mainFrame();
49 frame->setHtml( htmlSource );
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"
59 "processNode( document.documentElement );\n"
61 "function processNode( node ) {\n"
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"
73 " mIsQuotedLine = false;\n"
78 " mIsFirstTextNodeInLine = false;\n"
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"
90 " for( i = 0; i < lineBreakNodes.length; i++) {\n"
91 " if ( lineBreakNodes[i] == nodeName ) {\n"
92 " mIsFirstTextNodeInLine = true;\n"
97 " var returnNode = node;\n"
98 " var fontTagAdded = false;\n"
99 " if ( mIsQuotedLine && isTextNode ) {\n"
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"
113 " if ( !fontTagAdded ) {\n"
114 " var childNode = node.firstChild;\n"
115 " while ( childNode ) {\n"
116 " childNode = processNode( childNode );\n"
117 " childNode = childNode.nextSibling;\n"
120 " return returnNode;\n"
123 "function quoteLength( line )\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"
132 page.settings()->setAttribute( QWebSettings::JavascriptEnabled,
true );
133 frame->evaluateJavaScript( script );
134 page.settings()->setAttribute( QWebSettings::JavascriptEnabled,
false );
136 const QWebElement body = frame->documentElement().findFirst(QLatin1String(
"body"));
137 const QWebElement header = frame->documentElement().findFirst(QLatin1String(
"head"));
139 extraHead = header.toInnerXml();
140 return body.toInnerXml();
149 mQuoteColors[level] = color;
QString process(const QString &htmlSource, QString &extraHead)
Do the work and add nice colors to the HTML.
void setQuoteColor(unsigned int level, const QColor &color)
Sets the quote color of the specific leve.