File indexing completed on 2024-04-28 05:50:48

0001 /*
0002     SPDX-FileCopyrightText: 2007-2008 Robert Knight <robertknight@gmail.com>
0003     SPDX-FileCopyrightText: 1997, 1998 Lars Doelle <lars.doelle@on-line.de>
0004     SPDX-FileCopyrightText: 2009 Thomas Dreibholz <dreibh@iem.uni-due.de>
0005 
0006     SPDX-License-Identifier: GPL-2.0-or-later
0007 */
0008 
0009 // Konsole
0010 // TODO: Own header should be first, but this breaks compiling.
0011 #include "SessionGroup.h"
0012 #include "Emulation.h"
0013 #include "Session.h"
0014 
0015 namespace Konsole
0016 {
0017 SessionGroup::SessionGroup(QObject *parent)
0018     : QObject(parent)
0019 {
0020 }
0021 
0022 SessionGroup::~SessionGroup() = default;
0023 
0024 QList<Session *> SessionGroup::sessions() const
0025 {
0026     return _sessions.keys();
0027 }
0028 
0029 void SessionGroup::addSession(Session *session)
0030 {
0031     connect(session, &Konsole::Session::finished, this, &Konsole::SessionGroup::sessionFinished);
0032     _sessions.insert(session, false);
0033 }
0034 
0035 void SessionGroup::removeSession(Session *session)
0036 {
0037     disconnect(session, &Konsole::Session::finished, this, &Konsole::SessionGroup::sessionFinished);
0038     setMasterStatus(session, false);
0039     _sessions.remove(session);
0040 }
0041 
0042 void SessionGroup::sessionFinished(Session *session)
0043 {
0044     Q_ASSERT(session);
0045     removeSession(session);
0046 }
0047 
0048 void SessionGroup::setMasterMode(int mode)
0049 {
0050     _masterMode = mode;
0051 }
0052 
0053 void SessionGroup::setMasterStatus(Session *session, bool master)
0054 {
0055     const bool wasMaster = _sessions[session];
0056 
0057     if (wasMaster == master) {
0058         // No status change -> nothing to do.
0059         return;
0060     }
0061     _sessions[session] = master;
0062 
0063     if (master) {
0064         connect(session->emulation(), &Konsole::Emulation::sendData, this, &Konsole::SessionGroup::forwardData);
0065     } else {
0066         disconnect(session->emulation(), &Konsole::Emulation::sendData, this, &Konsole::SessionGroup::forwardData);
0067     }
0068 }
0069 
0070 void SessionGroup::forwardData(const QByteArray &data)
0071 {
0072     static bool _inForwardData = false;
0073     if (_inForwardData) { // Avoid recursive calls among session groups!
0074         // A recursive call happens when a master in group A calls forwardData()
0075         // in group B. If one of the destination sessions in group B is also a
0076         // master of a group including the master session of group A, this would
0077         // again call forwardData() in group A, and so on.
0078         return;
0079     }
0080 
0081     _inForwardData = true;
0082     const QList<Session *> sessionsKeys = _sessions.keys();
0083     for (Session *other : sessionsKeys) {
0084         if (!_sessions[other]) {
0085             other->emulation()->sendString(data);
0086         }
0087     }
0088     _inForwardData = false;
0089 }
0090 
0091 } // namespace konsole
0092 
0093 #include "moc_SessionGroup.cpp"