/*
* 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 .
*/
#pragma once
// Qt
#include
#include
#include
#include
#include
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() {
return !installRoot().isNull();
}
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 installRoot() + QStringLiteral("@CMAKE_INSTALL_PREFIX@/share/locale");
} else {
return buildDirectory() + QStringLiteral("/po/locale");
}
}
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 = 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);
}