Framework for looking up contacts directly in nepomuk in addition to going through...
[kdepim.git] / messageviewer / csshelperbase.cpp
blob21937c2f5463afaae8f86675f9f4ee0d0bbfc520
1 /* -*- mode: C++; c-file-style: "gnu" -*-
2 csshelper.cpp
4 This file is part of KMail, the KDE mail client.
5 Copyright (c) 2003 Marc Mutz <mutz@kde.org>
7 KMail is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License, version 2, as
9 published by the Free Software Foundation.
11 KMail is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 In addition, as a special exception, the copyright holders give
21 permission to link the code of this program with any edition of
22 the Qt library by Trolltech AS, Norway (or with modified versions
23 of Qt that use the same license as Qt), and distribute linked
24 combinations including the two. You must obey the GNU General
25 Public License in all respects for all of the code used other than
26 Qt. If you modify this file, you may extend this exception to
27 your version of the file, but you are not obligated to do so. If
28 you do not wish to do so, delete this exception statement from
29 your version.
32 #include <messagecore/globalsettings.h>
33 #include "csshelperbase.h"
35 #include <KColorScheme>
36 #include <KDebug>
37 #include <KGlobal>
38 #include <KGlobalSettings>
40 #include <QApplication>
41 #include <QPaintDevice>
43 namespace MessageViewer {
45 namespace {
46 // some QColor manipulators that hide the ugly QColor API w.r.t. HSV:
47 inline QColor darker( const QColor & c ) {
48 int h, s, v;
49 c.getHsv( &h, &s, &v );
50 return QColor::fromHsv( h, s, v*4/5 );
53 inline QColor desaturate( const QColor & c ) {
54 int h, s, v;
55 c.getHsv( &h, &s, &v );
56 return QColor::fromHsv( h, s/8, v );
59 inline QColor fixValue( const QColor & c, int newV ) {
60 int h, s, v;
61 c.getHsv( &h, &s, &v );
62 return QColor::fromHsv( h, s, newV );
65 inline int getValueOf( const QColor & c ) {
66 int h, s, v;
67 c.getHsv( &h, &s, &v );
68 return v;
72 CSSHelperBase::CSSHelperBase( const QPaintDevice *pd ) :
73 mShrinkQuotes( false ),
74 mPaintDevice( pd )
76 // initialize with defaults - should match the corresponding application defaults
77 mForegroundColor = QApplication::palette().color( QPalette::Text );
78 mLinkColor = KColorScheme( QPalette::Active, KColorScheme::View ).foreground( KColorScheme::LinkText ).color();
79 mVisitedLinkColor = KColorScheme( QPalette::Active, KColorScheme::View ).foreground( KColorScheme::VisitedText ).color();
80 mBackgroundColor = QApplication::palette().color( QPalette::Base );
81 cHtmlWarning = QColor( 0xFF, 0x40, 0x40 ); // warning frame color: light red
83 cPgpEncrH = QColor( 0x00, 0x80, 0xFF ); // light blue
84 cPgpOk1H = QColor( 0x40, 0xFF, 0x40 ); // light green
85 cPgpOk0H = QColor( 0xFF, 0xFF, 0x40 ); // light yellow
86 cPgpWarnH = QColor( 0xFF, 0xFF, 0x40 ); // light yellow
87 cPgpErrH = Qt::red;
89 if(MessageCore::GlobalSettings::self()->useDefaultColors()) {
90 for ( int i = 0 ; i < 3 ; ++i ) {
91 mQuoteColor[i] = QColor( 0x00, 0x80 - i * 0x10, 0x00 ); // shades of green
93 } else {
94 mQuoteColor[0] = MessageCore::GlobalSettings::self()->quotedText1();
95 mQuoteColor[1] = MessageCore::GlobalSettings::self()->quotedText2();
96 mQuoteColor[2] = MessageCore::GlobalSettings::self()->quotedText3();
98 mRecycleQuoteColors = false;
100 QFont defaultFont = KGlobalSettings::generalFont();
101 QFont defaultFixedFont = KGlobalSettings::fixedFont();
102 mBodyFont = MessageCore::GlobalSettings::self()->useDefaultFonts() ? defaultFont: MessageCore::GlobalSettings::self()->bodyFont();
103 mPrintFont = MessageCore::GlobalSettings::self()->useDefaultFonts() ? defaultFont : MessageCore::GlobalSettings::self()->printFont();
104 mFixedFont = mFixedPrintFont = defaultFixedFont;
105 defaultFont.setItalic( true );
106 for ( int i = 0 ; i < 3 ; ++i )
107 mQuoteFont[i] = defaultFont;
109 mBackingPixmapOn = false;
111 recalculatePGPColors();
114 void CSSHelperBase::recalculatePGPColors() {
115 // determine the frame and body color for PGP messages from the header color
116 // if the header color equals the background color then the other colors are
117 // also set to the background color (-> old style PGP message viewing)
118 // else
119 // the brightness of the frame is set to 4/5 of the brightness of the header
120 // and in case of a light background color
121 // the saturation of the body is set to 1/8 of the saturation of the header
122 // while in case of a dark background color
123 // the value of the body is set to the value of the background color
125 // Check whether the user uses a light color scheme
126 const int vBG = getValueOf( mBackgroundColor );
127 const bool lightBG = vBG >= 128;
128 if ( cPgpOk1H == mBackgroundColor ) {
129 cPgpOk1F = mBackgroundColor;
130 cPgpOk1B = mBackgroundColor;
131 } else {
132 cPgpOk1F= darker( cPgpOk1H );
133 cPgpOk1B = lightBG ? desaturate( cPgpOk1H ) : fixValue( cPgpOk1H, vBG );
135 if ( cPgpOk0H == mBackgroundColor ) {
136 cPgpOk0F = mBackgroundColor;
137 cPgpOk0B = mBackgroundColor;
138 } else {
139 cPgpOk0F = darker( cPgpOk0H );
140 cPgpOk0B = lightBG ? desaturate( cPgpOk0H ) : fixValue( cPgpOk0H, vBG );
142 if ( cPgpWarnH == mBackgroundColor ) {
143 cPgpWarnF = mBackgroundColor;
144 cPgpWarnB = mBackgroundColor;
145 } else {
146 cPgpWarnF = darker( cPgpWarnH );
147 cPgpWarnB = lightBG ? desaturate( cPgpWarnH ) : fixValue( cPgpWarnH, vBG );
149 if ( cPgpErrH == mBackgroundColor ) {
150 cPgpErrF = mBackgroundColor;
151 cPgpErrB = mBackgroundColor;
152 } else {
153 cPgpErrF = darker( cPgpErrH );
154 cPgpErrB = lightBG ? desaturate( cPgpErrH ) : fixValue( cPgpErrH, vBG );
156 if ( cPgpEncrH == mBackgroundColor ) {
157 cPgpEncrF = mBackgroundColor;
158 cPgpEncrB = mBackgroundColor;
159 } else {
160 cPgpEncrF = darker( cPgpEncrH );
161 cPgpEncrB = lightBG ? desaturate( cPgpEncrH ) : fixValue( cPgpEncrH, vBG );
165 QString CSSHelperBase::cssDefinitions( bool fixed ) const {
166 return
167 commonCssDefinitions()
169 "@media screen {\n\n"
171 screenCssDefinitions( this, fixed )
173 "}\n"
174 "@media print {\n\n"
176 printCssDefinitions( fixed )
178 "\n";
181 QString CSSHelperBase::htmlHead( bool /*fixed*/ ) const {
182 return
183 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n"
184 "<html><head><title></title></head>\n"
185 "<body>\n";
188 QString CSSHelperBase::quoteFontTag( int level ) const {
189 if ( level < 0 )
190 level = 0;
191 static const int numQuoteLevels = sizeof mQuoteFont / sizeof *mQuoteFont;
192 const int effectiveLevel = mRecycleQuoteColors
193 ? level % numQuoteLevels + 1
194 : qMin( level + 1, numQuoteLevels ) ;
195 if ( level >= numQuoteLevels )
196 return QString( "<div class=\"deepquotelevel%1\">" ).arg( effectiveLevel );
197 else
198 return QString( "<div class=\"quotelevel%1\">" ).arg( effectiveLevel );
201 QString CSSHelperBase::nonQuotedFontTag() const {
202 return "<div class=\"noquote\">";
205 QFont CSSHelperBase::bodyFont( bool fixed, bool print ) const {
206 return fixed ? ( print ? mFixedPrintFont : mFixedFont )
207 : ( print ? mPrintFont : mBodyFont );
210 int CSSHelperBase::fontSize( bool fixed, bool print ) const {
211 return bodyFont( fixed, print ).pointSize();
215 namespace {
216 int pointsToPixel( const QPaintDevice *pd, int pointSize ) {
217 return ( pointSize * pd->logicalDpiY() + 36 ) / 72 ;
221 static const char * const quoteFontSizes[] = { "85", "80", "75" };
223 QString CSSHelperBase::printCssDefinitions( bool fixed ) const {
224 const QString headerFont = QString( " font-family: \"%1\" ! important;\n"
225 " font-size: %2pt ! important;\n" )
226 .arg( mPrintFont.family() )
227 .arg( mPrintFont.pointSize() );
228 const QPalette &pal = QApplication::palette();
230 const QFont printFont = bodyFont( fixed, true /* print */ );
231 QString quoteCSS;
232 if ( printFont.italic() )
233 quoteCSS += " font-style: italic ! important;\n";
234 if ( printFont.bold() )
235 quoteCSS += " font-weight: bold ! important;\n";
236 if ( !quoteCSS.isEmpty() )
237 quoteCSS = "div.noquote {\n" + quoteCSS + "}\n\n";
239 return
240 QString( "body {\n"
241 " font-family: \"%1\" ! important;\n"
242 " font-size: %2pt ! important;\n"
243 " color: #000000 ! important;\n"
244 " background-color: #ffffff ! important\n"
245 "}\n\n" )
246 .arg( printFont.family(),
247 QString::number( printFont.pointSize() ) )
249 QString( "tr.textAtmH,\n"
250 "tr.signInProgressH,\n"
251 "tr.rfc822H,\n"
252 "tr.encrH,\n"
253 "tr.signOkKeyOkH,\n"
254 "tr.signOkKeyBadH,\n"
255 "tr.signWarnH,\n"
256 "tr.signErrH,\n"
257 "div.header {\n"
258 "%1"
259 "}\n\n"
261 "div.fancy.header > div {\n"
262 " background-color: %2 ! important;\n"
263 " color: %3 ! important;\n"
264 " padding: 4px ! important;\n"
265 " border: solid %3 1px ! important;\n"
266 "}\n\n"
268 "div.fancy.header > div a[href] { color: %3 ! important; }\n\n"
270 "div.fancy.header > table.outer{\n"
271 " background-color: %2 ! important;\n"
272 " color: %3 ! important;\n"
273 " border-bottom: solid %3 1px ! important;\n"
274 " border-left: solid %3 1px ! important;\n"
275 " border-right: solid %3 1px ! important;\n"
276 "}\n\n"
278 "div.spamheader {\n"
279 " display:none ! important;\n"
280 "}\n\n"
282 "div.htmlWarn {\n"
283 " border: 2px solid #ffffff ! important;\n"
284 "}\n\n"
286 "div.senderpic{\n"
287 " font-size:0.8em ! important;\n"
288 " border:1px solid black ! important;\n"
289 " background-color:%2 ! important;\n"
290 "}\n\n"
292 "div.senderstatus{\n"
293 " text-align:center ! important;\n"
294 "}\n\n"
296 "div.noprint {\n"
297 " display:none ! important;\n"
298 "}\n\n"
300 .arg( headerFont,
301 pal.color( QPalette::Background ).name(),
302 pal.color( QPalette::Foreground ).name() )
303 + quoteCSS;
306 QString CSSHelperBase::screenCssDefinitions( const CSSHelperBase * helper, bool fixed ) const {
307 const QString fgColor = mForegroundColor.name();
308 const QString bgColor = mBackgroundColor.name();
309 const QString linkColor = mLinkColor.name();
310 const QString headerFont = QString(" font-family: \"%1\" ! important;\n"
311 " font-size: %2px ! important;\n")
312 .arg( mBodyFont.family() )
313 .arg( pointsToPixel( helper->mPaintDevice, mBodyFont.pointSize() ) );
314 const QString background = ( mBackingPixmapOn
315 ? QString( " background-image:url(file:///%1) ! important;\n" )
316 .arg( mBackingPixmapStr )
317 : QString( " background-color: %1 ! important;\n" )
318 .arg( bgColor ) );
319 const QString bodyFontSize = QString::number( pointsToPixel( helper->mPaintDevice, fontSize( fixed ) ) ) + "px" ;
320 const QPalette & pal = QApplication::palette();
322 QString quoteCSS;
323 if ( bodyFont( fixed ).italic() )
324 quoteCSS += " font-style: italic ! important;\n";
325 if ( bodyFont( fixed ).bold() )
326 quoteCSS += " font-weight: bold ! important;\n";
327 if ( !quoteCSS.isEmpty() )
328 quoteCSS = "div.noquote {\n" + quoteCSS + "}\n\n";
330 // CSS definitions for quote levels 1-3
331 for ( int i = 0 ; i < 3 ; ++i ) {
332 quoteCSS += QString( "div.quotelevel%1 {\n"
333 " color: %2 ! important;\n" )
334 .arg( QString::number(i+1), mQuoteColor[i].name() );
335 if ( mQuoteFont[i].italic() )
336 quoteCSS += " font-style: italic ! important;\n";
337 if ( mQuoteFont[i].bold() )
338 quoteCSS += " font-weight: bold ! important;\n";
339 if ( mShrinkQuotes )
340 quoteCSS += " font-size: " + QString::fromLatin1( quoteFontSizes[i] )
341 + "% ! important;\n";
342 quoteCSS += "}\n\n";
345 // CSS definitions for quote levels 4+
346 for ( int i = 0 ; i < 3 ; ++i ) {
347 quoteCSS += QString( "div.deepquotelevel%1 {\n"
348 " color: %2 ! important;\n" )
349 .arg( QString::number(i+1), mQuoteColor[i].name() );
350 if ( mQuoteFont[i].italic() )
351 quoteCSS += " font-style: italic ! important;\n";
352 if ( mQuoteFont[i].bold() )
353 quoteCSS += " font-weight: bold ! important;\n";
354 if ( mShrinkQuotes )
355 quoteCSS += " font-size: 70% ! important;\n";
356 quoteCSS += "}\n\n";
359 return
360 QString( "body {\n"
361 " font-family: \"%1\" ! important;\n"
362 " font-size: %2 ! important;\n"
363 " color: %3 ! important;\n"
364 "%4"
365 "}\n\n" )
366 .arg( bodyFont( fixed ).family(),
367 bodyFontSize,
368 fgColor,
369 background )
371 /* This shouldn't be necessary because font properties are inherited
372 automatically and causes wrong font settings with QTextBrowser
373 because it doesn't understand the inherit statement
374 QString::fromLatin1( "table {\n"
375 " font-family: inherit ! important;\n"
376 " font-size: inherit ! important;\n"
377 " font-weight: inherit ! important;\n"
378 "}\n\n" )
381 QString( "a {\n"
382 " color: %1 ! important;\n"
383 " text-decoration: none ! important;\n"
384 "}\n\n"
386 "a.white {\n"
387 " color: white ! important;\n"
388 "}\n\n"
390 "a.black {\n"
391 " color: black ! important;\n"
392 "}\n\n"
394 "table.textAtm { background-color: %2 ! important; }\n\n"
396 "tr.textAtmH {\n"
397 " background-color: %3 ! important;\n"
398 "%4"
399 "}\n\n"
401 "tr.textAtmB {\n"
402 " background-color: %3 ! important;\n"
403 "}\n\n"
405 "table.signInProgress,\n"
406 "table.rfc822 {\n"
407 " background-color: %3 ! important;\n"
408 "}\n\n"
410 "tr.signInProgressH,\n"
411 "tr.rfc822H {\n"
412 "%4"
413 "}\n\n" )
414 .arg( linkColor, fgColor, bgColor, headerFont )
416 QString( "table.encr {\n"
417 " background-color: %1 ! important;\n"
418 "}\n\n"
420 "tr.encrH {\n"
421 " background-color: %2 ! important;\n"
422 "%3"
423 "}\n\n"
425 "tr.encrB { background-color: %4 ! important; }\n\n" )
426 .arg( cPgpEncrF.name(),
427 cPgpEncrH.name(),
428 headerFont,
429 cPgpEncrB.name() )
431 QString( "table.signOkKeyOk {\n"
432 " background-color: %1 ! important;\n"
433 "}\n\n"
435 "tr.signOkKeyOkH {\n"
436 " background-color: %2 ! important;\n"
437 "%3"
438 "}\n\n"
440 "tr.signOkKeyOkB { background-color: %4 ! important; }\n\n" )
441 .arg( cPgpOk1F.name(),
442 cPgpOk1H.name(),
443 headerFont,
444 cPgpOk1B.name() )
446 QString( "table.signOkKeyBad {\n"
447 " background-color: %1 ! important;\n"
448 "}\n\n"
450 "tr.signOkKeyBadH {\n"
451 " background-color: %2 ! important;\n"
452 "%3"
453 "}\n\n"
455 "tr.signOkKeyBadB { background-color: %4 ! important; }\n\n" )
456 .arg( cPgpOk0F.name(),
457 cPgpOk0H.name(),
458 headerFont,
459 cPgpOk0B.name() )
461 QString( "table.signWarn {\n"
462 " background-color: %1 ! important;\n"
463 "}\n\n"
465 "tr.signWarnH {\n"
466 " background-color: %2 ! important;\n"
467 "%3"
468 "}\n\n"
470 "tr.signWarnB { background-color: %4 ! important; }\n\n" )
471 .arg( cPgpWarnF.name(),
472 cPgpWarnH.name(),
473 headerFont,
474 cPgpWarnB.name() )
476 QString( "table.signErr {\n"
477 " background-color: %1 ! important;\n"
478 "}\n\n"
480 "tr.signErrH {\n"
481 " background-color: %2 ! important;\n"
482 "%3"
483 "}\n\n"
485 "tr.signErrB { background-color: %4 ! important; }\n\n" )
486 .arg( cPgpErrF.name(),
487 cPgpErrH.name(),
488 headerFont,
489 cPgpErrB.name() )
491 QString( "div.htmlWarn {\n"
492 " border: 2px solid %1 ! important;\n"
493 "}\n\n" )
494 .arg( cHtmlWarning.name() )
496 QString( "div.header {\n"
497 "%1"
498 "}\n\n"
500 "div.fancy.header > div {\n"
501 " background-color: %2 ! important;\n"
502 " color: %3 ! important;\n"
503 " border: solid %4 1px ! important;\n"
504 "}\n\n"
506 "div.fancy.header > div a[href] { color: %3 ! important; }\n\n"
508 "div.fancy.header > div a[href]:hover { text-decoration: underline ! important; }\n\n"
510 "div.fancy.header > div.spamheader {\n"
511 " background-color: #cdcdcd ! important;\n"
512 " border-top: 0px ! important;\n"
513 " padding: 3px ! important;\n"
514 " color: black ! important;\n"
515 " font-weight: bold ! important;\n"
516 " font-size: smaller ! important;\n"
517 "}\n\n"
519 "div.fancy.header > table.outer {\n"
520 " background-color: %5 ! important;\n"
521 " color: %4 ! important;\n"
522 " border-bottom: solid %4 1px ! important;\n"
523 " border-left: solid %4 1px ! important;\n"
524 " border-right: solid %4 1px ! important;\n"
525 "}\n\n"
527 "div.senderpic{\n"
528 " padding: 0px ! important;\n"
529 " font-size:0.8em ! important;\n"
530 " border:1px solid %6 ! important;\n"
531 // FIXME: InfoBackground crashes KHTML
532 //" background-color:InfoBackground ! important;\n"
533 " background-color:%5 ! important;\n"
534 "}\n\n"
536 "div.senderstatus{\n"
537 " text-align:center ! important;\n"
538 "}\n\n"
541 .arg( headerFont )
542 .arg( pal.color( QPalette::Highlight ).name(),
543 pal.color( QPalette::HighlightedText ).name(),
544 pal.color( QPalette::Foreground ).name(),
545 pal.color( QPalette::Background ).name() )
546 .arg( pal.color( QPalette::Mid ).name() )
547 + quoteCSS;
550 QString CSSHelperBase::commonCssDefinitions() const {
551 return
552 "div.header {\n"
553 " margin-bottom: 10pt ! important;\n"
554 "}\n\n"
556 "table.textAtm {\n"
557 " margin-top: 10pt ! important;\n"
558 " margin-bottom: 10pt ! important;\n"
559 "}\n\n"
561 "tr.textAtmH,\n"
562 "tr.textAtmB,\n"
563 "tr.rfc822B {\n"
564 " font-weight: normal ! important;\n"
565 "}\n\n"
567 "tr.signInProgressH,\n"
568 "tr.rfc822H,\n"
569 "tr.encrH,\n"
570 "tr.signOkKeyOkH,\n"
571 "tr.signOkKeyBadH,\n"
572 "tr.signWarnH,\n"
573 "tr.signErrH {\n"
574 " font-weight: bold ! important;\n"
575 "}\n\n"
577 "tr.textAtmH td,\n"
578 "tr.textAtmB td {\n"
579 " padding: 3px ! important;\n"
580 "}\n\n"
582 "table.rfc822 {\n"
583 " width: 100% ! important;\n"
584 " border: solid 1px black ! important;\n"
585 " margin-top: 10pt ! important;\n"
586 " margin-bottom: 10pt ! important;\n"
587 "}\n\n"
589 "table.textAtm,\n"
590 "table.encr,\n"
591 "table.signWarn,\n"
592 "table.signErr,\n"
593 "table.signOkKeyBad,\n"
594 "table.signOkKeyOk,\n"
595 "table.signInProgress,\n"
596 "div.fancy.header table {\n"
597 " width: 100% ! important;\n"
598 " border-width: 0px ! important;\n"
599 "}\n\n"
601 "div.htmlWarn {\n"
602 " margin: 0px 5% ! important;\n"
603 " padding: 10px ! important;\n"
604 " text-align: left ! important;\n"
605 "}\n\n"
607 "div.fancy.header > div {\n"
608 " font-weight: bold ! important;\n"
609 " padding: 4px ! important;\n"
610 "}\n\n"
612 "div.fancy.header table {\n"
613 " padding: 2px ! important;\n" // ### khtml bug: this is ignored
614 " text-align: left ! important\n"
615 "}\n\n"
617 "div.fancy.header table th {\n"
618 " padding: 0px ! important;\n"
619 " white-space: nowrap ! important;\n"
620 " border-spacing: 0px ! important;\n"
621 " text-align: left ! important;\n"
622 " vertical-align: top ! important;\n"
623 "}\n\n"
625 "div.fancy.header table td {\n"
626 " padding: 0px ! important;\n"
627 " border-spacing: 0px ! important;\n"
628 " text-align: left ! important;\n"
629 " vertical-align: top ! important;\n"
630 " width: 100% ! important;\n"
631 "}\n\n"
633 "span.pimsmileytext {\n"
634 " position: absolute;\n"
635 " top: 0px;\n"
636 " left: 0px;\n"
637 " visibility: hidden;\n"
638 "}\n\n"
640 "img.pimsmileyimg {\n"
641 "}\n\n"
643 "div.quotelevelmark {\n"
644 " position: absolute;\n"
645 " margin-left:-10px;\n"
646 "}\n\n"
651 void CSSHelperBase::setBodyFont( const QFont& font )
653 mBodyFont = font;
656 void CSSHelperBase::setPrintFont( const QFont& font )
658 mPrintFont = font;
661 QColor CSSHelperBase::quoteColor( int level )
663 const int actualLevel = qMin( qMax( level, 0 ), 2 );
664 return mQuoteColor[actualLevel];
667 QColor CSSHelperBase::pgpWarnColor() const
669 return cPgpWarnH;