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