summaryrefslogtreecommitdiffstats
path: root/amarok/src/htmlview.cpp
blob: c7c4c97e112be84b54236141a87f37b88a8b9b1c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
// (c) 2005 Christian Muehlhaeuser <chris@chris.de>
// (c) 2006 Seb Ruiz <me@sebruiz.net>
// License: GNU General Public License V2


#include "amarok.h"
#include "amarokconfig.h"
#include "app.h"
#include "contextbrowser.h"
#include "htmlview.h"
#include "playlist.h"      //appendMedia()

#include <tqclipboard.h>
#include <tqfile.h> // External CSS opening
#include <tqimage.h> // External CSS opening

#include <tdeapplication.h> //kapp
#include <tdeactioncollection.h>
#include <tdeglobal.h> //kapp
#include <kimageeffect.h> // gradient background image
#include <tdepopupmenu.h>
#include <kstandarddirs.h> //locate file
#include <tdetempfile.h>

KTempFile *HTMLView::m_bgGradientImage = 0;
KTempFile *HTMLView::m_headerGradientImage = 0;
KTempFile *HTMLView::m_shadowGradientImage = 0;
int HTMLView::m_instances = 0;

HTMLView::HTMLView( TQWidget *parentWidget, const char *widgetname, const bool DNDEnabled, const bool JScriptEnabled )
        : TDEHTMLPart( parentWidget, widgetname )
{
    m_instances++;
    setJavaEnabled( false );
    setPluginsEnabled( false );

    setDNDEnabled( DNDEnabled );
    setJScriptEnabled( JScriptEnabled );

    TDEActionCollection* ac = actionCollection();
    ac->setAutoConnectShortcuts( true );
    m_copy = KStdAction::copy( this, TQT_SLOT( copyText() ), ac, "htmlview_copy" );
    m_selectAll = KStdAction::selectAll( this, TQT_SLOT( selectAll() ), ac, "htmlview_select_all" );
    {
        TDEPopupMenu m;
        m_copy->plug( &m );
        m_selectAll->plug( &m );

        m_copy->unplug( &m );
        m_selectAll->unplug( &m );
    }

    connect( this, TQT_SIGNAL( selectionChanged() ), TQT_SLOT( enableCopyAction() ) );
    enableCopyAction();
}


HTMLView::~HTMLView()
{
    m_instances--;
    if ( m_instances < 1 ) {
        delete m_bgGradientImage;
        delete m_headerGradientImage;
        delete m_shadowGradientImage;
    }
}

void
HTMLView::enableCopyAction()
{
    m_copy->setEnabled( hasSelection() );
}

void
HTMLView::selectAll()
{
    TDEHTMLPart::selectAll();
}

void
HTMLView::copyText()
{
    TQString text = selectedText();

    // Copy both to clipboard and X11-selection
    TQApplication::clipboard()->setText( text, TQClipboard::Clipboard );
    TQApplication::clipboard()->setText( text, TQClipboard::Selection );
}

void HTMLView::paletteChange() {
    delete m_bgGradientImage;
    delete m_headerGradientImage;
    delete m_shadowGradientImage;
    m_bgGradientImage = m_headerGradientImage = m_shadowGradientImage = 0;
}

