on 6946049 help sign in
unity8/tests/mocks/Unity/Application/ApplicationInfo.cpp
394 lines 11 kB history 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 */
17#include "ApplicationInfo.h"
18#include "MirSurface.h"
19#include "SurfaceManager.h"
21#include <paths.h>
23#include <QGuiApplication>
24#include <QQuickItem>
25#include <QQuickView>
26#include <QQmlComponent>
28#define APPLICATION_DEBUG 0
30#if APPLICATION_DEBUG
31#define DEBUG_MSG(params) qDebug().nospace() << "Application["<<appId()<<"]::" << __func__ << " " << params
33QString stateToStr(ApplicationInfo::State state)
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 };
49#else
50#define DEBUG_MSG(params) ((void)0)
51#endif
53#define WARNING_MSG(params) qWarning().nospace() << "Application["<<appId()<<"]::" << __func__ << " " << params
55ApplicationInfo::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))
61 connect(m_surfaceList, &MirSurfaceListModel::countChanged,
62 this, &ApplicationInfo::onSurfaceCountChanged, Qt::QueuedConnection);
64 m_surfaceCreationTimer.setSingleShot(true);
65 m_surfaceCreationTimer.setInterval(500);
66 connect(&m_surfaceCreationTimer, &QTimer::timeout, this, &ApplicationInfo::createSurface);
69ApplicationInfo::ApplicationInfo(QObject *parent)
70 : ApplicationInfo(QString(), parent)
74ApplicationInfo::~ApplicationInfo()
78void ApplicationInfo::createSurface()
80 if (state() == ApplicationInfo::Stopped) { return; }
82 QString surfaceName = name();
83 if (m_surfaceList->count() > 0) {
84 surfaceName.append(QString(" %1").arg(m_surfaceList->count()+1));
85 }
87 auto surfaceManager = SurfaceManager::instance();
88 if (!surfaceManager) {
89 WARNING_MSG("No SurfaceManager");
90 return;
91 }
93 auto surface = surfaceManager->createSurface(surfaceName,
94 Mir::NormalType,
95 fullscreen() ? Mir::FullscreenState : Mir::MaximizedState,
96 m_screenshotFileName);
98 surface->setShellChrome(m_shellChrome);
100 m_surfaceList->appendSurface(surface);
102 ++m_liveSurfaceCount;
103 connect(surface, &MirSurface::liveChanged, this, [this, surface](){
104 if (!surface->live()) {
105 --m_liveSurfaceCount;
106 if (m_liveSurfaceCount == 0) {
107 if (m_closingSurfaces.contains(surface)
108 || (m_state == Running && m_requestedState == RequestedRunning)) {
109 Q_EMIT closed();
110 }
111 setState(Stopped);
112 } else {
113 if (m_closingSurfaces.contains(surface) && m_requestedState == RequestedSuspended
114 && m_closingSurfaces.count() == 1) {
115 setState(Suspended);
116 }
117 }
118 m_closingSurfaces.removeAll(surface);
119 }
120 });
121 connect(surface, &MirSurface::closeRequested, this, [this, surface](){
122 m_closingSurfaces.append(surface);
123 if (m_state == Suspended) {
124 // resume to allow application to close its surface
125 setState(Running);
126 }
127 });
128 connect(surface, &MirSurface::focusRequested, this, &ApplicationInfo::focusRequested);
130 if (m_state == Starting) {
131 if (m_requestedState == RequestedRunning) {
132 setState(Running);
133 } else {
134 setState(Suspended);
135 }
136 }
139void ApplicationInfo::setIconId(const QString &iconId)
141 setIcon(QString("../../tests/graphics/applicationIcons/%2@18.png")
142 .arg(iconId));
145void ApplicationInfo::setScreenshotId(const QString &screenshotId)
147 QString screenshotFileName;
149 if (screenshotId.endsWith(".svg")) {
150 screenshotFileName = QString("qrc:///Unity/Application/screenshots/%2")
151 .arg(screenshotId);
152 } else {
153 screenshotFileName = QString("qrc:///Unity/Application/screenshots/%2@12.png")
154 .arg(screenshotId);
155 }
157 if (screenshotFileName != m_screenshotFileName) {
158 m_screenshotFileName = screenshotFileName;
159 }
162void ApplicationInfo::setName(const QString &value)
164 if (value != m_name) {
165 m_name = value;
166 Q_EMIT nameChanged(value);
167 }
170void ApplicationInfo::setIcon(const QUrl &value)
172 if (value != m_icon) {
173 m_icon = value;
174 Q_EMIT iconChanged(value);
175 }
178void ApplicationInfo::setStage(Stage value)
180 if (value != m_stage) {
181 m_stage = value;
182 Q_EMIT stageChanged(value);
183 }
186void ApplicationInfo::setState(State value)
188 if (value != m_state) {
189 DEBUG_MSG(qPrintable(stateToStr(value)));
190 if (!m_manualSurfaceCreation && value == ApplicationInfo::Starting) {
191 Q_ASSERT(m_surfaceList->count() == 0);
192 m_surfaceCreationTimer.start();
193 } else if (value == ApplicationInfo::Stopped) {
194 m_surfaceCreationTimer.stop();
195 for (int i = 0; i < m_surfaceList->count(); ++i) {
196 MirSurface *surface = static_cast<MirSurface*>(m_surfaceList->get(i));
197 surface->setLive(false);
198 }
199 for (int i = 0; i < m_promptSurfaceList->count(); ++i) {
200 auto surface = static_cast<MirSurface*>(m_promptSurfaceList->get(i));
201 surface->setLive(false);
202 }
203 }
205 m_state = value;
206 Q_EMIT stateChanged(value);
207 }
210void ApplicationInfo::close()
212 DEBUG_MSG("");
214 if (m_surfaceList->count() > 0) {
215 for (int i = 0; i < m_surfaceList->count(); ++i) {
216 MirSurface *surface = static_cast<MirSurface*>(m_surfaceList->get(i));
217 surface->close();
218 }
219 } else {
220 setState(Stopped);
221 Q_EMIT closed();
222 }
225void ApplicationInfo::setFullscreen(bool value)
227 m_fullscreen = value;
228 if (m_surfaceList->rowCount() > 0) {
229 m_surfaceList->get(0)->setState(Mir::FullscreenState);
230 }
233bool ApplicationInfo::fullscreen() const
235 if (m_surfaceList->rowCount() > 0) {
236 return m_surfaceList->get(0)->state() == Mir::FullscreenState;
237 } else {
238 return m_fullscreen;
239 }
242void ApplicationInfo::setManualSurfaceCreation(bool value)
244 if (value != m_manualSurfaceCreation) {
245 m_manualSurfaceCreation = value;
246 Q_EMIT manualSurfaceCreationChanged(value);
248 if (m_manualSurfaceCreation && m_surfaceCreationTimer.isActive()) {
249 m_surfaceCreationTimer.stop();
250 }
251 }
254Qt::ScreenOrientations ApplicationInfo::supportedOrientations() const
256 return m_supportedOrientations;
259void ApplicationInfo::setSupportedOrientations(Qt::ScreenOrientations orientations)
261 m_supportedOrientations = orientations;
264bool ApplicationInfo::rotatesWindowContents() const
266 return m_rotatesWindowContents;
269void ApplicationInfo::setRotatesWindowContents(bool value)
271 m_rotatesWindowContents = value;
274ApplicationInfo::RequestedState ApplicationInfo::requestedState() const
276 return m_requestedState;
279void ApplicationInfo::setRequestedState(RequestedState value)
281 if (m_requestedState == value) {
282 return;
283 }
284 DEBUG_MSG((value == RequestedRunning ? "RequestedRunning" : "RequestedSuspended") );
286 m_requestedState = value;
287 Q_EMIT requestedStateChanged(m_requestedState);
289 if (m_requestedState == RequestedRunning) {
291 if (m_state == Suspended) {
292 Q_ASSERT(m_liveSurfaceCount > 0);
293 setState(Running);
294 } else if (m_state == Stopped) {
295 Q_ASSERT(m_liveSurfaceCount == 0);
296 // it's restarting
297 setState(Starting);
298 }
300 } else if (m_requestedState == RequestedSuspended && m_state == Running
301 && m_closingSurfaces.isEmpty()) {
302 setState(Suspended);
303 }
306bool ApplicationInfo::isTouchApp() const
308 return m_isTouchApp;
311void ApplicationInfo::setIsTouchApp(bool isTouchApp)
313 m_isTouchApp = isTouchApp;
316bool ApplicationInfo::exemptFromLifecycle() const
318 return m_exemptFromLifecycle;
321void ApplicationInfo::setExemptFromLifecycle(bool exemptFromLifecycle)
323 if (m_exemptFromLifecycle != exemptFromLifecycle)
324 {
325 m_exemptFromLifecycle = exemptFromLifecycle;
326 Q_EMIT exemptFromLifecycleChanged(m_exemptFromLifecycle);
327 }
330QSize ApplicationInfo::initialSurfaceSize() const
332 return m_initialSurfaceSize;
335void ApplicationInfo::setInitialSurfaceSize(const QSize &size)
337 if (size != m_initialSurfaceSize) {
338 m_initialSurfaceSize = size;
339 Q_EMIT initialSurfaceSizeChanged(m_initialSurfaceSize);
340 }
343void ApplicationInfo::setShellChrome(Mir::ShellChrome shellChrome)
345 m_shellChrome = shellChrome;
346 if (m_surfaceList->rowCount() > 0) {
347 static_cast<MirSurface*>(m_surfaceList->get(0))->setShellChrome(shellChrome);
348 }
351bool ApplicationInfo::focused() const
353 bool someSurfaceHasFocus = false; // to be proven wrong
354 for (int i = 0; i < m_surfaceList->count() && !someSurfaceHasFocus; ++i) {
355 someSurfaceHasFocus = m_surfaceList->get(i)->focused();
356 }
357 return someSurfaceHasFocus;
360void ApplicationInfo::setFocused(bool value)
362 if (focused() == value) {
363 return;
364 }
366 if (value) {
367 if (m_surfaceList->count() > 0) {
368 m_surfaceList->get(0)->requestFocus();
369 }
370 } else {
371 for (int i = 0; i < m_surfaceList->count(); ++i) {
372 MirSurface *surface = static_cast<MirSurface*>(m_surfaceList->get(i));
373 if (surface->focused()) {
374 surface->setFocused(false);
375 }
376 }
377 }
380void ApplicationInfo::onSurfaceCountChanged()
382 if (m_surfaceList->count() == 0 && m_state == Running) {
383 setState(Stopped);
384 }
387void ApplicationInfo::requestFocus()
389 if (m_surfaceList->count() == 0) {
390 Q_EMIT focusRequested();
391 } else {
392 m_surfaceList->get(0)->requestFocus();
393 }