kdecore Library API Documentation

kicontheme.cpp

00001 /* vi: ts=8 sts=4 sw=4
00002  *
00003  * $Id: kicontheme.cpp 357668 2004-10-25 19:22:32Z wildfox $
00004  *
00005  * This file is part of the KDE project, module kdecore.
00006  * Copyright (C) 2000 Geert Jansen <jansen@kde.org>
00007  *                    Antonio Larrosa <larrosa@kde.org>
00008  *
00009  * This is free software; it comes under the GNU Library General
00010  * Public License, version 2. See the file "COPYING.LIB" for the
00011  * exact licensing terms.
00012  *
00013  * kicontheme.cpp: Lowlevel icon theme handling.
00014  */
00015 
00016 #include <sys/stat.h>
00017 #include <unistd.h>
00018 #include <stdlib.h>
00019 #include <config.h>
00020 
00021 #include <qstring.h>
00022 #include <qstringlist.h>
00023 #include <qvaluelist.h>
00024 #include <qmap.h>
00025 #include <qpixmap.h>
00026 #include <qpixmapcache.h>
00027 #include <qimage.h>
00028 #include <qfileinfo.h>
00029 #include <qdir.h>
00030 
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 
00040 class KIconThemePrivate
00041 {
00042 public:
00043     QString example, screenshot;
00044     QString linkOverlay, lockOverlay, zipOverlay, shareOverlay;
00045     bool hidden;
00046     KSharedConfig::Ptr sharedConfig;
00047 };
00048 
00052 class KIconThemeDir
00053 {
00054 public:
00055     KIconThemeDir(const QString& dir, const KConfigBase *config);
00056 
00057     bool isValid() const { return mbValid; }
00058     QString iconPath(const QString& name) const;
00059     QStringList iconList() const;
00060     QString dir() const { return mDir; }
00061 
00062     KIcon::Context context() const { return mContext; }
00063     KIcon::Type type() const { return mType; }
00064     int size() const { return mSize; }
00065     int minSize() const { return mMinSize; }
00066     int maxSize() const { return mMaxSize; }
00067     int threshold() const { return mThreshold; }
00068 
00069 private:
00070     bool mbValid;
00071     KIcon::Type mType;
00072     KIcon::Context mContext;
00073     int mSize, mMinSize, mMaxSize;
00074     int mThreshold;
00075 
00076     QString mDir;
00077 };
00078 
00079 
00080 /*** KIconTheme ***/
00081 
00082 KIconTheme::KIconTheme(const QString& name, const QString& appName)
00083 {
00084     d = new KIconThemePrivate;
00085 
00086     QStringList icnlibs;
00087     QStringList::ConstIterator it, itDir;
00088     QStringList themeDirs;
00089     QString cDir;
00090 
00091     // Applications can have local additions to the global "locolor" and
00092     // "hicolor" icon themes. For these, the _global_ theme description
00093     // files are used..
00094 
00095     if (!appName.isEmpty() &&
00096        ( name == "crystalsvg" || name== "hicolor" || name == "locolor" ) )
00097     {
00098     icnlibs = KGlobal::dirs()->resourceDirs("data");
00099     for (it=icnlibs.begin(); it!=icnlibs.end(); ++it)
00100     {
00101         cDir = *it + appName + "/icons/" + name;
00102         if (QFile::exists( cDir ))
00103         themeDirs += cDir + "/";
00104     }
00105     }
00106     // Find the theme description file. These are always global.
00107 
00108     icnlibs = KGlobal::dirs()->resourceDirs("icon");
00109     for (it=icnlibs.begin(); it!=icnlibs.end(); ++it)
00110     {
00111         cDir = *it + name + "/";
00112         if (KStandardDirs::exists(cDir))
00113         {
00114             themeDirs += cDir;
00115         if (mDir.isEmpty()
00116             && (KStandardDirs::exists( cDir + "index.desktop") || KStandardDirs::exists( cDir + "index.theme")))
00117         mDir = cDir;
00118         }
00119     }
00120 
00121     if (mDir.isEmpty())
00122     {
00123         kdDebug(264) << "Icon theme " << name << " not found.\n";
00124         return;
00125     }
00126 
00127     QString fileName, mainSection;
00128     if(QFile::exists(mDir + "index.desktop")) {
00129     fileName = mDir + "index.desktop";
00130     mainSection="KDE Icon Theme";
00131     } else {
00132     fileName = mDir + "index.theme";
00133     mainSection="Icon Theme";
00134     }
00135     // Use KSharedConfig to avoid parsing the file many times, from each kinstance.
00136     // Need to keep a ref to it to make this useful
00137     d->sharedConfig = KSharedConfig::openConfig( fileName, true /*readonly*/, false /*useKDEGlobals*/ );
00138     KConfig& cfg = *d->sharedConfig;
00139     //was: KSimpleConfig cfg(fileName);
00140 
00141     cfg.setGroup(mainSection);
00142     mName = cfg.readEntry("Name");
00143     mDesc = cfg.readEntry("Comment");
00144     mDepth = cfg.readNumEntry("DisplayDepth", 32);
00145     mInherits = cfg.readListEntry("Inherits");
00146     if ( name != "crystalsvg" )
00147       for ( QStringList::Iterator it = mInherits.begin(); it != mInherits.end(); ++it )
00148          if ( *it == "default" || *it == "hicolor" ) *it="crystalsvg";
00149     if ( name == "hicolor" )
00150     mInherits = "crystalsuse";
00151 
00152     d->hidden = cfg.readBoolEntry("Hidden", false);
00153     d->example = cfg.readPathEntry("Example");
00154     d->screenshot = cfg.readPathEntry("ScreenShot");
00155     d->linkOverlay = cfg.readEntry("LinkOverlay", "link");
00156     d->lockOverlay = cfg.readEntry("LockOverlay", "lock");
00157     d->zipOverlay = cfg.readEntry("ZipOverlay", "zip");
00158     d->shareOverlay = cfg.readEntry("ShareOverlay","share");
00159 
00160     QStringList dirs = cfg.readPathListEntry("Directories");
00161     mDirs.setAutoDelete(true);
00162     for (it=dirs.begin(); it!=dirs.end(); ++it)
00163     {
00164     cfg.setGroup(*it);
00165     for (itDir=themeDirs.begin(); itDir!=themeDirs.end(); ++itDir)
00166     {
00167         if (KStandardDirs::exists(*itDir + *it + "/"))
00168         {
00169             KIconThemeDir *dir = new KIconThemeDir(*itDir + *it, &cfg);
00170             if (!dir->isValid())
00171             {
00172                 kdDebug(264) << "Icon directory " << *itDir << " group " << *it << " not valid.\n";
00173                 delete dir;
00174             }
00175             else
00176                 mDirs.append(dir);
00177             }
00178         }
00179     }
00180 
00181     // Expand available sizes for scalable icons to their full range
00182     int i;
00183     QMap<int,QValueList<int> > scIcons;
00184     for (KIconThemeDir *dir=mDirs.first(); dir!=0L; dir=mDirs.next())
00185     {
00186         if ((dir->type() == KIcon::Scalable) && !scIcons.contains(dir->size()))
00187         {
00188             QValueList<int> lst;
00189             for (i=dir->minSize(); i<=dir->maxSize(); i++)
00190                 lst += i;
00191             scIcons[dir->size()] = lst;
00192         }
00193     }
00194 
00195     QStringList groups;
00196     groups += "Desktop";
00197     groups += "Toolbar";
00198     groups += "MainToolbar";
00199     groups += "Small";
00200     groups += "Panel";
00201     const int defDefSizes[] = { 32, 22, 22, 16, 32 };
00202     cfg.setGroup(mainSection);
00203     for (it=groups.begin(), i=0; it!=groups.end(); ++it, i++)
00204     {
00205         mDefSize[i] = cfg.readNumEntry(*it + "Default", defDefSizes[i]);
00206         QValueList<int> exp, lst = cfg.readIntListEntry(*it + "Sizes");
00207         QValueList<int>::ConstIterator it2;
00208         for (it2=lst.begin(); it2!=lst.end(); ++it2)
00209         {
00210             if (scIcons.contains(*it2))
00211                 exp += scIcons[*it2];
00212             else
00213                 exp += *it2;
00214         }
00215         mSizes[i] = exp;
00216     }
00217 
00218 }
00219 
00220 KIconTheme::~KIconTheme()
00221 {
00222     delete d;
00223 }
00224 
00225 bool KIconTheme::isValid() const
00226 {
00227     return !mDirs.isEmpty();
00228 }
00229 
00230 bool KIconTheme::isHidden() const
00231 {
00232     return d->hidden;
00233 }
00234 
00235 QString KIconTheme::example() const { return d->example; }
00236 QString KIconTheme::screenshot() const { return d->screenshot; }
00237 QString KIconTheme::linkOverlay() const { return d->linkOverlay; }
00238 QString KIconTheme::lockOverlay() const { return d->lockOverlay; }
00239 QString KIconTheme::zipOverlay() const { return d->zipOverlay; }
00240 QString KIconTheme::shareOverlay() const { return d->shareOverlay; }
00241 
00242 int KIconTheme::defaultSize(KIcon::Group group) const
00243 {
00244     if ((group < 0) || (group >= KIcon::LastGroup))
00245     {
00246         kdDebug(264) << "Illegal icon group: " << group << "\n";
00247         return -1;
00248     }
00249     return mDefSize[group];
00250 }
00251 
00252 QValueList<int> KIconTheme::querySizes(KIcon::Group group) const
00253 {
00254     QValueList<int> empty;
00255     if ((group < 0) || (group >= KIcon::LastGroup))
00256     {
00257         kdDebug(264) << "Illegal icon group: " << group << "\n";
00258         return empty;
00259     }
00260     return mSizes[group];
00261 }
00262 
00263 QStringList KIconTheme::queryIcons(int size, KIcon::Context context) const
00264 {
00265     int delta = 1000, dw;
00266 
00267     QPtrListIterator<KIconThemeDir> dirs(mDirs);
00268     KIconThemeDir *dir;
00269 
00270     // Try to find exact match
00271     QStringList result;
00272     for ( ; dirs.current(); ++dirs)
00273     {
00274         dir = dirs.current();
00275         if ((context != KIcon::Any) && (context != dir->context()))
00276             continue;
00277         if ((dir->type() == KIcon::Fixed) && (dir->size() == size))
00278         {
00279             result += dir->iconList();
00280             continue;
00281         }
00282         if ((dir->type() == KIcon::Scalable) &&
00283             (size >= dir->minSize()) && (size <= dir->maxSize()))
00284         {
00285             result += dir->iconList();
00286             continue;
00287         }
00288     if ((dir->type() == KIcon::Threshold) &&
00289             (abs(size-dir->size())<dir->threshold()))
00290             result+=dir->iconList();
00291     }
00292 
00293     return result;
00294 
00295     dirs.toFirst();
00296 
00297     // Find close match
00298     KIconThemeDir *best = 0L;
00299     for ( ; dirs.current(); ++dirs)
00300     {
00301         dir = dirs.current();
00302         if ((context != KIcon::Any) && (context != dir->context()))
00303             continue;
00304         dw = dir->size() - size;
00305         if ((dw > 6) || (abs(dw) >= abs(delta)))
00306             continue;
00307         delta = dw;
00308         best = dir;
00309     }
00310     if (best == 0L)
00311         return QStringList();
00312 
00313     return best->iconList();
00314 }
00315 
00316 QStringList KIconTheme::queryIconsByContext(int size, KIcon::Context context) const
00317 {
00318     QPtrListIterator<KIconThemeDir> dirs(mDirs);
00319     int dw;
00320     KIconThemeDir *dir;
00321 
00322     // We want all the icons for a given context, but we prefer icons
00323     // of size size . Note that this may (will) include duplicate icons
00324     //QStringList iconlist[34]; // 33 == 48-16+1
00325     QStringList iconlist[128]; // 33 == 48-16+1
00326     // Usually, only the 0, 6 (22-16), 10 (32-22), 16 (48-32 or 32-16),
00327     // 26 (48-22) and 32 (48-16) will be used, but who knows if someone
00328     // will make icon themes with different icon sizes.
00329 
00330     for ( ; dirs.current(); ++dirs)
00331     {
00332         dir = dirs.current();
00333         if ((context != KIcon::Any) && (context != dir->context()))
00334             continue;
00335         dw = abs(dir->size() - size);
00336         iconlist[(dw<127)?dw:127]+=dir->iconList();
00337     }
00338 
00339     QStringList iconlistResult;
00340     for (int i=0; i<128; i++) iconlistResult+=iconlist[i];
00341 
00342     return iconlistResult;
00343 }
00344 
00345 KIcon KIconTheme::iconPath(const QString& name, int size, KIcon::MatchType match) const
00346 {
00347     KIcon icon;
00348     QString path;
00349     int delta = -1000, dw;
00350     KIconThemeDir *dir;
00351 
00352     dw = 1000; // shut up, gcc
00353     QPtrListIterator<KIconThemeDir> dirs(mDirs);
00354     for ( ; dirs.current(); ++dirs)
00355     {
00356         dir = dirs.current();
00357 
00358         if (match == KIcon::MatchExact)
00359         {
00360             if ((dir->type() == KIcon::Fixed) && (dir->size() != size))
00361                 continue;
00362             if ((dir->type() == KIcon::Scalable) &&
00363                 ((size < dir->minSize()) || (size > dir->maxSize())))
00364               continue;
00365             if ((dir->type() == KIcon::Threshold) &&
00366         (abs(dir->size()-size) > dir->threshold()))
00367                 continue;
00368         } else
00369         {
00370           // dw < 0 means need to scale up to get an icon of the requested size
00371           if (dir->type() == KIcon::Fixed)
00372           {
00373             dw = dir->size() - size;
00374           } else if (dir->type() == KIcon::Scalable)
00375           {
00376             if (size < dir->minSize())
00377               dw = dir->minSize() - size;
00378             else if (size > dir->maxSize())
00379               dw = dir->maxSize() - size;
00380             else
00381               dw = 0;
00382           } else if (dir->type() == KIcon::Threshold)
00383           {
00384             if (size < dir->size() - dir->threshold())
00385               dw = dir->size() - dir->threshold() - size;
00386             else if (size > dir->size() + dir->threshold())
00387               dw = dir->size() + dir->threshold() - size;
00388             else
00389               dw = 0;
00390           }
00391           /* Skip this if we've found a closer one, unless
00392              it's a downscale, and we only had upscales befores.
00393              This is to avoid scaling up unless we have to,
00394              since that looks very ugly */
00395           if ((abs(dw) >= abs(delta)) ||
00396               (delta > 0 && dw < 0))
00397             continue;
00398         }
00399 
00400         path = dir->iconPath(name);
00401         if (path.isEmpty())
00402             continue;
00403         icon.path = path;
00404         icon.size = dir->size();
00405         icon.type = dir->type();
00406     icon.threshold = dir->threshold();
00407         icon.context = dir->context();
00408 
00409         // if we got in MatchExact that far, we find no better
00410         if (match == KIcon::MatchExact)
00411             return icon;
00412     else
00413         {
00414         delta = dw;
00415         if (delta==0) return icon; // We won't find a better match anyway
00416         }
00417     }
00418     return icon;
00419 }
00420 
00421 // static
00422 QString *KIconTheme::_theme = 0L;
00423 
00424 // static
00425 QStringList *KIconTheme::_theme_list = 0L;
00426 
00427 // static
00428 QString KIconTheme::current()
00429 {
00430     // Static pointer because of unloading problems wrt DSO's.
00431     if (_theme != 0L)
00432         return *_theme;
00433 
00434     _theme = new QString();
00435     KConfig *config = KGlobal::config();
00436     KConfigGroupSaver saver(config, "Icons");
00437     *_theme = config->readEntry("Theme",defaultThemeName());
00438     if ( *_theme == QString::fromLatin1("hicolor") ) *_theme = defaultThemeName();
00439 /*    if (_theme->isEmpty())
00440     {
00441         if (QPixmap::defaultDepth() > 8)
00442             *_theme = defaultThemeName();
00443         else
00444             *_theme = QString::fromLatin1("locolor");
00445     }*/
00446     return *_theme;
00447 }
00448 
00449 // static
00450 QStringList KIconTheme::list()
00451 {
00452     // Static pointer because of unloading problems wrt DSO's.
00453     if (_theme_list != 0L)
00454         return *_theme_list;
00455 
00456     _theme_list = new QStringList();
00457     QStringList icnlibs = KGlobal::dirs()->resourceDirs("icon");
00458     QStringList::ConstIterator it;
00459     for (it=icnlibs.begin(); it!=icnlibs.end(); ++it)
00460     {
00461         QDir dir(*it);
00462         if (!dir.exists())
00463             continue;
00464         QStringList lst = dir.entryList(QDir::Dirs);
00465         QStringList::ConstIterator it2;
00466         for (it2=lst.begin(); it2!=lst.end(); ++it2)
00467         {
00468             if ((*it2 == ".") || (*it2 == "..") || (*it2).startsWith("default.") )
00469                 continue;
00470             if (!KStandardDirs::exists(*it + *it2 + "/index.desktop") && !KStandardDirs::exists(*it + *it2 + "/index.theme"))
00471                 continue;
00472         KIconTheme oink(*it2);
00473         if (!oink.isValid()) continue;
00474 
00475         if (!_theme_list->contains(*it2))
00476                 _theme_list->append(*it2);
00477         }
00478     }
00479     return *_theme_list;
00480 }
00481 
00482 // static
00483 void KIconTheme::reconfigure()
00484 {
00485     delete _theme;
00486     _theme=0L;
00487     delete _theme_list;
00488     _theme_list=0L;
00489 }
00490 
00491 // static
00492 QString KIconTheme::defaultThemeName()
00493 {
00494     return QString::fromLatin1("crystalsvg");
00495 }
00496 
00497 /*** KIconThemeDir ***/
00498 
00499 KIconThemeDir::KIconThemeDir(const QString& dir, const KConfigBase *config)
00500 {
00501     mbValid = false;
00502     mDir = dir;
00503     mSize = config->readNumEntry("Size");
00504     mMinSize = 1;    // just set the variables to something
00505     mMaxSize = 50;   // meaningful in case someone calls minSize or maxSize
00506     mType = KIcon::Fixed;
00507 
00508     if (mSize == 0)
00509         return;
00510 
00511     QString tmp = config->readEntry("Context");
00512     if (tmp == "Devices")
00513         mContext = KIcon::Device;
00514     else if (tmp == "MimeTypes")
00515         mContext = KIcon::MimeType;
00516     else if (tmp == "FileSystems")
00517         mContext = KIcon::FileSystem;
00518     else if (tmp == "Applications")
00519         mContext = KIcon::Application;
00520     else if (tmp == "Actions")
00521         mContext = KIcon::Action;
00522     else {
00523         kdDebug(264) << "Invalid Context= line for icon theme: " << mDir << "\n";
00524         return;
00525     }
00526     tmp = config->readEntry("Type");
00527     if (tmp == "Fixed")
00528         mType = KIcon::Fixed;
00529     else if (tmp == "Scalable")
00530         mType = KIcon::Scalable;
00531     else if (tmp == "Threshold")
00532         mType = KIcon::Threshold;
00533     else {
00534         kdDebug(264) << "Invalid Type= line for icon theme: " <<  mDir << "\n";
00535         return;
00536     }
00537     if (mType == KIcon::Scalable)
00538     {
00539         mMinSize = config->readNumEntry("MinSize", mSize);
00540         mMaxSize = config->readNumEntry("MaxSize", mSize);
00541     } else if (mType == KIcon::Threshold)
00542     mThreshold = config->readNumEntry("Threshold", 2);
00543     mbValid = true;
00544 }
00545 
00546 QString KIconThemeDir::iconPath(const QString& name) const
00547 {
00548     if (!mbValid)
00549         return QString::null;
00550     QString file = mDir + "/" + name;
00551 
00552     if (access(QFile::encodeName(file), R_OK) == 0)
00553         return file;
00554 
00555     return QString::null;
00556 }
00557 
00558 QStringList KIconThemeDir::iconList() const
00559 {
00560     QDir dir(mDir);
00561 #ifdef HAVE_LIBART
00562     QStringList lst = dir.entryList("*.png;*.svg;*.svgz;*.xpm", QDir::Files);
00563 #else
00564     QStringList lst = dir.entryList("*.png;*.xpm", QDir::Files);
00565 #endif
00566     QStringList result;
00567     QStringList::ConstIterator it;
00568     for (it=lst.begin(); it!=lst.end(); ++it)
00569         result += mDir + "/" + *it;
00570     return result;
00571 }
KDE Logo
This file is part of the documentation for kdecore Library Version 3.4.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Tue Sep 13 03:28:26 2005 by doxygen 1.4.4 written by Dimitri van Heesch, © 1997-2003