1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
|
/*
* Copyright (C) 2012-2016 Canonical Ltd.
*
* 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 <http://www.gnu.org/licenses/>.
*/
#pragma once
// Qt
#include <QtCore/QCoreApplication>
#include <QtCore/QDir>
#include <QtGui/QIcon>
#include <QtQml/QQmlEngine>
#include <QStandardPaths>
inline QString installRoot() {
static QString installRoot;
static bool installRootSet = false;
if (!installRootSet) {
QString snapRoot = QFile::decodeName(qgetenv("SNAP"));
if (!snapRoot.isEmpty() && QCoreApplication::applicationDirPath() ==
QDir(snapRoot + QStringLiteral("@CMAKE_INSTALL_PREFIX@/@CMAKE_INSTALL_BINDIR@")).canonicalPath()) {
installRoot = snapRoot;
} else if (QCoreApplication::applicationDirPath() ==
QDir(QStringLiteral("@CMAKE_INSTALL_PREFIX@/@CMAKE_INSTALL_BINDIR@")).canonicalPath()) {
installRoot = QStringLiteral("");
}
installRootSet = true;
}
return installRoot;
}
inline bool isRunningInstalled() {
// Tests don't use the normal binary, so we want to check binary *and*
// the environment. Installed tests have LOMIRI_TESTING_DATADIR set.
static bool installed_test_env = !qgetenv("LOMIRI_TESTING_DATADIR").isEmpty();
return !installRoot().isNull() || installed_test_env;
}
inline QString buildDirectory() {
if (!qEnvironmentVariableIsEmpty("LOMIRI_BINARY_DIR")) return qgetenv("LOMIRI_BINARY_DIR");
return QStringLiteral("@CMAKE_BINARY_DIR@");
}
inline QString sourceDirectory() {
if (!qEnvironmentVariableIsEmpty("LOMIRI_SOURCE_DIR")) return qgetenv("LOMIRI_SOURCE_DIR");
return QStringLiteral("@CMAKE_SOURCE_DIR@");
}
inline QString translationDirectory() {
if (isRunningInstalled()) {
return installRoot() + QStringLiteral("@CMAKE_INSTALL_PREFIX@/share/locale");
} else {
return buildDirectory() + QStringLiteral("/po/locale");
}
}
inline QString testDataDir() {
// Can't use isRunningInstalled, since tests don't use the normal lomiri
// executable. $LOMIRI_TESTING_DATADIR is set by installed tests.
QString datadir(qgetenv("LOMIRI_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 lomiri
// executable. $LOMIRI_TESTING_DATADIR is set by installed tests.
QString libdir(qgetenv("LOMIRI_TESTING_LIBEXECDIR"));
if (libdir.isEmpty()) {
return buildDirectory() + "/tests";
} else {
return libdir + "/tests";
}
}
inline QString qmlDirectory() {
if (isRunningInstalled()) {
return installRoot() + QStringLiteral("@CMAKE_INSTALL_PREFIX@/@SHELL_APP_DIR@/");
} else {
return sourceDirectory() + QStringLiteral("/qml");
}
}
inline QStringList overrideImportPaths() {
QStringList paths;
if (!isRunningInstalled()) {
paths << buildDirectory() + QStringLiteral("/plugins");
}
return paths;
}
inline QStringList nonMirImportPaths() {
QStringList paths;
if (isRunningInstalled()) {
paths << installRoot() + QStringLiteral("@CMAKE_INSTALL_PREFIX@/@SHELL_INSTALL_QML@/nonmirplugins");
} else {
paths << buildDirectory() + QStringLiteral("/nonmirplugins");
}
return paths;
}
inline QStringList fallbackImportPaths() {
QStringList paths;
if (isRunningInstalled()) {
paths << installRoot() + QStringLiteral("@CMAKE_INSTALL_PREFIX@/@SHELL_INSTALL_QML@");
paths << installRoot() + QStringLiteral("@USS_PRIVATE_PLUGINDIR@");
paths << installRoot() + QStringLiteral("@SHELL_PLUGINDIR@");
paths << installRoot() + QStringLiteral("@CMAKE_INSTALL_PREFIX@/@SHELL_INSTALL_QML@/mocks");
} else {
paths << QStringLiteral("@USS_PRIVATE_PLUGINDIR@");
paths << QStringLiteral("@SHELL_PLUGINDIR@");
paths << buildDirectory() + QStringLiteral("/tests/mocks");
}
return paths;
}
inline QString mockPluginsDir() {
if (isRunningInstalled()) {
return installRoot() + QStringLiteral("@CMAKE_INSTALL_PREFIX@/@SHELL_INSTALL_QML@/mocks");
} else {
return buildDirectory() + QStringLiteral("/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 = std::find(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 = std::find(importPathList.begin(), importPathList.end(), path);
if (iter == importPathList.end()) {
importPathList.append(path);
}
}
engine->setImportPathList(importPathList);
}
|