Lomiri
Loading...
Searching...
No Matches
LomiriApplication.cpp
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 */
16
17#include "LomiriApplication.h"
18
19// Qt
20#include <QLibrary>
21#include <QProcess>
22#include <QScreen>
23#include <QQmlContext>
24#include <QQmlComponent>
25
26#include <QGSettings>
27
28#include <libintl.h>
29
30// qtmir
31#include <qtmir/displayconfigurationstorage.h>
32
33// local
34#include <paths.h>
35#include "CachingNetworkManagerFactory.h"
36#include "LomiriCommandLineParser.h"
37#include "DebuggingController.h"
38#include "WindowManagementPolicy.h"
39#include "DisplayConfigurationStorage.h"
40
41#include <QDebug>
42
43
44
45LomiriApplication::LomiriApplication(int & argc, char ** argv)
46 : qtmir::MirServerApplication(argc, argv, { qtmir::SetWindowManagementPolicy<WindowManagementPolicy>(),
47 qtmir::SetDisplayConfigurationStorage<DisplayConfigurationStorage>() })
48 , m_qmlArgs(this)
49{
50 setApplicationName(QStringLiteral("lomiri"));
51 setOrganizationName(QStringLiteral("UBports"));
52
53 setupQmlEngine();
54
55 // The testability driver is only loaded by QApplication but not by QGuiApplication.
56 // However, QApplication depends on QWidget which would add some unneeded overhead => Let's load the testability driver on our own.
57 if (m_qmlArgs.hasTestability() || getenv("QT_LOAD_TESTABILITY")) {
58 QLibrary testLib(QStringLiteral("qttestability"));
59 if (testLib.load()) {
60 typedef void (*TasInitialize)(void);
61 TasInitialize initFunction = (TasInitialize)testLib.resolve("qt_testability_init");
62 if (initFunction) {
63 initFunction();
64 } else {
65 qCritical("Library qttestability resolve failed!");
66 }
67 } else {
68 qCritical("Library qttestability load failed!");
69 }
70 }
71
72 bindtextdomain("lomiri", translationDirectory().toUtf8().data());
73 textdomain("lomiri");
74
75 QByteArray pxpguEnv = qgetenv("GRID_UNIT_PX");
76 bool ok;
77 int pxpgu = pxpguEnv.toInt(&ok);
78 if (!ok) {
79 pxpgu = 8;
80 }
81 m_qmlEngine->rootContext()->setContextProperty("internalGu", pxpgu);
82 m_qmlEngine->rootContext()->setContextProperty(QStringLiteral("applicationArguments"), &m_qmlArgs);
83 m_qmlEngine->rootContext()->setContextProperty("DebuggingController", new DebuggingController(this));
84
85 auto component(new QQmlComponent(m_qmlEngine, m_qmlArgs.qmlfie()));
86 component->create();
87 if (component->status() == QQmlComponent::Error) {
88 qDebug().nospace().noquote() \
89 << "Lomiri encountered an unrecoverable error while loading:\n"
90 << component->errorString();
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 if (!errorComponent->errorString().isEmpty())
96 qDebug().nospace().noquote() \
97 << "Lomiri encountered an error while loading the error screen:\n"
98 << errorComponent->errorString();
99 return;
100 }
101
102 #ifdef LOMIRI_ENABLE_TOUCH_EMULATION
103 // You will need this if you want to interact with touch-only components using a mouse
104 // Needed only when manually testing on a desktop.
105 if (m_qmlArgs.hasMouseToTouch()) {
106 m_mouseTouchAdaptor = MouseTouchAdaptor::instance();
107 }
108 #endif
109}
110
111LomiriApplication::~LomiriApplication()
112{
113 destroyResources();
114}
115
116void LomiriApplication::destroyResources()
117{
118 #ifdef LOMIRI_ENABLE_TOUCH_EMULATION
119 delete m_mouseTouchAdaptor;
120 m_mouseTouchAdaptor = nullptr;
121 #endif
122
123 delete m_qmlEngine;
124 m_qmlEngine = nullptr;
125}
126
127void LomiriApplication::setupQmlEngine()
128{
129 m_qmlEngine = new QQmlEngine(this);
130
131 m_qmlEngine->setBaseUrl(QUrl::fromLocalFile(::qmlDirectory()));
132
133 prependImportPaths(m_qmlEngine, ::overrideImportPaths());
134 appendImportPaths(m_qmlEngine, ::fallbackImportPaths());
135
136 m_qmlEngine->setNetworkAccessManagerFactory(new CachingNetworkManagerFactory);
137
138 QObject::connect(m_qmlEngine, &QQmlEngine::quit, this, &QGuiApplication::quit);
139}