on 3ecd374 help sign in
183 lines 4.8 kB history raw
1/*
2 * Copyright (C) 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 "Session.h"
18#include "ApplicationInfo.h"
19#include "SurfaceManager.h"
21#include <QPainter>
22#include <QQmlEngine>
23#include <QTimer>
25Session::Session(const QString &name,
26 const QUrl& screenshot,
27 QObject *parent)
28 : QObject(parent)
29 , m_name(name)
30 , m_screenshot(screenshot)
31 , m_application(nullptr)
32 , m_surface(nullptr)
33 , m_parentSession(nullptr)
34 , m_children(new SessionModel(this))
35 , m_manualSurfaceCreation(false)
37 qDebug() << "Session::Session() " << this->name();
39 QQmlEngine::setObjectOwnership(this, QQmlEngine::CppOwnership);
42Session::~Session()
44 qDebug() << "Session::~Session() " << name();
46 QList<Session*> children(m_children->list());
47 for (Session* child : children) {
48 delete child;
49 }
50 if (m_parentSession) {
51 m_parentSession->removeChildSession(this);
52 }
53 if (m_application) {
54 m_application->setSession(nullptr);
55 }
56 delete m_surface;
57 delete m_children;
60void Session::release()
62 qDebug() << "Session::release " << name();
63 Q_EMIT aboutToBeDestroyed();
65 if (m_parentSession) {
66 m_parentSession->removeChildSession(this);
67 }
68 if (m_application) {
69 m_application->setSession(nullptr);
70 }
71 if (!parent()) {
72 deleteLater();
73 }
76void Session::setApplication(ApplicationInfo* application)
78 if (m_application == application)
79 return;
81 if (m_application) {
82 disconnect(m_application, 0, this, 0);
83 }
85 m_application = application;
87 if (m_application) {
88 connect(m_application, &ApplicationInfo::stateChanged, this, [this](ApplicationInfo::State state) {
89 if (!m_manualSurfaceCreation && state == ApplicationInfo::Running) {
90 QTimer::singleShot(500, this, SLOT(createSurface()));
91 }
92 });
93 if (!m_manualSurfaceCreation && m_application->state() == ApplicationInfo::Running) {
94 QTimer::singleShot(500, this, SLOT(createSurface()));
95 }
96 }
99void Session::setSurface(MirSurfaceItem* surface)
101 qDebug() << "Session::setSurface - session=" << name() << "surface=" << surface;
102 if (m_surface == surface)
103 return;
105 if (m_surface) {
106 m_surface->setSession(nullptr);
107 m_surface->setParent(nullptr);
108 }
110 m_surface = surface;
112 if (m_surface) {
113 m_surface->setSession(this);
114 m_surface->setParent(this);
115 }
117 Q_EMIT surfaceChanged(m_surface);
120void Session::setScreenshot(const QUrl& screenshot)
122 if (screenshot != m_screenshot) {
123 m_screenshot = screenshot;
124 if (m_surface) {
125 m_surface->setScreenshot(m_screenshot);
126 }
127 }
130void Session::setParentSession(Session* session)
132 if (m_parentSession == session || session == this)
133 return;
135 m_parentSession = session;
136 Q_EMIT parentSessionChanged(session);
139void Session::createSurface()
141 if (m_surface) return;
143 setSurface(SurfaceManager::singleton()->createSurface(name(),
144 MirSurfaceItem::Normal,
145 m_application && m_application->fullscreen() ? MirSurfaceItem::Fullscreen : MirSurfaceItem::Maximized,
146 m_screenshot));
149void Session::addChildSession(Session* session)
151 insertChildSession(m_children->rowCount(), session);
154void Session::insertChildSession(uint index, Session* session)
156 qDebug() << "Session::insertChildSession - " << session->name() << " to " << name() << " @ " << index;
158 session->setParentSession(this);
159 m_children->insert(index, session);
162void Session::removeChildSession(Session* session)
164 qDebug() << "Session::removeChildSession - " << session->name() << " from " << name();
166 if (m_children->contains(session)) {
167 m_children->remove(session);
168 session->setParentSession(nullptr);
169 }
172SessionModel* Session::childSessions() const
174 return m_children;
177void Session::setManualSurfaceCreation(bool value)
179 if (value != m_manualSurfaceCreation) {
180 m_manualSurfaceCreation = value;
181 Q_EMIT manualSurfaceCreationChanged(value);
182 }