1 /* This file is part of the KDE project
2 Copyright (C) 2000 Carsten Pfeiffer <pfeiffer@kde.org>
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; see the file COPYING. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
20 #include "konqpixmapprovider.h"
25 #include <kiconloader.h>
26 #include <kmimetype.h>
27 #include <kprotocolinfo.h>
29 #include <kconfiggroup.h>
31 class KonqPixmapProviderSingleton
34 KonqPixmapProvider self
;
36 K_GLOBAL_STATIC( KonqPixmapProviderSingleton
, globalPixmapProvider
)
38 KonqPixmapProvider
* KonqPixmapProvider::self()
40 return &globalPixmapProvider
->self
;
43 KonqPixmapProvider::KonqPixmapProvider()
45 org::kde::FavIcon("org.kde.kded", "/modules/favicons", QDBusConnection::sessionBus())
47 QObject::connect(this, SIGNAL(iconChanged(bool,QString
,QString
)),
48 this, SLOT(notifyChange(bool,QString
,QString
)) );
51 KonqPixmapProvider::~KonqPixmapProvider()
55 // at first, tries to find the iconname in the cache
56 // if not available, tries to find the pixmap for the mimetype of url
57 // if that fails, gets the icon for the protocol
58 // finally, inserts the url/icon pair into the cache
59 QString
KonqPixmapProvider::iconNameFor( const KUrl
& url
)
61 QMap
<KUrl
,QString
>::iterator it
= iconMap
.find( url
);
63 if ( it
!= iconMap
.end() ) {
65 if ( !icon
.isEmpty() )
69 if ( url
.url().isEmpty() ) {
70 // Use the folder icon for the empty URL
71 const KMimeType::Ptr directoryType
= KMimeType::mimeType( "inode/directory" );
72 if( directoryType
.isNull() ) // no mimetypes installed!
74 icon
= directoryType
->iconName();
75 Q_ASSERT( !icon
.isEmpty() );
79 icon
= KMimeType::iconNameForUrl( url
);
80 Q_ASSERT( !icon
.isEmpty() );
83 // cache the icon found for url
84 iconMap
.insert( url
, icon
);
89 QPixmap
KonqPixmapProvider::pixmapFor( const QString
& url
, int size
)
91 return loadIcon( iconNameFor( KUrl( url
) ), size
);
94 void KonqPixmapProvider::load( KConfigGroup
& kc
, const QString
& key
)
97 const QStringList list
= kc
.readPathEntry( key
, QStringList() );
98 QStringList::ConstIterator it
= list
.begin();
100 while ( it
!= list
.end() ) {
102 if ( ++it
== list
.end() )
105 iconMap
.insert( KUrl( url
), icon
);
111 // only saves the cache for the given list of items to prevent the cache
112 // from growing forever.
113 void KonqPixmapProvider::save( KConfigGroup
& kc
, const QString
& key
,
114 const QStringList
& items
)
117 QStringList::ConstIterator it
= items
.begin();
118 QMap
<KUrl
,QString
>::const_iterator mit
;
119 while ( it
!= items
.end() ) {
120 mit
= iconMap
.constFind( KUrl(*it
) );
121 if ( mit
!= iconMap
.constEnd() ) {
122 list
.append( mit
.key().url() );
123 list
.append( mit
.value() );
128 kc
.writePathEntry( key
, list
);
131 void KonqPixmapProvider::notifyChange( bool isHost
, const QString
& hostOrURL
,
132 const QString
& iconName
)
134 for ( QMap
<KUrl
,QString
>::iterator it
= iconMap
.begin();
138 KUrl
url( it
.key() );
139 if ( url
.protocol().startsWith("http") &&
140 ( ( isHost
&& url
.host() == hostOrURL
) ||
141 ( url
.host() + url
.path() == hostOrURL
) ) )
143 // For host default-icons still query the favicon manager to get
144 // the correct icon for pages that have an own one.
145 QString icon
= isHost
? KMimeType::favIconForUrl( url
) : iconName
;
146 if ( !icon
.isEmpty() )
154 void KonqPixmapProvider::clear()
159 QPixmap
KonqPixmapProvider::loadIcon( const QString
& icon
, int size
)
161 if ( size
<= KIconLoader::SizeSmall
)
162 return SmallIcon( icon
, size
);
164 return KIconLoader::global()->loadIcon( icon
, KIconLoader::Panel
, size
);
167 #include "konqpixmapprovider.moc"