Improve data path handling
[quassel.git] / src / common / quassel.cpp
index 6490eb9..f43423c 100644 (file)
@@ -1,5 +1,5 @@
 /***************************************************************************
- *   Copyright (C) 2005-2014 by the Quassel Project                        *
+ *   Copyright (C) 2005-2015 by the Quassel Project                        *
  *   devel@quassel-irc.org                                                 *
  *                                                                         *
  *   This program is free software; you can redistribute it and/or modify  *
@@ -22,7 +22,7 @@
 
 #include <iostream>
 #include <signal.h>
-#if !defined Q_OS_WIN32 && !defined Q_OS_MAC
+#if !defined Q_OS_WIN && !defined Q_OS_MAC
 #  include <sys/types.h>
 #  include <sys/time.h>
 #  include <sys/resource.h>
@@ -91,27 +91,28 @@ bool Quassel::init()
 
     if (_handleCrashes) {
         // we have crashhandler for win32 and unix (based on execinfo).
-#if defined(Q_OS_WIN32) || defined(HAVE_EXECINFO)
-# ifndef Q_OS_WIN32
+#if defined(Q_OS_WIN) || defined(HAVE_EXECINFO)
+# ifndef Q_OS_WIN
         // we only handle crashes ourselves if coredumps are disabled
         struct rlimit *limit = (rlimit *)malloc(sizeof(struct rlimit));
         int rc = getrlimit(RLIMIT_CORE, limit);
 
         if (rc == -1 || !((long)limit->rlim_cur > 0 || limit->rlim_cur == RLIM_INFINITY)) {
-# endif /* Q_OS_WIN32 */
+# endif /* Q_OS_WIN */
         signal(SIGABRT, handleSignal);
         signal(SIGSEGV, handleSignal);
-# ifndef Q_OS_WIN32
+# ifndef Q_OS_WIN
         signal(SIGBUS, handleSignal);
     }
     free(limit);
-# endif /* Q_OS_WIN32 */
-#endif /* Q_OS_WIN32 || HAVE_EXECINFO */
+# endif /* Q_OS_WIN */
+#endif /* Q_OS_WIN || HAVE_EXECINFO */
     }
 
     _initialized = true;
     qsrand(QTime(0, 0, 0).secsTo(QTime::currentTime()));
 
+    setupEnvironment();
     registerMetaTypes();
 
     Network::setDefaultCodecForServer("ISO-8859-1");
@@ -211,9 +212,40 @@ void Quassel::registerMetaTypes()
 }
 
 
+void Quassel::setupEnvironment()
+{
+    // On modern Linux systems, XDG_DATA_DIRS contains a list of directories containing application data. This
+    // is, for example, used by Qt for finding icons and other things. In case Quassel is installed in a non-standard
+    // prefix (or run from the build directory), it makes sense to add this to XDG_DATA_DIRS so we don't have to
+    // hack extra search paths into various places.
+#ifdef Q_OS_UNIX
+    QString xdgDataVar = QFile::decodeName(qgetenv("XDG_DATA_DIRS"));
+    if (xdgDataVar.isEmpty())
+        xdgDataVar = QLatin1String("/usr/local/share:/usr/share"); // sane defaults
+
+    QStringList xdgDirs = xdgDataVar.split(QLatin1Char(':'), QString::SkipEmptyParts);
+
+    // Add our install prefix (if we're not in a bindir, this just adds the current workdir)
+    QString appDir = QCoreApplication::applicationDirPath();
+    int binpos = appDir.lastIndexOf("/bin");
+    if (binpos >= 0) {
+        appDir.replace(binpos, 4, "/share");
+        xdgDirs.append(appDir);
+        // Also append apps/quassel, this is only for QIconLoader to find icons there
+        xdgDirs.append(appDir + "/apps/quassel");
+    } else
+        xdgDirs.append(appDir);  // build directory is always the last fallback
+
+    xdgDirs.removeDuplicates();
+
+    qputenv("XDG_DATA_DIRS", QFile::encodeName(xdgDirs.join(":")));
+#endif
+}
+
+
 void Quassel::setupBuildInfo()
 {
-    _buildInfo.applicationName = "Quassel IRC";
+    _buildInfo.applicationName = "quassel";
     _buildInfo.coreApplicationName = "quasselcore";
     _buildInfo.clientApplicationName = "quasselclient";
     _buildInfo.organizationName = "Quassel Project";
@@ -287,7 +319,7 @@ void Quassel::handleSignal(int sig)
         break;
     case SIGABRT:
     case SIGSEGV:
-#ifndef Q_OS_WIN32
+#ifndef Q_OS_WIN
     case SIGBUS:
 #endif
         logBacktrace(coreDumpFileName());
@@ -355,12 +387,12 @@ QString Quassel::configDirPath()
         _configDirPath = Quassel::optionValue("configdir");
     }
     else {
-#ifdef Q_WS_MAC
+#ifdef Q_OS_MAC
         // On Mac, the path is always the same
         _configDirPath = QDir::homePath() + "/Library/Application Support/Quassel/";
 #else
         // We abuse QSettings to find us a sensible path on the other platforms
-#  ifdef Q_WS_WIN
+#  ifdef Q_OS_WIN
         // don't use the registry
         QSettings::Format format = QSettings::IniFormat;
 #  else
@@ -369,7 +401,7 @@ QString Quassel::configDirPath()
         QSettings s(format, QSettings::UserScope, QCoreApplication::organizationDomain(), buildInfo().applicationName);
         QFileInfo fileInfo(s.fileName());
         _configDirPath = fileInfo.dir().absolutePath();
-#endif /* Q_WS_MAC */
+#endif /* Q_OS_MAC */
     }
 
     if (!_configDirPath.endsWith(QDir::separator()) && !_configDirPath.endsWith('/'))
