on da2e032 help sign in
unity8/tests/mocks/Unity/Application/ApplicationInfo.cpp
223 lines 6.1 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 Q_EMIT SurfaceManager::singleton()->surfaceDestroyed(m_surface);
58 m_surface->deleteLater();
59 setSurface(nullptr);
60 }
63void ApplicationInfo::createSurface()
65 if (m_surface || state() == ApplicationInfo::Stopped) return;
67 QUrl screenshotUrl = QString("file://%1").arg(m_screenshotFileName);
69 setSurface(new MirSurfaceItem(name(),
70 MirSurfaceItem::Normal,
71 fullscreen() ? MirSurfaceItem::Fullscreen : MirSurfaceItem::Maximized,
72 screenshotUrl));
75void ApplicationInfo::setSurface(MirSurfaceItem* surface)
77 qDebug() << "Application::setSurface - appId=" << appId() << "surface=" << surface;
78 if (m_surface == surface)
79 return;
81 if (m_surface) {
82 m_surface->setApplication(nullptr);
83 m_surface->setParent(nullptr);
84 SurfaceManager::singleton()->unregisterSurface(m_surface);
85 }
87 m_surface = surface;
89 if (m_surface) {
90 m_surface->setApplication(this);
91 m_surface->setParent(this);
92 SurfaceManager::singleton()->registerSurface(m_surface);
93 }
95 Q_EMIT surfaceChanged(m_surface);
96 SurfaceManager::singleton()->registerSurface(m_surface);
99void ApplicationInfo::addPromptSurface(MirSurfaceItem* surface)
101 qDebug() << "ApplicationInfo::addPromptSurface " << surface->name() << " to " << name();
102 if (surface == m_surface || m_promptSurfaces.contains(surface)) return;
104 surface->setApplication(this);
105 m_promptSurfaces.append(surface);
106 Q_EMIT promptSurfacesChanged();
109void ApplicationInfo::removeSurface(MirSurfaceItem* surface)
111 if (m_surface == surface) {
112 setSurface(nullptr);
113 } else if (m_promptSurfaces.contains(surface)) {
114 qDebug() << "Application::removeSurface " << surface->name() << " from " << name();
116 m_promptSurfaces.removeAll(surface);
117 surface->setApplication(nullptr);
119 Q_EMIT promptSurfacesChanged();
120 }
123QList<MirSurfaceItem*> ApplicationInfo::promptSurfaceList() const
125 return m_promptSurfaces;
128QQmlListProperty<MirSurfaceItem> ApplicationInfo::promptSurfaces()
130 return QQmlListProperty<MirSurfaceItem>(this,
131 0,
132 ApplicationInfo::promptSurfaceCount,
133 ApplicationInfo::promptSurfaceAt);
136int ApplicationInfo::promptSurfaceCount(QQmlListProperty<MirSurfaceItem> *prop)
138 ApplicationInfo *p = qobject_cast<ApplicationInfo*>(prop->object);
139 return p->m_promptSurfaces.count();
142MirSurfaceItem* ApplicationInfo::promptSurfaceAt(QQmlListProperty<MirSurfaceItem> *prop, int index)
144 ApplicationInfo *p = qobject_cast<ApplicationInfo*>(prop->object);
146 if (index < 0 || index >= p->m_promptSurfaces.count())
147 return nullptr;
148 return p->m_promptSurfaces[index];
151void ApplicationInfo::setIconId(const QString &iconId)
153 setIcon(QString("file://%1/graphics/applicationIcons/%2@18.png")
154 .arg(qmlDirectory())
155 .arg(iconId));
158void ApplicationInfo::setScreenshotId(const QString &screenshotId)
160 m_screenshotFileName = QString("%1/Dash/graphics/phone/screenshots/%2@12.png")
161 .arg(qmlDirectory())
162 .arg(screenshotId);
165void ApplicationInfo::setName(const QString &value)
167 if (value != m_name) {
168 m_name = value;
169 Q_EMIT nameChanged(value);
170 }
173void ApplicationInfo::setIcon(const QUrl &value)
175 if (value != m_icon) {
176 m_icon = value;
177 Q_EMIT iconChanged(value);
178 }
181void ApplicationInfo::setStage(Stage value)
183 if (value != m_stage) {
184 m_stage = value;
185 Q_EMIT stageChanged(value);
186 }
189void ApplicationInfo::setState(State value)
191 if (value != m_state) {
192 m_state = value;
193 Q_EMIT stateChanged(value);
195 if (!m_manualSurfaceCreation && m_state == ApplicationInfo::Running) {
196 QTimer::singleShot(1000, this, SLOT(createSurface()));
197 }
198 }
201void ApplicationInfo::setFocused(bool value)
203 if (value != m_focused) {
204 m_focused = value;
205 Q_EMIT focusedChanged(value);
206 }
209void ApplicationInfo::setFullscreen(bool value)
211 if (value != m_fullscreen) {
212 m_fullscreen = value;
213 Q_EMIT fullscreenChanged(value);
214 }
217void ApplicationInfo::setManualSurfaceCreation(bool value)
219 if (value != m_manualSurfaceCreation) {
220 m_manualSurfaceCreation = value;
221 Q_EMIT manualSurfaceCreationChanged(value);
222 }