libkonq Library API Documentation

konq_pixmapprovider.cc

00001 /* This file is part of the KDE project
00002    Copyright (C) 2000 Carsten Pfeiffer <pfeiffer@kde.org>
00003 
00004    This program is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License, or (at your option) any later version.
00008 
00009    This program is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012     General Public License for more details.
00013 
00014    You should have received a copy of the GNU General Public License
00015    along with this program; see the file COPYING.  If not, write to
00016    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00017    Boston, MA 02111-1307, USA.
00018 */
00019 
00020 #include <qbitmap.h>
00021 
00022 #include <kapplication.h>
00023 #include <kiconloader.h>
00024 #include <kmimetype.h>
00025 #include <kprotocolinfo.h>
00026 
00027 #include "konq_pixmapprovider.h"
00028 
00029 KonqPixmapProvider * KonqPixmapProvider::s_self = 0L;
00030 
00031 KonqPixmapProvider * KonqPixmapProvider::self()
00032 {
00033     if ( !s_self )
00034     s_self = new KonqPixmapProvider( kapp, "KonqPixmapProvider" );
00035 
00036     return s_self;
00037 }
00038 
00039 KonqPixmapProvider::KonqPixmapProvider( QObject *parent, const char *name )
00040     : KPixmapProvider(), 
00041       KonqFavIconMgr( parent, name )
00042 {
00043 }
00044 
00045 KonqPixmapProvider::~KonqPixmapProvider()
00046 {
00047     s_self = 0L;
00048 }
00049 
00050 // at first, tries to find the iconname in the cache
00051 // if not available, tries to find the pixmap for the mimetype of url
00052 // if that fails, gets the icon for the protocol
00053 // finally, inserts the url/icon pair into the cache
00054 QString KonqPixmapProvider::iconNameFor( const QString& url )
00055 {
00056     QMapIterator<QString,QString> it = iconMap.find( url );
00057     QString icon;
00058     if ( it != iconMap.end() ) {
00059         icon = it.data();
00060         if ( !icon.isEmpty() )
00061         return icon;
00062     }
00063 
00064     if ( url.isEmpty() ) {
00065         // Use the folder icon for the empty URL
00066         icon = KMimeType::mimeType( "inode/directory" )->KServiceType::icon();
00067     }
00068     else
00069     {
00070         KURL u;
00071         if ( url.at(0) == '/' )
00072         u.setPath( url );
00073         else
00074         u = url;
00075 
00076         icon = KMimeType::iconForURL( u );
00077     }
00078 
00079     Q_ASSERT( !icon.isEmpty() );
00080 
00081     // cache the icon found for url
00082     iconMap.insert( url, icon );
00083 
00084     return icon;
00085 }
00086 
00087 QPixmap KonqPixmapProvider::pixmapFor( const QString& url, int size )
00088 {
00089     return loadIcon( url, iconNameFor( url ), size );
00090 }
00091 
00092 void KonqPixmapProvider::load( KConfig *kc, const QString& key )
00093 {
00094     iconMap.clear();
00095     QStringList list;
00096     list = kc->readPathListEntry( key );
00097     QStringList::Iterator it = list.begin();
00098     QString url, icon;
00099     while ( it != list.end() ) {
00100     url = (*it);
00101     if ( ++it == list.end() )
00102         break;
00103     icon = (*it);
00104     iconMap.insert( url, icon );
00105 
00106     ++it;
00107     }
00108 }
00109 
00110 // only saves the cache for the given list of items to prevent the cache
00111 // from growing forever.
00112 void KonqPixmapProvider::save( KConfig *kc, const QString& key,
00113                    const QStringList& items )
00114 {
00115     QStringList list;
00116     QStringList::ConstIterator it = items.begin();
00117     QMapConstIterator<QString,QString> mit;
00118     while ( it != items.end() ) {
00119     mit = iconMap.find( *it );
00120     if ( mit != iconMap.end() ) {
00121         list.append( mit.key() );
00122         list.append( mit.data() );
00123     }
00124 
00125     ++it;
00126     }
00127     kc->writePathEntry( key, list );
00128 }
00129 
00130 void KonqPixmapProvider::notifyChange( bool isHost, QString hostOrURL,
00131     QString iconName )
00132 {
00133     for ( QMapIterator<QString,QString> it = iconMap.begin();
00134           it != iconMap.end();
00135           ++it )
00136     {
00137         KURL url( it.key() );
00138         if ( url.protocol().startsWith("http") &&
00139             ( ( isHost && url.host() == hostOrURL ) ||
00140                 ( url.host() + url.path() == hostOrURL ) ) )
00141         {
00142             // For host default-icons still query the favicon manager to get
00143             // the correct icon for pages that have an own one.
00144             QString icon = isHost ? KMimeType::favIconForURL( url ) : iconName;
00145             if ( !icon.isEmpty() )
00146                 *it = icon;
00147         }
00148     }
00149 
00150     emit changed();
00151 }
00152 
00153 void KonqPixmapProvider::clear()
00154 {
00155     iconMap.clear();
00156 }
00157 
00158 QPixmap KonqPixmapProvider::loadIcon( const QString& url, const QString& icon,
00159                       int size )
00160 {
00161     if ( size <= KIcon::SizeSmall )
00162     return SmallIcon( icon, size );
00163 
00164     KURL u;
00165     if ( url.at(0) == '/' )
00166     u.setPath( url );
00167     else
00168     u = url;
00169 
00170     QPixmap big;
00171 
00172     // favicon? => blend the favicon in the large
00173     if ( url.startsWith( "http:/" ) && icon.startsWith("favicons/") ) {
00174     QPixmap small = SmallIcon( icon, size );
00175     big = KGlobal::iconLoader()->loadIcon( KProtocolInfo::icon("http"),
00176                            KIcon::Panel, size );
00177 
00178     int x = big.width()  - small.width();
00179     int y = 0;
00180 
00181     if ( big.mask() ) {
00182         QBitmap mask = *big.mask();
00183         bitBlt( &mask, x, y,
00184             small.mask() ? const_cast<QBitmap *>(small.mask()) : &small, 0, 0,
00185             small.width(), small.height(),
00186             small.mask() ? OrROP : SetROP );
00187         big.setMask( mask );
00188     }
00189 
00190     bitBlt( &big, x, y, &small );
00191     }
00192 
00193     else // not a favicon..
00194     big = KGlobal::iconLoader()->loadIcon( icon, KIcon::Panel, size );
00195 
00196     return big;
00197 }
KDE Logo
This file is part of the documentation for libkonq Library Version 3.4.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Wed Jun 14 16:46:12 2006 by doxygen 1.4.4 written by Dimitri van Heesch, © 1997-2003