1/*
2 This source file is part of Konsole, a terminal emulator.
3
4 Copyright 2006-2008 by Robert Knight <robertknight@gmail.com>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
20*/
21
22#ifndef SESSIONMANAGER_H
23#define SESSIONMANAGER_H
24
25// Qt
26#include <QtCore/QHash>
27#include <QtCore/QList>
28
29// Konsole
30#include "Profile.h"
31
32class QSignalMapper;
33
34class KConfig;
35
36namespace Konsole
37{
38class Session;
39
40/**
41 * Manages running terminal sessions.
42 */
43class KONSOLEPRIVATE_EXPORT SessionManager : public QObject
44{
45 Q_OBJECT
46
47public:
48 /**
49 * Constructs a new session manager and loads information about the available
50 * profiles.
51 */
52 SessionManager();
53
54 /**
55 * Destroys the SessionManager. All running sessions should be closed
56 * (via closeAllSessions()) before the SessionManager is destroyed.
57 */
58 virtual ~SessionManager();
59
60 /**
61 * Returns the session manager instance.
62 */
63 static SessionManager* instance();
64
65 /** Kill all running sessions. */
66 void closeAllSessions();
67
68 /**
69 * Creates a new session using the settings specified by the specified
70 * profile.
71 *
72 * The new session has no views associated with it. A new TerminalDisplay view
73 * must be created in order to display the output from the terminal session and
74 * send keyboard or mouse input to it.
75 *
76 * @param profile A profile containing the settings for the new session. If @p profile
77 * is null the default profile (see ProfileManager::defaultProfile()) will be used.
78 */
79 Session* createSession(Profile::Ptr profile = Profile::Ptr());
80
81 /** Sets the profile associated with a session. */
82 void setSessionProfile(Session* session, Profile::Ptr profile);
83
84 /** Returns the profile associated with a session. */
85 Profile::Ptr sessionProfile(Session* session) const;
86
87 /**
88 * Returns a list of active sessions.
89 */
90 const QList<Session*> sessions() const;
91
92 // System session management
93 void saveSessions(KConfig* config);
94 void restoreSessions(KConfig* config);
95 int getRestoreId(Session* session);
96 Session* idToSession(int id);
97
98signals:
99 /**
100 * Emitted when a session's settings are updated to match
101 * its current profile.
102 */
103 void sessionUpdated(Session* session);
104
105protected slots:
106 /**
107 * Called to inform the manager that a session has finished executing.
108 *
109 * @param session The Session which has finished executing.
110 */
111 void sessionTerminated(QObject* session);
112
113private slots:
114 void sessionProfileCommandReceived(const QString& text);
115
116 void profileChanged(Profile::Ptr profile);
117
118private:
119 // applies updates to a profile
120 // to all sessions currently using that profile
121 // if modifiedPropertiesOnly is true, only properties which
122 // are set in the profile @p key are updated
123 void applyProfile(Profile::Ptr profile , bool modifiedPropertiesOnly);
124
125 // applies updates to the profile @p profile to the session @p session
126 // if modifiedPropertiesOnly is true, only properties which
127 // are set in @p profile are update ( ie. properties for which profile->isPropertySet(<property>)
128 // returns true )
129 void applyProfile(Session* session , const Profile::Ptr profile , bool modifiedPropertiesOnly);
130
131 QList<Session*> _sessions; // list of running sessions
132
133 QHash<Session*, Profile::Ptr> _sessionProfiles;
134 QHash<Session*, Profile::Ptr> _sessionRuntimeProfiles;
135 QHash<Session*, int> _restoreMapping;
136
137 QSignalMapper* _sessionMapper;
138};
139
140/** Utility class to simplify code in SessionManager::applyProfile(). */
141class ShouldApplyProperty
142{
143public:
144 ShouldApplyProperty(const Profile::Ptr profile , bool modifiedOnly) :
145 _profile(profile) , _modifiedPropertiesOnly(modifiedOnly) {}
146
147 bool shouldApply(Profile::Property property) const {
148 return !_modifiedPropertiesOnly || _profile->isPropertySet(property);
149 }
150private:
151 const Profile::Ptr _profile;
152 bool _modifiedPropertiesOnly;
153};
154}
155#endif //SESSIONMANAGER_H
156