We require cmake-2.6 now. Also LINGUAS should work again...
[quassel.git] / src / uisupport / iconloader.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-08 by the Quassel IRC Team                         *
3  *   devel@quassel-irc.org                                                 *
4  *                                                                         *
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.                                           *
9  *                                                                         *
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.                          *
14  *                                                                         *
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  ***************************************************************************/
20
21 #include <QDebug>
22 #include <QDir>
23 #include <QFile>
24
25 #include "iconloader.h"
26
27 IconLoader IconLoader::_iconLoader;
28 int IconLoader::_groupSize[] = { 48, 22, 22, 16, 32, 22 };  // default sizes taken from Oxygen
29
30 IconLoader *IconLoader::global() {
31   // Workaround: the static _iconLoader might be initialized before the resources it needs
32   // This way, first call to global() will init it by setting the theme
33   if(_iconLoader.theme().isEmpty())
34     _iconLoader.setTheme("oxygen");
35   return &_iconLoader;
36 }
37
38 IconLoader::IconLoader(QObject *parent) : QObject(parent) {
39
40   // setTheme("oxygen");
41 }
42
43 IconLoader::~IconLoader() {
44
45 }
46
47 void IconLoader::setTheme(const QString &theme) {
48   _theme = theme;
49   // check which dirs could contain themed icons
50   _themedIconDirNames.clear();
51   _plainIconDirNames.clear();
52   QString path;
53   QStringList dataDirNames = QString(qgetenv("XDG_DATA_DIRS")).split(':');
54
55   // System theme in $data/icons/$theme
56   foreach(QString dir, dataDirNames) {
57     path = QString("%1/icons/%2").arg(dir, theme);
58     if(QFile::exists(path))
59       _themedIconDirNames.append(path);
60   }
61   // Resource for system theme :/icons/$theme
62   path = QString(":/icons/%2");
63   if(QFile::exists(path))
64     _themedIconDirNames.append(path);
65
66   // Own icons in $data/apps/quassel/icons/hicolor
67   // Also, plain icon dirs $data/apps/quassel/pics
68   foreach(QString dir, dataDirNames) {
69     path = QString("%1/apps/quassel/icons/hicolor").arg(dir);
70     if(QFile::exists(path))
71       _themedIconDirNames.append(path);
72     path = QString("%1/apps/quassel/pics").arg(dir);
73     if(QFile::exists(path))
74       _plainIconDirNames.append(path);
75   }
76
77   // Same for :/icons/hicolor and :/pics
78   path = QString(":/icons/hicolor");
79   if(QFile::exists(path))
80     _themedIconDirNames.append(path);
81
82   path = QString(":/pics");
83   if(QFile::exists(path))
84     _plainIconDirNames.append(path);
85 }
86
87 // TODO: optionally implement cache (speed/memory tradeoff?)
88 QPixmap IconLoader::loadIcon(const QString &name, IconLoader::Group group, int size) {
89   if(group < 0 || group >= LastGroup) {
90     qWarning() << "Invalid icon group!";
91     return QPixmap();
92   }
93   if(size == 0)
94     size = _groupSize[group];
95
96   QString path = findIconPath(name, size);
97   if(path.isEmpty()) return QPixmap();
98
99   // load the icon
100   return QPixmap(path);
101 }
102
103 QString IconLoader::findIconPath(const QString &name, int size) {
104   QString fname = QString("%1.png").arg(name);  // we only support PNG so far
105   // First, look for a themed icon... we don't do anything fancy here, only exact match for both name and size
106   foreach(QString basedir, _themedIconDirNames) {
107     QDir sizedir(QString("%1/%2x%2").arg(basedir).arg(QString::number(size)));
108     if(sizedir.exists()) {
109       // ignore context, i.e. scan all subdirs
110       QStringList contextdirs = sizedir.entryList(QDir::Dirs | QDir::NoDotAndDotDot);
111       foreach(QString dir, contextdirs) {
112         QString path = QString("%1/%2/%3").arg(sizedir.absolutePath(), dir, fname);
113         if(QFile::exists(path)) return path;
114       }
115     }
116   }
117   // Now check the plain dirs
118   foreach(QString dir, _plainIconDirNames) {
119     QString path = QString("%1/%2").arg(dir, name);
120     if(QFile::exists(path)) return path;
121   }
122
123   return QString();
124 }
125
126 // Convenience constructors
127
128 QPixmap DesktopIcon(const QString& name, int force_size) {
129   IconLoader *loader = IconLoader::global();
130   return loader->loadIcon(name, IconLoader::Desktop, force_size);
131 }
132
133 QPixmap BarIcon(const QString& name, int force_size) {
134   IconLoader *loader = IconLoader::global();
135   return loader->loadIcon(name, IconLoader::Toolbar, force_size);
136 }
137
138 QPixmap MainBarIcon(const QString& name, int force_size) {
139   IconLoader *loader = IconLoader::global();
140   return loader->loadIcon(name, IconLoader::MainToolbar, force_size);
141 }
142
143 QPixmap SmallIcon(const QString& name, int force_size) {
144   IconLoader *loader = IconLoader::global();
145   return loader->loadIcon(name, IconLoader::Small, force_size);
146 }