on be6cc65 help sign in
unity8/src/ShellApplication.cpp
146 lines 5.0 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 "ShellApplication.h"
19// Qt
20#include <QLibrary>
21#include <QProcess>
22#include <QScreen>
23#include <QQmlContext>
24#include <QQmlComponent>
26#include <QGSettings>
28#include <libintl.h>
30// libandroid-properties
31#include <hybris/properties/properties.h>
33// local
34#include <paths.h>
35#include "CachingNetworkManagerFactory.h"
36#include "UnityCommandLineParser.h"
37#include "DebuggingController.h"
39ShellApplication::ShellApplication(int & argc, char ** argv, bool isMirServer)
40 : QGuiApplication(argc, argv)
41 , m_qmlArgs(this)
43 setApplicationName(QStringLiteral("unity8"));
44 setOrganizationName(QStringLiteral("Canonical"));
46 setupQmlEngine(isMirServer);
48 if (m_qmlArgs.deviceName().isEmpty()) {
49 char buffer[200];
50 property_get("ro.product.device", buffer /* value */, "desktop" /* default_value*/);
51 m_qmlArgs.setDeviceName(QString(buffer));
52 }
54 // The testability driver is only loaded by QApplication but not by QGuiApplication.
55 // However, QApplication depends on QWidget which would add some unneeded overhead => Let's load the testability driver on our own.
56 if (m_qmlArgs.hasTestability() || getenv("QT_LOAD_TESTABILITY")) {
57 QLibrary testLib(QStringLiteral("qttestability"));
58 if (testLib.load()) {
59 typedef void (*TasInitialize)(void);
60 TasInitialize initFunction = (TasInitialize)testLib.resolve("qt_testability_init");
61 if (initFunction) {
62 initFunction();
63 } else {
64 qCritical("Library qttestability resolve failed!");
65 }
66 } else {
67 qCritical("Library qttestability load failed!");
68 }
69 }
71 bindtextdomain("unity8", translationDirectory().toUtf8().data());
72 textdomain("unity8");
74 QScopedPointer<QGSettings> gSettings(new QGSettings("com.canonical.Unity8"));
75 gSettings->reset(QStringLiteral("alwaysShowOsk"));
78 QByteArray pxpguEnv = qgetenv("GRID_UNIT_PX");
79 bool ok;
80 int pxpgu = pxpguEnv.toInt(&ok);
81 if (!ok) {
82 pxpgu = 8;
83 }
84 m_qmlEngine->rootContext()->setContextProperty("internalGu", pxpgu);
85 m_qmlEngine->rootContext()->setContextProperty(QStringLiteral("applicationArguments"), &m_qmlArgs);
86 m_qmlEngine->rootContext()->setContextProperty("DebuggingController", new DebuggingController(this));
88 auto component(new QQmlComponent(m_qmlEngine,
89 QUrl::fromLocalFile(::qmlDirectory() + "/ShellApplication.qml")));
90 component->create();
91 if (component->status() == QQmlComponent::Error) {
92 m_qmlEngine->rootContext()->setContextProperty(QStringLiteral("errorString"), component->errorString());
93 auto errorComponent(new QQmlComponent(m_qmlEngine,
94 QUrl::fromLocalFile(::qmlDirectory() + "/ErrorApplication.qml")));
95 errorComponent->create();
96 qDebug() << errorComponent->errorString();
97 return;
98 }
100 #ifdef UNITY8_ENABLE_TOUCH_EMULATION
101 // You will need this if you want to interact with touch-only components using a mouse
102 // Needed only when manually testing on a desktop.
103 if (m_qmlArgs.hasMouseToTouch()) {
104 m_mouseTouchAdaptor = MouseTouchAdaptor::instance();
105 }
106 #endif
108 if (m_qmlArgs.mode().compare("greeter") == 0) {
109 if (!QProcess::startDetached("initctl emit --no-wait unity8-greeter-started")) {
110 qDebug() << "Unable to send unity8-greeter-started event to Upstart";
111 }
112 }
115ShellApplication::~ShellApplication()
117 destroyResources();
120void ShellApplication::destroyResources()
122 #ifdef UNITY8_ENABLE_TOUCH_EMULATION
123 delete m_mouseTouchAdaptor;
124 m_mouseTouchAdaptor = nullptr;
125 #endif
127 delete m_qmlEngine;
128 m_qmlEngine = nullptr;
131void ShellApplication::setupQmlEngine(bool isMirServer)
133 m_qmlEngine = new QQmlEngine(this);
135 m_qmlEngine->setBaseUrl(QUrl::fromLocalFile(::qmlDirectory()));
137 prependImportPaths(m_qmlEngine, ::overrideImportPaths());
138 if (!isMirServer) {
139 prependImportPaths(m_qmlEngine, ::nonMirImportPaths());
140 }
141 appendImportPaths(m_qmlEngine, ::fallbackImportPaths());
143 m_qmlEngine->setNetworkAccessManagerFactory(new CachingNetworkManagerFactory);
145 QObject::connect(m_qmlEngine, &QQmlEngine::quit, this, &QGuiApplication::quit);