kicontheme.cpp

00001 /* vi: ts=8 sts=4 sw=4
00002  *
00003  * $Id: kicontheme.cpp 576661 2006-08-24 14:47:38Z mueller $
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     icnlibs += KGlobal::dirs()->resourceDirs("xdgdata-icon");
00110     icnlibs += "/usr/share/pixmaps";
00111     for (it=icnlibs.begin(); it!=icnlibs.end(); ++it)
00112     {
00113         cDir = *it + name + "/";
00114         if (KStandardDirs::exists(cDir))
00115         {
00116             themeDirs += cDir;
00117         if (mDir.isEmpty()
00118             && (KStandardDirs::exists( cDir + "index.desktop") || KStandardDirs::exists( cDir + "index.theme")))
00119         mDir = cDir;
00120         }
00121     }
00122 
00123     if (mDir.isEmpty())
00124     {
00125         kdDebug(264) << "Icon theme " << name << " not found.\n";
00126         return;
00127     }
00128 
00129     QString fileName, mainSection;
00130     if(QFile::exists(mDir + "index.desktop")) {
00131     fileName = mDir + "index.desktop";
00132     mainSection="KDE Icon Theme";
00133     } else {
00134     fileName = mDir + "index.theme";
00135     mainSection="Icon Theme";
00136     }
00137     // Use KSharedConfig to avoid parsing the file many times, from each kinstance.
00138     // Need to keep a ref to it to make this useful
00139     d->sharedConfig = KSharedConfig::openConfig( fileName, true /*readonly*/, false /*useKDEGlobals*/ );
00140     KConfig& cfg = *d->sharedConfig;
00141     //was: KSimpleConfig cfg(fileName);
00142 
00143     cfg.setGroup(mainSection);
00144     mName = cfg.readEntry("Name");
00145     mDesc = cfg.readEntry("Comment");
00146     mDepth = cfg.readNumEntry("DisplayDepth", 32);
00147     mInherits = cfg.readListEntry("Inherits");
00148     if ( name != "crystalsvg" )
00149       for ( QStringList::Iterator it = mInherits.begin(); it != mInherits.end(); ++it )
00150          if ( *it == "default" || *it == "hicolor" ) *it="crystalsvg";
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 bool KIconTheme::hasContext(KIcon::Context context) const
00346 {
00347     QPtrListIterator<KIconThemeDir> dirs(mDirs);
00348     KIconThemeDir *dir;
00349 
00350     for ( ; dirs.current(); ++dirs)
00351     {
00352         dir = dirs.current();
00353         if ((context == KIcon::Any) || (context == dir->context()))
00354             return true;
00355     }
00356     return false;
00357 }
00358 
00359 KIcon KIconTheme::iconPath(const QString& name, int size, KIcon::MatchType match) const
00360 {
00361     KIcon icon;
00362     QString path;
00363     int delta = -1000, dw;
00364     KIconThemeDir *dir;
00365 
00366     dw = 1000; // shut up, gcc
00367     QPtrListIterator<KIconThemeDir> dirs(mDirs);
00368     for ( ; dirs.current(); ++dirs)
00369     {
00370         dir = dirs.current();
00371 
00372         if (match == KIcon::MatchExact)
00373         {
00374             if ((dir->type() == KIcon::Fixed) && (dir->size() != size))
00375                 continue;
00376             if ((dir->type() == KIcon::Scalable) &&
00377                 ((size < dir->minSize()) || (size > dir->maxSize())))
00378               continue;
00379             if ((dir->type() == KIcon::Threshold) &&
00380         (abs(dir->size()-size) > dir->threshold()))
00381                 continue;
00382         } else
00383         {
00384           // dw < 0 means need to scale up to get an icon of the requested size
00385           if (dir->type() == KIcon::Fixed)
00386           {
00387             dw = dir->size() - size;
00388           } else if (dir->type() == KIcon::Scalable)
00389           {
00390             if (size < dir->minSize())
00391               dw = dir->minSize() - size;
00392             else if (size > dir->maxSize())
00393               dw = dir->maxSize() - size;
00394             else
00395               dw = 0;
00396           } else if (dir->type() == KIcon::Threshold)
00397           {
00398             if (size < dir->size() - dir->threshold())
00399               dw = dir->size() - dir->threshold() - size;
00400             else if (size > dir->size() + dir->threshold())
00401               dw = dir->size() + dir->threshold() - size;
00402             else
00403               dw = 0;
00404           }
00405           /* Skip this if we've found a closer one, unless
00406              it's a downscale, and we only had upscales befores.
00407              This is to avoid scaling up unless we have to,
00408              since that looks very ugly */
00409           if ((abs(dw) >= abs(delta)) ||
00410               (delta > 0 && dw < 0))
00411             continue;
00412         }
00413 
00414         path = dir->iconPath(name);
00415         if (path.isEmpty())
00416             continue;
00417         icon.path = path;
00418         icon.size = dir->size();
00419         icon.type = dir->type();
00420     icon.threshold = dir->threshold();
00421         icon.context = dir->context();
00422 
00423         // if we got in MatchExact that far, we find no better
00424         if (match == KIcon::MatchExact)
00425             return icon;
00426     else
00427         {
00428         delta = dw;
00429         if (delta==0) return icon; // We won't find a better match anyway
00430         }
00431     }
00432     return icon;
00433 }
00434 
00435 // static
00436 QString *KIconTheme::_theme = 0L;
00437 
00438 // static
00439 QStringList *KIconTheme::_theme_list = 0L;
00440 
00441 // static
00442 QString KIconTheme::current()
00443 {
00444     // Static pointer because of unloading problems wrt DSO's.
00445     if (_theme != 0L)
00446         return *_theme;
00447 
00448     _theme = new QString();
00449     KConfig *config = KGlobal::config();
00450     KConfigGroupSaver saver(config, "Icons");
00451     *_theme = config->readEntry("Theme",defaultThemeName());
00452     if ( *_theme == QString::fromLatin1("hicolor") ) *_theme = defaultThemeName();
00453 /*    if (_theme->isEmpty())
00454     {
00455         if (QPixmap::defaultDepth() > 8)
00456             *_theme = defaultThemeName();
00457         else
00458             *_theme = QString::fromLatin1("locolor");
00459     }*/
00460     return *_theme;
00461 }
00462 
00463 // static
00464 QStringList KIconTheme::list()
00465 {
00466     // Static pointer because of unloading problems wrt DSO's.
00467     if (_theme_list != 0L)
00468         return *_theme_list;
00469 
00470     _theme_list = new QStringList();
00471     QStringList icnlibs = KGlobal::dirs()->resourceDirs("icon");
00472     icnlibs += (KGlobal::dirs()->resourceDirs("xdgdata-icon"));
00473     icnlibs += "/usr/share/pixmaps";
00474     QStringList::ConstIterator it;
00475     for (it=icnlibs.begin(); it!=icnlibs.end(); ++it)
00476     {
00477         QDir dir(*it);
00478         if (!dir.exists())
00479             continue;
00480         QStringList lst = dir.entryList(QDir::Dirs);
00481         QStringList::ConstIterator it2;
00482         for (it2=lst.begin(); it2!=lst.end(); ++it2)
00483         {
00484             if ((*it2 == ".") || (*it2 == "..") || (*it2).startsWith("default.") )
00485                 continue;
00486             if (!KStandardDirs::exists(*it + *it2 + "/index.desktop") && !KStandardDirs::exists(*it + *it2 + "/index.theme"))
00487                 continue;
00488         KIconTheme oink(*it2);
00489         if (!oink.isValid()) continue;
00490 
00491         if (!_theme_list->contains(*it2))
00492                 _theme_list->append(*it2);
00493         }
00494     }
00495     return *_theme_list;
00496 }
00497 
00498 // static
00499 void KIconTheme::reconfigure()
00500 {
00501     delete _theme;
00502     _theme=0L;
00503     delete _theme_list;
00504     _theme_list=0L;
00505 }
00506 
00507 // static
00508 QString KIconTheme::defaultThemeName()
00509 {
00510     return QString::fromLatin1("crystalsvg");
00511 }
00512 
00513 /*** KIconThemeDir ***/
00514 
00515 KIconThemeDir::KIconThemeDir(const QString& dir, const KConfigBase *config)
00516 {
00517     mbValid = false;
00518     mDir = dir;
00519     mSize = config->readNumEntry("Size");
00520     mMinSize = 1;    // just set the variables to something
00521     mMaxSize = 50;   // meaningful in case someone calls minSize or maxSize
00522     mType = KIcon::Fixed;
00523 
00524     if (mSize == 0)
00525         return;
00526 
00527     QString tmp = config->readEntry("Context");
00528     if (tmp == "Devices")
00529         mContext = KIcon::Device;
00530     else if (tmp == "MimeTypes")
00531         mContext = KIcon::MimeType;
00532     else if (tmp == "FileSystems")
00533         mContext = KIcon::FileSystem;
00534     else if (tmp == "Applications")
00535         mContext = KIcon::Application;
00536     else if (tmp == "Actions")
00537         mContext = KIcon::Action;
00538     else if (tmp == "Animations")
00539         mContext = KIcon::Animation;
00540     else if (tmp == "Categories")
00541         mContext = KIcon::Category;
00542     else if (tmp == "Emblems")
00543         mContext = KIcon::Emblem;
00544     else if (tmp == "Emotes")
00545         mContext = KIcon::Emote;
00546     else if (tmp == "International")
00547         mContext = KIcon::International;
00548     else if (tmp == "Places")
00549         mContext = KIcon::Place;
00550     else if (tmp == "Status")
00551         mContext = KIcon::StatusIcon;
00552     else {
00553         kdDebug(264) << "Invalid Context= line for icon theme: " << mDir << "\n";
00554         return;
00555     }
00556     tmp = config->readEntry("Type");
00557     if (tmp == "Fixed")
00558         mType = KIcon::Fixed;
00559     else if (tmp == "Scalable")
00560         mType = KIcon::Scalable;
00561     else if (tmp == "Threshold")
00562         mType = KIcon::Threshold;
00563     else {
00564         kdDebug(264) << "Invalid Type= line for icon theme: " <<  mDir << "\n";
00565         return;
00566     }
00567     if (mType == KIcon::Scalable)
00568     {
00569         mMinSize = config->readNumEntry("MinSize", mSize);
00570         mMaxSize = config->readNumEntry("MaxSize", mSize);
00571     } else if (mType == KIcon::Threshold)
00572     mThreshold = config->readNumEntry("Threshold", 2);
00573     mbValid = true;
00574 }
00575 
00576 QString KIconThemeDir::iconPath(const QString& name) const
00577 {
00578     if (!mbValid)
00579         return QString::null;
00580     QString file = mDir + "/" + name;
00581 
00582     if (access(QFile::encodeName(file), R_OK) == 0)
00583         return file;
00584 
00585     return QString::null;
00586 }
00587 
00588 QStringList KIconThemeDir::iconList() const
00589 {
00590     QDir dir(mDir);
00591 #ifdef HAVE_LIBART
00592     QStringList lst = dir.entryList("*.png;*.svg;*.svgz;*.xpm", QDir::Files);
00593 #else
00594     QStringList lst = dir.entryList("*.png;*.xpm", QDir::Files);
00595 #endif
00596     QStringList result;
00597     QStringList::ConstIterator it;
00598     for (it=lst.begin(); it!=lst.end(); ++it)
00599         result += mDir + "/" + *it;
00600     return result;
00601 }
KDE Home | KDE Accessibility Home | Description of Access Keys