on bcbc40d help sign in
unity8/src/UnityApplication.cpp
131 lines 4.2 kB history raw
1/*
2 * Copyright (C) 2015 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 */
17#include "UnityApplication.h"
19// Qt
20#include <QLibrary>
21#include <QProcess>
22#include <QScreen>
23#include <QQmlContext>
25#include <libintl.h>
27// libandroid-properties
28#include <hybris/properties/properties.h>
30// local
31#include <paths.h>
32#include "CachingNetworkManagerFactory.h"
33#include "UnityCommandLineParser.h"
34#include "DebuggingController.h"
35#include "WindowManagementPolicy.h"
37#include <QDebug>
41UnityApplication::UnityApplication(int & argc, char ** argv)
42 : qtmir::MirServerApplication(argc, argv, { qtmir::SetWindowManagementPolicy<WindowManagementPolicy>() })
43 , m_qmlArgs(this)
45 setApplicationName(QStringLiteral("unity8"));
46 setOrganizationName(QStringLiteral("Canonical"));
48 setupQmlEngine();
50 if (m_qmlArgs.deviceName().isEmpty()) {
51 char buffer[200];
52 property_get("ro.product.device", buffer /* value */, "desktop" /* default_value*/);
53 m_qmlArgs.setDeviceName(QString(buffer));
54 }
56 // The testability driver is only loaded by QApplication but not by QGuiApplication.
57 // However, QApplication depends on QWidget which would add some unneeded overhead => Let's load the testability driver on our own.
58 if (m_qmlArgs.hasTestability() || getenv("QT_LOAD_TESTABILITY")) {
59 QLibrary testLib(QStringLiteral("qttestability"));
60 if (testLib.load()) {
61 typedef void (*TasInitialize)(void);
62 TasInitialize initFunction = (TasInitialize)testLib.resolve("qt_testability_init");
63 if (initFunction) {
64 initFunction();
65 } else {
66 qCritical("Library qttestability resolve failed!");
67 }
68 } else {
69 qCritical("Library qttestability load failed!");
70 }
71 }
73 bindtextdomain("unity8", translationDirectory().toUtf8().data());
74 textdomain("unity8");
76 m_qmlEngine->rootContext()->setContextProperty(QStringLiteral("applicationArguments"), &m_qmlArgs);
77 m_qmlEngine->rootContext()->setContextProperty("DebuggingController", new DebuggingController(this));
79 QByteArray pxpguEnv = qgetenv("GRID_UNIT_PX");
80 bool ok;
81 int pxpgu = pxpguEnv.toInt(&ok);
82 if (!ok) {
83 pxpgu = 8;
84 }
85 m_qmlEngine->rootContext()->setContextProperty("internalGu", pxpgu);
86 m_qmlEngine->load(m_qmlArgs.qmlfie());
88 #ifdef UNITY8_ENABLE_TOUCH_EMULATION
89 // You will need this if you want to interact with touch-only components using a mouse
90 // Needed only when manually testing on a desktop.
91 if (m_qmlArgs.hasMouseToTouch()) {
92 m_mouseTouchAdaptor = MouseTouchAdaptor::instance();
93 }
94 #endif
96 if (m_qmlArgs.mode().compare("greeter") == 0) {
97 if (!QProcess::startDetached("initctl emit --no-wait unity8-greeter-started")) {
98 qDebug() << "Unable to send unity8-greeter-started event to Upstart";
99 }
100 }
103UnityApplication::~UnityApplication()
105 destroyResources();
108void UnityApplication::destroyResources()
110 #ifdef UNITY8_ENABLE_TOUCH_EMULATION
111 delete m_mouseTouchAdaptor;
112 m_mouseTouchAdaptor = nullptr;
113 #endif
115 delete m_qmlEngine;
116 m_qmlEngine = nullptr;
119void UnityApplication::setupQmlEngine()
121 m_qmlEngine = new QQmlApplicationEngine(this);
123 m_qmlEngine->setBaseUrl(QUrl::fromLocalFile(::qmlDirectory()));
125 prependImportPaths(m_qmlEngine, ::overrideImportPaths());
126 appendImportPaths(m_qmlEngine, ::fallbackImportPaths());
128 m_qmlEngine->setNetworkAccessManagerFactory(new CachingNetworkManagerFactory);
130 QObject::connect(m_qmlEngine, &QQmlEngine::quit, this, &QGuiApplication::quit);