on 6848933 help sign in
unity8/src/ShellApplication.cpp
138 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 "ShellApplication.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"
36ShellApplication::ShellApplication(int & argc, char ** argv, bool isMirServer)
37 : QGuiApplication(argc, argv)
38 , m_qmlArgs(this)
40 setApplicationName(QStringLiteral("unity8"));
41 setOrganizationName(QStringLiteral("Canonical"));
43 setupQmlEngine(isMirServer);
45 if (m_qmlArgs.deviceName().isEmpty()) {
46 char buffer[200];
47 property_get("ro.product.device", buffer /* value */, "desktop" /* default_value*/);
48 m_qmlArgs.setDeviceName(QString(buffer));
49 }
51 // The testability driver is only loaded by QApplication but not by QGuiApplication.
52 // However, QApplication depends on QWidget which would add some unneeded overhead => Let's load the testability driver on our own.
53 if (m_qmlArgs.hasTestability() || getenv("QT_LOAD_TESTABILITY")) {
54 QLibrary testLib(QStringLiteral("qttestability"));
55 if (testLib.load()) {
56 typedef void (*TasInitialize)(void);
57 TasInitialize initFunction = (TasInitialize)testLib.resolve("qt_testability_init");
58 if (initFunction) {
59 initFunction();
60 } else {
61 qCritical("Library qttestability resolve failed!");
62 }
63 } else {
64 qCritical("Library qttestability load failed!");
65 }
66 }
68 bindtextdomain("unity8", translationDirectory().toUtf8().data());
69 textdomain("unity8");
71 m_qmlEngine->rootContext()->setContextProperty(QStringLiteral("applicationArguments"), &m_qmlArgs);
72 m_qmlEngine->rootContext()->setContextProperty("DebuggingController", new DebuggingController(this));
74 QByteArray pxpguEnv = qgetenv("GRID_UNIT_PX");
75 bool ok;
76 int pxpgu = pxpguEnv.toInt(&ok);
77 if (!ok) {
78 pxpgu = 8;
79 }
80 m_qmlEngine->rootContext()->setContextProperty("internalGu", pxpgu);
81 m_qmlEngine->load(::qmlDirectory() + "/ShellApplication.qml");
83 #ifdef UNITY8_ENABLE_TOUCH_EMULATION
84 // You will need this if you want to interact with touch-only components using a mouse
85 // Needed only when manually testing on a desktop.
86 if (m_qmlArgs.hasMouseToTouch()) {
87 m_mouseTouchAdaptor = MouseTouchAdaptor::instance();
88 }
89 #endif
91 if (m_qmlArgs.mode().compare("greeter") == 0) {
92// QSize primaryScreenSize = this->primaryScreen()->size();
93// m_shellView->setHeight(primaryScreenSize.height());
94// m_shellView->setWidth(primaryScreenSize.width());
95// m_shellView->show();
96// m_shellView->requestActivate();
97 if (!QProcess::startDetached("initctl emit --no-wait unity8-greeter-started")) {
98 qDebug() << "Unable to send unity8-greeter-started event to Upstart";
99 }
100// } else if (isMirServer || parser.hasFullscreen()) {
101// m_shellView->showFullScreen();
102// } else {
103// m_shellView->show();
104 }
107ShellApplication::~ShellApplication()
109 destroyResources();
112void ShellApplication::destroyResources()
114 #ifdef UNITY8_ENABLE_TOUCH_EMULATION
115 delete m_mouseTouchAdaptor;
116 m_mouseTouchAdaptor = nullptr;
117 #endif
119 delete m_qmlEngine;
120 m_qmlEngine = nullptr;
123void ShellApplication::setupQmlEngine(bool isMirServer)
125 m_qmlEngine = new QQmlApplicationEngine(this);
127 m_qmlEngine->setBaseUrl(QUrl::fromLocalFile(::qmlDirectory()));
129 prependImportPaths(m_qmlEngine, ::overrideImportPaths());
130 if (!isMirServer) {
131 prependImportPaths(m_qmlEngine, ::nonMirImportPaths());
132 }
133 appendImportPaths(m_qmlEngine, ::fallbackImportPaths());
135 m_qmlEngine->setNetworkAccessManagerFactory(new CachingNetworkManagerFactory);
137 QObject::connect(m_qmlEngine, &QQmlEngine::quit, this, &QGuiApplication::quit);