Loading...
Searching...
No Matches
paths.h
/*
* 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("/usr/bin")).canonicalPath()) {
installRoot = snapRoot;
} else if (QCoreApplication::applicationDirPath() ==
QDir(QStringLiteral("/usr/bin")).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("/build/lomiri-98IpdH/lomiri-0.5.0/obj-x86_64-linux-gnu");
}
inline QString sourceDirectory() {
if (!qEnvironmentVariableIsEmpty("LOMIRI_SOURCE_DIR")) return qgetenv("LOMIRI_SOURCE_DIR");
return QStringLiteral("/build/lomiri-98IpdH/lomiri-0.5.0");
}
inline QString translationDirectory() {
if (isRunningInstalled()) {
return installRoot() + QStringLiteral("/usr/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("/usr/share/lomiri/");
} 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("/usr/lib/x86_64-linux-gnu/lomiri/qml/nonmirplugins");
} else {
paths << buildDirectory() + QStringLiteral("/nonmirplugins");
}
return paths;
}
inline QStringList fallbackImportPaths() {
QStringList paths;
if (isRunningInstalled()) {
paths << installRoot() + QStringLiteral("/usr/lib/x86_64-linux-gnu/lomiri/qml");
paths << installRoot() + QStringLiteral("/usr/lib/x86_64-linux-gnu/lomiri-system-settings/private");
paths << installRoot() + QStringLiteral("/usr/lib/x86_64-linux-gnu/lomiri/qml");
paths << installRoot() + QStringLiteral("/usr/lib/x86_64-linux-gnu/lomiri/qml/mocks");
} else {
paths << QStringLiteral("/usr/lib/x86_64-linux-gnu/lomiri-system-settings/private");
paths << QStringLiteral("/usr/lib/x86_64-linux-gnu/lomiri/qml");
paths << buildDirectory() + QStringLiteral("/tests/mocks");
}
return paths;
}
inline QString mockPluginsDir() {
if (isRunningInstalled()) {
return installRoot() + QStringLiteral("/usr/lib/x86_64-linux-gnu/lomiri/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);
}