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 <tqbitmap.h> 00021 00022 #include <tdeapplication.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( TQT_TQOBJECT(kapp), "KonqPixmapProvider" ); 00036 00037 return s_self; 00038 } 00039 00040 KonqPixmapProvider::KonqPixmapProvider( TQObject *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 TQString KonqPixmapProvider::iconNameFor( const TQString& url ) 00056 { 00057 TQMapIterator<TQString,TQString> it = iconMap.find( url ); 00058 TQString icon; 00059 if ( it != iconMap.end() ) { 00060 icon = it.data(); 00061 if ( !icon.isEmpty() ) { 00062 return icon; 00063 } 00064 } 00065 00066 if ( url.isEmpty() ) { 00067 // Use the folder icon for the empty URL 00068 icon = KMimeType::mimeType( "inode/directory" )->KServiceType::icon(); 00069 Q_ASSERT( !icon.isEmpty() ); 00070 } 00071 else 00072 { 00073 KURL u; 00074 if ( url.at(0) == '~' ) 00075 u.setPath( KShell::tildeExpand( url ) ); 00076 else if ( url.at(0) == '/' ) 00077 u.setPath( url ); 00078 else 00079 u = url; 00080 00081 icon = KMimeType::iconForURL( u ); 00082 //Q_ASSERT( !icon.isEmpty() ); 00083 } 00084 00085 00086 // cache the icon found for url 00087 iconMap.insert( url, icon ); 00088 00089 return icon; 00090 } 00091 00092 TQPixmap KonqPixmapProvider::pixmapFor( const TQString& url, int size ) 00093 { 00094 return loadIcon( url, iconNameFor( url ), size ); 00095 } 00096 00097 void KonqPixmapProvider::load( TDEConfig *kc, const TQString& key ) 00098 { 00099 iconMap.clear(); 00100 TQStringList list; 00101 list = kc->readPathListEntry( key ); 00102 TQStringList::Iterator it = list.begin(); 00103 TQString url, icon; 00104 while ( it != list.end() ) { 00105 url = (*it); 00106 if ( ++it == list.end() ) 00107 break; 00108 icon = (*it); 00109 iconMap.insert( url, icon ); 00110 00111 ++it; 00112 } 00113 } 00114 00115 // only saves the cache for the given list of items to prevent the cache 00116 // from growing forever. 00117 void KonqPixmapProvider::save( TDEConfig *kc, const TQString& key, 00118 const TQStringList& items ) 00119 { 00120 TQStringList list; 00121 TQStringList::ConstIterator it = items.begin(); 00122 TQMapConstIterator<TQString,TQString> mit; 00123 while ( it != items.end() ) { 00124 mit = iconMap.find( *it ); 00125 if ( mit != iconMap.end() ) { 00126 list.append( mit.key() ); 00127 list.append( mit.data() ); 00128 } 00129 00130 ++it; 00131 } 00132 kc->writePathEntry( key, list ); 00133 } 00134 00135 void KonqPixmapProvider::notifyChange( bool isHost, TQString hostOrURL, 00136 TQString iconName ) 00137 { 00138 for ( TQMapIterator<TQString,TQString> it = iconMap.begin(); 00139 it != iconMap.end(); 00140 ++it ) 00141 { 00142 KURL url( it.key() ); 00143 if ( url.protocol().startsWith("http") && 00144 ( ( isHost && url.host() == hostOrURL ) || 00145 ( url.host() + url.path() == hostOrURL ) ) ) 00146 { 00147 // For host default-icons still query the favicon manager to get 00148 // the correct icon for pages that have an own one. 00149 TQString icon = isHost ? KMimeType::favIconForURL( url ) : iconName; 00150 if ( !icon.isEmpty() ) 00151 *it = icon; 00152 } 00153 } 00154 00155 emit changed(); 00156 } 00157 00158 void KonqPixmapProvider::clear() 00159 { 00160 iconMap.clear(); 00161 } 00162 00163 TQPixmap KonqPixmapProvider::loadIcon( const TQString& url, const TQString& icon, 00164 int size ) 00165 { 00166 if ( size <= TDEIcon::SizeSmall ) 00167 return SmallIcon( icon, size ); 00168 00169 KURL u; 00170 if ( url.at(0) == '/' ) 00171 u.setPath( url ); 00172 else 00173 u = url; 00174 00175 TQPixmap big; 00176 00177 // favicon? => blend the favicon in the large 00178 if ( url.startsWith( "http:/" ) && icon.startsWith("favicons/") ) { 00179 TQPixmap small = SmallIcon( icon, size ); 00180 big = TDEGlobal::iconLoader()->loadIcon( KProtocolInfo::icon("http"), 00181 TDEIcon::Panel, size ); 00182 00183 int x = big.width() - small.width(); 00184 int y = 0; 00185 00186 if ( big.mask() ) { 00187 TQBitmap mask = *big.mask(); 00188 bitBlt( &mask, x, y, 00189 small.mask() ? TQT_TQPIXMAP(const_cast<TQBitmap *>(small.mask())) : &small, 0, 0, 00190 small.width(), small.height(), 00191 small.mask() ? OrROP : SetROP ); 00192 big.setMask( mask ); 00193 } 00194 00195 bitBlt( &big, x, y, &small ); 00196 } 00197 00198 else // not a favicon.. 00199 big = TDEGlobal::iconLoader()->loadIcon( icon, TDEIcon::Panel, size ); 00200 00201 return big; 00202 }