summaryrefslogtreecommitdiffstats
path: root/libkonq/konq_pixmapprovider.cpp
blob: 28d5a9992fcd632b1555f7bfca4cce64eb79f934 (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
/* This file is part of the KDE project
   Copyright (C) 2000 Carsten Pfeiffer <pfeiffer@kde.org>

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public
   License as published by the Free Software Foundation; either
   version 2 of the License, or (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; see the file COPYING.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

#include <tqbitmap.h>

#include <tdeapplication.h>
#include <kiconloader.h>
#include <kmimetype.h>
#include <kshell.h>
#include <kprotocolinfo.h>

#include "konq_pixmapprovider.h"

KonqPixmapProvider * KonqPixmapProvider::s_self = 0L;

KonqPixmapProvider * KonqPixmapProvider::self()
{
    if ( !s_self )
	s_self = new KonqPixmapProvider( TQT_TQOBJECT(kapp), "KonqPixmapProvider" );

    return s_self;
}

KonqPixmapProvider::KonqPixmapProvider( TQObject *parent, const char *name )
    : KPixmapProvider(), 
      KonqFavIconMgr( parent, name )
{
}

KonqPixmapProvider::~KonqPixmapProvider()
{
    s_self = 0L;
}

// at first, tries to find the iconname in the cache
// if not available, tries to find the pixmap for the mimetype of url
// if that fails, gets the icon for the protocol
// finally, inserts the url/icon pair into the cache
TQString KonqPixmapProvider::iconNameFor( const TQString& url )
{
    TQMapIterator<TQString,TQString> it = iconMap.find( url );
    TQString icon;
    if ( it != iconMap.end() ) {
        icon = it.data();
        if ( !icon.isEmpty() ) {
	    return icon;
	}
    }

    if ( url.isEmpty() ) {
        // Use the folder icon for the empty URL
        icon = KMimeType::mimeType( "inode/directory" )->KServiceType::icon();
        Q_ASSERT( !icon.isEmpty() );
    }
    else
    {
        KURL u;
        if ( url.at(0) == '~' )
	    u.setPath( KShell::tildeExpand( url ) );
        else if ( url.at(0) == '/' )
	    u.setPath( url );
        else
	    u = url;

        icon = KMimeType::iconForURL( u );
        //Q_ASSERT( !icon.isEmpty() );
    }


    // cache the icon found for url
    iconMap.insert( url, icon );

    return icon;
}

TQPixmap KonqPixmapProvider::pixmapFor( const TQString& url, int size )
{
    return loadIcon( url, iconNameFor( url ), size );
}

void KonqPixmapProvider::load( TDEConfig *kc, const TQString& key )
{
    iconMap.clear();
    TQStringList list;
    list = kc->readPathListEntry( key );
    TQStringList::Iterator it = list.begin();
    TQString url, icon;
    while ( it != list.end() ) {
	url = (*it);
	if ( ++it == list.end() )
	    break;
	icon = (*it);
	iconMap.insert( url, icon );

	++it;
    }
}

// only saves the cache for the given list of items to prevent the cache
// from growing forever.
void KonqPixmapProvider::save( TDEConfig *kc, const TQString& key,
			       const TQStringList& items )
{
    TQStringList list;
    TQStringList::ConstIterator it = items.begin();
    TQMapConstIterator<TQString,TQString> mit;
    while ( it != items.end() ) {
	mit = iconMap.find( *it );
	if ( mit != iconMap.end() ) {
	    list.append( mit.key() );
	    list.append( mit.data() );
	}

	++it;
    }
    kc->writePathEntry( key, list );
}

void KonqPixmapProvider::notifyChange( bool isHost, TQString hostOrURL,
    TQString iconName )
{
    for ( TQMapIterator<TQString,TQString> it = iconMap.begin();
          it != iconMap.end();
          ++it )
    {
        KURL url( it.key() );
        if ( url.protocol().startsWith("http") &&
            ( ( isHost && url.host() == hostOrURL ) ||
                ( url.host() + url.path() == hostOrURL ) ) )
        {
            // For host default-icons still query the favicon manager to get
            // the correct icon for pages that have an own one.
            TQString icon = isHost ? KMimeType::favIconForURL( url ) : iconName;
            if ( !icon.isEmpty() )
                *it = icon;
        }
    }

    emit changed();
}

void KonqPixmapProvider::clear()
{
    iconMap.clear();
}

TQPixmap KonqPixmapProvider::loadIcon( const TQString& url, const TQString& icon,
				      int size )
{
    if ( size <= TDEIcon::SizeSmall )
	return SmallIcon( icon, size );

    KURL u;
    if ( url.at(0) == '/' )
	u.setPath( url );
    else
	u = url;

    TQPixmap big;

    // favicon? => blend the favicon in the large
    if ( url.startsWith( "http:/" ) && icon.startsWith("favicons/") ) {
	TQPixmap small = SmallIcon( icon, size );
	big = TDEGlobal::iconLoader()->loadIcon( KProtocolInfo::icon("http"),
					       TDEIcon::Panel, size );

	int x = big.width()  - small.width();
	int y = 0;

 	if ( big.mask() ) {
 	    TQBitmap mask = *big.mask();
 	    bitBlt( &mask, x, y,
            small.mask() ? TQT_TQPIXMAP(const_cast<TQBitmap *>(small.mask())) : &small, 0, 0,
 		    small.width(), small.height(),
 		    small.mask() ? OrROP : SetROP );
 	    big.setMask( mask );
 	}

	bitBlt( &big, x, y, &small );
    }

    else // not a favicon..
	big = TDEGlobal::iconLoader()->loadIcon( icon, TDEIcon::Panel, size );

    return big;
}