on f36dd2a help sign in
unity8/tests/mocks/Unity/Application/ApplicationInfo.cpp
311 lines 8.1 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 "Session.h"
20#include "SessionManager.h"
22#include <paths.h>
24#include <QGuiApplication>
25#include <QQuickItem>
26#include <QQuickView>
27#include <QQmlComponent>
28#include <QTimer>
30ApplicationInfo::ApplicationInfo(const QString &appId, QObject *parent)
31 : ApplicationInfoInterface(appId, parent)
32 , m_appId(appId)
33 , m_stage(MainStage)
34 , m_state(Stopped)
35 , m_focused(false)
36 , m_fullscreen(false)
37 , m_session(0)
38 , m_supportedOrientations(Qt::PortraitOrientation |
39 Qt::LandscapeOrientation |
40 Qt::InvertedPortraitOrientation |
41 Qt::InvertedLandscapeOrientation)
42 , m_rotatesWindowContents(false)
43 , m_requestedState(RequestedRunning)
44 , m_isTouchApp(true)
45 , m_exemptFromLifecycle(false)
46 , m_manualSurfaceCreation(false)
47 , m_shellChrome(Mir::NormalChrome)
51ApplicationInfo::ApplicationInfo(QObject *parent)
52 : ApplicationInfoInterface(QString(), parent)
53 , m_stage(MainStage)
54 , m_state(Stopped)
55 , m_focused(false)
56 , m_fullscreen(false)
57 , m_session(0)
58 , m_supportedOrientations(Qt::PortraitOrientation |
59 Qt::LandscapeOrientation |
60 Qt::InvertedPortraitOrientation |
61 Qt::InvertedLandscapeOrientation)
62 , m_rotatesWindowContents(false)
63 , m_requestedState(RequestedRunning)
64 , m_isTouchApp(true)
65 , m_exemptFromLifecycle(false)
66 , m_manualSurfaceCreation(false)
67 , m_shellChrome(Mir::NormalChrome)
71ApplicationInfo::~ApplicationInfo()
73 delete m_session;
76void ApplicationInfo::createSession()
78 if (m_session || state() == ApplicationInfo::Stopped) { return; }
80 setSession(SessionManager::singleton()->createSession(appId(), m_screenshotFileName));
83void ApplicationInfo::destroySession()
85 Session *session = this->session();
86 setSession(nullptr);
87 delete session;
90void ApplicationInfo::setSession(Session* session)
92 if (m_session == session)
93 return;
95 if (m_session) {
96 disconnect(this, 0, m_session, 0);
97 m_session->setApplication(nullptr);
98 m_session->setParent(nullptr);
99 Q_EMIT m_session->deregister();
100 }
102 m_session = session;
104 if (m_session) {
105 m_session->setApplication(this);
106 m_session->setParent(this);
107 m_session->setFullscreen(m_fullscreen);
108 SessionManager::singleton()->registerSession(m_session);
109 connect(m_session, &Session::surfaceAdded,
110 this, &ApplicationInfo::onSessionSurfaceAdded);
111 connect(m_session, &Session::fullscreenChanged, this, &ApplicationInfo::fullscreenChanged);
113 if (!m_manualSurfaceCreation) {
114 QTimer::singleShot(500, m_session, &Session::createSurface);
115 }
116 }
118 Q_EMIT sessionChanged(m_session);
121void ApplicationInfo::setIconId(const QString &iconId)
123 setIcon(QString("../../tests/graphics/applicationIcons/%2@18.png")
124 .arg(iconId));
127void ApplicationInfo::setScreenshotId(const QString &screenshotId)
129 QString screenshotFileName;
131 if (screenshotId.endsWith(".svg")) {
132 screenshotFileName = QString("qrc:///Unity/Application/screenshots/%2")
133 .arg(screenshotId);
134 } else {
135 screenshotFileName = QString("qrc:///Unity/Application/screenshots/%2@12.png")
136 .arg(screenshotId);
137 }
139 if (screenshotFileName != m_screenshotFileName) {
140 m_screenshotFileName = screenshotFileName;
142 if (m_session) {
143 m_session->setScreenshot(screenshotFileName);
144 }
145 }
148void ApplicationInfo::setName(const QString &value)
150 if (value != m_name) {
151 m_name = value;
152 Q_EMIT nameChanged(value);
153 }
156void ApplicationInfo::setIcon(const QUrl &value)
158 if (value != m_icon) {
159 m_icon = value;
160 Q_EMIT iconChanged(value);
161 }
164void ApplicationInfo::setStage(Stage value)
166 if (value != m_stage) {
167 m_stage = value;
168 Q_EMIT stageChanged(value);
169 }
172void ApplicationInfo::setState(State value)
174 if (value != m_state) {
175 m_state = value;
176 Q_EMIT stateChanged(value);
178 if (!m_manualSurfaceCreation && !m_session && m_state == ApplicationInfo::Starting) {
179 QTimer::singleShot(500, this, &ApplicationInfo::createSession);
180 } else if (m_state == ApplicationInfo::Stopped) {
181 Session *session = m_session;
182 setSession(nullptr);
183 delete session;
184 }
185 }
188void ApplicationInfo::setFocused(bool value)
190 if (value != m_focused) {
191 m_focused = value;
192 Q_EMIT focusedChanged(value);
193 }
196void ApplicationInfo::setFullscreen(bool value)
198 m_fullscreen = value;
199 if (m_session) {
200 m_session->setFullscreen(value);
201 }
204bool ApplicationInfo::fullscreen() const
206 return m_session ? m_session->fullscreen() : m_fullscreen;
209void ApplicationInfo::setManualSurfaceCreation(bool value)
211 if (value != m_manualSurfaceCreation) {
212 m_manualSurfaceCreation = value;
213 Q_EMIT manualSurfaceCreationChanged(value);
214 }
217Qt::ScreenOrientations ApplicationInfo::supportedOrientations() const
219 return m_supportedOrientations;
222void ApplicationInfo::setSupportedOrientations(Qt::ScreenOrientations orientations)
224 m_supportedOrientations = orientations;
227bool ApplicationInfo::rotatesWindowContents() const
229 return m_rotatesWindowContents;
232void ApplicationInfo::setRotatesWindowContents(bool value)
234 m_rotatesWindowContents = value;
237ApplicationInfo::RequestedState ApplicationInfo::requestedState() const
239 return m_requestedState;
242void ApplicationInfo::setRequestedState(RequestedState value)
244 if (m_requestedState != value) {
245 m_requestedState = value;
246 Q_EMIT requestedStateChanged(m_requestedState);
248 if (m_requestedState == RequestedRunning && m_state == Suspended) {
249 setState(Running);
250 } else if (m_requestedState == RequestedSuspended && m_state == Running) {
251 setState(Suspended);
252 }
253 }
256bool ApplicationInfo::isTouchApp() const
258 return m_isTouchApp;
261void ApplicationInfo::setIsTouchApp(bool isTouchApp)
263 m_isTouchApp = isTouchApp;
266void ApplicationInfo::onSessionSurfaceAdded(MirSurface* surface)
268 if (surface != nullptr && m_state == Starting) {
269 if (m_requestedState == RequestedRunning) {
270 setState(Running);
271 } else {
272 setState(Suspended);
273 }
274 surface->setShellChrome(m_shellChrome);
275 }
278bool ApplicationInfo::exemptFromLifecycle() const
280 return m_exemptFromLifecycle;
283void ApplicationInfo::setExemptFromLifecycle(bool exemptFromLifecycle)
285 if (m_exemptFromLifecycle != exemptFromLifecycle)
286 {
287 m_exemptFromLifecycle = exemptFromLifecycle;
288 Q_EMIT exemptFromLifecycleChanged(m_exemptFromLifecycle);
289 }
292QSize ApplicationInfo::initialSurfaceSize() const
294 return m_initialSurfaceSize;
297void ApplicationInfo::setInitialSurfaceSize(const QSize &size)
299 if (size != m_initialSurfaceSize) {
300 m_initialSurfaceSize = size;
301 Q_EMIT initialSurfaceSizeChanged(m_initialSurfaceSize);
302 }
305void ApplicationInfo::setShellChrome(Mir::ShellChrome shellChrome)
307 m_shellChrome = shellChrome;
308 if (m_session && m_session->lastSurface()) {
309 m_session->lastSurface()->setShellChrome(shellChrome);
310 }