@@ -395,43 +427,50 @@ QStringList Quassel::dataDirPaths()
 
 QStringList Quassel::findDataDirPaths() const
 {
-    QStringList dataDirNames = QString(qgetenv("XDG_DATA_DIRS")).split(':', QString::SkipEmptyParts);
-
-    if (!dataDirNames.isEmpty()) {
-        for (int i = 0; i < dataDirNames.count(); i++)
-            dataDirNames[i].append("/apps/quassel/");
-    }
-    else {
-        // Provide a fallback
-#ifdef Q_OS_WIN32
-        dataDirNames << qgetenv("APPDATA") + QCoreApplication::organizationDomain() + "/share/apps/quassel/"
-                     << qgetenv("APPDATA") + QCoreApplication::organizationDomain()
-                     << QCoreApplication::applicationDirPath();
-    }
-#elif defined Q_WS_MAC
-        dataDirNames << QDir::homePath() + "/Library/Application Support/Quassel/"
-                     << QCoreApplication::applicationDirPath();
-    }
+    // We don't use QStandardPaths for now, as we still need to provide fallbacks for Qt4 and
+    // want to stay consistent.
+
+    QStringList dataDirNames;
+#ifdef Q_OS_WIN
+    dataDirNames << qgetenv("APPDATA") + QCoreApplication::organizationDomain() + "/share/apps/quassel/"
+                 << qgetenv("APPDATA") + QCoreApplication::organizationDomain()
+                 << QCoreApplication::applicationDirPath();
+#elif defined Q_OS_MAC
+    dataDirNames << QDir::homePath() + "/Library/Application Support/Quassel/"
+                 << QCoreApplication::applicationDirPath();
 #else
-        dataDirNames.append("/usr/share/apps/quassel/");
-    }
-    // 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");
-        appDir.append("/apps/quassel/");
-        if (!dataDirNames.contains(appDir))
-            dataDirNames.append(appDir);
-    }
+    // Linux et al
+
+    // XDG_DATA_HOME is the location for users to override system-installed files, usually in .local/share
+    // This should thus come first.
+    QString xdgDataHome = QFile::decodeName(qgetenv("XDG_DATA_HOME"));
+    if (xdgDataHome.isEmpty())
+        xdgDataHome = QDir::homePath() + QLatin1String("/.local/share");
+    dataDirNames << xdgDataHome;
+
+    // Now whatever is configured through XDG_DATA_DIRS
+    QString xdgDataDirs = QFile::decodeName(qgetenv("XDG_DATA_DIRS"));
+    if (xdgDataDirs.isEmpty())
+        dataDirNames << "/usr/local/share" << "/usr/share";
+    else
+        dataDirNames << xdgDataDirs.split(':', QString::SkipEmptyParts);
+
+    // Just in case, also check our install prefix
+    dataDirNames << QCoreApplication::applicationDirPath() + "/../share";
+
+    // Normalize and append our application name
+    for (int i = 0; i < dataDirNames.count(); i++)
+        dataDirNames[i] = QDir::cleanPath(dataDirNames.at(i)) + "/quassel/";
+
 #endif
 
-    // add resource path and workdir just in case
-    dataDirNames << QCoreApplication::applicationDirPath() + "/data/"
-                 << ":/data/";
+    // Add resource path and workdir just in case.
+    // Workdir should have precedence
+    dataDirNames.prepend(QCoreApplication::applicationDirPath() + "/data/");
+    dataDirNames.append(":/data/");
 
-    // append trailing '/' and check for existence
-    QStringList::Iterator iter = dataDirNames.begin();
+    // Append trailing '/' and check for existence
+    auto iter = dataDirNames.begin();
     while (iter != dataDirNames.end()) {
         if (!iter->endsWith(QDir::separator()) && !iter->endsWith('/'))
             iter->append(QDir::separator());
@@ -441,6 +480,8 @@ QStringList Quassel::findDataDirPaths() const
             ++iter;
     }
 
+    dataDirNames.removeDuplicates();
+
     return dataDirNames;
 }