on cef9b7b help sign in
unity8/tests/mocks/Unity/Application/ApplicationInfo.cpp
249 lines 6.7 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 "Session.h"
19#include "SessionManager.h"
21#include <paths.h>
23#include <QGuiApplication>
24#include <QQuickItem>
25#include <QQuickView>
26#include <QQmlComponent>
27#include <QTimer>
29ApplicationInfo::ApplicationInfo(const QString &appId, QObject *parent)
30 : ApplicationInfoInterface(appId, parent)
31 , m_appId(appId)
32 , m_stage(MainStage)
33 , m_state(Stopped)
34 , m_focused(false)
35 , m_fullscreen(false)
36 , m_session(0)
37 , m_supportedOrientations(Qt::PortraitOrientation |
38 Qt::LandscapeOrientation |
39 Qt::InvertedPortraitOrientation |
40 Qt::InvertedLandscapeOrientation)
41 , m_rotatesWindowContents(false)
42 , m_requestedState(RequestedRunning)
43 , m_manualSurfaceCreation(false)
47ApplicationInfo::ApplicationInfo(QObject *parent)
48 : ApplicationInfoInterface(QString(), parent)
49 , m_stage(MainStage)
50 , m_state(Stopped)
51 , m_focused(false)
52 , m_fullscreen(false)
53 , m_session(0)
54 , m_supportedOrientations(Qt::PortraitOrientation |
55 Qt::LandscapeOrientation |
56 Qt::InvertedPortraitOrientation |
57 Qt::InvertedLandscapeOrientation)
58 , m_rotatesWindowContents(false)
59 , m_requestedState(RequestedRunning)
60 , m_manualSurfaceCreation(false)
64ApplicationInfo::~ApplicationInfo()
66 delete m_session;
69void ApplicationInfo::createSession()
71 if (m_session || state() == ApplicationInfo::Stopped) { return; }
73 QUrl screenshotUrl = QString("file://%1").arg(m_screenshotFileName);
74 setSession(SessionManager::singleton()->createSession(appId(), screenshotUrl));
77void ApplicationInfo::setSession(Session* session)
79 if (m_session == session)
80 return;
82 if (m_session) {
83 disconnect(this, 0, m_session, 0);
84 m_session->setApplication(nullptr);
85 m_session->setParent(nullptr);
86 Q_EMIT m_session->deregister();
87 }
89 m_session = session;
91 if (m_session) {
92 m_session->setApplication(this);
93 m_session->setParent(this);
94 SessionManager::singleton()->registerSession(m_session);
95 connect(m_session, &Session::surfaceChanged,
96 this, &ApplicationInfo::onSessionSurfaceChanged);
98 if (!m_manualSurfaceCreation) {
99 QTimer::singleShot(500, m_session, SLOT(createSurface()));
100 }
101 }
103 Q_EMIT sessionChanged(m_session);
106void ApplicationInfo::setIconId(const QString &iconId)
108 setIcon(QString("file://%1/graphics/applicationIcons/%2@18.png")
109 .arg(qmlDirectory())
110 .arg(iconId));
113void ApplicationInfo::setScreenshotId(const QString &screenshotId)
115 QString screenshotFileName;
117 if (screenshotId.endsWith(".svg")) {
118 screenshotFileName = QString("%1/Dash/graphics/phone/screenshots/%2")
119 .arg(qmlDirectory())
120 .arg(screenshotId);
121 } else {
122 screenshotFileName = QString("%1/Dash/graphics/phone/screenshots/%2@12.png")
123 .arg(qmlDirectory())
124 .arg(screenshotId);
125 }
127 if (screenshotFileName != m_screenshotFileName) {
128 m_screenshotFileName = screenshotFileName;
130 QUrl screenshotUrl = QString("file://%1").arg(m_screenshotFileName);
131 if (m_session) {
132 m_session->setScreenshot(screenshotUrl);
133 }
134 }
137void ApplicationInfo::setName(const QString &value)
139 if (value != m_name) {
140 m_name = value;
141 Q_EMIT nameChanged(value);
142 }
145void ApplicationInfo::setIcon(const QUrl &value)
147 if (value != m_icon) {
148 m_icon = value;
149 Q_EMIT iconChanged(value);
150 }
153void ApplicationInfo::setStage(Stage value)
155 if (value != m_stage) {
156 m_stage = value;
157 Q_EMIT stageChanged(value);
158 }
161void ApplicationInfo::setState(State value)
163 if (value != m_state) {
164 m_state = value;
165 Q_EMIT stateChanged(value);
167 if (!m_manualSurfaceCreation && !m_session && m_state == ApplicationInfo::Starting) {
168 QTimer::singleShot(500, this, SLOT(createSession()));
169 } else if (m_state == ApplicationInfo::Stopped) {
170 Session *session = m_session;
171 setSession(nullptr);
172 delete session;
173 }
174 }
177void ApplicationInfo::setFocused(bool value)
179 if (value != m_focused) {
180 m_focused = value;
181 Q_EMIT focusedChanged(value);
182 }
185void ApplicationInfo::setFullscreen(bool value)
187 if (value != m_fullscreen) {
188 m_fullscreen = value;
189 Q_EMIT fullscreenChanged(value);
190 }
193void ApplicationInfo::setManualSurfaceCreation(bool value)
195 if (value != m_manualSurfaceCreation) {
196 m_manualSurfaceCreation = value;
197 Q_EMIT manualSurfaceCreationChanged(value);
198 }
201Qt::ScreenOrientations ApplicationInfo::supportedOrientations() const
203 return m_supportedOrientations;
206void ApplicationInfo::setSupportedOrientations(Qt::ScreenOrientations orientations)
208 m_supportedOrientations = orientations;
211bool ApplicationInfo::rotatesWindowContents() const
213 return m_rotatesWindowContents;
216void ApplicationInfo::setRotatesWindowContents(bool value)
218 m_rotatesWindowContents = value;
221ApplicationInfo::RequestedState ApplicationInfo::requestedState() const
223 return m_requestedState;
226void ApplicationInfo::setRequestedState(RequestedState value)
228 if (m_requestedState != value) {
229 m_requestedState = value;
230 Q_EMIT requestedStateChanged(m_requestedState);
232 if (m_requestedState == RequestedRunning && m_state == Suspended) {
233 setState(Running);
234 } else if (m_requestedState == RequestedSuspended && m_state == Running) {
235 setState(Suspended);
236 }
237 }
240void ApplicationInfo::onSessionSurfaceChanged(MirSurfaceItem* surface)
242 if (surface != nullptr && m_state == Starting) {
243 if (m_requestedState == RequestedRunning) {
244 setState(Running);
245 } else {
246 setState(Suspended);
247 }
248 }