File indexing completed on 2018-01-16 12:10:22

0001 /*
0002     This source file is part of Konsole, a terminal emulator.
0003 
0004     Copyright 2006-2008 by Robert Knight <robertknight@gmail.com>
0005 
0006     This program is free software; you can redistribute it and/or modify
0007     it under the terms of the GNU General Public License as published by
0008     the Free Software Foundation; either version 2 of the License, or
0009     (at your option) any later version.
0010 
0011     This program is distributed in the hope that it will be useful,
0012     but WITHOUT ANY WARRANTY; without even the implied warranty of
0013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0014     GNU General Public License for more details.
0015 
0016     You should have received a copy of the GNU General Public License
0017     along with this program; if not, write to the Free Software
0018     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
0019     02110-1301  USA.
0020 */
0021 
0022 #ifndef SESSIONMANAGER_H
0023 #define SESSIONMANAGER_H
0024 
0025 // Qt
0026 #include <QHash>
0027 #include <QList>
0028 
0029 // Konsole
0030 #include "Profile.h"
0031 
0032 class KConfig;
0033 
0034 namespace Konsole {
0035 class Session;
0036 
0037 /**
0038  * Manages running terminal sessions.
0039  */
0040 class KONSOLEPRIVATE_EXPORT SessionManager : public QObject
0041 {
0042     Q_OBJECT
0043 
0044 public:
0045     /**
0046      * Constructs a new session manager and loads information about the available
0047      * profiles.
0048      */
0049     SessionManager();
0050 
0051     /**
0052      * Destroys the SessionManager. All running sessions should be closed
0053      * (via closeAllSessions()) before the SessionManager is destroyed.
0054      */
0055     ~SessionManager() Q_DECL_OVERRIDE;
0056 
0057     /**
0058      * Returns the session manager instance.
0059      */
0060     static SessionManager *instance();
0061 
0062     /** Kill all running sessions. */
0063     void closeAllSessions();
0064 
0065     /**
0066      * Creates a new session using the settings specified by the specified
0067      * profile.
0068      *
0069      * The new session has no views associated with it.  A new TerminalDisplay view
0070      * must be created in order to display the output from the terminal session and
0071      * send keyboard or mouse input to it.
0072      *
0073      * @param profile A profile containing the settings for the new session.  If @p profile
0074      * is null the default profile (see ProfileManager::defaultProfile()) will be used.
0075      */
0076     Session *createSession(Profile::Ptr profile = Profile::Ptr());
0077 
0078     /** Sets the profile associated with a session. */
0079     void setSessionProfile(Session *session, Profile::Ptr profile);
0080 
0081     /** Returns the profile associated with a session. */
0082     Profile::Ptr sessionProfile(Session *session) const;
0083 
0084     /**
0085      * Returns a list of active sessions.
0086      */
0087     const QList<Session *> sessions() const;
0088 
0089     // System session management
0090     void saveSessions(KConfig *config);
0091     void restoreSessions(KConfig *config);
0092     int  getRestoreId(Session *session);
0093     Session *idToSession(int id);
0094 
0095 Q_SIGNALS:
0096     /**
0097      * Emitted when a session's settings are updated to match
0098      * its current profile.
0099      */
0100     void sessionUpdated(Session *session);
0101 
0102 protected Q_SLOTS:
0103     /**
0104      * Called to inform the manager that a session has finished executing.
0105      *
0106      * @param session The Session which has finished executing.
0107      */
0108     void sessionTerminated(Session *session);
0109 
0110 private Q_SLOTS:
0111     void sessionProfileCommandReceived(const QString &text);
0112 
0113     void profileChanged(Profile::Ptr profile);
0114 
0115 private:
0116     Q_DISABLE_COPY(SessionManager)
0117 
0118     // applies updates to a profile
0119     // to all sessions currently using that profile
0120     // if modifiedPropertiesOnly is true, only properties which
0121     // are set in the profile @p key are updated
0122     void applyProfile(Profile::Ptr profile, bool modifiedPropertiesOnly);
0123 
0124     // applies updates to the profile @p profile to the session @p session
0125     // if modifiedPropertiesOnly is true, only properties which
0126     // are set in @p profile are update ( ie. properties for which profile->isPropertySet(<property>)
0127     // returns true )
0128     void applyProfile(Session *session, const Profile::Ptr profile, bool modifiedPropertiesOnly);
0129 
0130     QList<Session *> _sessions; // list of running sessions
0131 
0132     QHash<Session *, Profile::Ptr> _sessionProfiles;
0133     QHash<Session *, Profile::Ptr> _sessionRuntimeProfiles;
0134     QHash<Session *, int> _restoreMapping;
0135 };
0136 
0137 /** Utility class to simplify code in SessionManager::applyProfile(). */
0138 class ShouldApplyProperty
0139 {
0140 public:
0141     ShouldApplyProperty(const Profile::Ptr profile, bool modifiedOnly) :
0142         _profile(profile),
0143         _modifiedPropertiesOnly(modifiedOnly)
0144     {
0145     }
0146 
0147     bool shouldApply(Profile::Property property) const
0148     {
0149         return !_modifiedPropertiesOnly || _profile->isPropertySet(property);
0150     }
0151 
0152 private:
0153     const Profile::Ptr _profile;
0154     bool _modifiedPropertiesOnly;
0155 };
0156 }
0157 #endif //SESSIONMANAGER_H