SessionManager.h 4.63 KB
Newer Older
Robert Knight's avatar
Robert Knight committed
1 2 3
/*
    This source file is part of Konsole, a terminal emulator.

4
    Copyright 2006-2008 by Robert Knight <robertknight@gmail.com>
Robert Knight's avatar
Robert Knight committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301  USA.
*/

Robert Knight's avatar
 
Robert Knight committed
22 23 24
#ifndef SESSIONMANAGER_H
#define SESSIONMANAGER_H

25
// Qt
Dirk Mueller's avatar
Dirk Mueller committed
26 27
#include <QtCore/QHash>
#include <QtCore/QList>
28

29 30 31
// Konsole
#include "Profile.h"

32 33
class QSignalMapper;

34 35
class KConfig;

36 37 38
namespace Konsole
{
class Session;
39

40
/**
41
 * Manages running terminal sessions.
42
 */
43
class KONSOLEPRIVATE_EXPORT SessionManager : public QObject
Robert Knight's avatar
 
Robert Knight committed
44
{
Kurt Hindenburg's avatar
Kurt Hindenburg committed
45
    Q_OBJECT
Robert Knight's avatar
 
Robert Knight committed
46

Robert Knight's avatar
 
Robert Knight committed
47
public:
Jekyll Wu's avatar
Jekyll Wu committed
48 49
    /**
     * Constructs a new session manager and loads information about the available
50
     * profiles.
51
     */
Robert Knight's avatar
 
Robert Knight committed
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 62 63 64
    /**
     * Returns the session manager instance.
     */
    static SessionManager* instance();

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.
Robert Knight's avatar
 
Robert Knight committed
75
     *
Jekyll Wu's avatar
Jekyll Wu committed
76
     * @param profile A profile containing the settings for the new session.  If @p profile
Jekyll Wu's avatar
Jekyll Wu committed
77
     * is null the default profile (see ProfileManager::defaultProfile()) will be used.
Robert Knight's avatar
 
Robert Knight committed
78
     */
79 80
    Session* createSession(Profile::Ptr profile = Profile::Ptr());

81 82
    /** Sets the profile associated with a session. */
    void setSessionProfile(Session* session, Profile::Ptr profile);
83

84 85 86
    /** Returns the profile associated with a session. */
    Profile::Ptr sessionProfile(Session* session) const;

Robert Knight's avatar
 
Robert Knight committed
87 88 89
    /**
     * Returns a list of active sessions.
     */
90
    const QList<Session*> sessions() const;
Robert Knight's avatar
 
Robert Knight committed
91

Jekyll Wu's avatar
Jekyll Wu committed
92
    // System session management
Robert Knight's avatar
 
Robert Knight committed
93 94
    void saveSessions(KConfig* config);
    void restoreSessions(KConfig* config);
95
    int  getRestoreId(Session* session);
96
    Session* idToSession(int id);
Robert Knight's avatar
 
Robert Knight committed
97

98
signals:
Jekyll Wu's avatar
Jekyll Wu committed
99 100 101
    /**
     * Emitted when a session's settings are updated to match
     * its current profile.
102 103 104
     */
    void sessionUpdated(Session* session);

Jekyll Wu's avatar
Jekyll Wu committed
105
protected slots:
Robert Knight's avatar
 
Robert Knight committed
106
    /**
107 108 109
     * Called to inform the manager that a session has finished executing.
     *
     * @param session The Session which has finished executing.
Robert Knight's avatar
 
Robert Knight committed
110
     */
Kurt Hindenburg's avatar
Kurt Hindenburg committed
111
    void sessionTerminated(QObject* session);
Robert Knight's avatar
 
Robert Knight committed
112

113
private slots:
114
    void sessionProfileCommandReceived(const QString& text);
115

116
    void profileChanged(Profile::Ptr profile);
117

118
private:
119
    // applies updates to a profile
120 121 122
    // to all sessions currently using that profile
    // if modifiedPropertiesOnly is true, only properties which
    // are set in the profile @p key are updated
123
    void applyProfile(Profile::Ptr profile , bool modifiedPropertiesOnly);
124

Jekyll Wu's avatar
Jekyll Wu committed
125
    // applies updates to the profile @p profile to the session @p session
126
    // if modifiedPropertiesOnly is true, only properties which
Jekyll Wu's avatar
Jekyll Wu committed
127
    // are set in @p profile are update ( ie. properties for which profile->isPropertySet(<property>)
128
    // returns true )
Kurt Hindenburg's avatar
Kurt Hindenburg committed
129
    void applyProfile(Session* session , const Profile::Ptr profile , bool modifiedPropertiesOnly);
130

Jekyll Wu's avatar
Jekyll Wu committed
131 132
    QList<Session*> _sessions; // list of running sessions

Kurt Hindenburg's avatar
Kurt Hindenburg committed
133 134 135
    QHash<Session*, Profile::Ptr> _sessionProfiles;
    QHash<Session*, Profile::Ptr> _sessionRuntimeProfiles;
    QHash<Session*, int> _restoreMapping;
136

137
    QSignalMapper* _sessionMapper;
Robert Knight's avatar
 
Robert Knight committed
138 139
};

140
/** Utility class to simplify code in SessionManager::applyProfile(). */
141
class ShouldApplyProperty
142 143
{
public:
144
    ShouldApplyProperty(const Profile::Ptr profile , bool modifiedOnly) :
Kurt Hindenburg's avatar
Kurt Hindenburg committed
145
        _profile(profile) , _modifiedPropertiesOnly(modifiedOnly) {}
146

Kurt Hindenburg's avatar
Kurt Hindenburg committed
147 148
    bool shouldApply(Profile::Property property) const {
        return !_modifiedPropertiesOnly || _profile->isPropertySet(property);
149
    }
150
private:
151 152
    const Profile::Ptr _profile;
    bool _modifiedPropertiesOnly;
153
};
Stephan Binner's avatar
Stephan Binner committed
154
}
Robert Knight's avatar
 
Robert Knight committed
155
#endif //SESSIONMANAGER_H