1 /***************************************************************************
2 * Copyright (C) 2005-08 by the Quassel IRC Team *
3 * devel@quassel-irc.org *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) version 3. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
23 #include <QCoreApplication>
28 #include "iconloader.h"
30 IconLoader IconLoader::_iconLoader;
31 int IconLoader::_groupSize[] = { 48, 22, 22, 16, 32, 22 }; // default sizes taken from Oxygen
33 IconLoader *IconLoader::global() {
34 // Workaround: the static _iconLoader might be initialized before the resources it needs
35 // This way, first call to global() will init it by setting the theme
36 if(_iconLoader.theme().isEmpty())
37 _iconLoader.setTheme("oxygen");
41 IconLoader::IconLoader(QObject *parent) : QObject(parent) {
43 // setTheme("oxygen");
46 IconLoader::~IconLoader() {
50 void IconLoader::setTheme(const QString &theme) {
52 // check which dirs could contain themed icons
53 _themedIconDirNames.clear();
54 _plainIconDirNames.clear();
56 QStringList dataDirNames = QString(qgetenv("XDG_DATA_DIRS")).split(':', QString::SkipEmptyParts);
60 if(dataDirNames.isEmpty()) dataDirNames.append("/usr/share");
61 // on UNIX, we always check our install prefix
62 QString appDir = QCoreApplication::applicationDirPath();
63 int binpos = appDir.lastIndexOf("/bin");
65 appDir.replace(binpos, 4, "/share");
66 if(!dataDirNames.contains(appDir)) dataDirNames.append(appDir);
70 // System theme in $data/icons/$theme
71 foreach(QString dir, dataDirNames) {
72 path = QString("%1/icons/%2").arg(dir, theme);
73 if(QFile::exists(path))
74 _themedIconDirNames.append(path);
76 // Resource for system theme :/icons/$theme
77 path = QString(":/icons/%1").arg(theme);
78 if(QFile::exists(path))
79 _themedIconDirNames.append(path);
81 // Own icons in $data/apps/quassel/icons/hicolor
82 // Also, plain icon dirs $data/apps/quassel/pics
83 foreach(QString dir, dataDirNames) {
84 path = QString("%1/icons/hicolor").arg(dir);
85 if(QFile::exists(path))
86 _themedIconDirNames.append(path);
87 path = QString("%1/apps/quassel/pics").arg(dir);
88 if(QFile::exists(path))
89 _plainIconDirNames.append(path);
92 // Same for :/icons/hicolor and :/pics
93 path = QString(":/icons/hicolor");
94 if(QFile::exists(path))
95 _themedIconDirNames.append(path);
97 path = QString(":/pics");
98 if(QFile::exists(path))
99 _plainIconDirNames.append(path);
102 // TODO: optionally implement cache (speed/memory tradeoff?)
103 QPixmap IconLoader::loadIcon(const QString &name, IconLoader::Group group, int size) {
104 if(group < 0 || group >= LastGroup) {
105 qWarning() << "Invalid icon group!";
109 size = _groupSize[group];
111 QString path = findIconPath(name, size);
112 if(path.isEmpty()) return QPixmap();
115 return QPixmap(path);
118 QString IconLoader::findIconPath(const QString &name, int size) {
119 QString fname = QString("%1.png").arg(name); // we only support PNG so far
120 // First, look for a themed icon... we don't do anything fancy here, only exact match for both name and size
121 foreach(QString basedir, _themedIconDirNames) {
122 QDir sizedir(QString("%1/%2x%2").arg(basedir).arg(QString::number(size)));
123 if(sizedir.exists()) {
124 // ignore context, i.e. scan all subdirs
125 QStringList contextdirs = sizedir.entryList(QDir::Dirs | QDir::NoDotAndDotDot);
126 foreach(QString dir, contextdirs) {
127 QString path = QString("%1/%2/%3").arg(sizedir.absolutePath(), dir, fname);
128 if(QFile::exists(path)) return path;
132 // Now check the plain dirs
133 foreach(QString dir, _plainIconDirNames) {
134 QString path = QString("%1/%2").arg(dir, name);
135 if(QFile::exists(path)) return path;
141 // Convenience constructors
143 QPixmap DesktopIcon(const QString& name, int force_size) {
144 IconLoader *loader = IconLoader::global();
145 return loader->loadIcon(name, IconLoader::Desktop, force_size);
148 QPixmap BarIcon(const QString& name, int force_size) {
149 IconLoader *loader = IconLoader::global();
150 return loader->loadIcon(name, IconLoader::Toolbar, force_size);
153 QPixmap MainBarIcon(const QString& name, int force_size) {
154 IconLoader *loader = IconLoader::global();
155 return loader->loadIcon(name, IconLoader::MainToolbar, force_size);
158 QPixmap SmallIcon(const QString& name, int force_size) {
159 IconLoader *loader = IconLoader::global();
160 return loader->loadIcon(name, IconLoader::Small, force_size);