1 /* -*- mode: C++; c-file-style: "gnu" -*-
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
32 #include <messagecore/globalsettings.h>
33 #include "csshelperbase.h"
35 #include <KColorScheme>
38 #include <KGlobalSettings>
40 #include <QApplication>
41 #include <QPaintDevice>
43 namespace MessageViewer
{
46 // some QColor manipulators that hide the ugly QColor API w.r.t. HSV:
47 inline QColor
darker( const QColor
& c
) {
49 c
.getHsv( &h
, &s
, &v
);
50 return QColor::fromHsv( h
, s
, v
*4/5 );
53 inline QColor
desaturate( const QColor
& c
) {
55 c
.getHsv( &h
, &s
, &v
);
56 return QColor::fromHsv( h
, s
/8, v
);
59 inline QColor
fixValue( const QColor
& c
, int newV
) {
61 c
.getHsv( &h
, &s
, &v
);
62 return QColor::fromHsv( h
, s
, newV
);
65 inline int getValueOf( const QColor
& c
) {
67 c
.getHsv( &h
, &s
, &v
);
72 CSSHelperBase::CSSHelperBase( const QPaintDevice
*pd
) :
73 mShrinkQuotes( false ),
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
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
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)
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
;
132 cPgpOk1F
= darker( cPgpOk1H
);
133 cPgpOk1B
= lightBG
? desaturate( cPgpOk1H
) : fixValue( cPgpOk1H
, vBG
);
135 if ( cPgpOk0H
== mBackgroundColor
) {
136 cPgpOk0F
= mBackgroundColor
;
137 cPgpOk0B
= mBackgroundColor
;
139 cPgpOk0F
= darker( cPgpOk0H
);
140 cPgpOk0B
= lightBG
? desaturate( cPgpOk0H
) : fixValue( cPgpOk0H
, vBG
);
142 if ( cPgpWarnH
== mBackgroundColor
) {
143 cPgpWarnF
= mBackgroundColor
;
144 cPgpWarnB
= mBackgroundColor
;
146 cPgpWarnF
= darker( cPgpWarnH
);
147 cPgpWarnB
= lightBG
? desaturate( cPgpWarnH
) : fixValue( cPgpWarnH
, vBG
);
149 if ( cPgpErrH
== mBackgroundColor
) {
150 cPgpErrF
= mBackgroundColor
;
151 cPgpErrB
= mBackgroundColor
;
153 cPgpErrF
= darker( cPgpErrH
);
154 cPgpErrB
= lightBG
? desaturate( cPgpErrH
) : fixValue( cPgpErrH
, vBG
);
156 if ( cPgpEncrH
== mBackgroundColor
) {
157 cPgpEncrF
= mBackgroundColor
;
158 cPgpEncrB
= mBackgroundColor
;
160 cPgpEncrF
= darker( cPgpEncrH
);
161 cPgpEncrB
= lightBG
? desaturate( cPgpEncrH
) : fixValue( cPgpEncrH
, vBG
);
165 QString
CSSHelperBase::cssDefinitions( bool fixed
) const {
167 commonCssDefinitions()
169 "@media screen {\n\n"
171 screenCssDefinitions( this, fixed
)
176 printCssDefinitions( fixed
)
181 QString
CSSHelperBase::htmlHead( bool /*fixed*/ ) const {
183 "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n"
184 "<html><head><title></title></head>\n"
188 QString
CSSHelperBase::quoteFontTag( int level
) const {
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
);
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();
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 */ );
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";
241 " font-family: \"%1\" ! important;\n"
242 " font-size: %2pt ! important;\n"
243 " color: #000000 ! important;\n"
244 " background-color: #ffffff ! important\n"
246 .arg( printFont
.family(),
247 QString::number( printFont
.pointSize() ) )
249 QString( "tr.textAtmH,\n"
250 "tr.signInProgressH,\n"
254 "tr.signOkKeyBadH,\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"
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"
279 " display:none ! important;\n"
283 " border: 2px solid #ffffff ! important;\n"
287 " font-size:0.8em ! important;\n"
288 " border:1px solid black ! important;\n"
289 " background-color:%2 ! important;\n"
292 "div.senderstatus{\n"
293 " text-align:center ! important;\n"
297 " display:none ! important;\n"
301 pal
.color( QPalette::Background
).name(),
302 pal
.color( QPalette::Foreground
).name() )
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" )
319 const QString bodyFontSize
= QString::number( pointsToPixel( helper
->mPaintDevice
, fontSize( fixed
) ) ) + "px" ;
320 const QPalette
& pal
= QApplication::palette();
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";
340 quoteCSS
+= " font-size: " + QString::fromLatin1( quoteFontSizes
[i
] )
341 + "% ! important;\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";
355 quoteCSS
+= " font-size: 70% ! important;\n";
361 " font-family: \"%1\" ! important;\n"
362 " font-size: %2 ! important;\n"
363 " color: %3 ! important;\n"
366 .arg( bodyFont( fixed
).family(),
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"
382 " color: %1 ! important;\n"
383 " text-decoration: none ! important;\n"
387 " color: white ! important;\n"
391 " color: black ! important;\n"
394 "table.textAtm { background-color: %2 ! important; }\n\n"
397 " background-color: %3 ! important;\n"
402 " background-color: %3 ! important;\n"
405 "table.signInProgress,\n"
407 " background-color: %3 ! important;\n"
410 "tr.signInProgressH,\n"
414 .arg( linkColor
, fgColor
, bgColor
, headerFont
)
416 QString( "table.encr {\n"
417 " background-color: %1 ! important;\n"
421 " background-color: %2 ! important;\n"
425 "tr.encrB { background-color: %4 ! important; }\n\n" )
426 .arg( cPgpEncrF
.name(),
431 QString( "table.signOkKeyOk {\n"
432 " background-color: %1 ! important;\n"
435 "tr.signOkKeyOkH {\n"
436 " background-color: %2 ! important;\n"
440 "tr.signOkKeyOkB { background-color: %4 ! important; }\n\n" )
441 .arg( cPgpOk1F
.name(),
446 QString( "table.signOkKeyBad {\n"
447 " background-color: %1 ! important;\n"
450 "tr.signOkKeyBadH {\n"
451 " background-color: %2 ! important;\n"
455 "tr.signOkKeyBadB { background-color: %4 ! important; }\n\n" )
456 .arg( cPgpOk0F
.name(),
461 QString( "table.signWarn {\n"
462 " background-color: %1 ! important;\n"
466 " background-color: %2 ! important;\n"
470 "tr.signWarnB { background-color: %4 ! important; }\n\n" )
471 .arg( cPgpWarnF
.name(),
476 QString( "table.signErr {\n"
477 " background-color: %1 ! important;\n"
481 " background-color: %2 ! important;\n"
485 "tr.signErrB { background-color: %4 ! important; }\n\n" )
486 .arg( cPgpErrF
.name(),
491 QString( "div.htmlWarn {\n"
492 " border: 2px solid %1 ! important;\n"
494 .arg( cHtmlWarning
.name() )
496 QString( "div.header {\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"
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"
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"
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"
536 "div.senderstatus{\n"
537 " text-align:center ! important;\n"
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() )
550 QString
CSSHelperBase::commonCssDefinitions() const {
553 " margin-bottom: 10pt ! important;\n"
557 " margin-top: 10pt ! important;\n"
558 " margin-bottom: 10pt ! important;\n"
564 " font-weight: normal ! important;\n"
567 "tr.signInProgressH,\n"
571 "tr.signOkKeyBadH,\n"
574 " font-weight: bold ! important;\n"
579 " padding: 3px ! important;\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"
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"
602 " margin: 0px 5% ! important;\n"
603 " padding: 10px ! important;\n"
604 " text-align: left ! important;\n"
607 "div.fancy.header > div {\n"
608 " font-weight: bold ! important;\n"
609 " padding: 4px ! important;\n"
612 "div.fancy.header table {\n"
613 " padding: 2px ! important;\n" // ### khtml bug: this is ignored
614 " text-align: left ! important\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"
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"
633 "span.pimsmileytext {\n"
634 " position: absolute;\n"
637 " visibility: hidden;\n"
640 "img.pimsmileyimg {\n"
643 "div.quotelevelmark {\n"
644 " position: absolute;\n"
645 " margin-left:-10px;\n"
651 void CSSHelperBase::setBodyFont( const QFont
& font
)
656 void CSSHelperBase::setPrintFont( const QFont
& 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