on 40897d6 help sign in
unity8/tests/mocks/Unity/Application/ApplicationInfo.cpp
310 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(QUrl(testDataDir() + "/graphics/applicationIcons/" + iconId + "@18.png"));
126void ApplicationInfo::setScreenshotId(const QString &screenshotId)
128 QString screenshotFileName;
130 if (screenshotId.endsWith(".svg")) {
131 screenshotFileName = QString("qrc:///Unity/Application/screenshots/%2")
132 .arg(screenshotId);
133 } else {
134 screenshotFileName = QString("qrc:///Unity/Application/screenshots/%2@12.png")
135 .arg(screenshotId);
136 }
138 if (screenshotFileName != m_screenshotFileName) {
139 m_screenshotFileName = screenshotFileName;
141 if (m_session) {
142 m_session->setScreenshot(screenshotFileName);
143 }
144 }
147void ApplicationInfo::setName(const QString &value)
149 if (value != m_name) {
150 m_name = value;
151 Q_EMIT nameChanged(value);
152 }
155void ApplicationInfo::setIcon(const QUrl &value)
157 if (value != m_icon) {
158 m_icon = value;
159 Q_EMIT iconChanged(value);
160 }
163void ApplicationInfo::setStage(Stage value)
165 if (value != m_stage) {
166 m_stage = value;
167 Q_EMIT stageChanged(value);
168 }
171void ApplicationInfo::setState(State value)
173 if (value != m_state) {
174 m_state = value;
175 Q_EMIT stateChanged(value);
177 if (!m_manualSurfaceCreation && !m_session && m_state == ApplicationInfo::Starting) {
178 QTimer::singleShot(500, this, &ApplicationInfo::createSession);
179 } else if (m_state == ApplicationInfo::Stopped) {
180 Session *session = m_session;
181 setSession(nullptr);
182 delete session;
183 }
184 }
187void ApplicationInfo::setFocused(bool value)
189 if (value != m_focused) {
190 m_focused = value;
191 Q_EMIT focusedChanged(value);
192 }
195void ApplicationInfo::setFullscreen(bool value)
197 m_fullscreen = value;
198 if (m_session) {
199 m_session->setFullscreen(value);
200 }
203bool ApplicationInfo::fullscreen() const
205 return m_session ? m_session->fullscreen() : m_fullscreen;
208void ApplicationInfo::setManualSurfaceCreation(bool value)
210 if (value != m_manualSurfaceCreation) {
211 m_manualSurfaceCreation = value;
212 Q_EMIT manualSurfaceCreationChanged(value);
213 }
216Qt::ScreenOrientations ApplicationInfo::supportedOrientations() const
218 return m_supportedOrientations;
221void ApplicationInfo::setSupportedOrientations(Qt::ScreenOrientations orientations)
223 m_supportedOrientations = orientations;
226bool ApplicationInfo::rotatesWindowContents() const
228 return m_rotatesWindowContents;
231void ApplicationInfo::setRotatesWindowContents(bool value)
233 m_rotatesWindowContents = value;
236ApplicationInfo::RequestedState ApplicationInfo::requestedState() const
238 return m_requestedState;
241void ApplicationInfo::setRequestedState(RequestedState value)
243 if (m_requestedState != value) {
244 m_requestedState = value;
245 Q_EMIT requestedStateChanged(m_requestedState);
247 if (m_requestedState == RequestedRunning && m_state == Suspended) {
248 setState(Running);
249 } else if (m_requestedState == RequestedSuspended && m_state == Running) {
250 setState(Suspended);
251 }
252 }
255bool ApplicationInfo::isTouchApp() const
257 return m_isTouchApp;
260void ApplicationInfo::setIsTouchApp(bool isTouchApp)
262 m_isTouchApp = isTouchApp;
265void ApplicationInfo::onSessionSurfaceAdded(MirSurface* surface)
267 if (surface != nullptr && m_state == Starting) {
268 if (m_requestedState == RequestedRunning) {
269 setState(Running);
270 } else {
271 setState(Suspended);
272 }
273 surface->setShellChrome(m_shellChrome);
274 }
277bool ApplicationInfo::exemptFromLifecycle() const
279 return m_exemptFromLifecycle;
282void ApplicationInfo::setExemptFromLifecycle(bool exemptFromLifecycle)
284 if (m_exemptFromLifecycle != exemptFromLifecycle)
285 {
286 m_exemptFromLifecycle = exemptFromLifecycle;
287 Q_EMIT exemptFromLifecycleChanged(m_exemptFromLifecycle);
288 }
291QSize ApplicationInfo::initialSurfaceSize() const
293 return m_initialSurfaceSize;
296void ApplicationInfo::setInitialSurfaceSize(const QSize &size)
298 if (size != m_initialSurfaceSize) {
299 m_initialSurfaceSize = size;
300 Q_EMIT initialSurfaceSizeChanged(m_initialSurfaceSize);
301 }
304void ApplicationInfo::setShellChrome(Mir::ShellChrome shellChrome)
306 m_shellChrome = shellChrome;
307 if (m_session && m_session->lastSurface()) {
308 m_session->lastSurface()->setShellChrome(shellChrome);
309 }