TQString
HTMLView::loadStyleSheet()
{
    TQString themeName = AmarokConfig::contextBrowserStyleSheet().latin1();
    const TQString file = kapp->dirs()->findResource( "data","amarok/themes/" + themeName + "/stylesheet.css" );

    TQString styleSheet;
    if ( themeName != "Default" && TQFile::exists( file ) )
    {
        const TQString CSSLocation = kapp->dirs()->findResource( "data","amarok/themes/" + themeName + "/stylesheet.css" );
        TQFile ExternalCSS( CSSLocation );
        if ( !ExternalCSS.open( IO_ReadOnly ) )
            return TQString(); //FIXME: should actually return the default style sheet, then

        const TQString pxSize = TQString::number( ContextBrowser::instance()->fontMetrics().height() - 4 );
        const TQString fontFamily = AmarokConfig::useCustomFonts() ?
                                        AmarokConfig::contextBrowserFont().family() :
                                        TQApplication::font().family();
        const TQString text   = ContextBrowser::instance()->colorGroup().text().name();
        const TQString link   = ContextBrowser::instance()->colorGroup().link().name();
        const TQString fg     = ContextBrowser::instance()->colorGroup().highlightedText().name();
        const TQString bg     = ContextBrowser::instance()->colorGroup().highlight().name();
        const TQString base   = ContextBrowser::instance()->colorGroup().base().name();
        const TQColor bgColor = ContextBrowser::instance()->colorGroup().highlight();
        TQColor gradientColor = bgColor;

        //we have to set the color for body due to a TDEHTML bug
        //TDEHTML sets the base color but not the text color
        styleSheet = TQString( "body { margin: 8px; font-size: %1px; color: %2; background-color: %3; font-family: %4; }" )
                .arg( pxSize )
                .arg( text )
                .arg( AmarokConfig::schemeAmarok() ? fg : TQString(gradientColor.name()) )
                .arg( fontFamily );

        TQTextStream eCSSts( &ExternalCSS );
        TQString tmpCSS = eCSSts.read();
        ExternalCSS.close();

        tmpCSS.replace( "./", KURL::fromPathOrURL( CSSLocation ).directory( false ) );
        tmpCSS.replace( "AMAROK_FONTSIZE-2", pxSize );
        tmpCSS.replace( "AMAROK_FONTSIZE", pxSize );
        tmpCSS.replace( "AMAROK_FONTSIZE+2", pxSize );
        tmpCSS.replace( "AMAROK_FONTFAMILY", fontFamily );
        tmpCSS.replace( "AMAROK_TEXTCOLOR", text );
        tmpCSS.replace( "AMAROK_LINKCOLOR", link );
        tmpCSS.replace( "AMAROK_BGCOLOR", bg );
        tmpCSS.replace( "AMAROK_FGCOLOR", fg );
        tmpCSS.replace( "AMAROK_BASECOLOR", base );
        tmpCSS.replace( "AMAROK_DARKBASECOLOR", ContextBrowser::instance()->colorGroup().base().dark( 120 ).name() );
        tmpCSS.replace( "AMAROK_GRADIENTCOLOR", gradientColor.name() );

        styleSheet += tmpCSS;
    }
    else
    {
        int pxSize = ContextBrowser::instance()->fontMetrics().height() - 4;
        const TQString fontFamily = AmarokConfig::useCustomFonts() ? AmarokConfig::contextBrowserFont().family() : TQApplication::font().family();
        const TQString text = ContextBrowser::instance()->colorGroup().text().name();
        const TQString link = ContextBrowser::instance()->colorGroup().link().name();
        const TQString fg   = ContextBrowser::instance()->colorGroup().highlightedText().name();
        const TQString bg   = ContextBrowser::instance()->colorGroup().highlight().name();
        const TQColor baseColor = ContextBrowser::instance()->colorGroup().base();
        const TQColor bgColor = ContextBrowser::instance()->colorGroup().highlight();
        const TQColor gradientColor = bgColor;

        if ( !m_bgGradientImage ) {
            m_bgGradientImage = new KTempFile( locateLocal( "tmp", "gradient" ), ".png", 0600 );
            TQImage image = KImageEffect::gradient( TQSize( 600, 1 ), gradientColor, gradientColor.light( 130 ), KImageEffect::PipeCrossGradient );
            image.save( m_bgGradientImage->file(), "PNG" );
            m_bgGradientImage->close();
        }

        if ( !m_headerGradientImage ) {
            m_headerGradientImage = new KTempFile( locateLocal( "tmp", "gradient_header" ), ".png", 0600 );
            TQImage imageH = KImageEffect::unbalancedGradient( TQSize( 1, 10 ), bgColor, gradientColor.light( 130 ), KImageEffect::VerticalGradient, 100, -100 );
            imageH.copy( 0, 1, 1, 9 ).save( m_headerGradientImage->file(), "PNG" );
            m_headerGradientImage->close();
        }

        if ( !m_shadowGradientImage ) {
            m_shadowGradientImage = new KTempFile( locateLocal( "tmp", "gradient_shadow" ), ".png", 0600 );
            TQImage imageS = KImageEffect::unbalancedGradient( TQSize( 1, 10 ), baseColor, TQt::gray, KImageEffect::VerticalGradient, 100, -100 );
            imageS.save( m_shadowGradientImage->file(), "PNG" );
            m_shadowGradientImage->close();
        }

        //unlink the files for us on deletion
        m_bgGradientImage->setAutoDelete( true );
        m_headerGradientImage->setAutoDelete( true );
        m_shadowGradientImage->setAutoDelete( true );

        //we have to set the color for body due to a TDEHTML bug
        //TDEHTML sets the base color but not the text color
        styleSheet = TQString( "body { margin: 4px; font-size: %1px; color: %2; background-color: %3; background-image: url( %4 ); background-repeat: repeat; font-family: %5; }" )
                .arg( pxSize )
                .arg( text )
                .arg( AmarokConfig::schemeAmarok() ? fg : TQString(gradientColor.name()) )
                .arg( m_bgGradientImage->name() )
                .arg( fontFamily );

        //text attributes
        styleSheet += TQString( "h1 { font-size: %1px; }" ).arg( pxSize + 8 );
        styleSheet += TQString( "h2 { font-size: %1px; }" ).arg( pxSize + 6 );
        styleSheet += TQString( "h3 { font-size: %1px; }" ).arg( pxSize + 4 );
        styleSheet += TQString( "h4 { font-size: %1px; }" ).arg( pxSize + 3 );
        styleSheet += TQString( "h5 { font-size: %1px; }" ).arg( pxSize + 2 );
        styleSheet += TQString( "h6 { font-size: %1px; }" ).arg( pxSize + 1 );
        styleSheet += TQString( "a { font-size: %1px; color: %2; }" ).arg( pxSize ).arg( text );
        styleSheet += TQString( ".info { display: block; margin-left: 4px; font-weight: normal; }" );

        styleSheet += TQString( ".song a { display: block; padding: 1px 2px; font-weight: normal; text-decoration: none; }" );
        styleSheet += TQString( ".song a:hover { color: %1; background-color: %2; }" ).arg( fg ).arg( bg );
        styleSheet += TQString( ".song-title { font-weight: bold; }" );
        styleSheet += TQString( ".song-place { font-size: %1px; font-weight: bold; }" ).arg( pxSize + 3 );

        //box: the base container for every block (border hilighted on hover, 'A' without underlining)
        styleSheet += TQString( ".box { border: solid %1 1px; text-align: left; margin-bottom: 10px; }" ).arg( bg );
        styleSheet += TQString( ".box a { text-decoration: none; }" );
        styleSheet += TQString( ".box:hover { border: solid %1 1px; }" ).arg( text );

        //box contents: header, body, rows and alternate-rows
        styleSheet += TQString( ".box-header { color: %1; background-color: %2; background-image: url( %4 ); background-repeat: repeat-x; font-size: %3px; font-weight: bold; padding: 1px 0.5em; border-bottom: 1px solid #000; }" )
                .arg( fg )
                .arg( bg )
                .arg( pxSize + 2 )
                .arg( m_headerGradientImage->name() );

        styleSheet += TQString( ".box-body { padding: 2px; background-color: %1; background-image: url( %2 ); background-repeat: repeat-x; font-size:%3px; }" )
                .arg( ContextBrowser::instance()->colorGroup().base().name() )
                .arg( m_shadowGradientImage->name() )
                .arg( pxSize );

        //"Related Artists" related styles
        styleSheet += TQString( ".box-header-nav { color: %1; background-color: %2; font-size: %3px; font-weight: bold; padding: 1px 0.5em; border-bottom: 1px solid #000; text-align: right; }" )
                .arg( fg )
                .arg( bg )
                .arg( pxSize );

        //"Albums by ..." related styles
        styleSheet += TQString( ".album-header:hover { color: %1; background-color: %2; cursor: pointer; }" ).arg( fg ).arg( bg );
        styleSheet += TQString( ".album-header:hover a { color: %1; }" ).arg( fg );
        styleSheet += TQString( ".album-body { background-color: %1; border-bottom: solid %2 1px; border-top: solid %3 1px; }" ).arg( ContextBrowser::instance()->colorGroup().base().name() ).arg( bg ).arg( bg );
        styleSheet += TQString( ".album-title { font-weight: bold; }" );
        styleSheet += TQString( ".album-info { float:right; padding-right:4px; font-size: %1px }" ).arg( pxSize );
        styleSheet += TQString( ".album-length { float:right; padding-right:4px; font-size: %1px; clear:right; }" ).arg( pxSize );
        styleSheet += TQString( ".album-image { padding-right: 4px; }" );
        styleSheet += TQString( ".album-song a { display: block; padding: 1px 2px; font-weight: normal; text-decoration: none; }" );
        styleSheet += TQString( ".album-song a:hover { color: %1; background-color: %2; }" ).arg( fg ).arg( bg );
        styleSheet += TQString( ".album-song-trackno { font-weight: bold; }" );

        styleSheet += TQString( ".disc-separator { color: %1; border-bottom: 1px solid %2; }" ).arg( bg ).arg( bg );
        styleSheet += TQString( ".disc-separator a { display: block; padding: 1px 2px; font-weight: normal; text-decoration: none; }" );
        styleSheet += TQString( ".disc-separator a:hover { color: %1; background-color: %2; }" ).arg( fg ).arg( bg );

        styleSheet += TQString( ".button { width: 100%; }" );

        //boxes used to display score (sb: score box)
        styleSheet += TQString( ".sbtext { text-align: right; padding: 0px 4px; }" );
        styleSheet += TQString( ".sbinner { height: 8px; background-color: %1; border: solid %2 1px; }" )
                            .arg( ContextBrowser::instance()->colorGroup().highlight().name() )
                            .arg( ContextBrowser::instance()->colorGroup().highlightedText().name() );
        styleSheet += TQString( ".sbouter { width: 52px; height: 10px; background-color: %1; border: solid %2 1px; }" )
                            .arg( ContextBrowser::instance()->colorGroup().base().dark( 120 ).name() )
                            .arg( ContextBrowser::instance()->colorGroup().highlight().name() );

        styleSheet += TQString( ".ratingBox { padding: 0px 4px; }" );
        styleSheet += TQString( ".ratingStar { height: 0.9em; }" );

        styleSheet += TQString( ".statsBox { border-left: solid %1 1px; }" )
                      .arg( ContextBrowser::instance()->colorGroup().base().dark( 120 ).name() );

        styleSheet += TQString( "#current_box-header-album { font-weight: normal; }" );
        styleSheet += TQString( "#current_box-information-td { text-align: right; vertical-align: bottom; padding: 3px; }" );
        styleSheet += TQString( "#current_box-largecover-td { text-align: left; width: 100px; padding: 0; vertical-align: bottom; }" );
        styleSheet += TQString( "#current_box-largecover-image { padding: 4px; vertical-align: bottom; }" );

        styleSheet += TQString( "#wiki_box-body a { color: %1; }" ).arg( link );
        styleSheet += TQString( "#wiki_box-body a:hover { text-decoration: underline; }" );

        //labels in tag dialog
        styleSheet += ".label a:hover { font-weight: bold; }";
        styleSheet += TQString( ".label.size1 { font-size: %1px; }" ).arg( pxSize );
        styleSheet += TQString( ".label.size2 { font-size: %1px; }" ).arg( pxSize + 1 );
        styleSheet += TQString( ".label.size3 { font-size: %1px; }" ).arg( pxSize + 2 );
        styleSheet += TQString( ".label.size4 { font-size: %1px; }" ).arg( pxSize + 3 );
        styleSheet += TQString( ".label.size5 { font-size: %1px; }" ).arg( pxSize + 4);
        styleSheet += TQString( ".label.size6 { font-size: %1px; }" ).arg( pxSize + 5 );
        styleSheet += TQString( ".label.size7 { font-size: %1px; }" ).arg( pxSize + 6 );
        styleSheet += TQString( ".label.size8 { font-size: %1px; }" ).arg( pxSize + 7 );
        styleSheet += TQString( ".label.size9 { font-size: %1px; }" ).arg( pxSize + 8 );
        styleSheet += TQString( ".label.size10 { font-size: %1px; }" ).arg( pxSize + 9 );
    }

    return styleSheet;
}


void
HTMLView::set( const TQString& data )
{
    begin();
    setUserStyleSheet( loadStyleSheet() );
    write( data );
    end();
}


void HTMLView::openURLRequest( const KURL &url )
{
    // here, http urls are streams. For webpages we use externalurl
    // NOTE there have been no links to streams! http now used for wiki tab.
    if ( url.protocol() == "file" )
        Playlist::instance()->insertMedia( url, Playlist::DefaultOptions );
}

#include "htmlview.moc"