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