add more spacing
[personal-kdebase.git] / apps / konqueror / src / konqpixmapprovider.cpp
blobcc8f232b8ac51662caa93e052e03ad632d141daa
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"
22 #include <QBitmap>
23 #include <QPainter>
25 #include <kiconloader.h>
26 #include <kmimetype.h>
27 #include <kprotocolinfo.h>
29 #include <kconfiggroup.h>
31 class KonqPixmapProviderSingleton
33 public:
34 KonqPixmapProvider self;
36 K_GLOBAL_STATIC( KonqPixmapProviderSingleton, globalPixmapProvider )
38 KonqPixmapProvider * KonqPixmapProvider::self()
40 return &globalPixmapProvider->self;
43 KonqPixmapProvider::KonqPixmapProvider()
44 : KPixmapProvider(),
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 );
62 QString icon;
63 if ( it != iconMap.end() ) {
64 icon = it.value();
65 if ( !icon.isEmpty() )
66 return icon;
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!
73 return QString();
74 icon = directoryType->iconName();
75 Q_ASSERT( !icon.isEmpty() );
77 else
79 icon = KMimeType::iconNameForUrl( url );
80 Q_ASSERT( !icon.isEmpty() );
83 // cache the icon found for url
84 iconMap.insert( url, icon );
86 return 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 )
96 iconMap.clear();
97 const QStringList list = kc.readPathEntry( key, QStringList() );
98 QStringList::ConstIterator it = list.begin();
99 QString url, icon;
100 while ( it != list.end() ) {
101 url = (*it);
102 if ( ++it == list.end() )
103 break;
104 icon = (*it);
105 iconMap.insert( KUrl( url ), icon );
107 ++it;
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 )
116 QStringList list;
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() );
126 ++it;
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();
135 it != iconMap.end();
136 ++it )
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() )
147 *it = icon;
151 emit changed();
154 void KonqPixmapProvider::clear()
156 iconMap.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"