Fix iconloader to find icons where we actually install them it -DQUASSEL_ICONS=Extern...
[quassel.git] / src / uisupport / iconloader.cpp
index 0fff1e8..b37c462 100644 (file)
@@ -18,6 +18,7 @@
  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
  ***************************************************************************/
 
+#include <QCoreApplication>
 #include <QDebug>
 #include <QDir>
 #include <QFile>
@@ -28,12 +29,16 @@ IconLoader IconLoader::_iconLoader;
 int IconLoader::_groupSize[] = { 48, 22, 22, 16, 32, 22 };  // default sizes taken from Oxygen
 
 IconLoader *IconLoader::global() {
+  // Workaround: the static _iconLoader might be initialized before the resources it needs
+  // This way, first call to global() will init it by setting the theme
+  if(_iconLoader.theme().isEmpty())
+    _iconLoader.setTheme("oxygen");
   return &_iconLoader;
 }
 
 IconLoader::IconLoader(QObject *parent) : QObject(parent) {
 
-  setTheme("oxygen");
+  // setTheme("oxygen");
 }
 
 IconLoader::~IconLoader() {
@@ -46,7 +51,19 @@ void IconLoader::setTheme(const QString &theme) {
   _themedIconDirNames.clear();
   _plainIconDirNames.clear();
   QString path;
-  QStringList dataDirNames = QString(qgetenv("XDG_DATA_DIRS")).split(':');
+  QStringList dataDirNames = QString(qgetenv("XDG_DATA_DIRS")).split(':', QString::SkipEmptyParts);
+
+// Provide a fallback
+# ifdef Q_OS_UNIX
+    if(dataDirNames.isEmpty()) dataDirNames.append("/usr/share");
+    // on UNIX, we always check our install prefix
+    QString appDir = QCoreApplication::applicationDirPath();
+    int binpos = appDir.lastIndexOf("/bin");
+    if(binpos >= 0) {
+      appDir.replace(binpos, 4, "/share");
+      if(!dataDirNames.contains(appDir)) dataDirNames.append(appDir);
+    }
+# endif
 
   // System theme in $data/icons/$theme
   foreach(QString dir, dataDirNames) {
@@ -55,21 +72,29 @@ void IconLoader::setTheme(const QString &theme) {
       _themedIconDirNames.append(path);
   }
   // Resource for system theme :/icons/$theme
-  path = QString(":/icons/%2");
+  path = QString(":/icons/%1").arg(theme);
   if(QFile::exists(path))
     _themedIconDirNames.append(path);
 
-  // Own icons in $data/apps/quassel/icons/hicolor and :/icons/hicolor
-  // Also, plain icon dirs $data/apps/quassel/pics and :/pics
-  dataDirNames.append(":");
+  // Own icons in $data/apps/quassel/icons/hicolor
+  // Also, plain icon dirs $data/apps/quassel/pics
   foreach(QString dir, dataDirNames) {
-    path = QString("%1/apps/quassel/icons/hicolor");
+    path = QString("%1/icons/hicolor").arg(dir);
     if(QFile::exists(path))
       _themedIconDirNames.append(path);
-    path = QString("%1/apps/quassel/pics");
+    path = QString("%1/apps/quassel/pics").arg(dir);
     if(QFile::exists(path))
       _plainIconDirNames.append(path);
   }
+
+  // Same for :/icons/hicolor and :/pics
+  path = QString(":/icons/hicolor");
+  if(QFile::exists(path))
+    _themedIconDirNames.append(path);
+
+  path = QString(":/pics");
+  if(QFile::exists(path))
+    _plainIconDirNames.append(path);
 }
 
 // TODO: optionally implement cache (speed/memory tradeoff?)
@@ -110,3 +135,25 @@ QString IconLoader::findIconPath(const QString &name, int size) {
 
   return QString();
 }
+
+// Convenience constructors
+
+QPixmap DesktopIcon(const QString& name, int force_size) {
+  IconLoader *loader = IconLoader::global();
+  return loader->loadIcon(name, IconLoader::Desktop, force_size);
+}
+
+QPixmap BarIcon(const QString& name, int force_size) {
+  IconLoader *loader = IconLoader::global();
+  return loader->loadIcon(name, IconLoader::Toolbar, force_size);
+}
+
+QPixmap MainBarIcon(const QString& name, int force_size) {
+  IconLoader *loader = IconLoader::global();
+  return loader->loadIcon(name, IconLoader::MainToolbar, force_size);
+}
+
+QPixmap SmallIcon(const QString& name, int force_size) {
+  IconLoader *loader = IconLoader::global();
+  return loader->loadIcon(name, IconLoader::Small, force_size);
+}