on 3caee59 help sign in
209 lines 5.4 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 "SessionManager.h"
20#include "SurfaceManager.h"
22#include <QPainter>
23#include <QQmlEngine>
24#include <QTimer>
26Session::Session(const QString &name,
27 const QUrl& screenshot,
28 QObject *parent)
29 : QObject(parent)
30 , m_name(name)
31 , m_live(true)
32 , m_screenshot(screenshot)
33 , m_application(nullptr)
34 , m_surface(nullptr)
35 , m_parentSession(nullptr)
36 , m_children(new SessionModel(this))
37 , m_fullscreen(false)
39// qDebug() << "Session::Session() " << this->name();
41 QQmlEngine::setObjectOwnership(this, QQmlEngine::CppOwnership);
44Session::~Session()
46// qDebug() << "Session::~Session() " << name();
48 QList<Session*> children(m_children->list());
49 for (Session* child : children) {
50 delete child;
51 }
52 if (m_parentSession) {
53 m_parentSession->removeChildSession(this);
54 }
55 if (m_application) {
56 m_application->setSession(nullptr);
57 }
58 delete m_children;
60 if (m_surface) {
61 if (m_surface->viewCount() == 0) {
62 delete m_surface;
63 } else {
64 m_surface->setLive(false);
65 }
66 }
69void Session::release()
71// qDebug() << "Session::release " << name();
72 deleteLater();
75void Session::updateFullscreenProperty()
77 if (m_surfaces.rowCount() > 0) {
78 // TODO: Figure out something better
79 setFullscreen(lastSurface()->state() == Mir::FullscreenState);
80 } else {
81 // Keep the current value of the fullscreen property until we get a new
82 // surface
83 }
86void Session::setFullscreen(bool fullscreen)
88 if (m_fullscreen != fullscreen) {
89 m_fullscreen = fullscreen;
90 Q_EMIT fullscreenChanged(m_fullscreen);
91 }
94void Session::setApplication(ApplicationInfo* application)
96 if (m_application == application)
97 return;
99 m_application = application;
100 Q_EMIT applicationChanged(application);
103void Session::appendSurface(MirSurface* surface)
105 qDebug() << "Session::appendSurface - session=" << name() << "surface=" << surface;
107 m_surfaces.insert(m_surfaces.rowCount(), surface);
109 connect(surface, &MirSurfaceInterface::stateChanged, this, &Session::updateFullscreenProperty);
110 connect(surface, &QObject::destroyed,
111 this, [this, surface]() { this->removeSurface(surface); });
113 Q_EMIT lastSurfaceChanged(surface);
114 Q_EMIT surfaceAdded(surface);
116 updateFullscreenProperty();
119void Session::removeSurface(MirSurface* surface)
121 disconnect(surface, nullptr, this, nullptr);
122 if (m_surfaces.contains(surface)) {
123 m_surfaces.remove(surface);
124 }
126 updateFullscreenProperty();
129void Session::setScreenshot(const QUrl& screenshot)
131 if (screenshot != m_screenshot) {
132 m_screenshot = screenshot;
133 if (m_surface) {
134 m_surface->setScreenshotUrl(m_screenshot);
135 }
136 }
139void Session::setLive(bool live)
141 if (m_live != live) {
142 m_live = live;
143 Q_EMIT liveChanged(m_live);
144 }
147void Session::setParentSession(Session* session)
149 if (m_parentSession == session || session == this)
150 return;
152 m_parentSession = session;
153 Q_EMIT parentSessionChanged(session);
156void Session::createSurface()
158 if (m_surface) return;
160 appendSurface(SurfaceManager::singleton()->createSurface(name(),
161 Mir::NormalType,
162 m_application && m_application->fullscreen() ? Mir::FullscreenState :
163 Mir::MaximizedState,
164 m_screenshot));
167void Session::addChildSession(Session* session)
169 insertChildSession(m_children->rowCount(), session);
172void Session::insertChildSession(uint index, Session* session)
174 qDebug() << "Session::insertChildSession - " << session->name() << " to " << name() << " @ " << index;
176 session->setParentSession(this);
177 m_children->insert(index, session);
178 SessionManager::singleton()->registerSession(session);
181void Session::removeChildSession(Session* session)
183 qDebug() << "Session::removeChildSession - " << session->name() << " from " << name();
185 if (m_children->contains(session)) {
186 m_children->remove(session);
187 session->setParentSession(nullptr);
188 Q_EMIT session->deregister();
189 }
192SessionModel* Session::childSessions() const
194 return m_children;
197ObjectListModel<MirSurface>* Session::surfaces() const
199 return const_cast<ObjectListModel<MirSurface>*>(&m_surfaces);
202MirSurface *Session::lastSurface() const
204 if (m_surfaces.rowCount() > 0) {
205 return m_surfaces.list().last();
206 } else {
207 return nullptr;
208 }