2 This source file is part of Konsole, a terminal emulator.
4 Copyright 2006-2008 by Robert Knight <robertknight@gmail.com>
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.
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.
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
22 #ifndef SESSIONMANAGER_H
23 #define SESSIONMANAGER_H
26 #include <QtGui/QFont>
27 #include <QtGui/QKeySequence>
29 #include <QtCore/QAbstractListModel>
30 #include <QtCore/QHash>
31 #include <QtCore/QList>
32 #include <QtCore/QSet>
33 #include <QtCore/QStringList>
34 #include <QtCore/QPair>
35 #include <QtCore/QPointer>
36 #include <QtCore/QVariant>
37 #include <QtCore/QStack>
51 * Manages running terminal sessions and the profiles which specify various
52 * settings for terminal sessions and their displays.
54 * Profiles in the manager have a concept of favorite status, which can be used
55 * by widgets and dialogs in the application decide which sessions to list and
56 * how to display them. The favorite status of a profile can be altered using
57 * setFavorite() and retrieved using isFavorite()
59 class KDE_EXPORT SessionManager
: public QObject
65 * Constructs a new session manager and loads information about the available
71 * Destroys the SessionManager. All running sessions should be closed (via closeAll()) and the
72 * SessionManager's state should be saved via saveState() before the SessionManager is destroyed.
74 virtual ~SessionManager();
76 /** Kill all running sessions. */
79 /** Saves state information (favorites, shortcuts, default profile etc.) to disk. */
83 * Returns a list of profiles which have been loaded.
84 * Initially only the profile currently set as the default is loaded.
86 * Favorite profiles are loaded automatically when findFavorites() is called.
88 * All other profiles can be loaded by calling loadAllProfiles(). This may
89 * involves opening, reading and parsing all profiles from disk, and
90 * should only be done when necessary.
92 QList
<Profile::Ptr
> loadedProfiles() const;
95 * Searches for available profiles on-disk and returns a list
96 * of paths of profiles which can be loaded.
98 QStringList
availableProfilePaths() const;
102 * Loads a profile from the specified path and registers
103 * it with the SessionManager.
105 * @p path may be relative or absolute. The path may just be the
106 * base name of the profile to load (eg. if the profile's full path
107 * is "<konsole data dir>/My Profile.profile" then both
108 * "konsole/My Profile.profile" , "My Profile.profile" and
109 * "My Profile" will be accepted)
111 * @return Pointer to a profile which can be passed to createSession()
112 * to create a new session using this profile.
114 Profile::Ptr
loadProfile(const QString
& path
);
117 * Updates a @p profile with the changes specified in @p propertyMap.
119 * All sessions currently using the profile will be updated to reflect the new settings.
121 * After the profile is updated, the profileChanged() signal will be emitted.
123 * @param profile The profile to change
124 * @param propertyMap A map between profile properties and values describing the changes
125 * @param persistant If true, the changes are saved to the profile's configuration file,
126 * set this to false if you want to preview possible changes to a profile but do not
127 * wish to make them permanent.
129 void changeProfile(Profile::Ptr profile
, QHash
<Profile::Property
,QVariant
> propertyMap
,
130 bool persistant
= true);
133 * Returns a Profile object describing the default type of session, which is used
134 * if createSession() is called with an empty configPath argument.
136 Profile::Ptr
defaultProfile() const;
138 * Returns a Profile object with hard-coded settings which is always available.
139 * This can be used as a parent for new profiles which provides suitable default settings
140 * for all properties.
142 Profile::Ptr
fallbackProfile() const;
145 * Creates a new session using the settings specified by the specified
148 * The new session has no views associated with it. A new TerminalDisplay view
149 * must be created in order to display the output from the terminal session and
150 * send keyboard or mouse input to it.
152 * @param profile A profile containing the settings for the new session. If @p profile
153 * is null the default profile (see defaultProfile()) will be used.
155 Session
* createSession(Profile::Ptr profile
= Profile::Ptr());
157 /** Returns the profile associated with a session. */
158 Profile::Ptr
sessionProfile(Session
* session
) const;
159 /** Sets the profile associated with a session. */
160 void setSessionProfile(Session
* session
, Profile::Ptr profile
);
163 * Updates a session's properties to match its current profile.
165 void updateSession(Session
* session
);
168 * Returns a list of active sessions.
170 const QList
<Session
*> sessions();
173 * Deletes the configuration file used to store a profile.
174 * The profile will continue to exist while sessions are still using it. The profile
175 * will be marked as hidden (see Profile::setHidden() ) so that it does not show
176 * up in profile lists and future changes to the profile are not stored to disk.
178 * Returns true if the profile was successfully deleted or false otherwise.
180 bool deleteProfile(Profile::Ptr profile
);
183 * Sets the @p profile as the default profile for new sessions created
184 * with createSession()
186 void setDefaultProfile(Profile::Ptr profile
);
189 * Returns the set of the user's favorite profiles.
191 QSet
<Profile::Ptr
> findFavorites();
194 * Returns the list of shortcut key sequences which
195 * can be used to create new sessions based on
198 * When one of the shortcuts is activated,
199 * use findByShortcut() to load the profile associated
202 QList
<QKeySequence
> shortcuts();
205 * Finds and loads the profile associated with
206 * the specified @p shortcut key sequence and returns a pointer to it.
208 Profile::Ptr
findByShortcut(const QKeySequence
& shortcut
);
211 * Associates a shortcut with a particular profile.
213 void setShortcut(Profile::Ptr profile
, const QKeySequence
& shortcut
);
215 /** Returns the shortcut associated with a particular profile. */
216 QKeySequence
shortcut(Profile::Ptr profile
) const;
219 * Registers a new type of session.
220 * The favorite status of the session ( as returned by isFavorite() ) is set to false by default.
222 void addProfile(Profile::Ptr type
);
225 * Specifies whether a profile should be included in the user's
226 * list of favorite sessions.
228 void setFavorite(Profile::Ptr profile
, bool favorite
);
231 * Loads all available profiles. This involves reading each
232 * profile configuration file from disk and parsing it.
233 * Therefore it should only be done when necessary.
235 void loadAllProfiles();
238 * Sets the global session manager instance.
240 static void setInstance(SessionManager
* instance
);
242 * Returns the session manager instance.
244 static SessionManager
* instance();
246 // session management
247 void saveSessions(KConfig
* config
);
248 int getRestoreId(Session
* session
);
249 void restoreSessions(KConfig
* config
);
250 Session
*idToSession(int id
);
253 /** Emitted when a profile is added to the manager. */
254 void profileAdded(Profile::Ptr ptr
);
255 /** Emitted when a profile is removed from the manager. */
256 void profileRemoved(Profile::Ptr ptr
);
257 /** Emitted when a profile's properties are modified. */
258 void profileChanged(Profile::Ptr ptr
);
261 * Emitted when a session's settings are updated to match
262 * its current profile.
264 void sessionUpdated(Session
* session
);
267 * Emitted when the favorite status of a profile changes.
269 * @param profile The profile to change
270 * @param favorite Specifies whether the session is a favorite or not
272 void favoriteStatusChanged(Profile::Ptr profile
, bool favorite
);
275 * Emitted when the shortcut for a profile is changed.
277 * @param profile The profile whoose status was changed
278 * @param newShortcut The new shortcut key sequence for the profile
280 void shortcutChanged(Profile::Ptr profile
, const QKeySequence
& newShortcut
);
285 * Called to inform the manager that a session has finished executing.
287 * @param session The Session which has finished executing.
289 void sessionTerminated( QObject
* session
);
292 void sessionProfileCommandReceived(const QString
& text
);
297 // loads the mappings between shortcut key sequences and
299 void loadShortcuts();
300 // saves the mappings between shortcut key sequences and
302 void saveShortcuts();
304 //loads the set of favorite sessions
305 void loadFavorites();
306 //saves the set of favorite sessions
307 void saveFavorites();
308 // saves a profile to a file
309 // returns the path to which the profile was saved, which will
310 // be the same as the path property of profile if valid or a newly generated path
312 QString
saveProfile(Profile::Ptr profile
);
314 // applies updates to a profile
315 // to all sessions currently using that profile
316 // if modifiedPropertiesOnly is true, only properties which
317 // are set in the profile @p key are updated
318 void applyProfile(Profile::Ptr ptr
, bool modifiedPropertiesOnly
);
319 // apples updates to the profile @p info to the session @p session
320 // if modifiedPropertiesOnly is true, only properties which
321 // are set in @p info are update ( ie. properties for which info->isPropertySet(<property>)
323 void applyProfile(Session
* session
, const Profile::Ptr info
, bool modifiedPropertiesOnly
);
325 QSet
<Profile::Ptr
> _types
;
326 QHash
<Session
*,Profile::Ptr
> _sessionProfiles
;
327 QHash
<Session
*,int> _restoreMapping
;
331 Profile::Ptr profileKey
;
334 QMap
<QKeySequence
,ShortcutData
> _shortcuts
; // shortcut keys -> profile path
336 QList
<Session
*> _sessions
; // list of running sessions
338 Profile::Ptr _defaultProfile
;
339 Profile::Ptr _fallbackProfile
;
341 QSet
<Profile::Ptr
> _favorites
; // list of favorite profiles
343 bool _loadedAllProfiles
; // set to true after loadAllProfiles has been called
344 bool _loadedFavorites
; // set to true after loadFavorites has been called
345 QSignalMapper
* _sessionMapper
;
348 /** Utility class to simplify code in SessionManager::applyProfile(). */
349 class ShouldApplyProperty
352 ShouldApplyProperty(const Profile::Ptr profile
, bool modifiedOnly
) :
353 _profile(profile
) , _modifiedPropertiesOnly(modifiedOnly
) {}
355 bool shouldApply(Profile::Property property
) const
357 return !_modifiedPropertiesOnly
|| _profile
->isPropertySet(property
);
360 const Profile::Ptr _profile
;
361 bool _modifiedPropertiesOnly
;
365 * PopStackOnExit is a utility to remove all values from a QStack which are added during
366 * the lifetime of a PopStackOnExit instance.
368 * When a PopStackOnExit instance is destroyed, elements are removed from the stack
369 * until the stack count is reduced the value when the PopStackOnExit instance was created.
375 PopStackOnExit(QStack
<T
>& stack
) : _stack(stack
) , _count(stack
.count()) {}
378 while (_stack
.count() > _count
)
386 * Item-view model which contains a flat list of sessions.
387 * After constructing the model, call setSessions() to set the sessions displayed
388 * in the list. When a session ends (after emitting the finished() signal) it is
389 * automatically removed from the list.
391 * The internal pointer for each item in the model (index.internalPointer()) is the
392 * associated Session*
394 class SessionListModel
: public QAbstractListModel
399 SessionListModel(QObject
* parent
= 0);
402 * Sets the list of sessions displayed in the model.
403 * To display all sessions that are currently running in the list,
404 * call setSessions(SessionManager::instance()->sessions())
406 void setSessions(const QList
<Session
*>& sessions
);
408 // reimplemented from QAbstractItemModel
409 virtual QModelIndex
index(int row
, int column
, const QModelIndex
& parent
) const;
410 virtual QVariant
data(const QModelIndex
& index
, int role
) const;
411 virtual QVariant
headerData(int section
, Qt::Orientation orientation
,
413 virtual int columnCount(const QModelIndex
& parent
) const;
414 virtual int rowCount(const QModelIndex
& parent
) const;
415 virtual QModelIndex
parent(const QModelIndex
& index
) const;
418 virtual void sessionRemoved(Session
*) {}
421 void sessionFinished();
424 QList
<Session
*> _sessions
;
428 #endif //SESSIONMANAGER_H
433 c-file-style: "stroustrup"
434 indent-tabs-mode: nil