on 3ebd113 help sign in
unity8/tests/mocks/Unity/Application/ApplicationInfo.cpp
257 lines 6.8 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_manualSurfaceCreation(false)
48ApplicationInfo::ApplicationInfo(QObject *parent)
49 : ApplicationInfoInterface(QString(), parent)
50 , m_stage(MainStage)
51 , m_state(Stopped)
52 , m_focused(false)
53 , m_fullscreen(false)
54 , m_session(0)
55 , m_supportedOrientations(Qt::PortraitOrientation |
56 Qt::LandscapeOrientation |
57 Qt::InvertedPortraitOrientation |
58 Qt::InvertedLandscapeOrientation)
59 , m_rotatesWindowContents(false)
60 , m_requestedState(RequestedRunning)
61 , m_manualSurfaceCreation(false)
65ApplicationInfo::~ApplicationInfo()
67 delete m_session;
70void ApplicationInfo::createSession()
72 if (m_session || state() == ApplicationInfo::Stopped) { return; }
74 QUrl screenshotUrl = QString("file://%1").arg(m_screenshotFileName);
75 setSession(SessionManager::singleton()->createSession(appId(), screenshotUrl));
78void ApplicationInfo::destroySession()
80 Session *session = this->session();
81 setSession(nullptr);
82 delete session;
85void ApplicationInfo::setSession(Session* session)
87 if (m_session == session)
88 return;
90 if (m_session) {
91 disconnect(this, 0, m_session, 0);
92 m_session->setApplication(nullptr);
93 m_session->setParent(nullptr);
94 Q_EMIT m_session->deregister();
95 }
97 m_session = session;
99 if (m_session) {
100 m_session->setApplication(this);
101 m_session->setParent(this);
102 SessionManager::singleton()->registerSession(m_session);
103 connect(m_session, &Session::surfaceChanged,
104 this, &ApplicationInfo::onSessionSurfaceChanged);
106 if (!m_manualSurfaceCreation) {
107 QTimer::singleShot(500, m_session, &Session::createSurface);
108 }
109 }
111 Q_EMIT sessionChanged(m_session);
114void ApplicationInfo::setIconId(const QString &iconId)
116 setIcon(QString("file://%1/graphics/applicationIcons/%2@18.png")
117 .arg(qmlDirectory())
118 .arg(iconId));
121void ApplicationInfo::setScreenshotId(const QString &screenshotId)
123 QString screenshotFileName;
125 if (screenshotId.endsWith(".svg")) {
126 screenshotFileName = QString("%1/Dash/graphics/phone/screenshots/%2")
127 .arg(qmlDirectory())
128 .arg(screenshotId);
129 } else {
130 screenshotFileName = QString("%1/Dash/graphics/phone/screenshots/%2@12.png")
131 .arg(qmlDirectory())
132 .arg(screenshotId);
133 }
135 if (screenshotFileName != m_screenshotFileName) {
136 m_screenshotFileName = screenshotFileName;
138 QUrl screenshotUrl = QString("file://%1").arg(m_screenshotFileName);
139 if (m_session) {
140 m_session->setScreenshot(screenshotUrl);
141 }
142 }
145void ApplicationInfo::setName(const QString &value)
147 if (value != m_name) {
148 m_name = value;
149 Q_EMIT nameChanged(value);
150 }
153void ApplicationInfo::setIcon(const QUrl &value)
155 if (value != m_icon) {
156 m_icon = value;
157 Q_EMIT iconChanged(value);
158 }
161void ApplicationInfo::setStage(Stage value)
163 if (value != m_stage) {
164 m_stage = value;
165 Q_EMIT stageChanged(value);
166 }
169void ApplicationInfo::setState(State value)
171 if (value != m_state) {
172 m_state = value;
173 Q_EMIT stateChanged(value);
175 if (!m_manualSurfaceCreation && !m_session && m_state == ApplicationInfo::Starting) {
176 QTimer::singleShot(500, this, &ApplicationInfo::createSession);
177 } else if (m_state == ApplicationInfo::Stopped) {
178 Session *session = m_session;
179 setSession(nullptr);
180 delete session;
181 }
182 }
185void ApplicationInfo::setFocused(bool value)
187 if (value != m_focused) {
188 m_focused = value;
189 Q_EMIT focusedChanged(value);
190 }
193void ApplicationInfo::setFullscreen(bool value)
195 if (value != m_fullscreen) {
196 m_fullscreen = value;
197 Q_EMIT fullscreenChanged(value);
198 }
201void ApplicationInfo::setManualSurfaceCreation(bool value)
203 if (value != m_manualSurfaceCreation) {
204 m_manualSurfaceCreation = value;
205 Q_EMIT manualSurfaceCreationChanged(value);
206 }
209Qt::ScreenOrientations ApplicationInfo::supportedOrientations() const
211 return m_supportedOrientations;
214void ApplicationInfo::setSupportedOrientations(Qt::ScreenOrientations orientations)
216 m_supportedOrientations = orientations;
219bool ApplicationInfo::rotatesWindowContents() const
221 return m_rotatesWindowContents;
224void ApplicationInfo::setRotatesWindowContents(bool value)
226 m_rotatesWindowContents = value;
229ApplicationInfo::RequestedState ApplicationInfo::requestedState() const
231 return m_requestedState;
234void ApplicationInfo::setRequestedState(RequestedState value)
236 if (m_requestedState != value) {
237 m_requestedState = value;
238 Q_EMIT requestedStateChanged(m_requestedState);
240 if (m_requestedState == RequestedRunning && m_state == Suspended) {
241 setState(Running);
242 } else if (m_requestedState == RequestedSuspended && m_state == Running) {
243 setState(Suspended);
244 }
245 }
248void ApplicationInfo::onSessionSurfaceChanged(MirSurface* surface)
250 if (surface != nullptr && m_state == Starting) {
251 if (m_requestedState == RequestedRunning) {
252 setState(Running);
253 } else {
254 setState(Suspended);
255 }
256 }