on e246ecd help sign in
unity8/tests/mocks/Unity/Application/ApplicationInfo.cpp
225 lines 6.2 kB history raw
1/*
2 * Copyright (C) 2013-2014 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 "MirSurfaceItem.h"
19#include "SurfaceManager.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(Starting)
34 , m_focused(false)
35 , m_fullscreen(false)
36 , m_surface(0)
37 , m_manualSurfaceCreation(false)
38 , m_parentItem(0)
42ApplicationInfo::ApplicationInfo(QObject *parent)
43 : ApplicationInfoInterface(QString(), parent)
44 , m_stage(MainStage)
45 , m_state(Starting)
46 , m_focused(false)
47 , m_fullscreen(false)
48 , m_surface(0)
49 , m_manualSurfaceCreation(false)
50 , m_parentItem(0)
54ApplicationInfo::~ApplicationInfo()
56 if (m_surface) {
57 // break the cyclic reference before destroying it
58 m_surface->setApplication(nullptr);
60 Q_EMIT SurfaceManager::singleton()->surfaceDestroyed(m_surface);
61 m_surface->deleteLater();
62 }
65void ApplicationInfo::createSurface()
67 if (m_surface || state() == ApplicationInfo::Stopped) return;
69 QUrl screenshotUrl = QString("file://%1").arg(m_screenshotFileName);
71 setSurface(new MirSurfaceItem(name(),
72 MirSurfaceItem::Normal,
73 fullscreen() ? MirSurfaceItem::Fullscreen : MirSurfaceItem::Maximized,
74 screenshotUrl));
77void ApplicationInfo::setSurface(MirSurfaceItem* surface)
79 qDebug() << "Application::setSurface - appId=" << appId() << "surface=" << surface;
80 if (m_surface == surface)
81 return;
83 if (m_surface) {
84 m_surface->setApplication(nullptr);
85 m_surface->setParent(nullptr);
86 SurfaceManager::singleton()->unregisterSurface(m_surface);
87 }
89 m_surface = surface;
91 if (m_surface) {
92 m_surface->setApplication(this);
93 m_surface->setParent(this);
94 SurfaceManager::singleton()->registerSurface(m_surface);
95 }
97 Q_EMIT surfaceChanged(m_surface);
98 SurfaceManager::singleton()->registerSurface(m_surface);
101void ApplicationInfo::addPromptSurface(MirSurfaceItem* surface)
103 qDebug() << "ApplicationInfo::addPromptSurface " << surface->name() << " to " << name();
104 if (surface == m_surface || m_promptSurfaces.contains(surface)) return;
106 surface->setApplication(this);
107 m_promptSurfaces.append(surface);
108 Q_EMIT promptSurfacesChanged();
111void ApplicationInfo::removeSurface(MirSurfaceItem* surface)
113 if (m_surface == surface) {
114 setSurface(nullptr);
115 } else if (m_promptSurfaces.contains(surface)) {
116 qDebug() << "Application::removeSurface " << surface->name() << " from " << name();
118 m_promptSurfaces.removeAll(surface);
119 surface->setApplication(nullptr);
121 Q_EMIT promptSurfacesChanged();
122 }
125QList<MirSurfaceItem*> ApplicationInfo::promptSurfaceList() const
127 return m_promptSurfaces;
130QQmlListProperty<MirSurfaceItem> ApplicationInfo::promptSurfaces()
132 return QQmlListProperty<MirSurfaceItem>(this,
133 0,
134 ApplicationInfo::promptSurfaceCount,
135 ApplicationInfo::promptSurfaceAt);
138int ApplicationInfo::promptSurfaceCount(QQmlListProperty<MirSurfaceItem> *prop)
140 ApplicationInfo *p = qobject_cast<ApplicationInfo*>(prop->object);
141 return p->m_promptSurfaces.count();
144MirSurfaceItem* ApplicationInfo::promptSurfaceAt(QQmlListProperty<MirSurfaceItem> *prop, int index)
146 ApplicationInfo *p = qobject_cast<ApplicationInfo*>(prop->object);
148 if (index < 0 || index >= p->m_promptSurfaces.count())
149 return nullptr;
150 return p->m_promptSurfaces[index];
153void ApplicationInfo::setIconId(const QString &iconId)
155 setIcon(QString("file://%1/graphics/applicationIcons/%2@18.png")
156 .arg(qmlDirectory())
157 .arg(iconId));
160void ApplicationInfo::setScreenshotId(const QString &screenshotId)
162 m_screenshotFileName = QString("%1/Dash/graphics/phone/screenshots/%2@12.png")
163 .arg(qmlDirectory())
164 .arg(screenshotId);
167void ApplicationInfo::setName(const QString &value)
169 if (value != m_name) {
170 m_name = value;
171 Q_EMIT nameChanged(value);
172 }
175void ApplicationInfo::setIcon(const QUrl &value)
177 if (value != m_icon) {
178 m_icon = value;
179 Q_EMIT iconChanged(value);
180 }
183void ApplicationInfo::setStage(Stage value)
185 if (value != m_stage) {
186 m_stage = value;
187 Q_EMIT stageChanged(value);
188 }
191void ApplicationInfo::setState(State value)
193 if (value != m_state) {
194 m_state = value;
195 Q_EMIT stateChanged(value);
197 if (!m_manualSurfaceCreation && m_state == ApplicationInfo::Running) {
198 QTimer::singleShot(1000, this, SLOT(createSurface()));
199 }
200 }
203void ApplicationInfo::setFocused(bool value)
205 if (value != m_focused) {
206 m_focused = value;
207 Q_EMIT focusedChanged(value);
208 }
211void ApplicationInfo::setFullscreen(bool value)
213 if (value != m_fullscreen) {
214 m_fullscreen = value;
215 Q_EMIT fullscreenChanged(value);
216 }
219void ApplicationInfo::setManualSurfaceCreation(bool value)
221 if (value != m_manualSurfaceCreation) {
222 m_manualSurfaceCreation = value;
223 Q_EMIT manualSurfaceCreationChanged(value);
224 }