Unity 8
paths.h
1 /*
2  * Copyright (C) 2012 Canonical, Ltd.
3  *
4  * Authors:
5  * Ugo Riboni <ugo.riboni@canonical.com>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; version 3.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #ifndef PATHS_H
21 #define PATHS_H
22 
23 // Qt
24 #include <QtCore/QCoreApplication>
25 #include <QtCore/QDir>
26 #include <QtGui/QIcon>
27 #include <QtQml/QQmlEngine>
28 #include <QStandardPaths>
29 
30 inline bool isRunningInstalled() {
31  static bool installed = (QCoreApplication::applicationDirPath() ==
32  QDir(QStringLiteral("/usr/bin")).canonicalPath());
33  return installed;
34 }
35 
36 inline QString buildDirectory() {
37  if (!qEnvironmentVariableIsEmpty("UNITY_BINARY_DIR")) return qgetenv("UNITY_BINARY_DIR");
38  return QStringLiteral("/build/unity8-R75PGO/unity8-8.11+16.04.20151104/obj-x86_64-linux-gnu");
39 }
40 
41 inline QString sourceDirectory() {
42  if (!qEnvironmentVariableIsEmpty("UNITY_SOURCE_DIR")) return qgetenv("UNITY_SOURCE_DIR");
43  return QStringLiteral("/build/unity8-R75PGO/unity8-8.11+16.04.20151104");
44 }
45 
46 inline QString translationDirectory() {
47  if (isRunningInstalled()) {
48  return QStringLiteral("/usr/share/locale");
49  } else {
50  return QString(buildDirectory() + "/po/locale");
51  }
52 }
53 
54 inline QString qmlDirectory() {
55  if (isRunningInstalled()) {
56  return QStringLiteral("/usr/share/unity8/");
57  } else {
58  return QString(sourceDirectory() + "/qml");
59  }
60 }
61 
62 inline QStringList overrideImportPaths() {
63  QStringList paths;
64  if (!isRunningInstalled()) {
65  paths << QString(buildDirectory() + "/plugins");
66  }
67  return paths;
68 }
69 
70 inline QStringList nonMirImportPaths() {
71  QStringList paths;
72  if (isRunningInstalled()) {
73  paths << QStringLiteral("/usr/lib/x86_64-linux-gnu/unity8/qml/nonmirplugins");
74  } else {
75  paths << QString(buildDirectory() + "/nonmirplugins");
76  }
77  return paths;
78 }
79 
80 inline QStringList fallbackImportPaths() {
81  QStringList paths;
82  if (isRunningInstalled()) {
83  paths << QStringLiteral("/usr/lib/x86_64-linux-gnu/unity8/qml");
84  paths << QStringLiteral("/usr/lib/x86_64-linux-gnu/ubuntu-system-settings/private");
85  paths << QStringLiteral("/usr/lib/x86_64-linux-gnu/unity8/qml");
86  paths << QStringLiteral("/usr/lib/x86_64-linux-gnu/unity8/qml/mocks");
87  } else {
88  paths << QStringLiteral("/usr/lib/x86_64-linux-gnu/ubuntu-system-settings/private");
89  paths << QStringLiteral("/usr/lib/x86_64-linux-gnu/unity8/qml");
90  paths << QString(buildDirectory() + "/tests/mocks");
91  }
92  return paths;
93 }
94 
95 inline QString mockPluginsDir() {
96  if (isRunningInstalled()) {
97  return QStringLiteral("/usr/lib/x86_64-linux-gnu/unity8/qml/mocks");
98  } else {
99  return QString(buildDirectory() + "/tests/mocks");
100  }
101 }
102 
103 inline QStringList shellDataDirs() {
104  QStringList dirs = QStandardPaths::standardLocations(QStandardPaths::GenericDataLocation);
105  if (isRunningInstalled()) {
106  // append so by default we use xdg files.
107  dirs.append(qmlDirectory());
108  }
109  return dirs;
110 }
111 
112 inline void prependImportPaths(QQmlEngine *engine, const QStringList &paths)
113 {
114  QStringList importPathList = engine->importPathList();
115  for (int i = paths.count() -1; i >= 0; i--) {
116  // don't duplicate
117  const QString& path = paths[i];
118  QStringList::iterator iter = qFind(importPathList.begin(), importPathList.end(), path);
119  if (iter == importPathList.end()) {
120  engine->addImportPath(path);
121  }
122  }
123 }
124 
125 /* When you append and import path to the list of import paths it will be the *last*
126  place where Qt will search for QML modules.
127  The usual QQmlEngine::addImportPath() actually prepends the given path.*/
128 inline void appendImportPaths(QQmlEngine *engine, const QStringList &paths)
129 {
130  QStringList importPathList = engine->importPathList();
131  Q_FOREACH(const QString& path, paths) {
132  // don't duplicate
133  QStringList::iterator iter = qFind(importPathList.begin(), importPathList.end(), path);
134  if (iter == importPathList.end()) {
135  importPathList.append(path);
136  }
137  }
138  engine->setImportPathList(importPathList);
139 }
140 
141 #endif