on 00a2ef4 help sign in
unity8/tests/mocks/Unity/Application/ApplicationInfo.cpp
269 lines 7.0 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_manualSurfaceCreation(false)
49ApplicationInfo::ApplicationInfo(QObject *parent)
50 : ApplicationInfoInterface(QString(), parent)
51 , m_stage(MainStage)
52 , m_state(Stopped)
53 , m_focused(false)
54 , m_fullscreen(false)
55 , m_session(0)
56 , m_supportedOrientations(Qt::PortraitOrientation |
57 Qt::LandscapeOrientation |
58 Qt::InvertedPortraitOrientation |
59 Qt::InvertedLandscapeOrientation)
60 , m_rotatesWindowContents(false)
61 , m_requestedState(RequestedRunning)
62 , m_isTouchApp(true)
63 , m_manualSurfaceCreation(false)
67ApplicationInfo::~ApplicationInfo()
69 delete m_session;
72void ApplicationInfo::createSession()
74 if (m_session || state() == ApplicationInfo::Stopped) { return; }
76 QUrl screenshotUrl = QString("file://%1").arg(m_screenshotFileName);
77 setSession(SessionManager::singleton()->createSession(appId(), screenshotUrl));
80void ApplicationInfo::destroySession()
82 Session *session = this->session();
83 setSession(nullptr);
84 delete session;
87void ApplicationInfo::setSession(Session* session)
89 if (m_session == session)
90 return;
92 if (m_session) {
93 disconnect(this, 0, m_session, 0);
94 m_session->setApplication(nullptr);
95 m_session->setParent(nullptr);
96 Q_EMIT m_session->deregister();
97 }
99 m_session = session;
101 if (m_session) {
102 m_session->setApplication(this);
103 m_session->setParent(this);
104 SessionManager::singleton()->registerSession(m_session);
105 connect(m_session, &Session::surfaceChanged,
106 this, &ApplicationInfo::onSessionSurfaceChanged);
108 if (!m_manualSurfaceCreation) {
109 QTimer::singleShot(500, m_session, &Session::createSurface);
110 }
111 }
113 Q_EMIT sessionChanged(m_session);
116void ApplicationInfo::setIconId(const QString &iconId)
118 setIcon(QString("file://%1/graphics/applicationIcons/%2@18.png")
119 .arg(qmlDirectory())
120 .arg(iconId));
123void ApplicationInfo::setScreenshotId(const QString &screenshotId)
125 QString screenshotFileName;
127 if (screenshotId.endsWith(".svg")) {
128 screenshotFileName = QString("%1/Dash/graphics/phone/screenshots/%2")
129 .arg(qmlDirectory())
130 .arg(screenshotId);
131 } else {
132 screenshotFileName = QString("%1/Dash/graphics/phone/screenshots/%2@12.png")
133 .arg(qmlDirectory())
134 .arg(screenshotId);
135 }
137 if (screenshotFileName != m_screenshotFileName) {
138 m_screenshotFileName = screenshotFileName;
140 QUrl screenshotUrl = QString("file://%1").arg(m_screenshotFileName);
141 if (m_session) {
142 m_session->setScreenshot(screenshotUrl);
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 if (value != m_fullscreen) {
198 m_fullscreen = value;
199 Q_EMIT fullscreenChanged(value);
200 }
203void ApplicationInfo::setManualSurfaceCreation(bool value)
205 if (value != m_manualSurfaceCreation) {
206 m_manualSurfaceCreation = value;
207 Q_EMIT manualSurfaceCreationChanged(value);
208 }
211Qt::ScreenOrientations ApplicationInfo::supportedOrientations() const
213 return m_supportedOrientations;
216void ApplicationInfo::setSupportedOrientations(Qt::ScreenOrientations orientations)
218 m_supportedOrientations = orientations;
221bool ApplicationInfo::rotatesWindowContents() const
223 return m_rotatesWindowContents;
226void ApplicationInfo::setRotatesWindowContents(bool value)
228 m_rotatesWindowContents = value;
231ApplicationInfo::RequestedState ApplicationInfo::requestedState() const
233 return m_requestedState;
236void ApplicationInfo::setRequestedState(RequestedState value)
238 if (m_requestedState != value) {
239 m_requestedState = value;
240 Q_EMIT requestedStateChanged(m_requestedState);
242 if (m_requestedState == RequestedRunning && m_state == Suspended) {
243 setState(Running);
244 } else if (m_requestedState == RequestedSuspended && m_state == Running) {
245 setState(Suspended);
246 }
247 }
250bool ApplicationInfo::isTouchApp() const
252 return m_isTouchApp;
255void ApplicationInfo::setIsTouchApp(bool isTouchApp)
257 m_isTouchApp = isTouchApp;
260void ApplicationInfo::onSessionSurfaceChanged(MirSurface* surface)
262 if (surface != nullptr && m_state == Starting) {
263 if (m_requestedState == RequestedRunning) {
264 setState(Running);
265 } else {
266 setState(Suspended);
267 }
268 }