on 80fc88d help sign in
unity8/src/UnityApplication.cpp
142 lines 4.7 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>
24#include <QQmlComponent>
26#include <libintl.h>
28// libandroid-properties
29#include <hybris/properties/properties.h>
31// local
32#include <paths.h>
33#include "CachingNetworkManagerFactory.h"
34#include "UnityCommandLineParser.h"
35#include "DebuggingController.h"
36#include "WindowManagementPolicy.h"
38#include <QDebug>
42UnityApplication::UnityApplication(int & argc, char ** argv)
43 : qtmir::MirServerApplication(argc, argv, { qtmir::SetWindowManagementPolicy<WindowManagementPolicy>() })
44 , m_qmlArgs(this)
46 setApplicationName(QStringLiteral("unity8"));
47 setOrganizationName(QStringLiteral("Canonical"));
49 setupQmlEngine();
51 if (m_qmlArgs.deviceName().isEmpty()) {
52 char buffer[200];
53 property_get("ro.product.device", buffer /* value */, "desktop" /* default_value*/);
54 m_qmlArgs.setDeviceName(QString(buffer));
55 }
57 // The testability driver is only loaded by QApplication but not by QGuiApplication.
58 // However, QApplication depends on QWidget which would add some unneeded overhead => Let's load the testability driver on our own.
59 if (m_qmlArgs.hasTestability() || getenv("QT_LOAD_TESTABILITY")) {
60 QLibrary testLib(QStringLiteral("qttestability"));
61 if (testLib.load()) {
62 typedef void (*TasInitialize)(void);
63 TasInitialize initFunction = (TasInitialize)testLib.resolve("qt_testability_init");
64 if (initFunction) {
65 initFunction();
66 } else {
67 qCritical("Library qttestability resolve failed!");
68 }
69 } else {
70 qCritical("Library qttestability load failed!");
71 }
72 }
74 bindtextdomain("unity8", translationDirectory().toUtf8().data());
75 textdomain("unity8");
77 m_qmlEngine->rootContext()->setContextProperty(QStringLiteral("applicationArguments"), &m_qmlArgs);
78 m_qmlEngine->rootContext()->setContextProperty("DebuggingController", new DebuggingController(this));
80 QByteArray pxpguEnv = qgetenv("GRID_UNIT_PX");
81 bool ok;
82 int pxpgu = pxpguEnv.toInt(&ok);
83 if (!ok) {
84 pxpgu = 8;
85 }
86 m_qmlEngine->rootContext()->setContextProperty("internalGu", pxpgu);
88 auto component(new QQmlComponent(m_qmlEngine, m_qmlArgs.qmlfie()));
89 component->create();
90 if (component->status() == QQmlComponent::Error) {
91 m_qmlEngine->rootContext()->setContextProperty(QStringLiteral("errorString"), component->errorString());
92 auto errorComponent(new QQmlComponent(m_qmlEngine,
93 QUrl::fromLocalFile(::qmlDirectory() + "/ErrorApplication.qml")));
94 errorComponent->create();
95 qDebug() << errorComponent->errorString();
96 return;
97 }
99 #ifdef UNITY8_ENABLE_TOUCH_EMULATION
100 // You will need this if you want to interact with touch-only components using a mouse
101 // Needed only when manually testing on a desktop.
102 if (m_qmlArgs.hasMouseToTouch()) {
103 m_mouseTouchAdaptor = MouseTouchAdaptor::instance();
104 }
105 #endif
107 if (m_qmlArgs.mode().compare("greeter") == 0) {
108 if (!QProcess::startDetached("initctl emit --no-wait unity8-greeter-started")) {
109 qDebug() << "Unable to send unity8-greeter-started event to Upstart";
110 }
111 }
114UnityApplication::~UnityApplication()
116 destroyResources();
119void UnityApplication::destroyResources()
121 #ifdef UNITY8_ENABLE_TOUCH_EMULATION
122 delete m_mouseTouchAdaptor;
123 m_mouseTouchAdaptor = nullptr;
124 #endif
126 delete m_qmlEngine;
127 m_qmlEngine = nullptr;
130void UnityApplication::setupQmlEngine()
132 m_qmlEngine = new QQmlEngine(this);
134 m_qmlEngine->setBaseUrl(QUrl::fromLocalFile(::qmlDirectory()));
136 prependImportPaths(m_qmlEngine, ::overrideImportPaths());
137 appendImportPaths(m_qmlEngine, ::fallbackImportPaths());
139 m_qmlEngine->setNetworkAccessManagerFactory(new CachingNetworkManagerFactory);
141 QObject::connect(m_qmlEngine, &QQmlEngine::quit, this, &QGuiApplication::quit);