on 73bdb21 help sign in
unity8/tests/mocks/Unity/Application/ApplicationInfo.cpp
280 lines 7.2 kB history raw
1/*
2 * Copyright (C) 2013-2015 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)
50ApplicationInfo::ApplicationInfo(QObject *parent)
51 : ApplicationInfoInterface(QString(), parent)
52 , m_stage(MainStage)
53 , m_state(Stopped)
54 , m_focused(false)
55 , m_fullscreen(false)
56 , m_session(0)
57 , m_supportedOrientations(Qt::PortraitOrientation |
58 Qt::LandscapeOrientation |
59 Qt::InvertedPortraitOrientation |
60 Qt::InvertedLandscapeOrientation)
61 , m_rotatesWindowContents(false)
62 , m_requestedState(RequestedRunning)
63 , m_isTouchApp(true)
64 , m_exemptFromLifecycle(false)
65 , m_manualSurfaceCreation(false)
69ApplicationInfo::~ApplicationInfo()
71 delete m_session;
74void ApplicationInfo::createSession()
76 if (m_session || state() == ApplicationInfo::Stopped) { return; }
78 setSession(SessionManager::singleton()->createSession(appId(), m_screenshotFileName));
81void ApplicationInfo::destroySession()
83 Session *session = this->session();
84 setSession(nullptr);
85 delete session;
88void ApplicationInfo::setSession(Session* session)
90 if (m_session == session)
91 return;
93 if (m_session) {
94 disconnect(this, 0, m_session, 0);
95 m_session->setApplication(nullptr);
96 m_session->setParent(nullptr);
97 Q_EMIT m_session->deregister();
98 }
100 m_session = session;
102 if (m_session) {
103 m_session->setApplication(this);
104 m_session->setParent(this);
105 SessionManager::singleton()->registerSession(m_session);
106 connect(m_session, &Session::surfaceAdded,
107 this, &ApplicationInfo::onSessionSurfaceAdded);
109 if (!m_manualSurfaceCreation) {
110 QTimer::singleShot(500, m_session, &Session::createSurface);
111 }
112 }
114 Q_EMIT sessionChanged(m_session);
117void ApplicationInfo::setIconId(const QString &iconId)
119 setIcon(QString("../../tests/graphics/applicationIcons/%2@18.png")
120 .arg(iconId));
123void ApplicationInfo::setScreenshotId(const QString &screenshotId)
125 QString screenshotFileName;
127 if (screenshotId.endsWith(".svg")) {
128 screenshotFileName = QString("qrc:///Unity/Application/screenshots/%2")
129 .arg(screenshotId);
130 } else {
131 screenshotFileName = QString("qrc:///Unity/Application/screenshots/%2@12.png")
132 .arg(screenshotId);
133 }
135 if (screenshotFileName != m_screenshotFileName) {
136 m_screenshotFileName = screenshotFileName;
138 if (m_session) {
139 m_session->setScreenshot(screenshotFileName);
140 }
141 }
144void ApplicationInfo::setName(const QString &value)
146 if (value != m_name) {
147 m_name = value;
148 Q_EMIT nameChanged(value);
149 }
152void ApplicationInfo::setIcon(const QUrl &value)
154 if (value != m_icon) {
155 m_icon = value;
156 Q_EMIT iconChanged(value);
157 }
160void ApplicationInfo::setStage(Stage value)
162 if (value != m_stage) {
163 m_stage = value;
164 Q_EMIT stageChanged(value);
165 }
168void ApplicationInfo::setState(State value)
170 if (value != m_state) {
171 m_state = value;
172 Q_EMIT stateChanged(value);
174 if (!m_manualSurfaceCreation && !m_session && m_state == ApplicationInfo::Starting) {
175 QTimer::singleShot(500, this, &ApplicationInfo::createSession);
176 } else if (m_state == ApplicationInfo::Stopped) {
177 Session *session = m_session;
178 setSession(nullptr);
179 delete session;
180 }
181 }
184void ApplicationInfo::setFocused(bool value)
186 if (value != m_focused) {
187 m_focused = value;
188 Q_EMIT focusedChanged(value);
189 }
192void ApplicationInfo::setFullscreen(bool value)
194 if (value != m_fullscreen) {
195 m_fullscreen = value;
196 Q_EMIT fullscreenChanged(value);
197 }
200void ApplicationInfo::setManualSurfaceCreation(bool value)
202 if (value != m_manualSurfaceCreation) {
203 m_manualSurfaceCreation = value;
204 Q_EMIT manualSurfaceCreationChanged(value);
205 }
208Qt::ScreenOrientations ApplicationInfo::supportedOrientations() const
210 return m_supportedOrientations;
213void ApplicationInfo::setSupportedOrientations(Qt::ScreenOrientations orientations)
215 m_supportedOrientations = orientations;
218bool ApplicationInfo::rotatesWindowContents() const
220 return m_rotatesWindowContents;
223void ApplicationInfo::setRotatesWindowContents(bool value)
225 m_rotatesWindowContents = value;
228ApplicationInfo::RequestedState ApplicationInfo::requestedState() const
230 return m_requestedState;
233void ApplicationInfo::setRequestedState(RequestedState value)
235 if (m_requestedState != value) {
236 m_requestedState = value;
237 Q_EMIT requestedStateChanged(m_requestedState);
239 if (m_requestedState == RequestedRunning && m_state == Suspended) {
240 setState(Running);
241 } else if (m_requestedState == RequestedSuspended && m_state == Running) {
242 setState(Suspended);
243 }
244 }
247bool ApplicationInfo::isTouchApp() const
249 return m_isTouchApp;
252void ApplicationInfo::setIsTouchApp(bool isTouchApp)
254 m_isTouchApp = isTouchApp;
257void ApplicationInfo::onSessionSurfaceAdded(MirSurface* surface)
259 if (surface != nullptr && m_state == Starting) {
260 if (m_requestedState == RequestedRunning) {
261 setState(Running);
262 } else {
263 setState(Suspended);
264 }
265 }
268bool ApplicationInfo::exemptFromLifecycle() const
270 return m_exemptFromLifecycle;
273void ApplicationInfo::setExemptFromLifecycle(bool exemptFromLifecycle)
275 if (m_exemptFromLifecycle != exemptFromLifecycle)
276 {
277 m_exemptFromLifecycle = exemptFromLifecycle;
278 Q_EMIT exemptFromLifecycleChanged(m_exemptFromLifecycle);
279 }