Enable multi-selection in nick list
[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 #ifndef HAVE_KDE
22
23 #include <QCoreApplication>
24 #include <QDebug>
25 #include <QDir>
26 #include <QFile>
27
28 #include "iconloader.h"
29
30 IconLoader IconLoader::_iconLoader;
31 int IconLoader::_groupSize[] = { 48, 22, 22, 16, 32, 22 };  // default sizes taken from Oxygen
32
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");
38   return &_iconLoader;
39 }
40
41 IconLoader::IconLoader(QObject *parent) : QObject(parent) {
42
43   // setTheme("oxygen");
44 }
45
46 IconLoader::~IconLoader() {
47
48 }
49
50 void IconLoader::setTheme(const QString &theme) {
51   _theme = theme;
52   // check which dirs could contain themed icons
53   _themedIconDirNames.clear();
54   _plainIconDirNames.clear();
55   QString path;
56   QStringList dataDirNames = QString(qgetenv("XDG_DATA_DIRS")).split(':', QString::SkipEmptyParts);
57
58 // Provide a fallback
59 # ifdef Q_OS_UNIX
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");
64     if(binpos >= 0) {
65       appDir.replace(binpos, 4, "/share");
66       if(!dataDirNames.contains(appDir)) dataDirNames.append(appDir);
67     }
68 # endif
69
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);
75   }
76   // Resource for system theme :/icons/$theme
77   path = QString(":/icons/%1").arg(theme);
78   if(QFile::exists(path))
79     _themedIconDirNames.append(path);
80
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);
90   }
91
92   // Same for :/icons/hicolor and :/pics
93   path = QString(":/icons/hicolor");
94   if(QFile::exists(path))
95     _themedIconDirNames.append(path);
96
97   path = QString(":/pics");
98   if(QFile::exists(path))
99     _plainIconDirNames.append(path);
100 }
101
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!";
106     return QPixmap();
107   }
108   if(size == 0)
109     size = _groupSize[group];
110
111   QString path = findIconPath(name, size);
112   if(path.isEmpty()) return QPixmap();
113
114   // load the icon
115   return QPixmap(path);
116 }
117
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;
129       }
130     }
131   }
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;
136   }
137
138   return QString();
139 }
140
141 // Convenience constructors
142
143 QPixmap DesktopIcon(const QString& name, int force_size) {
144   IconLoader *loader = IconLoader::global();
145   return loader->loadIcon(name, IconLoader::Desktop, force_size);
146 }
147
148 QPixmap BarIcon(const QString& name, int force_size) {
149   IconLoader *loader = IconLoader::global();
150   return loader->loadIcon(name, IconLoader::Toolbar, force_size);
151 }
152
153 QPixmap MainBarIcon(const QString& name, int force_size) {
154   IconLoader *loader = IconLoader::global();
155   return loader->loadIcon(name, IconLoader::MainToolbar, force_size);
156 }
157
158 QPixmap SmallIcon(const QString& name, int force_size) {
159   IconLoader *loader = IconLoader::global();
160   return loader->loadIcon(name, IconLoader::Small, force_size);
161 }
162
163 #endif