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 QPixmap KonqPixmapProvider::pixmapFor( const QString& url, int size )
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 loadIcon( url, icon, size );
00062     }
00063 
00064     KURL u;
00065     if ( url.at(0) == '/' )
00066     u.setPath( url );
00067     else
00068     u = url;
00069 
00070     icon = KMimeType::iconForURL( u );
00071 
00072     Q_ASSERT( !icon.isEmpty() );
00073 
00074     // cache the icon found for url
00075     iconMap.insert( url, icon );
00076 
00077     return loadIcon( url, icon, size );
00078 }
00079 
00080 
00081 void KonqPixmapProvider::load( KConfig *kc, const QString& key )
00082 {
00083     iconMap.clear();
00084     QStringList list;
00085     list = kc->readListEntry( key );
00086     QStringList::Iterator it = list.begin();
00087     QString url, icon;
00088     while ( it != list.end() ) {
00089     url = (*it);
00090     if ( ++it == list.end() )
00091         break;
00092     icon = (*it);
00093     iconMap.insert( url, icon );
00094 
00095     ++it;
00096     }
00097 }
00098 
00099 // only saves the cache for the given list of items to prevent the cache
00100 // from growing forever.
00101 void KonqPixmapProvider::save( KConfig *kc, const QString& key,
00102                    const QStringList& items )
00103 {
00104     QStringList list;
00105     QStringList::ConstIterator it = items.begin();
00106     QMapConstIterator<QString,QString> mit;
00107     while ( it != items.end() ) {
00108     mit = iconMap.find( *it );
00109     if ( mit != iconMap.end() ) {
00110         list.append( mit.key() );
00111         list.append( mit.data() );
00112     }
00113 
00114     ++it;
00115     }
00116     kc->writeEntry( key, list );
00117 }
00118 
00119 void KonqPixmapProvider::notifyChange( bool isHost, QString hostOrURL,
00120     QString iconName )
00121 {
00122     for ( QMapIterator<QString,QString> it = iconMap.begin();
00123           it != iconMap.end();
00124           ++it )
00125     {
00126         KURL url( it.key() );
00127         if ( url.protocol().startsWith("http") &&
00128             ( ( isHost && url.host() == hostOrURL ) ||
00129                 ( url.host() + url.path() == hostOrURL ) ) )
00130         {
00131             // For host default-icons still query the favicon manager to get
00132             // the correct icon for pages that have an own one.
00133             QString icon = isHost ? KMimeType::favIconForURL( url ) : iconName;
00134             if ( !icon.isEmpty() )
00135                 *it = icon;
00136         }
00137     }
00138 
00139     emit changed();
00140 }
00141 
00142 void KonqPixmapProvider::clear()
00143 {
00144     iconMap.clear();
00145 }
00146 
00147 QPixmap KonqPixmapProvider::loadIcon( const QString& url, const QString& icon,
00148                       int size )
00149 {
00150     if ( size <= KIcon::SizeSmall )
00151     return SmallIcon( icon, size );
00152 
00153     KURL u;
00154     if ( url.at(0) == '/' )
00155     u.setPath( url );
00156     else
00157     u = url;
00158 
00159     QPixmap big;
00160 
00161     // favicon? => blend the favicon in the large
00162     if ( url.startsWith( "http:/" ) && icon.startsWith("favicons/") ) {
00163     QPixmap small = SmallIcon( icon, size );
00164     big = KGlobal::iconLoader()->loadIcon( KProtocolInfo::icon("http"),
00165                            KIcon::Panel, size );
00166 
00167     int x = big.width()  - small.width();
00168     int y = 0;
00169 
00170     if ( big.mask() ) {
00171         QBitmap mask = *big.mask();
00172         bitBlt( &mask, x, y,
00173             small.mask() ? const_cast<QBitmap *>(small.mask()) : &small, 0, 0,
00174             small.width(), small.height(),
00175             small.mask() ? OrROP : SetROP );
00176         big.setMask( mask );
00177     }
00178 
00179     bitBlt( &big, x, y, &small );
00180     }
00181 
00182     else // not a favicon..
00183     big = KGlobal::iconLoader()->loadIcon( icon, KIcon::Panel, size );
00184 
00185     return big;
00186 }
KDE Logo
This file is part of the documentation for libkonq Library Version 3.2.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Sun Apr 11 13:44:47 2004 by doxygen 1.3.6-20040222 written by Dimitri van Heesch, © 1997-2003