+#endif
+}
+
+
+Quassel::Features Quassel::features()
+{
+ Features feats = 0;
+ for (int i = 1; i <= NumFeatures; i <<= 1)
+ feats |= (Feature)i;
+
+ return feats;
+}
+
+
+const QString &Quassel::coreDumpFileName()
+{
+ if (_coreDumpFileName.isEmpty()) {
+ QDir configDir(configDirPath());
+ _coreDumpFileName = configDir.absoluteFilePath(QString("Quassel-Crash-%1.log").arg(QDateTime::currentDateTime().toString("yyyyMMdd-hhmm")));
+ QFile dumpFile(_coreDumpFileName);
+ dumpFile.open(QIODevice::Append);
+ QTextStream dumpStream(&dumpFile);
+ dumpStream << "Quassel IRC: " << _buildInfo.baseVersion << ' ' << _buildInfo.commitHash << '\n';
+ qDebug() << "Quassel IRC: " << _buildInfo.baseVersion << ' ' << _buildInfo.commitHash;
+ dumpStream.flush();
+ dumpFile.close();
+ }
+ return _coreDumpFileName;
+}
+
+
+QString Quassel::configDirPath()
+{
+ if (!_configDirPath.isEmpty())
+ return _configDirPath;
+
+ if (Quassel::isOptionSet("datadir")) {
+ qWarning() << "Obsolete option --datadir used!";
+ _configDirPath = Quassel::optionValue("datadir");
+ }
+ else if (Quassel::isOptionSet("configdir")) {
+ _configDirPath = Quassel::optionValue("configdir");
+ }
+ else {
+#ifdef Q_WS_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
+ // don't use the registry
+ QSettings::Format format = QSettings::IniFormat;
+# else
+ QSettings::Format format = QSettings::NativeFormat;
+# endif
+ QSettings s(format, QSettings::UserScope, QCoreApplication::organizationDomain(), buildInfo().applicationName);
+ QFileInfo fileInfo(s.fileName());
+ _configDirPath = fileInfo.dir().absolutePath();
+#endif /* Q_WS_MAC */
+ }
+
+ if (!_configDirPath.endsWith(QDir::separator()) && !_configDirPath.endsWith('/'))
+ _configDirPath += QDir::separator();
+
+ QDir qDir(_configDirPath);
+ if (!qDir.exists(_configDirPath)) {
+ if (!qDir.mkpath(_configDirPath)) {
+ qCritical() << "Unable to create Quassel config directory:" << qPrintable(qDir.absolutePath());
+ return QString();
+ }
+ }
+
+ return _configDirPath;
+}
+
+
+QStringList Quassel::dataDirPaths()
+{
+ return _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();
+ }
+#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);
+ }
+#endif
+
+ // add resource path and workdir just in case
+ dataDirNames << QCoreApplication::applicationDirPath() + "/data/"
+ << ":/data/";
+
+ // append trailing '/' and check for existence
+ QStringList::Iterator iter = dataDirNames.begin();
+ while (iter != dataDirNames.end()) {
+ if (!iter->endsWith(QDir::separator()) && !iter->endsWith('/'))
+ iter->append(QDir::separator());
+ if (!QFile::exists(*iter))
+ iter = dataDirNames.erase(iter);
+ else
+ ++iter;
+ }
+
+ return dataDirNames;
+}
+
+
+QString Quassel::findDataFilePath(const QString &fileName)
+{
+ QStringList dataDirs = dataDirPaths();
+ foreach(QString dataDir, dataDirs) {
+ QString path = dataDir + fileName;
+ if (QFile::exists(path))
+ return path;
+ }
+ return QString();
+}
+
+
+QStringList Quassel::scriptDirPaths()
+{
+ QStringList res(configDirPath() + "scripts/");
+ foreach(QString path, dataDirPaths())
+ res << path + "scripts/";
+ return res;
+}
+
+
+QString Quassel::translationDirPath()
+{
+ if (_translationDirPath.isEmpty()) {
+ // We support only one translation dir; fallback mechanisms wouldn't work else.
+ // This means that if we have a $data/translations dir, the internal :/i18n resource won't be considered.
+ foreach(const QString &dir, dataDirPaths()) {
+ if (QFile::exists(dir + "translations/")) {
+ _translationDirPath = dir + "translations/";
+ break;
+ }
+ }
+ if (_translationDirPath.isEmpty())
+ _translationDirPath = ":/i18n/";
+ }
+ return _translationDirPath;
+}
+
+
+void Quassel::loadTranslation(const QLocale &locale)
+{
+ QTranslator *qtTranslator = QCoreApplication::instance()->findChild<QTranslator *>("QtTr");
+ QTranslator *quasselTranslator = QCoreApplication::instance()->findChild<QTranslator *>("QuasselTr");
+
+ if (qtTranslator)
+ qApp->removeTranslator(qtTranslator);
+ if (quasselTranslator)
+ qApp->removeTranslator(quasselTranslator);
+
+ // We use QLocale::C to indicate that we don't want a translation
+ if (locale.language() == QLocale::C)
+ return;
+
+ qtTranslator = new QTranslator(qApp);
+ qtTranslator->setObjectName("QtTr");
+ qApp->installTranslator(qtTranslator);
+
+ quasselTranslator = new QTranslator(qApp);
+ quasselTranslator->setObjectName("QuasselTr");
+ qApp->installTranslator(quasselTranslator);
+
+#if QT_VERSION >= 0x040800
+ bool success = qtTranslator->load(locale, QString("qt_%1"), translationDirPath());
+ if (!success)
+ qtTranslator->load(locale, QString("qt_"), QLibraryInfo::location(QLibraryInfo::TranslationsPath));
+ quasselTranslator->load(locale, QString(""), translationDirPath());
+#else
+ QString localeName = locale.name();
+
+ // if the user did not specify a language in the settings, the system locale
+ // is used, but Qt < 4.8 does not respect language settings. This bit is
+ // based on QLocale::uiLanguages() as in Qt 4.8.3
+ if (locale == QLocale::system()) {
+ // FIXME: does it make sense to set the locale to the system locale?
+ QLocale::setDefault(locale);
+ QVariant res = QSystemLocale().query(QSystemLocale::UILanguages, QVariant());
+ if (!res.isNull()) {
+ QString newName = res.toStringList()[0];
+ if (!newName.isEmpty()) {
+ localeName = newName.replace('-', "_"); // silly Qt.
+ }
+ }
+ }
+
+ bool success = qtTranslator->load(QString("qt_%1").arg(localeName), translationDirPath());
+ if (!success)
+ qtTranslator->load(QString("qt_%1").arg(localeName), QLibraryInfo::location(QLibraryInfo::TranslationsPath));
+ quasselTranslator->load(QString("%1").arg(localeName), translationDirPath());
+#endif