on 73bdb21 help sign in
184 lines 4.7 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))
38// qDebug() << "Session::Session() " << this->name();
40 QQmlEngine::setObjectOwnership(this, QQmlEngine::CppOwnership);
43Session::~Session()
45// qDebug() << "Session::~Session() " << name();
47 QList<Session*> children(m_children->list());
48 for (Session* child : children) {
49 delete child;
50 }
51 if (m_parentSession) {
52 m_parentSession->removeChildSession(this);
53 }
54 if (m_application) {
55 m_application->setSession(nullptr);
56 }
57 delete m_children;
59 if (m_surface) {
60 if (m_surface->viewCount() == 0) {
61 delete m_surface;
62 } else {
63 m_surface->setLive(false);
64 }
65 }
68void Session::release()
70// qDebug() << "Session::release " << name();
71 deleteLater();
74void Session::setApplication(ApplicationInfo* application)
76 if (m_application == application)
77 return;
79 m_application = application;
80 Q_EMIT applicationChanged(application);
83void Session::appendSurface(MirSurface* surface)
85 // qDebug() << "Session::appendSurface - session=" << name() << "surface=" << surface;
87 m_surfaces.insert(m_surfaces.rowCount(), surface);
89 connect(surface, &QObject::destroyed,
90 this, [this, surface]() { this->removeSurface(surface); });
92 Q_EMIT lastSurfaceChanged(surface);
93 Q_EMIT surfaceAdded(surface);
96void Session::removeSurface(MirSurface* surface)
98 disconnect(surface, nullptr, this, nullptr);
99 if (m_surfaces.contains(surface)) {
100 m_surfaces.remove(surface);
101 }
104void Session::setScreenshot(const QUrl& screenshot)
106 if (screenshot != m_screenshot) {
107 m_screenshot = screenshot;
108 if (m_surface) {
109 m_surface->setScreenshotUrl(m_screenshot);
110 }
111 }
114void Session::setLive(bool live)
116 if (m_live != live) {
117 m_live = live;
118 Q_EMIT liveChanged(m_live);
119 }
122void Session::setParentSession(Session* session)
124 if (m_parentSession == session || session == this)
125 return;
127 m_parentSession = session;
128 Q_EMIT parentSessionChanged(session);
131void Session::createSurface()
133 if (m_surface) return;
135 appendSurface(SurfaceManager::singleton()->createSurface(name(),
136 Mir::NormalType,
137 m_application && m_application->fullscreen() ? Mir::FullscreenState :
138 Mir::MaximizedState,
139 m_screenshot));
142void Session::addChildSession(Session* session)
144 insertChildSession(m_children->rowCount(), session);
147void Session::insertChildSession(uint index, Session* session)
149 qDebug() << "Session::insertChildSession - " << session->name() << " to " << name() << " @ " << index;
151 session->setParentSession(this);
152 m_children->insert(index, session);
153 SessionManager::singleton()->registerSession(session);
156void Session::removeChildSession(Session* session)
158 qDebug() << "Session::removeChildSession - " << session->name() << " from " << name();
160 if (m_children->contains(session)) {
161 m_children->remove(session);
162 session->setParentSession(nullptr);
163 Q_EMIT session->deregister();
164 }
167SessionModel* Session::childSessions() const
169 return m_children;
172ObjectListModel<MirSurface>* Session::surfaces() const
174 return const_cast<ObjectListModel<MirSurface>*>(&m_surfaces);
177MirSurface *Session::lastSurface() const
179 if (m_surfaces.rowCount() > 0) {
180 return m_surfaces.list().last();
181 } else {
182 return nullptr;
183 }