on df345d2 help sign in
unity8/src/ShellApplication.cpp
139 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);
73 QByteArray pxpguEnv = qgetenv("GRID_UNIT_PX");
74 bool ok;
75 int pxpgu = pxpguEnv.toInt(&ok);
76 if (!ok) {
77 pxpgu = 8;
78 }
79 m_qmlEngine->rootContext()->setContextProperty("internalGu", pxpgu);
80 m_qmlEngine->load(::qmlDirectory() + "/ShellApplication.qml");
82 #ifdef UNITY8_ENABLE_TOUCH_EMULATION
83 // You will need this if you want to interact with touch-only components using a mouse
84 // Needed only when manually testing on a desktop.
85 if (m_qmlArgs.hasMouseToTouch()) {
86 m_mouseTouchAdaptor = MouseTouchAdaptor::instance();
87 }
88 #endif
90 m_qmlEngine->rootContext()->setContextProperty("DebuggingController", new DebuggingController(this));
92// if (parser.mode().compare("greeter") == 0) {
93// QSize primaryScreenSize = this->primaryScreen()->size();
94// m_shellView->setHeight(primaryScreenSize.height());
95// m_shellView->setWidth(primaryScreenSize.width());
96// m_shellView->show();
97// m_shellView->requestActivate();
98// if (!QProcess::startDetached("initctl emit --no-wait unity8-greeter-started")) {
99// qDebug() << "Unable to send unity8-greeter-started event to Upstart";
100// }
101// } else if (isMirServer || parser.hasFullscreen()) {
102// m_shellView->showFullScreen();
103// } else {
104// m_shellView->show();
105// }
108ShellApplication::~ShellApplication()
110 destroyResources();
113void ShellApplication::destroyResources()
115 #ifdef UNITY8_ENABLE_TOUCH_EMULATION
116 delete m_mouseTouchAdaptor;
117 m_mouseTouchAdaptor = nullptr;
118 #endif
120 delete m_qmlEngine;
121 m_qmlEngine = nullptr;
124void ShellApplication::setupQmlEngine(bool isMirServer)
126 m_qmlEngine = new QQmlApplicationEngine(this);
128 m_qmlEngine->setBaseUrl(QUrl::fromLocalFile(::qmlDirectory()));
130 prependImportPaths(m_qmlEngine, ::overrideImportPaths());
131 if (!isMirServer) {
132 prependImportPaths(m_qmlEngine, ::nonMirImportPaths());
133 }
134 appendImportPaths(m_qmlEngine, ::fallbackImportPaths());
136 m_qmlEngine->setNetworkAccessManagerFactory(new CachingNetworkManagerFactory);
138 QObject::connect(m_qmlEngine, &QQmlEngine::quit, this, &QGuiApplication::quit);