on dbb8563 help sign in
unity8/include/paths.h.in
166 lines 5.6 kB history raw
1/*
2 * Copyright (C) 2012 Canonical, Ltd.
3 *
4 * Authors:
5 * Ugo Riboni <ugo.riboni@canonical.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 3.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
20#ifndef PATHS_H
21#define PATHS_H
23// Qt
24#include <QtCore/QCoreApplication>
25#include <QtCore/QDir>
26#include <QtGui/QIcon>
27#include <QtQml/QQmlEngine>
28#include <QStandardPaths>
30inline bool isRunningInstalled() {
31 // Tests don't use the normal binary, so we want to check binary *and*
32 // the environment. Installed tests have UNITY_TESTING_DATADIR set.
33 static bool installed = (QCoreApplication::applicationDirPath() ==
34 QDir(QStringLiteral("@CMAKE_INSTALL_PREFIX@/@CMAKE_INSTALL_BINDIR@")).canonicalPath());
35 static bool installed_test_env = !qgetenv("UNITY_TESTING_DATADIR").isEmpty();
36 return installed || installed_test_env;
39inline QString buildDirectory() {
40 if (!qEnvironmentVariableIsEmpty("UNITY_BINARY_DIR")) return qgetenv("UNITY_BINARY_DIR");
41 return QStringLiteral("@CMAKE_BINARY_DIR@");
44inline QString sourceDirectory() {
45 if (!qEnvironmentVariableIsEmpty("UNITY_SOURCE_DIR")) return qgetenv("UNITY_SOURCE_DIR");
46 return QStringLiteral("@CMAKE_SOURCE_DIR@");
49inline QString translationDirectory() {
50 if (isRunningInstalled()) {
51 return QStringLiteral("@CMAKE_INSTALL_PREFIX@/share/locale");
52 } else {
53 return QString(buildDirectory() + "/po/locale");
54 }
57inline QString testDataDir() {
58 // Can't use isRunningInstalled, since tests don't use the normal unity8
59 // executable. $UNITY_TESTING_DATADIR is set by installed tests.
60 QString datadir(qgetenv("UNITY_TESTING_DATADIR"));
61 if (datadir.isEmpty()) {
62 return sourceDirectory() + "/tests";
63 } else {
64 return datadir + "/tests";
65 }
68inline QString testLibDir() {
69 // Can't use isRunningInstalled, since tests don't use the normal unity8
70 // executable. $UNITY_TESTING_DATADIR is set by installed tests.
71 QString libdir(qgetenv("UNITY_TESTING_LIBDIR"));
72 if (libdir.isEmpty()) {
73 return buildDirectory() + "/tests";
74 } else {
75 return libdir + "/tests";
76 }
79inline QString qmlDirectory() {
80 if (isRunningInstalled()) {
81 return QStringLiteral("@CMAKE_INSTALL_PREFIX@/@SHELL_APP_DIR@/");
82 } else {
83 return QString(sourceDirectory() + "/qml");
84 }
87inline QStringList overrideImportPaths() {
88 QStringList paths;
89 if (!isRunningInstalled()) {
90 paths << QString(buildDirectory() + "/plugins");
91 }
92 return paths;
95inline QStringList nonMirImportPaths() {
96 QStringList paths;
97 if (isRunningInstalled()) {
98 paths << QStringLiteral("@CMAKE_INSTALL_PREFIX@/@SHELL_INSTALL_QML@/nonmirplugins");
99 } else {
100 paths << QString(buildDirectory() + "/nonmirplugins");
101 }
102 return paths;
105inline QStringList fallbackImportPaths() {
106 QStringList paths;
107 if (isRunningInstalled()) {
108 paths << QStringLiteral("@CMAKE_INSTALL_PREFIX@/@SHELL_INSTALL_QML@");
109 paths << QStringLiteral("@USS_PRIVATE_PLUGINDIR@");
110 paths << QStringLiteral("@SHELL_PLUGINDIR@");
111 paths << QStringLiteral("@CMAKE_INSTALL_PREFIX@/@SHELL_INSTALL_QML@/mocks");
112 } else {
113 paths << QStringLiteral("@USS_PRIVATE_PLUGINDIR@");
114 paths << QStringLiteral("@SHELL_PLUGINDIR@");
115 paths << QString(buildDirectory() + "/tests/mocks");
116 }
117 return paths;
120inline QString mockPluginsDir() {
121 if (isRunningInstalled()) {
122 return QStringLiteral("@CMAKE_INSTALL_PREFIX@/@SHELL_INSTALL_QML@/mocks");
123 } else {
124 return QString(buildDirectory() + "/tests/mocks");
125 }
128inline QStringList shellDataDirs() {
129 QStringList dirs = QStandardPaths::standardLocations(QStandardPaths::GenericDataLocation);
130 if (isRunningInstalled()) {
131 // append so by default we use xdg files.
132 dirs.append(qmlDirectory());
133 }
134 return dirs;
137inline void prependImportPaths(QQmlEngine *engine, const QStringList &paths)
139 QStringList importPathList = engine->importPathList();
140 for (int i = paths.count() -1; i >= 0; i--) {
141 // don't duplicate
142 const QString& path = paths[i];
143 QStringList::iterator iter = qFind(importPathList.begin(), importPathList.end(), path);
144 if (iter == importPathList.end()) {
145 engine->addImportPath(path);
146 }
147 }
150/* When you append and import path to the list of import paths it will be the *last*
151 place where Qt will search for QML modules.
152 The usual QQmlEngine::addImportPath() actually prepends the given path.*/
153inline void appendImportPaths(QQmlEngine *engine, const QStringList &paths)
155 QStringList importPathList = engine->importPathList();
156 Q_FOREACH(const QString& path, paths) {
157 // don't duplicate
158 QStringList::iterator iter = qFind(importPathList.begin(), importPathList.end(), path);
159 if (iter == importPathList.end()) {
160 importPathList.append(path);
161 }
162 }
163 engine->setImportPathList(importPathList);
166#endif