File indexing completed on 2024-04-21 05:51:12

0001 /*
0002     This source file is part of Konsole, a terminal emulator.
0003 
0004     SPDX-FileCopyrightText: 2006-2008 Robert Knight <robertknight@gmail.com>
0005 
0006     SPDX-License-Identifier: GPL-2.0-or-later
0007 */
0008 
0009 #ifndef SESSIONMANAGER_H
0010 #define SESSIONMANAGER_H
0011 
0012 // Qt
0013 #include <QExplicitlySharedDataPointer>
0014 #include <QHash>
0015 #include <QList>
0016 
0017 // TODO: Move the Property away from Profile.h
0018 #include "profile/Profile.h"
0019 
0020 class KConfig;
0021 
0022 namespace Konsole
0023 {
0024 class Session;
0025 class Profile;
0026 
0027 /**
0028  * Manages running terminal sessions.
0029  */
0030 class KONSOLEPRIVATE_EXPORT SessionManager : public QObject
0031 {
0032     Q_OBJECT
0033 
0034 public:
0035     /**
0036      * Constructs a new session manager and loads information about the available
0037      * profiles.
0038      */
0039     SessionManager();
0040 
0041     /**
0042      * Destroys the SessionManager. All running sessions should be closed
0043      * (via closeAllSessions()) before the SessionManager is destroyed.
0044      */
0045     ~SessionManager() override;
0046 
0047     /**
0048      * Returns the session manager instance.
0049      */
0050     static SessionManager *instance();
0051 
0052     /** Kill all running sessions. */
0053     void closeAllSessions();
0054 
0055     /**
0056      * Creates a new session using the settings specified by the specified
0057      * profile.
0058      *
0059      * The new session has no views associated with it.  A new TerminalDisplay view
0060      * must be created in order to display the output from the terminal session and
0061      * send keyboard or mouse input to it.
0062      *
0063      * @param profile A profile containing the settings for the new session.  If @p profile
0064      * is null the default profile (see ProfileManager::defaultProfile()) will be used.
0065      */
0066     Session *createSession(QExplicitlySharedDataPointer<Profile> profile = QExplicitlySharedDataPointer<Profile>());
0067 
0068     /** Sets the profile associated with a session. */
0069     void setSessionProfile(Session *session, QExplicitlySharedDataPointer<Profile> profile);
0070 
0071     /** Returns the profile associated with a session. */
0072     QExplicitlySharedDataPointer<Profile> sessionProfile(Session *session) const;
0073 
0074     /**
0075      * Returns a list of active sessions.
0076      */
0077     const QList<Session *> sessions() const;
0078 
0079     // System session management
0080     void saveSessions(KConfig *config);
0081     void restoreSessions(KConfig *config);
0082     int getRestoreId(Session *session);
0083     Session *idToSession(int id);
0084     bool isClosingAllSessions() const;
0085 
0086 Q_SIGNALS:
0087     /**
0088      * Emitted when a session's settings are updated to match
0089      * its current profile.
0090      */
0091     void sessionUpdated(Session *session);
0092 
0093 protected Q_SLOTS:
0094     /**
0095      * Called to inform the manager that a session has finished executing.
0096      *
0097      * @param session The Session which has finished executing.
0098      */
0099     void sessionTerminated(Session *session);
0100 
0101 private Q_SLOTS:
0102     void sessionProfileCommandReceived(Session *session, const QString &text);
0103 
0104     void profileChanged(const QExplicitlySharedDataPointer<Profile> &profile);
0105 
0106 private:
0107     Q_DISABLE_COPY(SessionManager)
0108 
0109     // applies updates to a profile
0110     // to all sessions currently using that profile
0111     // if modifiedPropertiesOnly is true, only properties which
0112     // are set in the profile @p key are updated
0113     void applyProfile(const QExplicitlySharedDataPointer<Profile> &profile, bool modifiedPropertiesOnly);
0114 
0115     // applies updates to the profile @p profile to the session @p session
0116     // if modifiedPropertiesOnly is true, only properties which
0117     // are set in @p profile are update ( ie. properties for which profile->isPropertySet(<property>)
0118     // returns true )
0119     void applyProfile(Session *session, const QExplicitlySharedDataPointer<Profile> &profile, bool modifiedPropertiesOnly);
0120 
0121     QList<Session *> _sessions; // list of running sessions
0122 
0123     QHash<Session *, QExplicitlySharedDataPointer<Profile>> _sessionProfiles;
0124     QHash<Session *, QExplicitlySharedDataPointer<Profile>> _sessionRuntimeProfiles;
0125     QHash<Session *, int> _restoreMapping;
0126     bool _isClosingAllSessions = false;
0127 };
0128 
0129 }
0130 
0131 #endif // SESSIONMANAGER_H