00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #include <qstring.h>
00017 #include <qstringlist.h>
00018 #include <qptrlist.h>
00019 #include <qintdict.h>
00020 #include <qpixmap.h>
00021 #include <qpixmapcache.h>
00022 #include <qimage.h>
00023 #include <qfileinfo.h>
00024 #include <qdir.h>
00025 #include <qiconset.h>
00026 #include <qmovie.h>
00027 #include <qbitmap.h>
00028
00029 #include <kapplication.h>
00030 #include <kipc.h>
00031 #include <kdebug.h>
00032 #include <kstandarddirs.h>
00033 #include <kglobal.h>
00034 #include <kconfig.h>
00035 #include <ksimpleconfig.h>
00036 #include <kinstance.h>
00037
00038 #include <kicontheme.h>
00039 #include <kiconloader.h>
00040 #include <kiconeffect.h>
00041
00042 #include <sys/types.h>
00043 #include <stdlib.h>
00044 #include <unistd.h>
00045 #include <dirent.h>
00046 #include <config.h>
00047 #include <assert.h>
00048
00049 #ifdef HAVE_LIBART
00050 #include "svgicons/ksvgiconengine.h"
00051 #include "svgicons/ksvgiconpainter.h"
00052 #endif
00053
00054 #include "kiconloader_p.h"
00055
00056
00057
00058 KIconThemeNode::KIconThemeNode(KIconTheme *_theme)
00059 {
00060 theme = _theme;
00061 }
00062
00063 KIconThemeNode::~KIconThemeNode()
00064 {
00065 delete theme;
00066 }
00067
00068 void KIconThemeNode::printTree(QString& dbgString) const
00069 {
00070
00071
00072 dbgString += "(";
00073 dbgString += theme->name();
00074 dbgString += ")";
00075 }
00076
00077 void KIconThemeNode::queryIcons(QStringList *result,
00078 int size, KIcon::Context context) const
00079 {
00080
00081 *result += theme->queryIcons(size, context);
00082 }
00083
00084 void KIconThemeNode::queryIconsByContext(QStringList *result,
00085 int size, KIcon::Context context) const
00086 {
00087
00088 *result += theme->queryIconsByContext(size, context);
00089 }
00090
00091 KIcon KIconThemeNode::findIcon(const QString& name, int size,
00092 KIcon::MatchType match) const
00093 {
00094 return theme->iconPath(name, size, match);
00095 }
00096
00097
00098
00099
00100 struct KIconGroup
00101 {
00102 int size;
00103 bool dblPixels;
00104 bool alphaBlending;
00105 };
00106
00107 #define KICONLOADER_CHECKS
00108 #ifdef KICONLOADER_CHECKS
00109
00110
00111 struct KIconLoaderDebug
00112 {
00113 KIconLoaderDebug( KIconLoader* l, const QString& a )
00114 : loader( l ), appname( a ), valid( true )
00115 {}
00116 KIconLoaderDebug() {};
00117 KIconLoader* loader;
00118 QString appname;
00119 bool valid;
00120 QString delete_bt;
00121 };
00122
00123 static QValueList< KIconLoaderDebug > *kiconloaders;
00124 #endif
00125
00126
00127
00128 KIconLoader::KIconLoader(const QString& _appname, KStandardDirs *_dirs)
00129 {
00130 #ifdef KICONLOADER_CHECKS
00131 if( kiconloaders == NULL )
00132 kiconloaders = new QValueList< KIconLoaderDebug>();
00133
00134
00135 for( QValueList< KIconLoaderDebug >::Iterator it = kiconloaders->begin();
00136 it != kiconloaders->end();
00137 )
00138 {
00139 if( (*it).loader == this )
00140 it = kiconloaders->remove( it );
00141 else
00142 ++it;
00143 }
00144 kiconloaders->append( KIconLoaderDebug( this, _appname ));
00145 #endif
00146 d = new KIconLoaderPrivate;
00147 d->q = this;
00148 d->mpGroups = 0L;
00149 d->imgDict.setAutoDelete(true);
00150 d->links.setAutoDelete(true);
00151
00152 if (kapp) {
00153 kapp->addKipcEventMask(KIPC::IconChanged);
00154 QObject::connect(kapp, SIGNAL(updateIconLoaders()), d, SLOT(reconfigure()));
00155 }
00156
00157 init( _appname, _dirs );
00158 }
00159
00160 void KIconLoader::reconfigure( const QString& _appname, KStandardDirs *_dirs )
00161 {
00162 d->links.clear();
00163 d->imgDict.clear();
00164 d->mThemesInTree.clear();
00165 d->lastImage.reset();
00166 d->lastImageKey = QString::null;
00167 delete [] d->mpGroups;
00168
00169 init( _appname, _dirs );
00170 }
00171
00172 void KIconLoader::init( const QString& _appname, KStandardDirs *_dirs )
00173 {
00174
00175
00176 d->mpThemeRoot = 0L;
00177
00178 d->appname = _appname;
00179 d->extraDesktopIconsLoaded = false;
00180 d->delayedLoading = false;
00181
00182 if (_dirs)
00183 d->mpDirs = _dirs;
00184 else
00185 d->mpDirs = KGlobal::dirs();
00186
00187 QString appname = _appname;
00188 if (appname.isEmpty())
00189 appname = KGlobal::instance()->instanceName();
00190
00191
00192 KIconTheme *def = new KIconTheme(KIconTheme::current(), appname);
00193 if (!def->isValid())
00194 {
00195 delete def;
00196
00197 kdDebug(264) << "Couldn't find current icon theme, falling back to default." << endl;
00198 def = new KIconTheme(KIconTheme::defaultThemeName(), appname);
00199 if (!def->isValid())
00200 {
00201 kdError(264) << "Error: standard icon theme"
00202 << " \"" << KIconTheme::defaultThemeName() << "\" "
00203 << " not found!" << endl;
00204 d->mpGroups=0L;
00205 return;
00206 }
00207 }
00208 d->mpThemeRoot = new KIconThemeNode(def);
00209 d->links.append(d->mpThemeRoot);
00210 d->mThemesInTree += KIconTheme::current();
00211 addBaseThemes(d->mpThemeRoot, appname);
00212
00213
00214 static const char * const groups[] = { "Desktop", "Toolbar", "MainToolbar", "Small", "Panel", 0L };
00215 KConfig *config = KGlobal::config();
00216 KConfigGroupSaver cs(config, "dummy");
00217
00218
00219 d->mpGroups = new KIconGroup[(int) KIcon::LastGroup];
00220 for (KIcon::Group i=KIcon::FirstGroup; i<KIcon::LastGroup; i++)
00221 {
00222 if (groups[i] == 0L)
00223 break;
00224 config->setGroup(QString::fromLatin1(groups[i]) + "Icons");
00225 d->mpGroups[i].size = config->readNumEntry("Size", 0);
00226 d->mpGroups[i].dblPixels = config->readBoolEntry("DoublePixels", false);
00227 if (QPixmap::defaultDepth()>8)
00228 d->mpGroups[i].alphaBlending = config->readBoolEntry("AlphaBlending", true);
00229 else
00230 d->mpGroups[i].alphaBlending = false;
00231
00232 if (!d->mpGroups[i].size)
00233 d->mpGroups[i].size = d->mpThemeRoot->theme->defaultSize(i);
00234 }
00235
00236
00237 d->mpDirs->addResourceType("appicon", KStandardDirs::kde_default("data") +
00238 appname + "/pics/");
00239
00240 d->mpDirs->addResourceType("appicon", KStandardDirs::kde_default("data") +
00241 appname + "/toolbar/");
00242
00243
00244 QStringList dirs;
00245 dirs += d->mpDirs->resourceDirs("icon");
00246 dirs += d->mpDirs->resourceDirs("pixmap");
00247 dirs += d->mpDirs->resourceDirs("xdgdata-icon");
00248 dirs += "/usr/share/pixmaps";
00249
00250 dirs += d->mpDirs->resourceDirs("xdgdata-pixmap");
00251 for (QStringList::ConstIterator it = dirs.begin(); it != dirs.end(); ++it)
00252 d->mpDirs->addResourceDir("appicon", *it);
00253
00254 #ifndef NDEBUG
00255 QString dbgString = "Theme tree: ";
00256 d->mpThemeRoot->printTree(dbgString);
00257 kdDebug(264) << dbgString << endl;
00258 #endif
00259 }
00260
00261 KIconLoader::~KIconLoader()
00262 {
00263 #ifdef KICONLOADER_CHECKS
00264 for( QValueList< KIconLoaderDebug >::Iterator it = kiconloaders->begin();
00265 it != kiconloaders->end();
00266 ++it )
00267 {
00268 if( (*it).loader == this )
00269 {
00270 (*it).valid = false;
00271 (*it).delete_bt = kdBacktrace();
00272 break;
00273 }
00274 }
00275 #endif
00276
00277
00278 d->mpThemeRoot=0;
00279 delete[] d->mpGroups;
00280 delete d;
00281 }
00282
00283 void KIconLoader::enableDelayedIconSetLoading( bool enable )
00284 {
00285 d->delayedLoading = enable;
00286 }
00287
00288 bool KIconLoader::isDelayedIconSetLoadingEnabled() const
00289 {
00290 return d->delayedLoading;
00291 }
00292
00293 void KIconLoader::addAppDir(const QString& appname)
00294 {
00295 d->mpDirs->addResourceType("appicon", KStandardDirs::kde_default("data") +
00296 appname + "/pics/");
00297
00298 d->mpDirs->addResourceType("appicon", KStandardDirs::kde_default("data") +
00299 appname + "/toolbar/");
00300 addAppThemes(appname);
00301 }
00302
00303 void KIconLoader::addAppThemes(const QString& appname)
00304 {
00305 if ( KIconTheme::current() != KIconTheme::defaultThemeName() )
00306 {
00307 KIconTheme *def = new KIconTheme(KIconTheme::current(), appname);
00308 if (def->isValid())
00309 {
00310 KIconThemeNode* node = new KIconThemeNode(def);
00311 d->links.append(node);
00312 addBaseThemes(node, appname);
00313 }
00314 else
00315 delete def;
00316 }
00317
00318 KIconTheme *def = new KIconTheme(KIconTheme::defaultThemeName(), appname);
00319 KIconThemeNode* node = new KIconThemeNode(def);
00320 d->links.append(node);
00321 addBaseThemes(node, appname);
00322 }
00323
00324 void KIconLoader::addBaseThemes(KIconThemeNode *node, const QString &appname)
00325 {
00326 QStringList lst = node->theme->inherits();
00327 QStringList::ConstIterator it;
00328
00329 for (it=lst.begin(); it!=lst.end(); ++it)
00330 {
00331 if( d->mThemesInTree.contains(*it) && (*it) != "hicolor")
00332 continue;
00333 KIconTheme *theme = new KIconTheme(*it,appname);
00334 if (!theme->isValid()) {
00335 delete theme;
00336 continue;
00337 }
00338 KIconThemeNode *n = new KIconThemeNode(theme);
00339 d->mThemesInTree.append(*it);
00340 d->links.append(n);
00341 addBaseThemes(n, appname);
00342 }
00343 }
00344
00345 void KIconLoader::addExtraDesktopThemes()
00346 {
00347 if ( d->extraDesktopIconsLoaded ) return;
00348
00349 QStringList list;
00350 QStringList icnlibs = KGlobal::dirs()->resourceDirs("icon");
00351 QStringList::ConstIterator it;
00352 char buf[1000];
00353 int r;
00354 for (it=icnlibs.begin(); it!=icnlibs.end(); ++it)
00355 {
00356 QDir dir(*it);
00357 if (!dir.exists())
00358 continue;
00359 QStringList lst = dir.entryList("default.*", QDir::Dirs);
00360 QStringList::ConstIterator it2;
00361 for (it2=lst.begin(); it2!=lst.end(); ++it2)
00362 {
00363 if (!KStandardDirs::exists(*it + *it2 + "/index.desktop")
00364 && !KStandardDirs::exists(*it + *it2 + "/index.theme"))
00365 continue;
00366 r=readlink( QFile::encodeName(*it + *it2) , buf, sizeof(buf)-1);
00367 if ( r>0 )
00368 {
00369 buf[r]=0;
00370 QDir dir2( buf );
00371 QString themeName=dir2.dirName();
00372
00373 if (!list.contains(themeName))
00374 list.append(themeName);
00375 }
00376 }
00377 }
00378
00379 for (it=list.begin(); it!=list.end(); ++it)
00380 {
00381 if ( d->mThemesInTree.contains(*it) )
00382 continue;
00383 if ( *it == QString("default.kde") ) continue;
00384
00385 KIconTheme *def = new KIconTheme( *it, "" );
00386 KIconThemeNode* node = new KIconThemeNode(def);
00387 d->mThemesInTree.append(*it);
00388 d->links.append(node);
00389 addBaseThemes(node, "" );
00390 }
00391
00392 d->extraDesktopIconsLoaded=true;
00393
00394 }
00395
00396 bool KIconLoader::extraDesktopThemesAdded() const
00397 {
00398 return d->extraDesktopIconsLoaded;
00399 }
00400
00401 QString KIconLoader::removeIconExtension(const QString &name) const
00402 {
00403 int extensionLength=0;
00404
00405 QString ext = name.right(4);
00406
00407 static const QString &png_ext = KGlobal::staticQString(".png");
00408 static const QString &xpm_ext = KGlobal::staticQString(".xpm");
00409 if (ext == png_ext || ext == xpm_ext)
00410 extensionLength=4;
00411 #ifdef HAVE_LIBART
00412 else
00413 {
00414 static const QString &svgz_ext = KGlobal::staticQString(".svgz");
00415 static const QString &svg_ext = KGlobal::staticQString(".svg");
00416
00417 if (name.right(5) == svgz_ext)
00418 extensionLength=5;
00419 else if (ext == svg_ext)
00420 extensionLength=4;
00421 }
00422 #endif
00423
00424 if ( extensionLength > 0 )
00425 {
00426 return name.left(name.length() - extensionLength);
00427 }
00428 return name;
00429 }
00430
00431 QString KIconLoader::removeIconExtensionInternal(const QString &name) const
00432 {
00433 QString name_noext = removeIconExtension(name);
00434
00435 #ifndef NDEBUG
00436 if (name != name_noext)
00437 {
00438 kdDebug(264) << "Application " << KGlobal::instance()->instanceName()
00439 << " loads icon " << name << " with extension." << endl;
00440 }
00441 #endif
00442
00443 return name_noext;
00444 }
00445
00446 KIcon KIconLoader::findMatchingIcon(const QString& name, int size) const
00447 {
00448 KIcon icon;
00449
00450 const QString *ext[4];
00451 int count=0;
00452 static const QString &png_ext = KGlobal::staticQString(".png");
00453 ext[count++]=&png_ext;
00454 #ifdef HAVE_LIBART
00455 static const QString &svgz_ext = KGlobal::staticQString(".svgz");
00456 ext[count++]=&svgz_ext;
00457 static const QString &svg_ext = KGlobal::staticQString(".svg");
00458 ext[count++]=&svg_ext;
00459 #endif
00460 static const QString &xpm_ext = KGlobal::staticQString(".xpm");
00461 ext[count++]=&xpm_ext;
00462
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475 for ( KIconThemeNode *themeNode = d->links.first() ; themeNode ;
00476 themeNode = d->links.next() )
00477 {
00478 for (int i = 0 ; i < count ; i++)
00479 {
00480 icon = themeNode->theme->iconPath(name + *ext[i], size, KIcon::MatchExact);
00481 if (icon.isValid()) goto icon_found ;
00482 }
00483
00484 for (int i = 0 ; i < count ; i++)
00485 {
00486 icon = themeNode->theme->iconPath(name + *ext[i], size, KIcon::MatchBest);
00487 if (icon.isValid()) goto icon_found;
00488 }
00489 }
00490 icon_found:
00491 return icon;
00492 }
00493
00494 inline QString KIconLoader::unknownIconPath( int size ) const
00495 {
00496 static const QString &str_unknown = KGlobal::staticQString("unknown");
00497
00498 KIcon icon = findMatchingIcon(str_unknown, size);
00499 if (!icon.isValid())
00500 {
00501 kdDebug(264) << "Warning: could not find \"Unknown\" icon for size = "
00502 << size << endl;
00503 return QString::null;
00504 }
00505 return icon.path;
00506 }
00507
00508
00509
00510 QString KIconLoader::iconPath(const QString& _name, int group_or_size,
00511 bool canReturnNull) const
00512 {
00513 if (d->mpThemeRoot == 0L)
00514 return QString::null;
00515
00516 if (!QDir::isRelativePath(_name))
00517 return _name;
00518
00519 QString name = removeIconExtensionInternal( _name );
00520
00521 QString path;
00522 if (group_or_size == KIcon::User)
00523 {
00524 static const QString &png_ext = KGlobal::staticQString(".png");
00525 static const QString &xpm_ext = KGlobal::staticQString(".xpm");
00526 path = d->mpDirs->findResource("appicon", name + png_ext);
00527
00528 #ifdef HAVE_LIBART
00529 static const QString &svgz_ext = KGlobal::staticQString(".svgz");
00530 static const QString &svg_ext = KGlobal::staticQString(".svg");
00531 if (path.isEmpty())
00532 path = d->mpDirs->findResource("appicon", name + svgz_ext);
00533 if (path.isEmpty())
00534 path = d->mpDirs->findResource("appicon", name + svg_ext);
00535 #endif
00536 if (path.isEmpty())
00537 path = d->mpDirs->findResource("appicon", name + xpm_ext);
00538 return path;
00539 }
00540
00541 if (group_or_size >= KIcon::LastGroup)
00542 {
00543 kdDebug(264) << "Illegal icon group: " << group_or_size << endl;
00544 return path;
00545 }
00546
00547 int size;
00548 if (group_or_size >= 0)
00549 size = d->mpGroups[group_or_size].size;
00550 else
00551 size = -group_or_size;
00552
00553 if (_name.isEmpty()) {
00554 if (canReturnNull)
00555 return QString::null;
00556 else
00557 return unknownIconPath(size);
00558 }
00559
00560 KIcon icon = findMatchingIcon(name, size);
00561
00562 if (!icon.isValid())
00563 {
00564
00565 path = iconPath(name, KIcon::User, true);
00566 if (!path.isEmpty() || canReturnNull)
00567 return path;
00568
00569 if (canReturnNull)
00570 return QString::null;
00571 else
00572 return unknownIconPath(size);
00573 }
00574 return icon.path;
00575 }
00576
00577 QPixmap KIconLoader::loadIcon(const QString& _name, KIcon::Group group, int size,
00578 int state, QString *path_store, bool canReturnNull) const
00579 {
00580 QString name = _name;
00581 QPixmap pix;
00582 QString key;
00583 bool absolutePath=false, favIconOverlay=false;
00584
00585 if (d->mpThemeRoot == 0L)
00586 return pix;
00587
00588
00589 if (name.startsWith("favicons/"))
00590 {
00591 favIconOverlay = true;
00592 name = locateLocal("cache", name+".png");
00593 }
00594 if (!QDir::isRelativePath(name)) absolutePath=true;
00595
00596 static const QString &str_unknown = KGlobal::staticQString("unknown");
00597
00598
00599 if (group == KIcon::User)
00600 {
00601 key = "$kicou_";
00602 key += QString::number(size); key += '_';
00603 key += name;
00604 bool inCache = QPixmapCache::find(key, pix);
00605 if (inCache && (path_store == 0L))
00606 return pix;
00607
00608 QString path = (absolutePath) ? name :
00609 iconPath(name, KIcon::User, canReturnNull);
00610 if (path.isEmpty())
00611 {
00612 if (canReturnNull)
00613 return pix;
00614
00615 path = iconPath(str_unknown, KIcon::Small, true);
00616 if (path.isEmpty())
00617 {
00618 kdDebug(264) << "Warning: Cannot find \"unknown\" icon." << endl;
00619 return pix;
00620 }
00621 }
00622
00623 if (path_store != 0L)
00624 *path_store = path;
00625 if (inCache)
00626 return pix;
00627 QImage img(path);
00628 if (size != 0)
00629 img=img.smoothScale(size,size);
00630
00631 pix.convertFromImage(img);
00632 QPixmapCache::insert(key, pix);
00633 return pix;
00634 }
00635
00636
00637
00638 if ((group < -1) || (group >= KIcon::LastGroup))
00639 {
00640 kdDebug(264) << "Illegal icon group: " << group << endl;
00641 group = KIcon::Desktop;
00642 }
00643
00644 int overlay = (state & KIcon::OverlayMask);
00645 state &= ~KIcon::OverlayMask;
00646 if ((state < 0) || (state >= KIcon::LastState))
00647 {
00648 kdDebug(264) << "Illegal icon state: " << state << endl;
00649 state = KIcon::DefaultState;
00650 }
00651
00652 if (size == 0 && group < 0)
00653 {
00654 kdDebug(264) << "Neither size nor group specified!" << endl;
00655 group = KIcon::Desktop;
00656 }
00657
00658 if (!absolutePath)
00659 {
00660 if (!canReturnNull && name.isEmpty())
00661 name = str_unknown;
00662 else
00663 name = removeIconExtensionInternal(name);
00664 }
00665
00666
00667 if (size == 0)
00668 {
00669 size = d->mpGroups[group].size;
00670 }
00671 favIconOverlay = favIconOverlay && size > 22;
00672
00673
00674
00675 key = "$kico_";
00676 key += name; key += '_';
00677 key += QString::number(size); key += '_';
00678
00679 QString overlayStr = QString::number( overlay );
00680
00681 QString noEffectKey = key + '_' + overlayStr;
00682
00683 if (group >= 0)
00684 {
00685 key += d->mpEffect.fingerprint(group, state);
00686 if (d->mpGroups[group].dblPixels)
00687 key += QString::fromLatin1(":dblsize");
00688 } else
00689 key += QString::fromLatin1("noeffect");
00690 key += '_';
00691 key += overlayStr;
00692
00693
00694 bool inCache = QPixmapCache::find(key, pix);
00695 if (inCache && (path_store == 0L))
00696 return pix;
00697
00698 QImage *img = 0;
00699 int iconType;
00700 int iconThreshold;
00701
00702 if ( ( path_store != 0L ) ||
00703 noEffectKey != d->lastImageKey )
00704 {
00705
00706 KIcon icon;
00707 if (absolutePath && !favIconOverlay)
00708 {
00709 icon.context=KIcon::Any;
00710 icon.type=KIcon::Scalable;
00711 icon.path=name;
00712 }
00713 else
00714 {
00715 if (!name.isEmpty())
00716 icon = findMatchingIcon(favIconOverlay ? QString("www") : name, size);
00717
00718 if (!icon.isValid())
00719 {
00720
00721 if (!name.isEmpty())
00722 pix = loadIcon(name, KIcon::User, size, state, path_store, true);
00723 if (!pix.isNull() || canReturnNull) {
00724 QPixmapCache::insert(key, pix);
00725 return pix;
00726 }
00727
00728 icon = findMatchingIcon(str_unknown, size);
00729 if (!icon.isValid())
00730 {
00731 kdDebug(264)
00732 << "Warning: could not find \"Unknown\" icon for size = "
00733 << size << endl;
00734 return pix;
00735 }
00736 }
00737 }
00738
00739 if (path_store != 0L)
00740 *path_store = icon.path;
00741 if (inCache)
00742 return pix;
00743
00744
00745 QString ext = icon.path.right(3).upper();
00746 if(ext != "SVG" && ext != "VGZ")
00747 {
00748 img = new QImage(icon.path, ext.latin1());
00749 if (img->isNull()) {
00750 delete img;
00751 return pix;
00752 }
00753 }
00754 #ifdef HAVE_LIBART
00755 else
00756 {
00757
00758 KSVGIconEngine *svgEngine = new KSVGIconEngine();
00759
00760 if(svgEngine->load(size, size, icon.path))
00761 img = svgEngine->painter()->image();
00762 else
00763 img = new QImage();
00764
00765 delete svgEngine;
00766 }
00767 #endif
00768
00769 iconType = icon.type;
00770 iconThreshold = icon.threshold;
00771
00772 d->lastImage = img->copy();
00773 d->lastImageKey = noEffectKey;
00774 d->lastIconType = iconType;
00775 d->lastIconThreshold = iconThreshold;
00776 }
00777 else
00778 {
00779 img = new QImage( d->lastImage.copy() );
00780 iconType = d->lastIconType;
00781 iconThreshold = d->lastIconThreshold;
00782 }
00783
00784
00785 if (overlay)
00786 {
00787 QImage *ovl;
00788 KIconTheme *theme = d->mpThemeRoot->theme;
00789 if ((overlay & KIcon::LockOverlay) &&
00790 ((ovl = loadOverlay(theme->lockOverlay(), size)) != 0L))
00791 KIconEffect::overlay(*img, *ovl);
00792 if ((overlay & KIcon::LinkOverlay) &&
00793 ((ovl = loadOverlay(theme->linkOverlay(), size)) != 0L))
00794 KIconEffect::overlay(*img, *ovl);
00795 if ((overlay & KIcon::ZipOverlay) &&
00796 ((ovl = loadOverlay(theme->zipOverlay(), size)) != 0L))
00797 KIconEffect::overlay(*img, *ovl);
00798 if ((overlay & KIcon::ShareOverlay) &&
00799 ((ovl = loadOverlay(theme->shareOverlay(), size)) != 0L))
00800 KIconEffect::overlay(*img, *ovl);
00801 if (overlay & KIcon::HiddenOverlay)
00802 {
00803 if (img->depth() != 32)
00804 *img = img->convertDepth(32);
00805 for (int y = 0; y < img->height(); y++)
00806 {
00807 QRgb *line = reinterpret_cast<QRgb *>(img->scanLine(y));
00808 for (int x = 0; x < img->width(); x++)
00809 line[x] = (line[x] & 0x00ffffff) | (QMIN(0x80, qAlpha(line[x])) << 24);
00810 }
00811 }
00812 }
00813
00814
00815 if (iconType == KIcon::Scalable && size != img->width())
00816 {
00817 *img = img->smoothScale(size, size);
00818 }
00819 if (iconType == KIcon::Threshold && size != img->width())
00820 {
00821 if ( abs(size-img->width())>iconThreshold )
00822 *img = img->smoothScale(size, size);
00823 }
00824 if (group >= 0 && d->mpGroups[group].dblPixels)
00825 {
00826 *img = d->mpEffect.doublePixels(*img);
00827 }
00828 if (group >= 0)
00829 {
00830 *img = d->mpEffect.apply(*img, group, state);
00831 }
00832
00833 if (favIconOverlay)
00834 {
00835 QImage favIcon(name, "PNG");
00836 int x = img->width() - favIcon.width() - 1,
00837 y = img->height() - favIcon.height() - 1;
00838 if( favIcon.depth() != 32 )
00839 favIcon = favIcon.convertDepth( 32 );
00840 if( img->depth() != 32 )
00841 *img = img->convertDepth( 32 );
00842 for( int line = 0;
00843 line < favIcon.height();
00844 ++line )
00845 {
00846 QRgb* fpos = reinterpret_cast< QRgb* >( favIcon.scanLine( line ));
00847 QRgb* ipos = reinterpret_cast< QRgb* >( img->scanLine( line + y )) + x;
00848 for( int i = 0;
00849 i < favIcon.width();
00850 ++i, ++fpos, ++ipos )
00851 *ipos = qRgba( ( qRed( *ipos ) * ( 255 - qAlpha( *fpos )) + qRed( *fpos ) * qAlpha( *fpos )) / 255,
00852 ( qGreen( *ipos ) * ( 255 - qAlpha( *fpos )) + qGreen( *fpos ) * qAlpha( *fpos )) / 255,
00853 ( qBlue( *ipos ) * ( 255 - qAlpha( *fpos )) + qBlue( *fpos ) * qAlpha( *fpos )) / 255,
00854 ( qAlpha( *ipos ) * ( 255 - qAlpha( *fpos )) + qAlpha( *fpos ) * qAlpha( *fpos )) / 255 );
00855 }
00856 }
00857
00858 pix.convertFromImage(*img);
00859
00860 delete img;
00861
00862 QPixmapCache::insert(key, pix);
00863 return pix;
00864 }
00865
00866 QImage *KIconLoader::loadOverlay(const QString &name, int size) const
00867 {
00868 QString key = name + '_' + QString::number(size);
00869 QImage *image = d->imgDict.find(key);
00870 if (image != 0L)
00871 return image;
00872
00873 KIcon icon = findMatchingIcon(name, size);
00874 if (!icon.isValid())
00875 {
00876 kdDebug(264) << "Overlay " << name << "not found." << endl;
00877 return 0L;
00878 }
00879 image = new QImage(icon.path);
00880
00881
00882 if ( size != image->width() )
00883 *image = image->smoothScale( size, size );
00884 d->imgDict.insert(key, image);
00885 return image;
00886 }
00887
00888
00889
00890 QMovie KIconLoader::loadMovie(const QString& name, KIcon::Group group, int size) const
00891 {
00892 QString file = moviePath( name, group, size );
00893 if (file.isEmpty())
00894 return QMovie();
00895 int dirLen = file.findRev('/');
00896 QString icon = iconPath(name, size ? -size : group, true);
00897 if (!icon.isEmpty() && file.left(dirLen) != icon.left(dirLen))
00898 return QMovie();
00899 return QMovie(file);
00900 }
00901
00902 QString KIconLoader::moviePath(const QString& name, KIcon::Group group, int size) const
00903 {
00904 if (!d->mpGroups) return QString::null;
00905
00906 if ( (group < -1 || group >= KIcon::LastGroup) && group != KIcon::User )
00907 {
00908 kdDebug(264) << "Illegal icon group: " << group << endl;
00909 group = KIcon::Desktop;
00910 }
00911 if (size == 0 && group < 0)
00912 {
00913 kdDebug(264) << "Neither size nor group specified!" << endl;
00914 group = KIcon::Desktop;
00915 }
00916
00917 QString file = name + ".mng";
00918 if (group == KIcon::User)
00919 {
00920 file = d->mpDirs->findResource("appicon", file);
00921 }
00922 else
00923 {
00924 if (size == 0)
00925 size = d->mpGroups[group].size;
00926
00927 KIcon icon;
00928
00929 for ( KIconThemeNode *themeNode = d->links.first() ; themeNode ;
00930 themeNode = d->links.next() )
00931 {
00932 icon = themeNode->theme->iconPath(file, size, KIcon::MatchExact);
00933 if (icon.isValid()) goto icon_found ;
00934
00935 icon = themeNode->theme->iconPath(file, size, KIcon::MatchBest);
00936 if (icon.isValid()) goto icon_found ;
00937 }
00938
00939 icon_found:
00940 file = icon.isValid() ? icon.path : QString::null;
00941 }
00942 return file;
00943 }
00944
00945
00946 QStringList KIconLoader::loadAnimated(const QString& name, KIcon::Group group, int size) const
00947 {
00948 QStringList lst;
00949
00950 if (!d->mpGroups) return lst;
00951
00952 if ((group < -1) || (group >= KIcon::LastGroup))
00953 {
00954 kdDebug(264) << "Illegal icon group: " << group << endl;
00955 group = KIcon::Desktop;
00956 }
00957 if ((size == 0) && (group < 0))
00958 {
00959 kdDebug(264) << "Neither size nor group specified!" << endl;
00960 group = KIcon::Desktop;
00961 }
00962
00963 QString file = name + "/0001";
00964 if (group == KIcon::User)
00965 {
00966 file = d->mpDirs->findResource("appicon", file + ".png");
00967 } else
00968 {
00969 if (size == 0)
00970 size = d->mpGroups[group].size;
00971 KIcon icon = findMatchingIcon(file, size);
00972 file = icon.isValid() ? icon.path : QString::null;
00973
00974 }
00975 if (file.isEmpty())
00976 return lst;
00977
00978 QString path = file.left(file.length()-8);
00979 DIR* dp = opendir( QFile::encodeName(path) );
00980 if(!dp)
00981 return lst;
00982
00983 struct dirent* ep;
00984 while( ( ep = readdir( dp ) ) != 0L )
00985 {
00986 QString fn(QFile::decodeName(ep->d_name));
00987 if(!(fn.left(4)).toUInt())
00988 continue;
00989
00990 lst += path + fn;
00991 }
00992 closedir ( dp );
00993 lst.sort();
00994 return lst;
00995 }
00996
00997 KIconTheme *KIconLoader::theme() const
00998 {
00999 if (d->mpThemeRoot) return d->mpThemeRoot->theme;
01000 return 0L;
01001 }
01002
01003 int KIconLoader::currentSize(KIcon::Group group) const
01004 {
01005 if (!d->mpGroups) return -1;
01006
01007 if (group < 0 || group >= KIcon::LastGroup)
01008 {
01009 kdDebug(264) << "Illegal icon group: " << group << endl;
01010 return -1;
01011 }
01012 return d->mpGroups[group].size;
01013 }
01014
01015 QStringList KIconLoader::queryIconsByDir( const QString& iconsDir ) const
01016 {
01017 QDir dir(iconsDir);
01018 QStringList lst = dir.entryList("*.png;*.xpm", QDir::Files);
01019 QStringList result;
01020 QStringList::ConstIterator it;
01021 for (it=lst.begin(); it!=lst.end(); ++it)
01022 result += iconsDir + "/" + *it;
01023 return result;
01024 }
01025
01026 QStringList KIconLoader::queryIconsByContext(int group_or_size,
01027 KIcon::Context context) const
01028 {
01029 QStringList result;
01030 if (group_or_size >= KIcon::LastGroup)
01031 {
01032 kdDebug(264) << "Illegal icon group: " << group_or_size << endl;
01033 return result;
01034 }
01035 int size;
01036 if (group_or_size >= 0)
01037 size = d->mpGroups[group_or_size].size;
01038 else
01039 size = -group_or_size;
01040
01041 for ( KIconThemeNode *themeNode = d->links.first() ; themeNode ;
01042 themeNode = d->links.next() )
01043 themeNode->queryIconsByContext(&result, size, context);
01044
01045
01046 QString name;
01047 QStringList res2, entries;
01048 QStringList::ConstIterator it;
01049 for (it=result.begin(); it!=result.end(); ++it)
01050 {
01051 int n = (*it).findRev('/');
01052 if (n == -1)
01053 name = *it;
01054 else
01055 name = (*it).mid(n+1);
01056 name = removeIconExtension(name);
01057 if (!entries.contains(name))
01058 {
01059 entries += name;
01060 res2 += *it;
01061 }
01062 }
01063 return res2;
01064
01065 }
01066
01067 QStringList KIconLoader::queryIcons(int group_or_size, KIcon::Context context) const
01068 {
01069 QStringList result;
01070 if (group_or_size >= KIcon::LastGroup)
01071 {
01072 kdDebug(264) << "Illegal icon group: " << group_or_size << endl;
01073 return result;
01074 }
01075 int size;
01076 if (group_or_size >= 0)
01077 size = d->mpGroups[group_or_size].size;
01078 else
01079 size = -group_or_size;
01080
01081 for ( KIconThemeNode *themeNode = d->links.first() ; themeNode ;
01082 themeNode = d->links.next() )
01083 themeNode->queryIcons(&result, size, context);
01084
01085
01086 QString name;
01087 QStringList res2, entries;
01088 QStringList::ConstIterator it;
01089 for (it=result.begin(); it!=result.end(); ++it)
01090 {
01091 int n = (*it).findRev('/');
01092 if (n == -1)
01093 name = *it;
01094 else
01095 name = (*it).mid(n+1);
01096 name = removeIconExtension(name);
01097 if (!entries.contains(name))
01098 {
01099 entries += name;
01100 res2 += *it;
01101 }
01102 }
01103 return res2;
01104 }
01105
01106
01107 bool KIconLoader::hasContext(KIcon::Context context) const
01108 {
01109 for ( KIconThemeNode *themeNode = d->links.first() ; themeNode ;
01110 themeNode = d->links.next() )
01111 if( themeNode->theme->hasContext( context ))
01112 return true;
01113 return false;
01114 }
01115
01116 KIconEffect * KIconLoader::iconEffect() const
01117 {
01118 return &d->mpEffect;
01119 }
01120
01121 bool KIconLoader::alphaBlending(KIcon::Group group) const
01122 {
01123 if (!d->mpGroups) return false;
01124
01125 if (group < 0 || group >= KIcon::LastGroup)
01126 {
01127 kdDebug(264) << "Illegal icon group: " << group << endl;
01128 return false;
01129 }
01130 return d->mpGroups[group].alphaBlending;
01131 }
01132
01133 QIconSet KIconLoader::loadIconSet(const QString& name, KIcon::Group group, int size, bool canReturnNull)
01134 {
01135 return loadIconSet( name, group, size, canReturnNull, true );
01136 }
01137
01138 QIconSet KIconLoader::loadIconSet(const QString& name, KIcon::Group group, int size)
01139 {
01140 return loadIconSet( name, group, size, false );
01141 }
01142
01143
01144
01145 class KIconFactory
01146 : public QIconFactory
01147 {
01148 public:
01149 KIconFactory( const QString& iconName_P, KIcon::Group group_P,
01150 int size_P, KIconLoader* loader_P );
01151 KIconFactory( const QString& iconName_P, KIcon::Group group_P,
01152 int size_P, KIconLoader* loader_P, bool canReturnNull );
01153 virtual QPixmap* createPixmap( const QIconSet&, QIconSet::Size, QIconSet::Mode, QIconSet::State );
01154 private:
01155 QString iconName;
01156 KIcon::Group group;
01157 int size;
01158 KIconLoader* loader;
01159 bool canReturnNull;
01160 };
01161
01162
01163 QIconSet KIconLoader::loadIconSet( const QString& name, KIcon::Group g, int s,
01164 bool canReturnNull, bool immediateExistenceCheck)
01165 {
01166 if ( !d->delayedLoading )
01167 return loadIconSetNonDelayed( name, g, s, canReturnNull );
01168
01169 if (g < -1 || g > 6) {
01170 kdDebug() << "KIconLoader::loadIconSet " << name << " " << (int)g << " " << s << endl;
01171 qDebug("%s", kdBacktrace().latin1());
01172 abort();
01173 }
01174
01175 if(canReturnNull && immediateExistenceCheck)
01176 {
01177 QPixmap pm = loadIcon( name, g, s, KIcon::DefaultState, NULL, true );
01178 if( pm.isNull())
01179 return QIconSet();
01180
01181 QIconSet ret( pm );
01182 ret.installIconFactory( new KIconFactory( name, g, s, this ));
01183 return ret;
01184 }
01185
01186 QIconSet ret;
01187 ret.installIconFactory( new KIconFactory( name, g, s, this, canReturnNull ));
01188 return ret;
01189 }
01190
01191 QIconSet KIconLoader::loadIconSetNonDelayed( const QString& name,
01192 KIcon::Group g,
01193 int s, bool canReturnNull )
01194 {
01195 QIconSet iconset;
01196 QPixmap tmp = loadIcon(name, g, s, KIcon::ActiveState, NULL, canReturnNull);
01197 iconset.setPixmap( tmp, QIconSet::Small, QIconSet::Active );
01198
01199 iconset.setPixmap( tmp, QIconSet::Large, QIconSet::Active );
01200 tmp = loadIcon(name, g, s, KIcon::DisabledState, NULL, canReturnNull);
01201 iconset.setPixmap( tmp, QIconSet::Small, QIconSet::Disabled );
01202 iconset.setPixmap( tmp, QIconSet::Large, QIconSet::Disabled );
01203 tmp = loadIcon(name, g, s, KIcon::DefaultState, NULL, canReturnNull);
01204 iconset.setPixmap( tmp, QIconSet::Small, QIconSet::Normal );
01205 iconset.setPixmap( tmp, QIconSet::Large, QIconSet::Normal );
01206 return iconset;
01207 }
01208
01209 KIconFactory::KIconFactory( const QString& iconName_P, KIcon::Group group_P,
01210 int size_P, KIconLoader* loader_P )
01211 : iconName( iconName_P ), group( group_P ), size( size_P ), loader( loader_P )
01212 {
01213 canReturnNull = false;
01214 setAutoDelete( true );
01215 }
01216
01217 KIconFactory::KIconFactory( const QString& iconName_P, KIcon::Group group_P,
01218 int size_P, KIconLoader* loader_P, bool canReturnNull_P )
01219 : iconName( iconName_P ), group( group_P ), size( size_P ),
01220 loader( loader_P ), canReturnNull( canReturnNull_P)
01221 {
01222 setAutoDelete( true );
01223 }
01224
01225 QPixmap* KIconFactory::createPixmap( const QIconSet&, QIconSet::Size, QIconSet::Mode mode_P, QIconSet::State )
01226 {
01227 #ifdef KICONLOADER_CHECKS
01228 bool found = false;
01229 for( QValueList< KIconLoaderDebug >::Iterator it = kiconloaders->begin();
01230 it != kiconloaders->end();
01231 ++it )
01232 {
01233 if( (*it).loader == loader )
01234 {
01235 found = true;
01236 if( !(*it).valid )
01237 {
01238 #ifdef NDEBUG
01239 loader = KGlobal::iconLoader();
01240 iconName = "no_way_man_you_will_get_broken_icon";
01241 #else
01242 kdWarning() << "Using already destroyed KIconLoader for loading an icon!" << endl;
01243 kdWarning() << "Appname:" << (*it).appname << ", icon:" << iconName << endl;
01244 kdWarning() << "Deleted at:" << endl;
01245 kdWarning() << (*it).delete_bt << endl;
01246 kdWarning() << "Current:" << endl;
01247 kdWarning() << kdBacktrace() << endl;
01248 abort();
01249 return NULL;
01250 #endif
01251 }
01252 break;
01253 }
01254 }
01255 if( !found )
01256 {
01257 #ifdef NDEBUG
01258 loader = KGlobal::iconLoader();
01259 iconName = "no_way_man_you_will_get_broken_icon";
01260 #else
01261 kdWarning() << "Using unknown KIconLoader for loading an icon!" << endl;
01262 kdWarning() << "Icon:" << iconName << endl;
01263 kdWarning() << kdBacktrace() << endl;
01264 abort();
01265 return NULL;
01266 #endif
01267 }
01268 #endif
01269
01270 static const KIcon::States tbl[] = { KIcon::DefaultState, KIcon::DisabledState, KIcon::ActiveState };
01271 int state = KIcon::DefaultState;
01272 if( mode_P <= QIconSet::Active )
01273 state = tbl[ mode_P ];
01274 if( group >= 0 && state == KIcon::ActiveState )
01275 {
01276 if( loader->iconEffect()->fingerprint(group, KIcon::ActiveState )
01277 == loader->iconEffect()->fingerprint(group, KIcon::DefaultState ))
01278 return 0;
01279 }
01280
01281
01282 QPixmap pm = loader->loadIcon( iconName, group, size, state, 0, canReturnNull );
01283 return new QPixmap( pm );
01284 }
01285
01286
01287
01288 QPixmap DesktopIcon(const QString& name, int force_size, int state,
01289 KInstance *instance)
01290 {
01291 KIconLoader *loader = instance->iconLoader();
01292 return loader->loadIcon(name, KIcon::Desktop, force_size, state);
01293 }
01294
01295 QPixmap DesktopIcon(const QString& name, KInstance *instance)
01296 {
01297 return DesktopIcon(name, 0, KIcon::DefaultState, instance);
01298 }
01299
01300 QIconSet DesktopIconSet(const QString& name, int force_size, KInstance *instance)
01301 {
01302 KIconLoader *loader = instance->iconLoader();
01303 return loader->loadIconSet( name, KIcon::Desktop, force_size );
01304 }
01305
01306 QPixmap BarIcon(const QString& name, int force_size, int state,
01307 KInstance *instance)
01308 {
01309 KIconLoader *loader = instance->iconLoader();
01310 return loader->loadIcon(name, KIcon::Toolbar, force_size, state);
01311 }
01312
01313 QPixmap BarIcon(const QString& name, KInstance *instance)
01314 {
01315 return BarIcon(name, 0, KIcon::DefaultState, instance);
01316 }
01317
01318 QIconSet BarIconSet(const QString& name, int force_size, KInstance *instance)
01319 {
01320 KIconLoader *loader = instance->iconLoader();
01321 return loader->loadIconSet( name, KIcon::Toolbar, force_size );
01322 }
01323
01324 QPixmap SmallIcon(const QString& name, int force_size, int state,
01325 KInstance *instance)
01326 {
01327 KIconLoader *loader = instance->iconLoader();
01328 return loader->loadIcon(name, KIcon::Small, force_size, state);
01329 }
01330
01331 QPixmap SmallIcon(const QString& name, KInstance *instance)
01332 {
01333 return SmallIcon(name, 0, KIcon::DefaultState, instance);
01334 }
01335
01336 QIconSet SmallIconSet(const QString& name, int force_size, KInstance *instance)
01337 {
01338 KIconLoader *loader = instance->iconLoader();
01339 return loader->loadIconSet( name, KIcon::Small, force_size );
01340 }
01341
01342 QPixmap MainBarIcon(const QString& name, int force_size, int state,
01343 KInstance *instance)
01344 {
01345 KIconLoader *loader = instance->iconLoader();
01346 return loader->loadIcon(name, KIcon::MainToolbar, force_size, state);
01347 }
01348
01349 QPixmap MainBarIcon(const QString& name, KInstance *instance)
01350 {
01351 return MainBarIcon(name, 0, KIcon::DefaultState, instance);
01352 }
01353
01354 QIconSet MainBarIconSet(const QString& name, int force_size, KInstance *instance)
01355 {
01356 KIconLoader *loader = instance->iconLoader();
01357 return loader->loadIconSet( name, KIcon::MainToolbar, force_size );
01358 }
01359
01360 QPixmap UserIcon(const QString& name, int state, KInstance *instance)
01361 {
01362 KIconLoader *loader = instance->iconLoader();
01363 return loader->loadIcon(name, KIcon::User, 0, state);
01364 }
01365
01366 QPixmap UserIcon(const QString& name, KInstance *instance)
01367 {
01368 return UserIcon(name, KIcon::DefaultState, instance);
01369 }
01370
01371 QIconSet UserIconSet(const QString& name, KInstance *instance)
01372 {
01373 KIconLoader *loader = instance->iconLoader();
01374 return loader->loadIconSet( name, KIcon::User );
01375 }
01376
01377 int IconSize(KIcon::Group group, KInstance *instance)
01378 {
01379 KIconLoader *loader = instance->iconLoader();
01380 return loader->currentSize(group);
01381 }
01382
01383 QPixmap KIconLoader::unknown()
01384 {
01385 QPixmap pix;
01386 if ( QPixmapCache::find("unknown", pix) )
01387 return pix;
01388
01389 QString path = KGlobal::iconLoader()->iconPath("unknown", KIcon::Small, true);
01390 if (path.isEmpty())
01391 {
01392 kdDebug(264) << "Warning: Cannot find \"unknown\" icon." << endl;
01393 pix.resize(32,32);
01394 } else
01395 {
01396 pix.load(path);
01397 QPixmapCache::insert("unknown", pix);
01398 }
01399
01400 return pix;
01401 }
01402
01403 void KIconLoaderPrivate::reconfigure()
01404 {
01405 q->reconfigure(appname, mpDirs);
01406 }
01407
01408 #include "kiconloader_p.moc"