Lomiri
Loading...
Searching...
No Matches
LomiriApplication.cpp
1/*
2 * Copyright (C) 2015 Canonical Ltd.
3 * Copyright (C) 2026 UBports Foundation
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; version 3.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include "LomiriApplication.h"
19
20// Qt
21#include <QLibrary>
22#include <QProcess>
23#include <QScreen>
24#include <QQmlContext>
25#include <QQmlComponent>
26
27#include <QGSettings>
28
29#include <libintl.h>
30#include <deviceinfo.h>
31
32// qtmir
33#include <qtmir/displayconfigurationstorage.h>
34
35// local
36#include <paths.h>
37#include "CachingNetworkManagerFactory.h"
38#include "LomiriCommandLineParser.h"
39#include "DebuggingController.h"
40#include "WindowManagementPolicy.h"
41#include "DisplayConfigurationStorage.h"
42
43#include <QDebug>
44
45
46LomiriApplication::LomiriApplication(int & argc, char ** argv)
47 : qtmir::MirServerApplication(argc, argv, { qtmir::SetWindowManagementPolicy<WindowManagementPolicy>(),
48 qtmir::SetDisplayConfigurationStorage<DisplayConfigurationStorage>() })
49 , m_qmlArgs(this)
50{
51 setApplicationName(QStringLiteral("lomiri"));
52 setOrganizationName(QStringLiteral("UBports"));
53
54 setupQmlEngine();
55
56 // The testability driver is only loaded by QApplication but not by QGuiApplication.
57 // However, QApplication depends on QWidget which would add some unneeded overhead => Let's load the testability driver on our own.
58 if (m_qmlArgs.hasTestability() || getenv("QT_LOAD_TESTABILITY")) {
59 QLibrary testLib(QStringLiteral("qttestability"));
60 if (testLib.load()) {
61 typedef void (*TasInitialize)(void);
62 TasInitialize initFunction = (TasInitialize)testLib.resolve("qt_testability_init");
63 if (initFunction) {
64 initFunction();
65 } else {
66 qCritical("Library qttestability resolve failed!");
67 }
68 } else {
69 qCritical("Library qttestability load failed!");
70 }
71 }
72
73 bindtextdomain("lomiri", translationDirectory().toUtf8().data());
74 textdomain("lomiri");
75
76 QByteArray pxpguEnv = qgetenv("GRID_UNIT_PX");
77 bool ok;
78 int pxpgu = pxpguEnv.toInt(&ok);
79 if (!ok) {
80 pxpgu = 8;
81 }
82 m_qmlEngine->rootContext()->setContextProperty("internalGu", pxpgu);
83 m_qmlEngine->rootContext()->setContextProperty(QStringLiteral("applicationArguments"), &m_qmlArgs);
84 m_qmlEngine->rootContext()->setContextProperty("DebuggingController", new DebuggingController(this));
85
86 auto component(new QQmlComponent(m_qmlEngine, m_qmlArgs.qmlfie()));
87 component->create();
88 if (component->status() == QQmlComponent::Error) {
89 qDebug().nospace().noquote() \
90 << "Lomiri encountered an unrecoverable error while loading:\n"
91 << component->errorString();
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 if (!errorComponent->errorString().isEmpty())
97 qDebug().nospace().noquote() \
98 << "Lomiri encountered an error while loading the error screen:\n"
99 << errorComponent->errorString();
100 return;
101 }
102
103 #ifdef LOMIRI_ENABLE_TOUCH_EMULATION
104 // You will need this if you want to interact with touch-only components using a mouse
105 // Needed only when manually testing on a desktop.
106 if (m_qmlArgs.hasMouseToTouch()) {
107 m_mouseTouchAdaptor = MouseTouchAdaptor::instance();
108 }
109 #endif
110}
111
112LomiriApplication::~LomiriApplication()
113{
114 destroyResources();
115}
116
117void LomiriApplication::destroyResources()
118{
119 #ifdef LOMIRI_ENABLE_TOUCH_EMULATION
120 delete m_mouseTouchAdaptor;
121 m_mouseTouchAdaptor = nullptr;
122 #endif
123
124 delete m_qmlEngine;
125 m_qmlEngine = nullptr;
126}
127
128void LomiriApplication::setupQmlEngine()
129{
130 m_qmlEngine = new QQmlEngine(this);
131
132 m_qmlEngine->setBaseUrl(QUrl::fromLocalFile(::qmlDirectory()));
133
134 prependImportPaths(m_qmlEngine, ::overrideImportPaths());
135 appendImportPaths(m_qmlEngine, ::fallbackImportPaths());
136
137 DeviceInfo info;
138 auto panelPath = info.contains("DisplayCutoutsCollapsed") ? "Panel/WithCutouts" : "Panel/WithoutCutouts";
139 prependImportPaths(m_qmlEngine, {QDir(::qmlDirectory() + panelPath).canonicalPath()});
140
141 m_qmlEngine->setNetworkAccessManagerFactory(new CachingNetworkManagerFactory);
142
143 QObject::connect(m_qmlEngine, &QQmlEngine::quit, this, [=](){
144 qDebug().nospace().noquote() << "Quitting Lomiri... Bye!";
145 ::exit(0);
146 });
147 QObject::connect(m_qmlEngine, &QQmlEngine::exit, this, [=](int ret){
148 qDebug().nospace().noquote() << "Exiting Lomiri... Bye!";
149 ::exit(ret);
150 });
151}