Unity 8
paths.h
1 /*
2  * Copyright (C) 2012-2016 Canonical, Ltd.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 3.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  */
16 
17 #pragma once
18 
19 // Qt
20 #include <QtCore/QCoreApplication>
21 #include <QtCore/QDir>
22 #include <QtGui/QIcon>
23 #include <QtQml/QQmlEngine>
24 #include <QStandardPaths>
25 
26 inline QString installRoot() {
27  static QString installRoot;
28  static bool installRootSet = false;
29  if (!installRootSet) {
30  QString snapRoot = QFile::decodeName(qgetenv("SNAP"));
31  if (!snapRoot.isEmpty() && QCoreApplication::applicationDirPath() ==
32  QDir(snapRoot + QStringLiteral("/usr/bin")).canonicalPath()) {
33  installRoot = snapRoot;
34  } else if (QCoreApplication::applicationDirPath() ==
35  QDir(QStringLiteral("/usr/bin")).canonicalPath()) {
36  installRoot = QStringLiteral("");
37  }
38  installRootSet = true;
39  }
40  return installRoot;
41 }
42 
43 inline bool isRunningInstalled() {
44  // Tests don't use the normal binary, so we want to check binary *and*
45  // the environment. Installed tests have UNITY_TESTING_DATADIR set.
46  static bool installed_test_env = !qgetenv("UNITY_TESTING_DATADIR").isEmpty();
47  return !installRoot().isNull() || installed_test_env;
48 }
49 
50 inline QString buildDirectory() {
51  if (!qEnvironmentVariableIsEmpty("UNITY_BINARY_DIR")) return qgetenv("UNITY_BINARY_DIR");
52  return QStringLiteral("/build/unity8-8.20+ubports+0~20210126050841.1~1.gbp886c0f/obj-x86_64-linux-gnu");
53 }
54 
55 inline QString sourceDirectory() {
56  if (!qEnvironmentVariableIsEmpty("UNITY_SOURCE_DIR")) return qgetenv("UNITY_SOURCE_DIR");
57  return QStringLiteral("/build/unity8-8.20+ubports+0~20210126050841.1~1.gbp886c0f");
58 }
59 
60 inline QString translationDirectory() {
61  if (isRunningInstalled()) {
62  return installRoot() + QStringLiteral("/usr/share/locale");
63  } else {
64  return buildDirectory() + QStringLiteral("/po/locale");
65  }
66 }
67 
68 inline QString testDataDir() {
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 datadir(qgetenv("UNITY_TESTING_DATADIR"));
72  if (datadir.isEmpty()) {
73  return sourceDirectory() + "/tests";
74  } else {
75  return datadir + "/tests";
76  }
77 }
78 
79 inline QString testLibDir() {
80  // Can't use isRunningInstalled, since tests don't use the normal unity8
81  // executable. $UNITY_TESTING_DATADIR is set by installed tests.
82  QString libdir(qgetenv("UNITY_TESTING_LIBDIR"));
83  if (libdir.isEmpty()) {
84  return buildDirectory() + "/tests";
85  } else {
86  return libdir + "/tests";
87  }
88 }
89 
90 inline QString qmlDirectory() {
91  if (isRunningInstalled()) {
92  return installRoot() + QStringLiteral("/usr/share/unity8/");
93  } else {
94  return sourceDirectory() + QStringLiteral("/qml");
95  }
96 }
97 
98 inline QStringList overrideImportPaths() {
99  QStringList paths;
100  if (!isRunningInstalled()) {
101  paths << buildDirectory() + QStringLiteral("/plugins");
102  }
103  return paths;
104 }
105 
106 inline QStringList nonMirImportPaths() {
107  QStringList paths;
108  if (isRunningInstalled()) {
109  paths << installRoot() + QStringLiteral("/usr/lib/x86_64-linux-gnu/unity8/qml/nonmirplugins");
110  } else {
111  paths << buildDirectory() + QStringLiteral("/nonmirplugins");
112  }
113  return paths;
114 }
115 
116 inline QStringList fallbackImportPaths() {
117  QStringList paths;
118  if (isRunningInstalled()) {
119  paths << installRoot() + QStringLiteral("/usr/lib/x86_64-linux-gnu/unity8/qml");
120  paths << installRoot() + QStringLiteral("/usr/lib/x86_64-linux-gnu/ubuntu-system-settings/private");
121  paths << installRoot() + QStringLiteral("/usr/lib/x86_64-linux-gnu/unity8/qml");
122  paths << installRoot() + QStringLiteral("/usr/lib/x86_64-linux-gnu/unity8/qml/mocks");
123  } else {
124  paths << QStringLiteral("/usr/lib/x86_64-linux-gnu/ubuntu-system-settings/private");
125  paths << QStringLiteral("/usr/lib/x86_64-linux-gnu/unity8/qml");
126  paths << buildDirectory() + QStringLiteral("/tests/mocks");
127  }
128  return paths;
129 }
130 
131 inline QString mockPluginsDir() {
132  if (isRunningInstalled()) {
133  return installRoot() + QStringLiteral("/usr/lib/x86_64-linux-gnu/unity8/qml/mocks");
134  } else {
135  return buildDirectory() + QStringLiteral("/tests/mocks");
136  }
137 }
138 
139 inline QStringList shellDataDirs() {
140  QStringList dirs = QStandardPaths::standardLocations(QStandardPaths::GenericDataLocation);
141  if (isRunningInstalled()) {
142  // append so by default we use xdg files.
143  dirs.append(qmlDirectory());
144  }
145  return dirs;
146 }
147 
148 inline void prependImportPaths(QQmlEngine *engine, const QStringList &paths)
149 {
150  QStringList importPathList = engine->importPathList();
151  for (int i = paths.count() -1; i >= 0; i--) {
152  // don't duplicate
153  const QString& path = paths[i];
154  QStringList::iterator iter = qFind(importPathList.begin(), importPathList.end(), path);
155  if (iter == importPathList.end()) {
156  engine->addImportPath(path);
157  }
158  }
159 }
160 
161 /* When you append and import path to the list of import paths it will be the *last*
162  place where Qt will search for QML modules.
163  The usual QQmlEngine::addImportPath() actually prepends the given path.*/
164 inline void appendImportPaths(QQmlEngine *engine, const QStringList &paths)
165 {
166  QStringList importPathList = engine->importPathList();
167  Q_FOREACH(const QString& path, paths) {
168  // don't duplicate
169  QStringList::iterator iter = qFind(importPathList.begin(), importPathList.end(), path);
170  if (iter == importPathList.end()) {
171  importPathList.append(path);
172  }
173  }
174  engine->setImportPathList(importPathList);
175 }