/* * Copyright (C) 2012 Canonical, Ltd. * * Authors: * Ugo Riboni * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; version 3. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #ifndef PATHS_H #define PATHS_H // Qt #include #include #include #include #include inline bool isRunningInstalled() { // Tests don't use the normal binary, so we want to check binary *and* // the environment. Installed tests have UNITY_TESTING_DATADIR set. static bool installed = (QCoreApplication::applicationDirPath() == QDir(QStringLiteral("@CMAKE_INSTALL_PREFIX@/@CMAKE_INSTALL_BINDIR@")).canonicalPath()); static bool installed_test_env = !qgetenv("UNITY_TESTING_DATADIR").isEmpty(); return installed || installed_test_env; } inline QString buildDirectory() { if (!qEnvironmentVariableIsEmpty("UNITY_BINARY_DIR")) return qgetenv("UNITY_BINARY_DIR"); return QStringLiteral("@CMAKE_BINARY_DIR@"); } inline QString sourceDirectory() { if (!qEnvironmentVariableIsEmpty("UNITY_SOURCE_DIR")) return qgetenv("UNITY_SOURCE_DIR"); return QStringLiteral("@CMAKE_SOURCE_DIR@"); } inline QString translationDirectory() { if (isRunningInstalled()) { return QStringLiteral("@CMAKE_INSTALL_PREFIX@/share/locale"); } else { return QString(buildDirectory() + "/po/locale"); } } inline QString testDataDir() { // Can't use isRunningInstalled, since tests don't use the normal unity8 // executable. $UNITY_TESTING_DATADIR is set by installed tests. QString datadir(qgetenv("UNITY_TESTING_DATADIR")); if (datadir.isEmpty()) { return sourceDirectory() + "/tests"; } else { return datadir + "/tests"; } } inline QString testLibDir() { // Can't use isRunningInstalled, since tests don't use the normal unity8 // executable. $UNITY_TESTING_DATADIR is set by installed tests. QString libdir(qgetenv("UNITY_TESTING_LIBDIR")); if (libdir.isEmpty()) { return buildDirectory() + "/tests"; } else { return libdir + "/tests"; } } inline QString qmlDirectory() { if (isRunningInstalled()) { return QStringLiteral("@CMAKE_INSTALL_PREFIX@/@SHELL_APP_DIR@/"); } else { return QString(sourceDirectory() + "/qml"); } } inline QStringList overrideImportPaths() { QStringList paths; if (!isRunningInstalled()) { paths << QString(buildDirectory() + "/plugins"); } return paths; } inline QStringList nonMirImportPaths() { QStringList paths; if (isRunningInstalled()) { paths << QStringLiteral("@CMAKE_INSTALL_PREFIX@/@SHELL_INSTALL_QML@/nonmirplugins"); } else { paths << QString(buildDirectory() + "/nonmirplugins"); } return paths; } inline QStringList fallbackImportPaths() { QStringList paths; if (isRunningInstalled()) { paths << QStringLiteral("@CMAKE_INSTALL_PREFIX@/@SHELL_INSTALL_QML@"); paths << QStringLiteral("@USS_PRIVATE_PLUGINDIR@"); paths << QStringLiteral("@SHELL_PLUGINDIR@"); paths << QStringLiteral("@CMAKE_INSTALL_PREFIX@/@SHELL_INSTALL_QML@/mocks"); } else { paths << QStringLiteral("@USS_PRIVATE_PLUGINDIR@"); paths << QStringLiteral("@SHELL_PLUGINDIR@"); paths << QString(buildDirectory() + "/tests/mocks"); } return paths; } inline QString mockPluginsDir() { if (isRunningInstalled()) { return QStringLiteral("@CMAKE_INSTALL_PREFIX@/@SHELL_INSTALL_QML@/mocks"); } else { return QString(buildDirectory() + "/tests/mocks"); } } inline QStringList shellDataDirs() { QStringList dirs = QStandardPaths::standardLocations(QStandardPaths::GenericDataLocation); if (isRunningInstalled()) { // append so by default we use xdg files. dirs.append(qmlDirectory()); } return dirs; } inline void prependImportPaths(QQmlEngine *engine, const QStringList &paths) { QStringList importPathList = engine->importPathList(); for (int i = paths.count() -1; i >= 0; i--) { // don't duplicate const QString& path = paths[i]; QStringList::iterator iter = qFind(importPathList.begin(), importPathList.end(), path); if (iter == importPathList.end()) { engine->addImportPath(path); } } } /* When you append and import path to the list of import paths it will be the *last* place where Qt will search for QML modules. The usual QQmlEngine::addImportPath() actually prepends the given path.*/ inline void appendImportPaths(QQmlEngine *engine, const QStringList &paths) { QStringList importPathList = engine->importPathList(); Q_FOREACH(const QString& path, paths) { // don't duplicate QStringList::iterator iter = qFind(importPathList.begin(), importPathList.end(), path); if (iter == importPathList.end()) { importPathList.append(path); } } engine->setImportPathList(importPathList); } #endif