on 62ef20c help sign in
unity8/tests/mocks/Unity/Application/ApplicationInfo.cpp
375 lines 10.0 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
53ApplicationInfo::ApplicationInfo(const QString &appId, QObject *parent)
54 : ApplicationInfoInterface(appId, parent)
55 , m_appId(appId)
57 connect(&m_surfaceList, &MirSurfaceListModel::countChanged,
58 this, &ApplicationInfo::onSurfaceCountChanged, Qt::QueuedConnection);
60 m_surfaceCreationTimer.setSingleShot(true);
61 m_surfaceCreationTimer.setInterval(500);
62 connect(&m_surfaceCreationTimer, &QTimer::timeout, this, &ApplicationInfo::createSurface);
65ApplicationInfo::ApplicationInfo(QObject *parent)
66 : ApplicationInfo(QString(), parent)
70ApplicationInfo::~ApplicationInfo()
74void ApplicationInfo::createSurface()
76 if (state() == ApplicationInfo::Stopped) { return; }
78 QString surfaceName = name();
79 if (m_surfaceList.count() > 0) {
80 surfaceName.append(QString(" %1").arg(m_surfaceList.count()+1));
81 }
83 auto surface = SurfaceManager::singleton()->createSurface(surfaceName,
84 Mir::NormalType,
85 fullscreen() ? Mir::FullscreenState : Mir::MaximizedState,
86 m_screenshotFileName);
88 surface->setShellChrome(m_shellChrome);
90 m_surfaceList.appendSurface(surface);
92 ++m_liveSurfaceCount;
93 connect(surface, &MirSurface::liveChanged, this, [this, surface](){
94 if (!surface->live()) {
95 --m_liveSurfaceCount;
96 if (m_liveSurfaceCount == 0) {
97 if (m_closingSurfaces.contains(surface)
98 || (m_state == Running && m_requestedState == RequestedRunning)) {
99 Q_EMIT closed();
100 }
101 setState(Stopped);
102 } else {
103 if (m_closingSurfaces.contains(surface) && m_requestedState == RequestedSuspended
104 && m_closingSurfaces.count() == 1) {
105 setState(Suspended);
106 }
107 }
108 m_closingSurfaces.removeAll(surface);
109 }
110 });
111 connect(surface, &MirSurface::closeRequested, this, [this, surface](){
112 m_closingSurfaces.append(surface);
113 if (m_state == Suspended) {
114 // resume to allow application to close its surface
115 setState(Running);
116 }
117 });
119 if (m_state == Starting) {
120 if (m_requestedState == RequestedRunning) {
121 setState(Running);
122 } else {
123 setState(Suspended);
124 }
125 }
128void ApplicationInfo::setIconId(const QString &iconId)
130 setIcon(QString("../../tests/graphics/applicationIcons/%2@18.png")
131 .arg(iconId));
134void ApplicationInfo::setScreenshotId(const QString &screenshotId)
136 QString screenshotFileName;
138 if (screenshotId.endsWith(".svg")) {
139 screenshotFileName = QString("qrc:///Unity/Application/screenshots/%2")
140 .arg(screenshotId);
141 } else {
142 screenshotFileName = QString("qrc:///Unity/Application/screenshots/%2@12.png")
143 .arg(screenshotId);
144 }
146 if (screenshotFileName != m_screenshotFileName) {
147 m_screenshotFileName = screenshotFileName;
148 }
151void ApplicationInfo::setName(const QString &value)
153 if (value != m_name) {
154 m_name = value;
155 Q_EMIT nameChanged(value);
156 }
159void ApplicationInfo::setIcon(const QUrl &value)
161 if (value != m_icon) {
162 m_icon = value;
163 Q_EMIT iconChanged(value);
164 }
167void ApplicationInfo::setStage(Stage value)
169 if (value != m_stage) {
170 m_stage = value;
171 Q_EMIT stageChanged(value);
172 }
175void ApplicationInfo::setState(State value)
177 if (value != m_state) {
178 DEBUG_MSG(qPrintable(stateToStr(value)));
179 if (!m_manualSurfaceCreation && value == ApplicationInfo::Starting) {
180 Q_ASSERT(m_surfaceList.count() == 0);
181 m_surfaceCreationTimer.start();
182 } else if (value == ApplicationInfo::Stopped) {
183 m_surfaceCreationTimer.stop();
184 for (int i = 0; i < m_surfaceList.count(); ++i) {
185 MirSurface *surface = static_cast<MirSurface*>(m_surfaceList.get(i));
186 surface->setLive(false);
187 }
188 }
190 m_state = value;
191 Q_EMIT stateChanged(value);
192 }
195void ApplicationInfo::close()
197 DEBUG_MSG("");
199 if (m_surfaceList.count() > 0) {
200 for (int i = 0; i < m_surfaceList.count(); ++i) {
201 MirSurface *surface = static_cast<MirSurface*>(m_surfaceList.get(i));
202 surface->close();
203 }
204 } else {
205 setState(Stopped);
206 Q_EMIT closed();
207 }
210void ApplicationInfo::setFullscreen(bool value)
212 m_fullscreen = value;
213 if (m_surfaceList.rowCount() > 0) {
214 m_surfaceList.get(0)->setState(Mir::FullscreenState);
215 }
218bool ApplicationInfo::fullscreen() const
220 if (m_surfaceList.rowCount() > 0) {
221 return m_surfaceList.get(0)->state() == Mir::FullscreenState;
222 } else {
223 return m_fullscreen;
224 }
227void ApplicationInfo::setManualSurfaceCreation(bool value)
229 if (value != m_manualSurfaceCreation) {
230 m_manualSurfaceCreation = value;
231 Q_EMIT manualSurfaceCreationChanged(value);
232 }
235Qt::ScreenOrientations ApplicationInfo::supportedOrientations() const
237 return m_supportedOrientations;
240void ApplicationInfo::setSupportedOrientations(Qt::ScreenOrientations orientations)
242 m_supportedOrientations = orientations;
245bool ApplicationInfo::rotatesWindowContents() const
247 return m_rotatesWindowContents;
250void ApplicationInfo::setRotatesWindowContents(bool value)
252 m_rotatesWindowContents = value;
255ApplicationInfo::RequestedState ApplicationInfo::requestedState() const
257 return m_requestedState;
260void ApplicationInfo::setRequestedState(RequestedState value)
262 if (m_requestedState == value) {
263 return;
264 }
265 DEBUG_MSG((value == RequestedRunning ? "RequestedRunning" : "RequestedSuspended") );
267 m_requestedState = value;
268 Q_EMIT requestedStateChanged(m_requestedState);
270 if (m_requestedState == RequestedRunning) {
272 if (m_state == Suspended) {
273 Q_ASSERT(m_liveSurfaceCount > 0);
274 setState(Running);
275 } else if (m_state == Stopped) {
276 Q_ASSERT(m_liveSurfaceCount == 0);
277 // it's restarting
278 setState(Starting);
279 }
281 } else if (m_requestedState == RequestedSuspended && m_state == Running
282 && m_closingSurfaces.isEmpty()) {
283 setState(Suspended);
284 }
287bool ApplicationInfo::isTouchApp() const
289 return m_isTouchApp;
292void ApplicationInfo::setIsTouchApp(bool isTouchApp)
294 m_isTouchApp = isTouchApp;
297bool ApplicationInfo::exemptFromLifecycle() const
299 return m_exemptFromLifecycle;
302void ApplicationInfo::setExemptFromLifecycle(bool exemptFromLifecycle)
304 if (m_exemptFromLifecycle != exemptFromLifecycle)
305 {
306 m_exemptFromLifecycle = exemptFromLifecycle;
307 Q_EMIT exemptFromLifecycleChanged(m_exemptFromLifecycle);
308 }
311QSize ApplicationInfo::initialSurfaceSize() const
313 return m_initialSurfaceSize;
316void ApplicationInfo::setInitialSurfaceSize(const QSize &size)
318 if (size != m_initialSurfaceSize) {
319 m_initialSurfaceSize = size;
320 Q_EMIT initialSurfaceSizeChanged(m_initialSurfaceSize);
321 }
324void ApplicationInfo::setShellChrome(Mir::ShellChrome shellChrome)
326 m_shellChrome = shellChrome;
327 if (m_surfaceList.rowCount() > 0) {
328 static_cast<MirSurface*>(m_surfaceList.get(0))->setShellChrome(shellChrome);
329 }
332bool ApplicationInfo::focused() const
334 bool someSurfaceHasFocus = false; // to be proven wrong
335 for (int i = 0; i < m_surfaceList.count() && !someSurfaceHasFocus; ++i) {
336 someSurfaceHasFocus = m_surfaceList.get(i)->focused();
337 }
338 return someSurfaceHasFocus;
341void ApplicationInfo::setFocused(bool value)
343 if (focused() == value) {
344 return;
345 }
347 if (value) {
348 if (m_surfaceList.count() > 0) {
349 m_surfaceList.get(0)->requestFocus();
350 }
351 } else {
352 for (int i = 0; i < m_surfaceList.count(); ++i) {
353 MirSurface *surface = static_cast<MirSurface*>(m_surfaceList.get(i));
354 if (surface->focused()) {
355 surface->setFocused(false);
356 }
357 }
358 }
361void ApplicationInfo::onSurfaceCountChanged()
363 if (m_surfaceList.count() == 0 && m_state == Running) {
364 setState(Stopped);
365 }
368void ApplicationInfo::requestFocus()
370 if (m_surfaceList.count() == 0) {
371 Q_EMIT focusRequested();
372 } else {
373 m_surfaceList.get(0)->requestFocus();
374 }