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., 51 Franklin Street, Fifth Floor,
00017    Boston, MA 02110-1301, USA.
00018 */
00019 
00020 #include <qbitmap.h>
00021 
00022 #include <kapplication.h>
00023 #include <kiconloader.h>
00024 #include <kmimetype.h>
00025 #include <kshell.h>
00026 #include <kprotocolinfo.h>
00027 
00028 #include "konq_pixmapprovider.h"
00029 
00030 KonqPixmapProvider * KonqPixmapProvider::s_self = 0L;
00031 
00032 KonqPixmapProvider * KonqPixmapProvider::self()
00033 {
00034     if ( !s_self )
00035     s_self = new KonqPixmapProvider( kapp, "KonqPixmapProvider" );
00036 
00037     return s_self;
00038 }
00039 
00040 KonqPixmapProvider::KonqPixmapProvider( QObject *parent, const char *name )
00041     : KPixmapProvider(), 
00042       KonqFavIconMgr( parent, name )
00043 {
00044 }
00045 
00046 KonqPixmapProvider::~KonqPixmapProvider()
00047 {
00048     s_self = 0L;
00049 }
00050 
00051 // at first, tries to find the iconname in the cache
00052 // if not available, tries to find the pixmap for the mimetype of url
00053 // if that fails, gets the icon for the protocol
00054 // finally, inserts the url/icon pair into the cache
00055 QString KonqPixmapProvider::iconNameFor( const QString& url )
00056 {
00057     QMapIterator<QString,QString> it = iconMap.find( url );
00058     QString icon;
00059     if ( it != iconMap.end() ) {
00060         icon = it.data();
00061         if ( !icon.isEmpty() )
00062         return icon;
00063     }
00064 
00065     if ( url.isEmpty() ) {
00066         // Use the folder icon for the empty URL
00067         icon = KMimeType::mimeType( "inode/directory" )->KServiceType::icon();
00068         Q_ASSERT( !icon.isEmpty() );
00069     }
00070     else
00071     {
00072         KURL u;
00073         if ( url.at(0) == '~' )
00074         u.setPath( KShell::tildeExpand( url ) );
00075         else if ( url.at(0) == '/' )
00076         u.setPath( url );
00077         else
00078         u = url;
00079 
00080         icon = KMimeType::iconForURL( u );
00081         Q_ASSERT( !icon.isEmpty() );
00082     }
00083 
00084 
00085     // cache the icon found for url
00086     iconMap.insert( url, icon );
00087 
00088     return icon;
00089 }
00090 
00091 QPixmap KonqPixmapProvider::pixmapFor( const QString& url, int size )
00092 {
00093     return loadIcon( url, iconNameFor( url ), size );
00094 }
00095 
00096 void KonqPixmapProvider::load( KConfig *kc, const QString& key )
00097 {
00098     iconMap.clear();
00099     QStringList list;
00100     list = kc->readPathListEntry( key );
00101     QStringList::Iterator it = list.begin();
00102     QString url, icon;
00103     while ( it != list.end() ) {
00104     url = (*it);
00105     if ( ++it == list.end() )
00106         break;
00107     icon = (*it);
00108     iconMap.insert( url, icon );
00109 
00110     ++it;
00111     }
00112 }
00113 
00114 // only saves the cache for the given list of items to prevent the cache
00115 // from growing forever.
00116 void KonqPixmapProvider::save( KConfig *kc, const QString& key,
00117                    const QStringList& items )
00118 {
00119     QStringList list;
00120     QStringList::ConstIterator it = items.begin();
00121     QMapConstIterator<QString,QString> mit;
00122     while ( it != items.end() ) {
00123     mit = iconMap.find( *it );
00124     if ( mit != iconMap.end() ) {
00125         list.append( mit.key() );
00126         list.append( mit.data() );
00127     }
00128 
00129     ++it;
00130     }
00131     kc->writePathEntry( key, list );
00132 }
00133 
00134 void KonqPixmapProvider::notifyChange( bool isHost, QString hostOrURL,
00135     QString iconName )
00136 {
00137     for ( QMapIterator<QString,QString> it = iconMap.begin();
00138           it != iconMap.end();
00139           ++it )
00140     {
00141         KURL url( it.key() );
00142         if ( url.protocol().startsWith("http") &&
00143             ( ( isHost && url.host() == hostOrURL ) ||
00144                 ( url.host() + url.path() == hostOrURL ) ) )
00145         {
00146             // For host default-icons still query the favicon manager to get
00147             // the correct icon for pages that have an own one.
00148             QString icon = isHost ? KMimeType::favIconForURL( url ) : iconName;
00149             if ( !icon.isEmpty() )
00150                 *it = icon;
00151         }
00152     }
00153 
00154     emit changed();
00155 }
00156 
00157 void KonqPixmapProvider::clear()
00158 {
00159     iconMap.clear();
00160 }
00161 
00162 QPixmap KonqPixmapProvider::loadIcon( const QString& url, const QString& icon,
00163                       int size )
00164 {
00165     if ( size <= KIcon::SizeSmall )
00166     return SmallIcon( icon, size );
00167 
00168     KURL u;
00169     if ( url.at(0) == '/' )
00170     u.setPath( url );
00171     else
00172     u = url;
00173 
00174     QPixmap big;
00175 
00176     // favicon? => blend the favicon in the large
00177     if ( url.startsWith( "http:/" ) && icon.startsWith("favicons/") ) {
00178     QPixmap small = SmallIcon( icon, size );
00179     big = KGlobal::iconLoader()->loadIcon( KProtocolInfo::icon("http"),
00180                            KIcon::Panel, size );
00181 
00182     int x = big.width()  - small.width();
00183     int y = 0;
00184 
00185     if ( big.mask() ) {
00186         QBitmap mask = *big.mask();
00187         bitBlt( &mask, x, y,
00188             small.mask() ? const_cast<QBitmap *>(small.mask()) : &small, 0, 0,
00189             small.width(), small.height(),
00190             small.mask() ? OrROP : SetROP );
00191         big.setMask( mask );
00192     }
00193 
00194     bitBlt( &big, x, y, &small );
00195     }
00196 
00197     else // not a favicon..
00198     big = KGlobal::iconLoader()->loadIcon( icon, KIcon::Panel, size );
00199 
00200     return big;
00201 }
KDE Home | KDE Accessibility Home | Description of Access Keys