Lomiri
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 
45 LomiriApplication::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  QScopedPointer<QGSettings> gSettings(new QGSettings("com.lomiri.Shell"));
76  gSettings->reset(QStringLiteral("alwaysShowOsk"));
77 
78 
79  QByteArray pxpguEnv = qgetenv("GRID_UNIT_PX");
80  bool ok;
81  int pxpgu = pxpguEnv.toInt(&ok);
82  if (!ok) {
83  pxpgu = 8;
84  }
85  m_qmlEngine->rootContext()->setContextProperty("internalGu", pxpgu);
86  m_qmlEngine->rootContext()->setContextProperty(QStringLiteral("applicationArguments"), &m_qmlArgs);
87  m_qmlEngine->rootContext()->setContextProperty("DebuggingController", new DebuggingController(this));
88 
89  auto component(new QQmlComponent(m_qmlEngine, m_qmlArgs.qmlfie()));
90  component->create();
91  if (component->status() == QQmlComponent::Error) {
92  qDebug().nospace().noquote() \
93  << "Lomiri encountered an unrecoverable error while loading:\n"
94  << component->errorString();
95  m_qmlEngine->rootContext()->setContextProperty(QStringLiteral("errorString"), component->errorString());
96  auto errorComponent(new QQmlComponent(m_qmlEngine,
97  QUrl::fromLocalFile(::qmlDirectory() + "/ErrorApplication.qml")));
98  errorComponent->create();
99  if (!errorComponent->errorString().isEmpty())
100  qDebug().nospace().noquote() \
101  << "Lomiri encountered an error while loading the error screen:\n"
102  << errorComponent->errorString();
103  return;
104  }
105 
106  #ifdef LOMIRI_ENABLE_TOUCH_EMULATION
107  // You will need this if you want to interact with touch-only components using a mouse
108  // Needed only when manually testing on a desktop.
109  if (m_qmlArgs.hasMouseToTouch()) {
110  m_mouseTouchAdaptor = MouseTouchAdaptor::instance();
111  }
112  #endif
113 }
114 
115 LomiriApplication::~LomiriApplication()
116 {
117  destroyResources();
118 }
119 
120 void LomiriApplication::destroyResources()
121 {
122  #ifdef LOMIRI_ENABLE_TOUCH_EMULATION
123  delete m_mouseTouchAdaptor;
124  m_mouseTouchAdaptor = nullptr;
125  #endif
126 
127  delete m_qmlEngine;
128  m_qmlEngine = nullptr;
129 }
130 
131 void LomiriApplication::setupQmlEngine()
132 {
133  m_qmlEngine = new QQmlEngine(this);
134 
135  m_qmlEngine->setBaseUrl(QUrl::fromLocalFile(::qmlDirectory()));
136 
137  prependImportPaths(m_qmlEngine, ::overrideImportPaths());
138  appendImportPaths(m_qmlEngine, ::fallbackImportPaths());
139 
140  m_qmlEngine->setNetworkAccessManagerFactory(new CachingNetworkManagerFactory);
141 
142  QObject::connect(m_qmlEngine, &QQmlEngine::quit, this, &QGuiApplication::quit);
143 }