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