on b83f384 help sign in
unity8/tests/mocks/Unity/Application/ApplicationInfo.cpp
282 lines 7.3 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_canSuspend(true)
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_canSuspend(true)
65 , m_manualSurfaceCreation(false)
69ApplicationInfo::~ApplicationInfo()
71 delete m_session;
74void ApplicationInfo::createSession()
76 if (m_session || state() == ApplicationInfo::Stopped) { return; }
78 QUrl screenshotUrl = QString("file://%1").arg(m_screenshotFileName);
79 setSession(SessionManager::singleton()->createSession(appId(), screenshotUrl));
82void ApplicationInfo::destroySession()
84 Session *session = this->session();
85 setSession(nullptr);
86 delete session;
89void ApplicationInfo::setSession(Session* session)
91 if (m_session == session)
92 return;
94 if (m_session) {
95 disconnect(this, 0, m_session, 0);
96 m_session->setApplication(nullptr);
97 m_session->setParent(nullptr);
98 Q_EMIT m_session->deregister();
99 }
101 m_session = session;
103 if (m_session) {
104 m_session->setApplication(this);
105 m_session->setParent(this);
106 SessionManager::singleton()->registerSession(m_session);
107 connect(m_session, &Session::surfaceChanged,
108 this, &ApplicationInfo::onSessionSurfaceChanged);
110 if (!m_manualSurfaceCreation) {
111 QTimer::singleShot(500, m_session, &Session::createSurface);
112 }
113 }
115 Q_EMIT sessionChanged(m_session);
118void ApplicationInfo::setIconId(const QString &iconId)
120 setIcon(QString("file://%1/graphics/applicationIcons/%2@18.png")
121 .arg(qmlDirectory())
122 .arg(iconId));
125void ApplicationInfo::setScreenshotId(const QString &screenshotId)
127 QString screenshotFileName;
129 if (screenshotId.endsWith(".svg")) {
130 screenshotFileName = QString("%1/Dash/graphics/phone/screenshots/%2")
131 .arg(qmlDirectory())
132 .arg(screenshotId);
133 } else {
134 screenshotFileName = QString("%1/Dash/graphics/phone/screenshots/%2@12.png")
135 .arg(qmlDirectory())
136 .arg(screenshotId);
137 }
139 if (screenshotFileName != m_screenshotFileName) {
140 m_screenshotFileName = screenshotFileName;
142 QUrl screenshotUrl = QString("file://%1").arg(m_screenshotFileName);
143 if (m_session) {
144 m_session->setScreenshot(screenshotUrl);
145 }
146 }
149void ApplicationInfo::setName(const QString &value)
151 if (value != m_name) {
152 m_name = value;
153 Q_EMIT nameChanged(value);
154 }
157void ApplicationInfo::setIcon(const QUrl &value)
159 if (value != m_icon) {
160 m_icon = value;
161 Q_EMIT iconChanged(value);
162 }
165void ApplicationInfo::setStage(Stage value)
167 if (value != m_stage) {
168 m_stage = value;
169 Q_EMIT stageChanged(value);
170 }
173void ApplicationInfo::setState(State value)
175 if (value != m_state) {
176 m_state = value;
177 Q_EMIT stateChanged(value);
179 if (!m_manualSurfaceCreation && !m_session && m_state == ApplicationInfo::Starting) {
180 QTimer::singleShot(500, this, &ApplicationInfo::createSession);
181 } else if (m_state == ApplicationInfo::Stopped) {
182 Session *session = m_session;
183 setSession(nullptr);
184 delete session;
185 }
186 }
189void ApplicationInfo::setFocused(bool value)
191 if (value != m_focused) {
192 m_focused = value;
193 Q_EMIT focusedChanged(value);
194 }
197void ApplicationInfo::setFullscreen(bool value)
199 if (value != m_fullscreen) {
200 m_fullscreen = value;
201 Q_EMIT fullscreenChanged(value);
202 }
205void ApplicationInfo::setManualSurfaceCreation(bool value)
207 if (value != m_manualSurfaceCreation) {
208 m_manualSurfaceCreation = value;
209 Q_EMIT manualSurfaceCreationChanged(value);
210 }
213Qt::ScreenOrientations ApplicationInfo::supportedOrientations() const
215 return m_supportedOrientations;
218void ApplicationInfo::setSupportedOrientations(Qt::ScreenOrientations orientations)
220 m_supportedOrientations = orientations;
223bool ApplicationInfo::rotatesWindowContents() const
225 return m_rotatesWindowContents;
228void ApplicationInfo::setRotatesWindowContents(bool value)
230 m_rotatesWindowContents = value;
233ApplicationInfo::RequestedState ApplicationInfo::requestedState() const
235 return m_requestedState;
238void ApplicationInfo::setRequestedState(RequestedState value)
240 if (m_requestedState != value) {
241 m_requestedState = value;
242 Q_EMIT requestedStateChanged(m_requestedState);
244 if (m_requestedState == RequestedRunning && m_state == Suspended) {
245 setState(Running);
246 } else if (m_requestedState == RequestedSuspended && m_state == Running) {
247 setState(Suspended);
248 }
249 }
252bool ApplicationInfo::isTouchApp() const
254 return m_isTouchApp;
257void ApplicationInfo::setIsTouchApp(bool isTouchApp)
259 m_isTouchApp = isTouchApp;
262bool ApplicationInfo::canSuspend() const
264 return m_canSuspend;
267void ApplicationInfo::setCanSuspend(bool canSuspend)
269 m_canSuspend = canSuspend;
270 Q_EMIT canSuspendChanged(canSuspend);
273void ApplicationInfo::onSessionSurfaceChanged(MirSurface* surface)
275 if (surface != nullptr && m_state == Starting) {
276 if (m_requestedState == RequestedRunning) {
277 setState(Running);
278 } else {
279 setState(Suspended);
280 }
281 }