Debian Codegraph @

tests/mocks/QtMir/Application/ApplicationInfo.cpp

raw
1 /*
2 * Copyright (C) 2013-2016 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 "ApplicationInfo.h"
18 #include "MirSurface.h"
19 #include "SurfaceManager.h"
20
21 #include <paths.h>
22
23 #include <QGuiApplication>
24 #include <QQuickItem>
25 #include <QQuickView>
26 #include <QQmlComponent>
27
28 #define APPLICATION_DEBUG 0
29
30 #if APPLICATION_DEBUG
31 #define DEBUG_MSG(params) qDebug().nospace() << "Application["<<appId()<<"]::" << __func__ << " " << params
32
33 QString stateToStr(ApplicationInfo::State state)
34 {
35 switch (state) {
36 case ApplicationInfo::Starting:
37 return "starting";
38 case ApplicationInfo::Running:
39 return "running";
40 case ApplicationInfo::Suspended:
41 return "suspended";
42 case ApplicationInfo::Stopped:
43 return "stopped";
44 default:
45 return "???";
46 };
47 }
48
49 #else
50 #define DEBUG_MSG(params) ((void)0)
51 #endif
52
53 #define WARNING_MSG(params) qWarning().nospace() << "Application["<<appId()<<"]::" << __func__ << " " << params
54
55 ApplicationInfo::ApplicationInfo(const QString &appId, QObject *parent)
56 : ApplicationInfoInterface(appId, parent)
57 , m_appId(appId)
58 , m_surfaceList(new MirSurfaceListModel(this))
59 , m_promptSurfaceList(new MirSurfaceListModel(this))
60 {
61 connect(m_surfaceList, &MirSurfaceListModel::countChanged,
62 this, &ApplicationInfo::onSurfaceCountChanged, Qt::QueuedConnection);
63
64 m_surfaceCreationTimer.setSingleShot(true);
65 m_surfaceCreationTimer.setInterval(500);
66 connect(&m_surfaceCreationTimer, &QTimer::timeout, this, &ApplicationInfo::createSurface);
67 }
68
69 ApplicationInfo::ApplicationInfo(QObject *parent)
70 : ApplicationInfo(QString(), parent)
71 {
72 }
73
74 ApplicationInfo::~ApplicationInfo()
75 {
76 }
77
78 void ApplicationInfo::createPromptSurface()
79 {
80 if (state() == ApplicationInfo::Stopped) { return; }
81
82 auto surfaceManager = SurfaceManager::instance();
83 if (!surfaceManager) {
84 WARNING_MSG("No SurfaceManager");
85 return;
86 }
87
88 QStringList screenshotIds = {"gallery", "map", "facebook", "camera", "browser", "music", "twitter"};
89 int i = rand() % screenshotIds.count();
90
91 QUrl screenshotUrl = QString("qrc:///QtMir/Application/screenshots/%[email protected]")
92 .arg(screenshotIds[i]);
93
94 auto surface = surfaceManager->createSurface(QString("prompt foo"),
95 Mir::NormalType,
96 Mir::RestoredState,
97 nullptr, /* parentSurface */
98 screenshotUrl);
99 surfaceManager->notifySurfaceCreated(surface);
100
101 m_promptSurfaceList->addSurface(surface);
102 }
103
104 void ApplicationInfo::createSurface()
105 {
106 if (state() == ApplicationInfo::Stopped) { return; }
107
108 QString surfaceName = name() + "s window title";
109 if (m_surfaceList->count() > 0) {
110 surfaceName.append(QString(" %1").arg(m_surfaceList->count()+1));
111 }
112
113 auto surfaceManager = SurfaceManager::instance();
114 if (!surfaceManager) {
115 WARNING_MSG("No SurfaceManager");
116 return;
117 }
118
119 bool wasFocused = focused();
120
121 auto surface = surfaceManager->createSurface(surfaceName,
122 Mir::NormalType,
123 fullscreen() ? Mir::FullscreenState : Mir::RestoredState,
124 nullptr, /* parentSurface */
125 m_screenshotFileName,
126 m_qmlFilePath);
127
128 surface->setShellChrome(m_shellChrome);
129
130 m_surfaceList->addSurface(surface);
131
132 ++m_liveSurfaceCount;
133 connect(surface, &MirSurface::liveChanged, this, [this, surface](){
134 if (!surface->live()) {
135 --m_liveSurfaceCount;
136 if (m_liveSurfaceCount == 0) {
137 if (m_closingSurfaces.contains(surface)
138 || (m_state == Running && m_requestedState == RequestedRunning)) {
139 Q_EMIT closed();
140 }
141 setState(Stopped);
142 } else {
143 if (m_closingSurfaces.contains(surface) && m_requestedState == RequestedSuspended
144 && m_closingSurfaces.count() == 1) {
145 setState(Suspended);
146 }
147 }
148 m_closingSurfaces.removeAll(surface);
149 }
150 });
151 connect(surface, &MirSurface::closeRequested, this, [this, surface](){
152 m_closingSurfaces.append(surface);
153 if (m_state == Suspended) {
154 // resume to allow application to close its surface
155 setState(Running);
156 }
157 });
158 connect(surface, &MirSurfaceInterface::focusedChanged, this, [&](bool /*value*/) {
159 #if APPLICATION_DEBUG
160 qDebug().nospace() << "Application[" << appId() << "].focusedChanged(" << focused() << ")";
161 #endif
162 Q_EMIT focusedChanged(focused());
163 });
164
165 connect(surface, &MirSurface::focusRequested, this, &ApplicationInfo::focusRequested);
166
167 if (wasFocused != focused()) {
168 Q_EMIT focusedChanged(focused());
169 }
170
171 if (m_state == Starting) {
172 if (m_requestedState == RequestedRunning) {
173 setState(Running);
174 } else {
175 setState(Suspended);
176 }
177 }
178
179 surfaceManager->notifySurfaceCreated(surface);
180 }
181
182 void ApplicationInfo::setIconId(const QString &iconId)
183 {
184 setIcon(QUrl(testDataDir() + "/graphics/applicationIcons/" + iconId + "@18.png"));
185 }
186
187 void ApplicationInfo::setScreenshotId(const QString &screenshotId)
188 {
189 QString screenshotFileName;
190
191 if (screenshotId.endsWith(".svg")) {
192 screenshotFileName = QString("qrc:///QtMir/Application/screenshots/%2")
193 .arg(screenshotId);
194 } else {
195 screenshotFileName = QString("qrc:///QtMir/Application/screenshots/%[email protected]")
196 .arg(screenshotId);
197 }
198
199 if (screenshotFileName != m_screenshotFileName) {
200 m_screenshotFileName = screenshotFileName;
201 }
202 }
203
204 void ApplicationInfo::setQmlFilename(const QString &qmlFilename)
205 {
206 m_qmlFilePath = QString("qrc:///QtMir/Application/%1").arg(qmlFilename);
207 }
208
209 void ApplicationInfo::setName(const QString &value)
210 {
211 if (value != m_name) {
212 m_name = value;
213 Q_EMIT nameChanged(value);
214 }
215 }
216
217 void ApplicationInfo::setIcon(const QUrl &value)
218 {
219 if (value != m_icon) {
220 m_icon = value;
221 Q_EMIT iconChanged(value);
222 }
223 }
224
225 void ApplicationInfo::setState(State value)
226 {
227 if (value != m_state) {
228 DEBUG_MSG(qPrintable(stateToStr(value)));
229 if (!m_manualSurfaceCreation && value == ApplicationInfo::Starting) {
230 Q_ASSERT(m_surfaceList->count() == 0);
231 m_surfaceCreationTimer.start();
232 } else if (value == ApplicationInfo::Stopped) {
233 m_surfaceCreationTimer.stop();
234 for (int i = 0; i < m_surfaceList->count(); ++i) {
235 MirSurface *surface = static_cast<MirSurface*>(m_surfaceList->get(i));
236 surface->setLive(false);
237 }
238 for (int i = 0; i < m_promptSurfaceList->count(); ++i) {
239 auto surface = static_cast<MirSurface*>(m_promptSurfaceList->get(i));
240 surface->setLive(false);
241 }
242 }
243
244 m_state = value;
245 Q_EMIT stateChanged(value);
246 }
247 }
248
249 void ApplicationInfo::close()
250 {
251 DEBUG_MSG("");
252
253 if (m_surfaceList->count() > 0) {
254 for (int i = 0; i < m_surfaceList->count(); ++i) {
255 MirSurface *surface = static_cast<MirSurface*>(m_surfaceList->get(i));
256 surface->close();
257 }
258 } else {
259 setState(Stopped);
260 Q_EMIT closed();
261 }
262 }
263
264 void ApplicationInfo::setFullscreen(bool value)
265 {
266 m_fullscreen = value;
267 if (m_surfaceList->rowCount() > 0) {
268 Mir::State newState;
269 if (value) {
270 newState = Mir::FullscreenState;
271 } else {
272 newState = Mir::RestoredState;
273 }
274 m_surfaceList->get(0)->requestState(newState);
275 }
276 }
277
278 bool ApplicationInfo::fullscreen() const
279 {
280 if (m_surfaceList->rowCount() > 0) {
281 return m_surfaceList->get(0)->state() == Mir::FullscreenState;
282 } else {
283 return m_fullscreen;
284 }
285 }
286
287 void ApplicationInfo::setManualSurfaceCreation(bool value)
288 {
289 if (value != m_manualSurfaceCreation) {
290 m_manualSurfaceCreation = value;
291 Q_EMIT manualSurfaceCreationChanged(value);
292
293 if (m_manualSurfaceCreation && m_surfaceCreationTimer.isActive()) {
294 m_surfaceCreationTimer.stop();
295 }
296 }
297 }
298
299 Qt::ScreenOrientations ApplicationInfo::supportedOrientations() const
300 {
301 return m_supportedOrientations;
302 }
303
304 void ApplicationInfo::setSupportedOrientations(Qt::ScreenOrientations orientations)
305 {
306 m_supportedOrientations = orientations;
307 }
308
309 bool ApplicationInfo::rotatesWindowContents() const
310 {
311 return m_rotatesWindowContents;
312 }
313
314 void ApplicationInfo::setRotatesWindowContents(bool value)
315 {
316 m_rotatesWindowContents = value;
317 }
318
319 ApplicationInfo::RequestedState ApplicationInfo::requestedState() const
320 {
321 return m_requestedState;
322 }
323
324 void ApplicationInfo::setRequestedState(RequestedState value)
325 {
326 if (m_requestedState == value) {
327 return;
328 }
329 DEBUG_MSG((value == RequestedRunning ? "RequestedRunning" : "RequestedSuspended") );
330
331 m_requestedState = value;
332 Q_EMIT requestedStateChanged(m_requestedState);
333
334 if (m_requestedState == RequestedRunning) {
335
336 if (m_state == Suspended) {
337 Q_ASSERT(m_liveSurfaceCount > 0);
338 setState(Running);
339 } else if (m_state == Stopped) {
340 Q_ASSERT(m_liveSurfaceCount == 0);
341 // it's restarting
342 setState(Starting);
343 }
344
345 } else if (m_requestedState == RequestedSuspended && m_state == Running
346 && m_closingSurfaces.isEmpty()) {
347 setState(Suspended);
348 }
349 }
350
351 bool ApplicationInfo::isTouchApp() const
352 {
353 return m_isTouchApp;
354 }
355
356 void ApplicationInfo::setIsTouchApp(bool isTouchApp)
357 {
358 m_isTouchApp = isTouchApp;
359 }
360
361 bool ApplicationInfo::exemptFromLifecycle() const
362 {
363 return m_exemptFromLifecycle;
364 }
365
366 void ApplicationInfo::setExemptFromLifecycle(bool exemptFromLifecycle)
367 {
368 if (m_exemptFromLifecycle != exemptFromLifecycle)
369 {
370 m_exemptFromLifecycle = exemptFromLifecycle;
371 Q_EMIT exemptFromLifecycleChanged(m_exemptFromLifecycle);
372 }
373 }
374
375 QSize ApplicationInfo::initialSurfaceSize() const
376 {
377 return m_initialSurfaceSize;
378 }
379
380 void ApplicationInfo::setInitialSurfaceSize(const QSize &size)
381 {
382 if (size != m_initialSurfaceSize) {
383 m_initialSurfaceSize = size;
384 Q_EMIT initialSurfaceSizeChanged(m_initialSurfaceSize);
385 }
386 }
387
388 void ApplicationInfo::setShellChrome(Mir::ShellChrome shellChrome)
389 {
390 m_shellChrome = shellChrome;
391 if (m_surfaceList->rowCount() > 0) {
392 static_cast<MirSurface*>(m_surfaceList->get(0))->setShellChrome(shellChrome);
393 }
394 }
395
396 bool ApplicationInfo::focused() const
397 {
398 bool someSurfaceHasFocus = false; // to be proven wrong
399 for (int i = 0; i < m_surfaceList->count() && !someSurfaceHasFocus; ++i) {
400 someSurfaceHasFocus = m_surfaceList->get(i)->focused();
401 }
402 return someSurfaceHasFocus;
403 }
404
405 void ApplicationInfo::onSurfaceCountChanged()
406 {
407 if (m_surfaceList->count() == 0 && m_state == Running) {
408 setState(Stopped);
409 }
410 }
411
412 void ApplicationInfo::requestFocus()
413 {
414 if (m_surfaceList->count() == 0) {
415 Q_EMIT focusRequested();
416 } else {
417 auto surface = static_cast<MirSurface*>(m_surfaceList->get(0));
418 surface->requestFocus();
419 }
420 }