• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • applications API Reference
  • KDE Home
  • Contact Us
 

Konsole

  • kde-4.14
  • applications
  • konsole
  • src
SessionManager.cpp
Go to the documentation of this file.
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 // Own
23 #include "SessionManager.h"
24 
25 // Qt
26 #include <QtCore/QStringList>
27 #include <QtCore/QSignalMapper>
28 #include <QtCore/QTextCodec>
29 
30 // KDE
31 #include <KConfig>
32 #include <KConfigGroup>
33 #include <KGlobal>
34 #include <KDebug>
35 
36 // Konsole
37 #include "Session.h"
38 #include "ProfileManager.h"
39 #include "History.h"
40 #include "Enumeration.h"
41 
42 using namespace Konsole;
43 
44 SessionManager::SessionManager()
45 {
46  //map finished() signals from sessions
47  _sessionMapper = new QSignalMapper(this);
48  connect(_sessionMapper , SIGNAL(mapped(QObject*)) , this ,
49  SLOT(sessionTerminated(QObject*)));
50 
51  ProfileManager* profileMananger = ProfileManager::instance();
52  connect(profileMananger , SIGNAL(profileChanged(Profile::Ptr)) ,
53  this , SLOT(profileChanged(Profile::Ptr)));
54 }
55 
56 SessionManager::~SessionManager()
57 {
58  if (_sessions.count() > 0) {
59  kWarning() << "Konsole SessionManager destroyed with sessions still alive";
60  // ensure that the Session doesn't later try to call back and do things to the
61  // SessionManager
62  foreach(Session* session, _sessions) {
63  disconnect(session , 0 , this , 0);
64  }
65  }
66 }
67 
68 K_GLOBAL_STATIC(SessionManager , theSessionManager)
69 SessionManager* SessionManager::instance()
70 {
71  return theSessionManager;
72 }
73 
74 void SessionManager::closeAllSessions()
75 {
76  // close remaining sessions
77  foreach(Session* session , _sessions) {
78  session->close();
79  }
80  _sessions.clear();
81 }
82 
83 const QList<Session*> SessionManager::sessions() const
84 {
85  return _sessions;
86 }
87 
88 Session* SessionManager::createSession(Profile::Ptr profile)
89 {
90  if (!profile)
91  profile = ProfileManager::instance()->defaultProfile();
92 
93  // TODO: check whether this is really needed
94  if (!ProfileManager::instance()->loadedProfiles().contains(profile))
95  ProfileManager::instance()->addProfile(profile);
96 
97  //configuration information found, create a new session based on this
98  Session* session = new Session();
99  Q_ASSERT(session);
100  applyProfile(session, profile, false);
101 
102  connect(session , SIGNAL(profileChangeCommandReceived(QString)) , this ,
103  SLOT(sessionProfileCommandReceived(QString)));
104 
105  //ask for notification when session dies
106  _sessionMapper->setMapping(session, session);
107  connect(session , SIGNAL(finished()) , _sessionMapper ,
108  SLOT(map()));
109 
110  //add session to active list
111  _sessions << session;
112  _sessionProfiles.insert(session, profile);
113 
114  return session;
115 }
116 void SessionManager::profileChanged(Profile::Ptr profile)
117 {
118  applyProfile(profile, true);
119 }
120 
121 void SessionManager::sessionTerminated(QObject* sessionObject)
122 {
123  Session* session = qobject_cast<Session*>(sessionObject);
124 
125  Q_ASSERT(session);
126 
127  _sessions.removeAll(session);
128  _sessionProfiles.remove(session);
129  _sessionRuntimeProfiles.remove(session);
130 
131  session->deleteLater();
132 }
133 
134 void SessionManager::applyProfile(Profile::Ptr profile , bool modifiedPropertiesOnly)
135 {
136  foreach(Session* session, _sessions) {
137  if (_sessionProfiles[session] == profile)
138  applyProfile(session, profile, modifiedPropertiesOnly);
139  }
140 }
141 Profile::Ptr SessionManager::sessionProfile(Session* session) const
142 {
143  return _sessionProfiles[session];
144 }
145 void SessionManager::setSessionProfile(Session* session, Profile::Ptr profile)
146 {
147  if (!profile)
148  profile = ProfileManager::instance()->defaultProfile();
149 
150  Q_ASSERT(profile);
151 
152  _sessionProfiles[session] = profile;
153 
154  applyProfile(session, profile, false);
155 
156  emit sessionUpdated(session);
157 }
158 void SessionManager::applyProfile(Session* session, const Profile::Ptr profile , bool modifiedPropertiesOnly)
159 {
160  Q_ASSERT(profile);
161 
162  _sessionProfiles[session] = profile;
163 
164  ShouldApplyProperty apply(profile, modifiedPropertiesOnly);
165 
166  // Basic session settings
167  if (apply.shouldApply(Profile::Name))
168  session->setTitle(Session::NameRole, profile->name());
169 
170  if (apply.shouldApply(Profile::Command))
171  session->setProgram(profile->command());
172 
173  if (apply.shouldApply(Profile::Arguments))
174  session->setArguments(profile->arguments());
175 
176  if (apply.shouldApply(Profile::Directory))
177  session->setInitialWorkingDirectory(profile->defaultWorkingDirectory());
178 
179  if (apply.shouldApply(Profile::Environment)) {
180  // add environment variable containing home directory of current profile
181  // (if specified)
182  QStringList environment = profile->environment();
183  environment << QString("PROFILEHOME=%1").arg(profile->defaultWorkingDirectory());
184  environment << QString("KONSOLE_PROFILE_NAME=%1").arg(profile->name());
185 
186  session->setEnvironment(environment);
187  }
188 
189  if ( apply.shouldApply(Profile::TerminalColumns) ||
190  apply.shouldApply(Profile::TerminalRows) ) {
191  const int columns = profile->property<int>(Profile::TerminalColumns);
192  const int rows = profile->property<int>(Profile::TerminalRows);
193  session->setPreferredSize(QSize(columns, rows));
194  }
195 
196  if (apply.shouldApply(Profile::Icon))
197  session->setIconName(profile->icon());
198 
199  // Key bindings
200  if (apply.shouldApply(Profile::KeyBindings))
201  session->setKeyBindings(profile->keyBindings());
202 
203  // Tab formats
204  if (apply.shouldApply(Profile::LocalTabTitleFormat))
205  session->setTabTitleFormat(Session::LocalTabTitle ,
206  profile->localTabTitleFormat());
207  if (apply.shouldApply(Profile::RemoteTabTitleFormat))
208  session->setTabTitleFormat(Session::RemoteTabTitle ,
209  profile->remoteTabTitleFormat());
210 
211  // History
212  if (apply.shouldApply(Profile::HistoryMode) || apply.shouldApply(Profile::HistorySize)) {
213  const int mode = profile->property<int>(Profile::HistoryMode);
214  switch (mode) {
215  case Enum::NoHistory:
216  session->setHistoryType(HistoryTypeNone());
217  break;
218 
219  case Enum::FixedSizeHistory: {
220  int lines = profile->historySize();
221  session->setHistoryType(CompactHistoryType(lines));
222  }
223  break;
224 
225  case Enum::UnlimitedHistory:
226  session->setHistoryType(HistoryTypeFile());
227  break;
228  }
229  }
230 
231  // Terminal features
232  if (apply.shouldApply(Profile::FlowControlEnabled))
233  session->setFlowControlEnabled(profile->flowControlEnabled());
234 
235  // Encoding
236  if (apply.shouldApply(Profile::DefaultEncoding)) {
237  QByteArray name = profile->defaultEncoding().toUtf8();
238  session->setCodec(QTextCodec::codecForName(name));
239  }
240 
241  // Monitor Silence
242  if (apply.shouldApply(Profile::SilenceSeconds))
243  session->setMonitorSilenceSeconds(profile->silenceSeconds());
244 }
245 
246 void SessionManager::sessionProfileCommandReceived(const QString& text)
247 {
248  Session* session = qobject_cast<Session*>(sender());
249  Q_ASSERT(session);
250 
251  ProfileCommandParser parser;
252  QHash<Profile::Property, QVariant> changes = parser.parse(text);
253 
254  Profile::Ptr newProfile;
255  if (!_sessionRuntimeProfiles.contains(session)) {
256  newProfile = new Profile(_sessionProfiles[session]);
257  _sessionRuntimeProfiles.insert(session, newProfile);
258  } else {
259  newProfile = _sessionRuntimeProfiles[session];
260  }
261 
262  QHashIterator<Profile::Property, QVariant> iter(changes);
263  while (iter.hasNext()) {
264  iter.next();
265  newProfile->setProperty(iter.key(), iter.value());
266  }
267 
268  _sessionProfiles[session] = newProfile;
269  applyProfile(newProfile, true);
270  emit sessionUpdated(session);
271 }
272 
273 void SessionManager::saveSessions(KConfig* config)
274 {
275  // The session IDs can't be restored.
276  // So we need to map the old ID to the future new ID.
277  int n = 1;
278  _restoreMapping.clear();
279 
280  foreach(Session * session, _sessions) {
281  QString name = QLatin1String("Session") + QString::number(n);
282  KConfigGroup group(config, name);
283 
284  group.writePathEntry("Profile",
285  _sessionProfiles.value(session)->path());
286  session->saveSession(group);
287  _restoreMapping.insert(session, n);
288  n++;
289  }
290 
291  KConfigGroup group(config, "Number");
292  group.writeEntry("NumberOfSessions", _sessions.count());
293 }
294 
295 int SessionManager::getRestoreId(Session* session)
296 {
297  return _restoreMapping.value(session);
298 }
299 
300 void SessionManager::restoreSessions(KConfig* config)
301 {
302  KConfigGroup group(config, "Number");
303  int sessions;
304 
305  // Any sessions saved?
306  if ((sessions = group.readEntry("NumberOfSessions", 0)) > 0) {
307  for (int n = 1; n <= sessions; n++) {
308  QString name = QLatin1String("Session") + QString::number(n);
309  KConfigGroup sessionGroup(config, name);
310 
311  QString profile = sessionGroup.readPathEntry("Profile", QString());
312  Profile::Ptr ptr = ProfileManager::instance()->defaultProfile();
313  if (!profile.isEmpty()) ptr = ProfileManager::instance()->loadProfile(profile);
314 
315  Session* session = createSession(ptr);
316  session->restoreSession(sessionGroup);
317  }
318  }
319 }
320 
321 Session* SessionManager::idToSession(int id)
322 {
323  Q_ASSERT(id);
324  foreach(Session * session, _sessions) {
325  if (session->sessionId() == id)
326  return session;
327  }
328  // this should not happen
329  Q_ASSERT(0);
330  return 0;
331 }
332 
333 
334 #include "SessionManager.moc"
335 
Session.h
Konsole::Profile::HistoryMode
(HistoryModeEnum) Specifies the storage type used for keeping the output produced by terminal session...
Definition: Profile.h:137
Konsole::Profile::DefaultEncoding
(String) Default text codec
Definition: Profile.h:221
Konsole::Session
Represents a terminal session consisting of a pseudo-teletype and a terminal emulation.
Definition: Session.h:78
Konsole::Session::restoreSession
void restoreSession(KConfigGroup &group)
Definition: Session.cpp:1434
Konsole::SessionManager::setSessionProfile
void setSessionProfile(Session *session, Profile::Ptr profile)
Sets the profile associated with a session.
Definition: SessionManager.cpp:145
Konsole::Session::setEnvironment
Q_SCRIPTABLE void setEnvironment(const QStringList &environment)
Sets the environment for this session.
Definition: Session.cpp:889
Konsole::SessionManager::idToSession
Session * idToSession(int id)
Definition: SessionManager.cpp:321
Konsole::SessionManager::sessionUpdated
void sessionUpdated(Session *session)
Emitted when a session's settings are updated to match its current profile.
Konsole::SessionManager::SessionManager
SessionManager()
Constructs a new session manager and loads information about the available profiles.
Definition: SessionManager.cpp:44
Konsole::ProfileCommandParser
Parses an input string consisting of property names and assigned values and returns a table of proper...
Definition: Profile.h:681
Konsole::ProfileManager
Manages profiles which specify various settings for terminal sessions and their displays.
Definition: ProfileManager.h:48
QByteArray
Konsole::Session::sessionId
int sessionId() const
Returns the unique ID for this session.
Definition: Session.cpp:899
Konsole::Profile::FlowControlEnabled
(bool) Specifies whether the flow control keys (typically Ctrl+S, Ctrl+Q) have any effect...
Definition: Profile.h:165
Konsole::HistoryTypeNone
Definition: History.h:348
QObject::sender
QObject * sender() const
Konsole::Session::setPreferredSize
void setPreferredSize(const QSize &size)
Definition: Session.cpp:1303
Konsole::Session::setCodec
void setCodec(QTextCodec *codec)
Definition: Session.cpp:245
Konsole::Profile::RemoteTabTitleFormat
(QString) The format used for tab titles when the session is running a remote command (eg...
Definition: Profile.h:116
Konsole::SessionManager::sessions
const QList< Session * > sessions() const
Returns a list of active sessions.
Definition: SessionManager.cpp:83
Konsole::Profile::KeyBindings
(QString) The name of the key bindings.
Definition: Profile.h:131
Konsole::CompactHistoryType
Definition: History.h:373
Konsole::Profile::SilenceSeconds
(int) Specifies the threshold of detected silence in seconds.
Definition: Profile.h:232
Konsole::Session::setProgram
void setProgram(const QString &program)
Sets the program to be executed when run() is called.
Definition: Session.cpp:267
Konsole::Profile
Represents a terminal set-up which can be used to set the initial state of new terminal sessions or a...
Definition: Profile.h:60
QObject::disconnect
bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *method)
Konsole::SessionManager::sessionProfile
Profile::Ptr sessionProfile(Session *session) const
Returns the profile associated with a session.
Definition: SessionManager.cpp:141
Konsole::Profile::Directory
(QString) The initial working directory for sessions created using this profile.
Definition: Profile.h:108
Konsole::SessionManager::saveSessions
void saveSessions(KConfig *config)
Definition: SessionManager.cpp:273
Konsole::Session::setHistoryType
void setHistoryType(const HistoryType &type)
Sets the type of history store used by this session.
Definition: Session.cpp:1067
Konsole::Profile::Command
(QString) The command to execute ( excluding arguments ) when creating a new terminal session using t...
Definition: Profile.h:93
Konsole::ProfileManager::defaultProfile
Profile::Ptr defaultProfile() const
Returns a Profile object describing the default profile.
Definition: ProfileManager.cpp:308
Konsole::SessionManager::getRestoreId
int getRestoreId(Session *session)
Definition: SessionManager.cpp:295
Konsole::Session::setTabTitleFormat
void setTabTitleFormat(TabTitleContext context, const QString &format)
Sets the format used by this session for tab titles.
Definition: Session.cpp:588
QObject::name
const char * name() const
QSignalMapper::setMapping
void setMapping(QObject *sender, int id)
History.h
QString::number
QString number(int n, int base)
Konsole::ProfileManager::instance
static ProfileManager * instance()
Returns the profile manager instance.
Definition: ProfileManager.cpp:114
QHash
QObject
Konsole::ShouldApplyProperty
Utility class to simplify code in SessionManager::applyProfile().
Definition: SessionManager.h:141
Konsole::SessionManager::closeAllSessions
void closeAllSessions()
Kill all running sessions.
Definition: SessionManager.cpp:74
QHashIterator
QString::isEmpty
bool isEmpty() const
Konsole::Session::setTitle
void setTitle(TitleRole role, const QString &title)
Sets the session's title for the specified role to title.
Definition: Session.cpp:909
Konsole::Profile::Arguments
(QStringList) The arguments which are passed to the program specified by the Command property when cr...
Definition: Profile.h:98
Konsole::Session::setIconName
void setIconName(const QString &iconName)
Sets the name of the icon associated with this session.
Definition: Session.cpp:1044
Konsole::Session::setInitialWorkingDirectory
void setInitialWorkingDirectory(const QString &dir)
Sets the initial working directory for the session when it is run This has no effect once the session...
Definition: Session.cpp:277
Konsole::Profile::Icon
(QString) The name of the icon associated with this profile.
Definition: Profile.h:89
Konsole::Session::RemoteTabTitle
Tab title format used session currently contains a connection to a remote computer (via SSH) ...
Definition: Session.h:166
QObject::deleteLater
void deleteLater()
QString
QList
Enumeration.h
Konsole::Enum::NoHistory
No output is remembered.
Definition: Enumeration.h:42
QStringList
Konsole::Session::setKeyBindings
void setKeyBindings(const QString &name)
Sets the key bindings used by this session.
Definition: Session.cpp:904
QSize
Konsole::ProfileCommandParser::parse
QHash< Profile::Property, QVariant > parse(const QString &input)
Parses an input string consisting of property names and assigned values and returns a table of proper...
Definition: Profile.cpp:305
Konsole::SessionManager::~SessionManager
virtual ~SessionManager()
Destroys the SessionManager.
Definition: SessionManager.cpp:56
Konsole::HistoryTypeFile
Definition: History.h:359
Konsole::Session::close
void close()
Closes the terminal session.
Definition: Session.cpp:770
Konsole::Profile::Ptr
KSharedPtr< Profile > Ptr
Definition: Profile.h:67
Konsole::Profile::HistorySize
(int) Specifies the number of lines of output to remember in terminal sessions using this profile...
Definition: Profile.h:143
Konsole::Session::NameRole
The name of the session.
Definition: Session.h:253
Konsole::Session::LocalTabTitle
Default tab title format.
Definition: Session.h:161
QLatin1String
Konsole::SessionManager::sessionTerminated
void sessionTerminated(QObject *session)
Called to inform the manager that a session has finished executing.
Definition: SessionManager.cpp:121
ProfileManager.h
Konsole::Enum::UnlimitedHistory
All output is remembered for the duration of the session.
Definition: Enumeration.h:51
Konsole::Profile::TerminalColumns
(int) Specifies the preferred columns.
Definition: Profile.h:239
QTextCodec::codecForName
QTextCodec * codecForName(const QByteArray &name)
Konsole::Session::setArguments
void setArguments(const QStringList &arguments)
Sets the command line arguments which the session's program will be passed when run() is called...
Definition: Session.cpp:272
Konsole::Enum::FixedSizeHistory
A fixed number of lines of output are remembered.
Definition: Enumeration.h:46
Konsole::Session::setFlowControlEnabled
Q_SCRIPTABLE void setFlowControlEnabled(bool enabled)
Sets whether flow control is enabled for this terminal session.
Definition: Session.cpp:1153
SessionManager.h
Konsole::SessionManager::createSession
Session * createSession(Profile::Ptr profile=Profile::Ptr())
Creates a new session using the settings specified by the specified profile.
Definition: SessionManager.cpp:88
Konsole::ProfileManager::loadProfile
Profile::Ptr loadProfile(const QString &path)
Loads a profile from the specified path and registers it with the ProfileManager. ...
Definition: ProfileManager.cpp:119
Konsole::ProfileManager::addProfile
void addProfile(Profile::Ptr type)
Registers a new type of session.
Definition: ProfileManager.cpp:369
Konsole::Profile::LocalTabTitleFormat
(QString) The format used for tab titles when running normal commands.
Definition: Profile.h:112
QObject::connect
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
QString::arg
QString arg(qlonglong a, int fieldWidth, int base, const QChar &fillChar) const
Konsole::SessionManager
Manages running terminal sessions.
Definition: SessionManager.h:43
QSignalMapper
Konsole::Profile::TerminalRows
(int) Specifies the preferred rows.
Definition: Profile.h:241
Konsole::Session::setMonitorSilenceSeconds
Q_SCRIPTABLE void setMonitorSilenceSeconds(int seconds)
See setMonitorSilence()
Definition: Session.cpp:1130
Konsole::Session::saveSession
void saveSession(KConfigGroup &group)
Definition: Session.cpp:1425
Konsole::Profile::Name
(QString) The descriptive name of this profile.
Definition: Profile.h:81
Konsole::SessionManager::restoreSessions
void restoreSessions(KConfig *config)
Definition: SessionManager.cpp:300
Konsole::Profile::Environment
(QStringList) Additional environment variables (in the form of NAME=VALUE pairs) which are passed to ...
Definition: Profile.h:104
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sat May 9 2020 03:56:27 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

Konsole

Skip menu "Konsole"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

applications API Reference

Skip menu "applications API Reference"
  •   kate
  •       kate
  •   KTextEditor
  •   Kate
  • Konsole

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal