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  return !installRoot().isNull();
45 }
46 
47 inline QString buildDirectory() {
48  if (!qEnvironmentVariableIsEmpty("UNITY_BINARY_DIR")) return qgetenv("UNITY_BINARY_DIR");
49  return QStringLiteral("/build/unity8-wkBWU2/unity8-8.15+17.04.20161215/obj-x86_64-linux-gnu");
50 }
51 
52 inline QString sourceDirectory() {
53  if (!qEnvironmentVariableIsEmpty("UNITY_SOURCE_DIR")) return qgetenv("UNITY_SOURCE_DIR");
54  return QStringLiteral("/build/unity8-wkBWU2/unity8-8.15+17.04.20161215");
55 }
56 
57 inline QString translationDirectory() {
58  if (isRunningInstalled()) {
59  return installRoot() + QStringLiteral("/usr/share/locale");
60  } else {
61  return buildDirectory() + QStringLiteral("/po/locale");
62  }
63 }
64 
65 inline QString qmlDirectory() {
66  if (isRunningInstalled()) {
67  return installRoot() + QStringLiteral("/usr/share/unity8/");
68  } else {
69  return sourceDirectory() + QStringLiteral("/qml");
70  }
71 }
72 
73 inline QStringList overrideImportPaths() {
74  QStringList paths;
75  if (!isRunningInstalled()) {
76  paths << buildDirectory() + QStringLiteral("/plugins");
77  }
78  return paths;
79 }
80 
81 inline QStringList nonMirImportPaths() {
82  QStringList paths;
83  if (isRunningInstalled()) {
84  paths << installRoot() + QStringLiteral("/usr/lib/x86_64-linux-gnu/unity8/qml/nonmirplugins");
85  } else {
86  paths << buildDirectory() + QStringLiteral("/nonmirplugins");
87  }
88  return paths;
89 }
90 
91 inline QStringList fallbackImportPaths() {
92  QStringList paths;
93  if (isRunningInstalled()) {
94  paths << installRoot() + QStringLiteral("/usr/lib/x86_64-linux-gnu/unity8/qml");
95  paths << installRoot() + QStringLiteral("/usr/lib/x86_64-linux-gnu/ubuntu-system-settings/private");
96  paths << installRoot() + QStringLiteral("/usr/lib/x86_64-linux-gnu/unity8/qml");
97  paths << installRoot() + QStringLiteral("/usr/lib/x86_64-linux-gnu/unity8/qml/mocks");
98  } else {
99  paths << QStringLiteral("/usr/lib/x86_64-linux-gnu/ubuntu-system-settings/private");
100  paths << QStringLiteral("/usr/lib/x86_64-linux-gnu/unity8/qml");
101  paths << buildDirectory() + QStringLiteral("/tests/mocks");
102  }
103  return paths;
104 }
105 
106 inline QString mockPluginsDir() {
107  if (isRunningInstalled()) {
108  return installRoot() + QStringLiteral("/usr/lib/x86_64-linux-gnu/unity8/qml/mocks");
109  } else {
110  return buildDirectory() + QStringLiteral("/tests/mocks");
111  }
112 }
113 
114 inline QStringList shellDataDirs() {
115  QStringList dirs = QStandardPaths::standardLocations(QStandardPaths::GenericDataLocation);
116  if (isRunningInstalled()) {
117  // append so by default we use xdg files.
118  dirs.append(qmlDirectory());
119  }
120  return dirs;
121 }
122 
123 inline void prependImportPaths(QQmlEngine *engine, const QStringList &paths)
124 {
125  QStringList importPathList = engine->importPathList();
126  for (int i = paths.count() -1; i >= 0; i--) {
127  // don't duplicate
128  const QString& path = paths[i];
129  QStringList::iterator iter = qFind(importPathList.begin(), importPathList.end(), path);
130  if (iter == importPathList.end()) {
131  engine->addImportPath(path);
132  }
133  }
134 }
135 
136 /* When you append and import path to the list of import paths it will be the *last*
137  place where Qt will search for QML modules.
138  The usual QQmlEngine::addImportPath() actually prepends the given path.*/
139 inline void appendImportPaths(QQmlEngine *engine, const QStringList &paths)
140 {
141  QStringList importPathList = engine->importPathList();
142  Q_FOREACH(const QString& path, paths) {
143  // don't duplicate
144  QStringList::iterator iter = qFind(importPathList.begin(), importPathList.end(), path);
145  if (iter == importPathList.end()) {
146  importPathList.append(path);
147  }
148  }
149  engine->setImportPathList(importPathList);
150 }