File indexing completed on 2018-06-17 11:14:15

0001 /*
0002     This file is part of Konsole, an X terminal.
0003 
0004     Copyright 2007-2008 by Robert Knight <robertknight@gmail.com>
0005     Copyright 1997,1998 by Lars Doelle <lars.doelle@on-line.de>
0006     Copyright 2009 by Thomas Dreibholz <dreibh@iem.uni-due.de>
0007 
0008     This program is free software; you can redistribute it and/or modify
0009     it under the terms of the GNU General Public License as published by
0010     the Free Software Foundation; either version 2 of the License, or
0011     (at your option) any later version.
0012 
0013     This program is distributed in the hope that it will be useful,
0014     but WITHOUT ANY WARRANTY; without even the implied warranty of
0015     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0016     GNU General Public License for more details.
0017 
0018     You should have received a copy of the GNU General Public License
0019     along with this program; if not, write to the Free Software
0020     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
0021     02110-1301  USA.
0022 */
0023 
0024 #ifndef SESSION_H
0025 #define SESSION_H
0026 
0027 // Qt
0028 #include <QStringList>
0029 #include <QHash>
0030 #include <QUuid>
0031 #include <QSize>
0032 #include <QProcess>
0033 #include <QWidget>
0034 #include <QUrl>
0035 
0036 // Konsole
0037 #include "konsoleprivate_export.h"
0038 #include "config-konsole.h" //krazy:exclude=includes
0039 #include "Shortcut_p.h"
0040 
0041 class QColor;
0042 
0043 class KConfigGroup;
0044 class KProcess;
0045 
0046 namespace Konsole {
0047 class Emulation;
0048 class Pty;
0049 class ProcessInfo;
0050 class TerminalDisplay;
0051 class ZModemDialog;
0052 class HistoryType;
0053 
0054 /**
0055  * Represents a terminal session consisting of a pseudo-teletype and a terminal emulation.
0056  * The pseudo-teletype (or PTY) handles I/O between the terminal process and Konsole.
0057  * The terminal emulation ( Emulation and subclasses ) processes the output stream from the
0058  * PTY and produces a character image which is then shown on views connected to the session.
0059  *
0060  * Each Session can be connected to one or more views by using the addView() method.
0061  * The attached views can then display output from the program running in the terminal
0062  * or send input to the program in the terminal in the form of keypresses and mouse
0063  * activity.
0064  */
0065 class KONSOLEPRIVATE_EXPORT Session : public QObject
0066 {
0067     Q_OBJECT
0068     Q_CLASSINFO("D-Bus Interface", "org.kde.konsole.Session")
0069 
0070 public:
0071     Q_PROPERTY(QString name READ nameTitle)
0072     Q_PROPERTY(int processId READ processId)
0073     Q_PROPERTY(QString keyBindings READ keyBindings WRITE setKeyBindings)
0074     Q_PROPERTY(QSize size READ size WRITE setSize)
0075 
0076     /**
0077      * Constructs a new session.
0078      *
0079      * To start the terminal process, call the run() method,
0080      * after specifying the program and arguments
0081      * using setProgram() and setArguments()
0082      *
0083      * If no program or arguments are specified explicitly, the Session
0084      * falls back to using the program specified in the SHELL environment
0085      * variable.
0086      */
0087     explicit Session(QObject *parent = nullptr);
0088     ~Session() Q_DECL_OVERRIDE;
0089 
0090     /**
0091      * Connect to an existing terminal.  When a new Session() is constructed it
0092      * automatically searches for and opens a new teletype.  If you want to
0093      * use an existing teletype (given its file descriptor) call this after
0094      * constructing the session.
0095      *
0096      * Calling openTeletype() while a session is running has no effect.
0097      *
0098      * @param fd The file descriptor of the pseudo-teletype master (See KPtyProcess::KPtyProcess())
0099      */
0100     void openTeletype(int fd);
0101 
0102     /**
0103      * Returns true if the session is currently running.  This will be true
0104      * after run() has been called successfully.
0105      */
0106     bool isRunning() const;
0107 
0108     /**
0109      * Adds a new view for this session.
0110      *
0111      * The viewing widget will display the output from the terminal and
0112      * input from the viewing widget (key presses, mouse activity etc.)
0113      * will be sent to the terminal.
0114      *
0115      * Views can be removed using removeView().  The session is automatically
0116      * closed when the last view is removed.
0117      */
0118     void addView(TerminalDisplay *widget);
0119     /**
0120      * Removes a view from this session.  When the last view is removed,
0121      * the session will be closed automatically.
0122      *
0123      * @p widget will no longer display output from or send input
0124      * to the terminal
0125      */
0126     void removeView(TerminalDisplay *widget);
0127 
0128     /**
0129      * Returns the views connected to this session
0130      */
0131     QList<TerminalDisplay *> views() const;
0132 
0133     /**
0134      * Returns the terminal emulation instance being used to encode / decode
0135      * characters to / from the process.
0136      */
0137     Emulation *emulation() const;
0138 
0139     /** Returns the unique ID for this session. */
0140     int sessionId() const;
0141 
0142     /**
0143      * This enum describes the contexts for which separate
0144      * tab title formats may be specified.
0145      */
0146     enum TabTitleContext {
0147         /** Default tab title format */
0148         LocalTabTitle,
0149         /**
0150          * Tab title format used session currently contains
0151          * a connection to a remote computer (via SSH)
0152          */
0153         RemoteTabTitle
0154     };
0155 
0156     /**
0157      * Returns true if the session currently contains a connection to a
0158      * remote computer.  It currently supports ssh.
0159      */
0160     bool isRemote();
0161 
0162     /**
0163      * Sets the format used by this session for tab titles.
0164      *
0165      * @param context The context whose format should be set.
0166      * @param format The tab title format.  This may be a mixture
0167      * of plain text and dynamic elements denoted by a '%' character
0168      * followed by a letter.  (eg. %d for directory).  The dynamic
0169      * elements available depend on the @p context
0170      */
0171     void setTabTitleFormat(TabTitleContext context, const QString &format);
0172     /** Returns the format used by this session for tab titles. */
0173     QString tabTitleFormat(TabTitleContext context) const;
0174 
0175     /**
0176      * Returns true if the tab title has been changed by the user via the
0177      * rename-tab dialog.
0178      */
0179     bool isTabTitleSetByUser() const;
0180 
0181     /** Returns the arguments passed to the shell process when run() is called. */
0182     QStringList arguments() const;
0183     /** Returns the program name of the shell process started when run() is called. */
0184     QString program() const;
0185 
0186     /**
0187      * Sets the command line arguments which the session's program will be passed when
0188      * run() is called.
0189      */
0190     void setArguments(const QStringList &arguments);
0191     /** Sets the program to be executed when run() is called. */
0192     void setProgram(const QString &program);
0193 
0194     /** Returns the session's current working directory. */
0195     QString initialWorkingDirectory()
0196     {
0197         return _initialWorkingDir;
0198     }
0199 
0200     /**
0201      * Sets the initial working directory for the session when it is run
0202      * This has no effect once the session has been started.
0203      */
0204     void setInitialWorkingDirectory(const QString &dir);
0205 
0206     /**
0207      * Returns the current directory of the foreground process in the session
0208      */
0209     QString currentWorkingDirectory();
0210 
0211     /**
0212      * Sets the type of history store used by this session.
0213      * Lines of output produced by the terminal are added
0214      * to the history store.  The type of history store
0215      * used affects the number of lines which can be
0216      * remembered before they are lost and the storage
0217      * (in memory, on-disk etc.) used.
0218      */
0219     void setHistoryType(const HistoryType &hType);
0220     /**
0221      * Returns the type of history store used by this session.
0222      */
0223     const HistoryType &historyType() const;
0224     /**
0225      * Clears the history store used by this session.
0226      */
0227     void clearHistory();
0228 
0229     /**
0230      * Sets the key bindings used by this session.  The bindings
0231      * specify how input key sequences are translated into
0232      * the character stream which is sent to the terminal.
0233      *
0234      * @param name The name of the key bindings to use.  The
0235      * names of available key bindings can be determined using the
0236      * KeyboardTranslatorManager class.
0237      */
0238     void setKeyBindings(const QString &name);
0239     /** Returns the name of the key bindings used by this session. */
0240     QString keyBindings() const;
0241 
0242     /**
0243      * This enum describes the available title roles.
0244      */
0245     enum TitleRole {
0246         /** The name of the session. */
0247         NameRole,
0248         /** The title of the session which is displayed in tabs etc. */
0249         DisplayedTitleRole
0250     };
0251 
0252     /**
0253      * Return the session title set by the user (ie. the program running
0254      * in the terminal), or an empty string if the user has not set a custom title
0255      */
0256     QString userTitle() const;
0257 
0258     /** Convenience method used to read the name property.  Returns title(Session::NameRole). */
0259     QString nameTitle() const
0260     {
0261         return title(Session::NameRole);
0262     }
0263 
0264     /** Returns a title generated from tab format and process information. */
0265     QString getDynamicTitle();
0266 
0267     /** Sets the name of the icon associated with this session. */
0268     void setIconName(const QString &iconName);
0269     /** Returns the name of the icon associated with this session. */
0270     QString iconName() const;
0271 
0272     /** Return URL for the session. */
0273     QUrl getUrl();
0274 
0275     /** Sets the text of the icon associated with this session. */
0276     void setIconText(const QString &iconText);
0277     /** Returns the text of the icon associated with this session. */
0278     QString iconText() const;
0279 
0280     /** Sets the session's title for the specified @p role to @p title. */
0281     void setTitle(TitleRole role, const QString &newTitle);
0282 
0283     /** Returns the session's title for the specified @p role. */
0284     QString title(TitleRole role) const;
0285 
0286     /**
0287      * Specifies whether a utmp entry should be created for the pty used by this session.
0288      * If true, KPty::login() is called when the session is started.
0289      */
0290     void setAddToUtmp(bool);
0291 
0292     /**
0293      * Specifies whether to close the session automatically when the terminal
0294      * process terminates.
0295      */
0296     void setAutoClose(bool close);
0297 
0298     /** See setAutoClose() */
0299     bool autoClose() const;
0300 
0301     /** Returns true if the user has started a program in the session. */
0302     bool isForegroundProcessActive();
0303 
0304     /** Returns the name of the current foreground process. */
0305     QString foregroundProcessName();
0306 
0307     /** Returns the terminal session's window size in lines and columns. */
0308     QSize size();
0309     /**
0310      * Emits a request to resize the session to accommodate
0311      * the specified window size.
0312      *
0313      * @param size The size in lines and columns to request.
0314      */
0315     void setSize(const QSize &size);
0316 
0317     QSize preferredSize() const;
0318 
0319     void setPreferredSize(const QSize &size);
0320 
0321     /**
0322      * Sets whether the session has a dark background or not.  The session
0323      * uses this information to set the COLORFGBG variable in the process's
0324      * environment, which allows the programs running in the terminal to determine
0325      * whether the background is light or dark and use appropriate colors by default.
0326      *
0327      * This has no effect once the session is running.
0328      */
0329     void setDarkBackground(bool darkBackground);
0330 
0331     /**
0332      * Attempts to get the shell program to redraw the current display area.
0333      * This can be used after clearing the screen, for example, to get the
0334      * shell to redraw the prompt line.
0335      */
0336     void refresh();
0337 
0338     void startZModem(const QString &zmodem, const QString &dir, const QStringList &list);
0339     void cancelZModem();
0340     bool isZModemBusy()
0341     {
0342         return _zmodemBusy;
0343     }
0344     void setZModemBusy(bool busy)
0345     {
0346         _zmodemBusy = busy;
0347     }
0348 
0349     /**
0350       * Possible values of the @p what parameter for setSessionAttribute().
0351       * See the "Operating System Commands" section at:
0352       * http://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h2-Operating-System-Commands
0353       */
0354     enum SessionAttributes {
0355         IconNameAndWindowTitle = 0,
0356         IconName = 1,
0357         WindowTitle = 2,
0358         CurrentDirectory = 7,         // From VTE (supposedly 6 was for dir, 7 for file, but whatever)
0359         TextColor = 10,
0360         BackgroundColor = 11,
0361         SessionName = 30,             // Non-standard
0362         SessionIcon = 32,             // Non-standard
0363         ProfileChange = 50            // this clashes with Xterm's font change command
0364     };
0365 
0366     // Sets the text codec used by this sessions terminal emulation.
0367     void setCodec(QTextCodec *codec);
0368 
0369     // session management
0370     void saveSession(KConfigGroup &group);
0371     void restoreSession(KConfigGroup &group);
0372 
0373     void sendSignal(int signal);
0374 
0375     void reportBackgroundColor(const QColor &c);
0376 
0377     bool isReadOnly() const;
0378     void setReadOnly(bool readOnly);
0379 
0380     // Returns true if the current screen is the secondary/alternate one
0381     // or false if it's the primary/normal buffer
0382     bool isPrimaryScreen();
0383 
0384 public Q_SLOTS:
0385 
0386     /**
0387      * Starts the terminal session.
0388      *
0389      * This creates the terminal process and connects the teletype to it.
0390      */
0391     void run();
0392 
0393     /**
0394      * Returns the environment of this session as a list of strings like
0395      * VARIABLE=VALUE
0396      */
0397     Q_SCRIPTABLE QStringList environment() const;
0398 
0399     /**
0400      * Sets the environment for this session.
0401      * @p environment should be a list of strings like
0402      * VARIABLE=VALUE
0403      */
0404     Q_SCRIPTABLE void setEnvironment(const QStringList &environment);
0405 
0406     /**
0407      * Adds one entry for the environment of this session
0408      * @p entry should be like VARIABLE=VALUE
0409      */
0410     void addEnvironmentEntry(const QString &entry);
0411 
0412     /**
0413      * Closes the terminal session. It kills the terminal process by calling
0414      * closeInNormalWay() and, optionally, closeInForceWay().
0415      */
0416     //Q_SCRIPTABLE void close(); // This cause the menu issues bko 185466
0417     void close();
0418 
0419     /**
0420      * Kill the terminal process in normal way. This sends a hangup signal
0421      * (SIGHUP) to the terminal process and causes the finished() signal to
0422      * be emitted. If the process does not respond to the SIGHUP signal then
0423      * the terminal connection (the pty) is closed and Konsole waits for the
0424      * process to exit. This method works most of the time, but fails with some
0425      * programs which respond to SIGHUP signal in special way, such as autossh
0426      * and irssi.
0427      */
0428     bool closeInNormalWay();
0429 
0430     /**
0431      * kill terminal process in force way. This send a SIGKILL signal to the
0432      * terminal process. It should be called only after closeInNormalWay() has
0433      * failed. Take it as last resort.
0434      */
0435     bool closeInForceWay();
0436 
0437     /**
0438      * Changes one of certain session attributes in the terminal emulation
0439      * display. For a list of what may be changed see the
0440      * Emulation::sessionAttributeChanged() signal.
0441      *
0442      * @param what The session attribute being changed, it is one of the
0443      * SessionAttributes enum
0444      * @param caption The text part of the terminal command
0445      */
0446     void setSessionAttribute(int what, const QString &caption);
0447 
0448     /**
0449      * Enables monitoring for activity in the session.
0450      * This will cause notifySessionState() to be emitted
0451      * with the NOTIFYACTIVITY state flag when output is
0452      * received from the terminal.
0453      */
0454     Q_SCRIPTABLE void setMonitorActivity(bool);
0455 
0456     /** Returns true if monitoring for activity is enabled. */
0457     Q_SCRIPTABLE bool isMonitorActivity() const;
0458 
0459     /**
0460      * Enables monitoring for silence in the session.
0461      * This will cause notifySessionState() to be emitted
0462      * with the NOTIFYSILENCE state flag when output is not
0463      * received from the terminal for a certain period of
0464      * time, specified with setMonitorSilenceSeconds()
0465      */
0466     Q_SCRIPTABLE void setMonitorSilence(bool);
0467 
0468     /**
0469      * Returns true if monitoring for inactivity (silence)
0470      * in the session is enabled.
0471      */
0472     Q_SCRIPTABLE bool isMonitorSilence() const;
0473 
0474     /** See setMonitorSilence() */
0475     Q_SCRIPTABLE void setMonitorSilenceSeconds(int seconds);
0476 
0477     /**
0478      * Sets whether flow control is enabled for this terminal
0479      * session.
0480      */
0481     Q_SCRIPTABLE void setFlowControlEnabled(bool enabled);
0482 
0483     /** Returns whether flow control is enabled for this terminal session. */
0484     Q_SCRIPTABLE bool flowControlEnabled() const;
0485 
0486     /**
0487      * @param text to send to the current foreground terminal program.
0488      * @param eol send this after @p text
0489      */
0490     void sendTextToTerminal(const QString &text, const QChar &eol = QChar()) const;
0491 
0492 #if defined(REMOVE_SENDTEXT_RUNCOMMAND_DBUS_METHODS)
0493     void sendText(const QString &text) const;
0494 #else
0495     Q_SCRIPTABLE void sendText(const QString &text) const;
0496 #endif
0497 
0498     /**
0499      * Sends @p command to the current foreground terminal program.
0500      */
0501 #if defined(REMOVE_SENDTEXT_RUNCOMMAND_DBUS_METHODS)
0502     void runCommand(const QString &command) const;
0503 #else
0504     Q_SCRIPTABLE void runCommand(const QString &command) const;
0505 #endif
0506 
0507     /**
0508      * Sends a mouse event of type @p eventType emitted by button
0509      * @p buttons on @p column/@p line to the current foreground
0510      * terminal program
0511      */
0512     Q_SCRIPTABLE void sendMouseEvent(int buttons, int column, int line, int eventType);
0513 
0514     /**
0515     * Returns the process id of the terminal process.
0516     * This is the id used by the system API to refer to the process.
0517     */
0518     Q_SCRIPTABLE int processId() const;
0519 
0520     /**
0521      * Returns the process id of the terminal's foreground process.
0522      * This is initially the same as processId() but can change
0523      * as the user starts other programs inside the terminal.
0524      */
0525     Q_SCRIPTABLE int foregroundProcessId();
0526 
0527     /** Sets the text codec used by this sessions terminal emulation.
0528       * Overloaded to accept a QByteArray for convenience since DBus
0529       * does not accept QTextCodec directly.
0530       */
0531     Q_SCRIPTABLE bool setCodec(const QByteArray &name);
0532 
0533     /** Returns the codec used to decode incoming characters in this
0534      * terminal emulation
0535      */
0536     Q_SCRIPTABLE QByteArray codec();
0537 
0538     /** Sets the session's title for the specified @p role to @p title.
0539      *  This is an overloaded member function for setTitle(TitleRole, QString)
0540      *  provided for convenience since enum data types may not be
0541      *  exported directly through DBus
0542      */
0543     Q_SCRIPTABLE void setTitle(int role, const QString &title);
0544 
0545     /** Returns the session's title for the specified @p role.
0546      * This is an overloaded member function for  setTitle(TitleRole)
0547      * provided for convenience since enum data types may not be
0548      * exported directly through DBus
0549      */
0550     Q_SCRIPTABLE QString title(int role) const;
0551 
0552     /** Returns the "friendly" version of the QUuid of this session.
0553     * This is a QUuid with the braces and dashes removed, so it cannot be
0554     * used to construct a new QUuid. The same text appears in the
0555     * SHELL_SESSION_ID environment variable.
0556     */
0557     Q_SCRIPTABLE QString shellSessionId() const;
0558 
0559     /** Sets the session's tab title format for the specified @p context to @p format.
0560      *  This is an overloaded member function for setTabTitleFormat(TabTitleContext, QString)
0561      *  provided for convenience since enum data types may not be
0562      *  exported directly through DBus
0563      */
0564     Q_SCRIPTABLE void setTabTitleFormat(int context, const QString &format);
0565 
0566     /** Returns the session's tab title format for the specified @p context.
0567      * This is an overloaded member function for tabTitleFormat(TitleRole)
0568      * provided for convenience since enum data types may not be
0569      * exported directly through DBus
0570      */
0571     Q_SCRIPTABLE QString tabTitleFormat(int context) const;
0572 
0573     /**
0574      * Sets the history capacity of this session.
0575      *
0576      * @param lines The history capacity in unit of lines. Its value can be:
0577      * <ul>
0578      * <li> positive integer  -  fixed size history</li>
0579      * <li> 0 -  no history</li>
0580      * <li> negative integer -  unlimited history</li>
0581      * </ul>
0582      */
0583     Q_SCRIPTABLE void setHistorySize(int lines);
0584 
0585     /**
0586      * Returns the history capacity of this session.
0587      */
0588     Q_SCRIPTABLE int historySize() const;
0589 
0590     /**
0591      * Sets the current session's profile
0592      */
0593     Q_SCRIPTABLE void setProfile(const QString &profile);
0594 
0595 Q_SIGNALS:
0596 
0597     /** Emitted when the terminal process starts. */
0598     void started();
0599 
0600     /**
0601      * Emitted when the terminal process exits.
0602      */
0603     void finished();
0604 
0605     /**
0606      * Emitted when one of certain session attributes has been changed.
0607      * See setSessionAttribute().
0608      */
0609     void sessionAttributeChanged();
0610 
0611     /** Emitted when the session gets locked / unlocked. */
0612     void readOnlyChanged();
0613 
0614     /**
0615      * Emitted when the activity state of this session changes.
0616      *
0617      * @param state The new state of the session.  This may be one
0618      * of NOTIFYNORMAL, NOTIFYSILENCE or NOTIFYACTIVITY
0619      */
0620     void stateChanged(int state);
0621 
0622     /**
0623      * Emitted when the current working directory of this session changes.
0624      *
0625      * @param dir The new current working directory of the session.
0626      */
0627     void currentDirectoryChanged(const QString &dir);
0628 
0629     /** Emitted when a bell event occurs in the session. */
0630     void bellRequest(const QString &message);
0631 
0632     /**
0633      * Requests that the color the text for any tabs associated with
0634      * this session should be changed;
0635      *
0636      * TODO: Document what the parameter does
0637      */
0638     void changeTabTextColorRequest(int);
0639 
0640     /**
0641      * Requests that the background color of views on this session
0642      * should be changed.
0643      */
0644     void changeBackgroundColorRequest(const QColor &);
0645     /**
0646      * Requests that the text color of views on this session should
0647      * be changed to @p color.
0648      */
0649     void changeForegroundColorRequest(const QColor &);
0650 
0651     /** TODO: Document me. */
0652     void openUrlRequest(const QString &url);
0653 
0654     /**
0655      * Emitted when the request for data transmission through ZModem
0656      * protocol is detected.
0657      */
0658     void zmodemDownloadDetected();
0659     void zmodemUploadDetected();
0660 
0661     /**
0662      * Emitted when the terminal process requests a change
0663      * in the size of the terminal window.
0664      *
0665      * @param size The requested window size in terms of lines and columns.
0666      */
0667     void resizeRequest(const QSize &size);
0668 
0669     /**
0670      * Emitted when a profile change command is received from the terminal.
0671      *
0672      * @param text The text of the command.  This is a string of the form
0673      * "PropertyName=Value;PropertyName=Value ..."
0674      */
0675     void profileChangeCommandReceived(const QString &text);
0676 
0677     /**
0678      * Emitted when the flow control state changes.
0679      *
0680      * @param enabled True if flow control is enabled or false otherwise.
0681      */
0682     void flowControlEnabledChanged(bool enabled);
0683 
0684     /**
0685      * Emitted when the active screen is switched, to indicate whether the primary
0686      * screen is in use.
0687      *
0688      * This signal serves as a relayer of Emulation::priamyScreenInUse(bool),
0689      * making it usable for higher level component.
0690      */
0691     void primaryScreenInUse(bool use);
0692 
0693     /**
0694      * Emitted when the text selection is changed.
0695      *
0696      * This signal serves as a relayer of Emulation::selectedText(QString),
0697      * making it usable for higher level component.
0698      */
0699     void selectionChanged(const QString &text);
0700 
0701     /**
0702      * Emitted when background request ("\033]11;?\a") terminal code received.
0703      * Terminal is expected send "\033]11;rgb:RRRR/GGGG/BBBB\a" response.
0704      *
0705      * Originally implemented to support vim's background detection feature
0706      * (without explictly setting 'bg=dark' within local/remote vimrc)
0707      */
0708     void getBackgroundColor();
0709 
0710     /**
0711      * Relays the tabRenamedByUser signal from SessionController
0712      */
0713     void tabRenamedByUser(bool renamed) const;
0714 
0715 private Q_SLOTS:
0716     void done(int, QProcess::ExitStatus);
0717 
0718     void fireZModemDownloadDetected();
0719     void fireZModemUploadDetected();
0720 
0721     void onReceiveBlock(const char *buf, int len);
0722     void silenceTimerDone();
0723     void activityTimerDone();
0724 
0725     void onViewSizeChange(int height, int width);
0726 
0727     void activityStateSet(int);
0728 
0729     //automatically detach views from sessions when view is destroyed
0730     void viewDestroyed(QObject *view);
0731 
0732     void zmodemReadStatus();
0733     void zmodemReadAndSendBlock();
0734     void zmodemReceiveBlock(const char *data, int len);
0735     void zmodemFinished();
0736 
0737     void updateFlowControlState(bool suspended);
0738     void updateWindowSize(int lines, int columns);
0739 
0740     // Relays the signal from Emulation and sets _isPrimaryScreen
0741     void onPrimaryScreenInUse(bool use);
0742 
0743     void sessionAttributeRequest(int id);
0744 
0745     void tabTitleSetByUser(bool set);
0746 
0747 private:
0748     Q_DISABLE_COPY(Session)
0749 
0750     // checks that the binary 'program' is available and can be executed
0751     // returns the binary name if available or an empty string otherwise
0752     static QString checkProgram(const QString &program);
0753 
0754     void updateTerminalSize();
0755     WId windowId() const;
0756     bool kill(int signal);
0757     // print a warning message in the terminal.  This is used
0758     // if the program fails to start, or if the shell exits in
0759     // an unsuccessful manner
0760     void terminalWarning(const QString &message);
0761     ProcessInfo *getProcessInfo();
0762     void updateSessionProcessInfo();
0763     bool updateForegroundProcessInfo();
0764     void updateWorkingDirectory();
0765 
0766     QString validDirectory(const QString &dir) const;
0767 
0768     QUuid _uniqueIdentifier;            // SHELL_SESSION_ID
0769 
0770     Pty *_shellProcess;
0771     Emulation *_emulation;
0772 
0773     QList<TerminalDisplay *> _views;
0774 
0775     // monitor activity & silence
0776     bool _monitorActivity;
0777     bool _monitorSilence;
0778     bool _notifiedActivity;
0779     int _silenceSeconds;
0780     QTimer *_silenceTimer;
0781     QTimer *_activityTimer;
0782 
0783     bool _autoClose;
0784     bool _closePerUserRequest;
0785 
0786     QString _nameTitle;
0787     QString _displayTitle;
0788     QString _userTitle;
0789 
0790     QString _localTabTitleFormat;
0791     QString _remoteTabTitleFormat;
0792 
0793     bool _tabTitleSetByUser;
0794 
0795     QString _iconName;
0796     QString _iconText;        // not actually used
0797     bool _addToUtmp;
0798     bool _flowControlEnabled;
0799 
0800     QString _program;
0801     QStringList _arguments;
0802 
0803     QStringList _environment;
0804     int _sessionId;
0805 
0806     QString _initialWorkingDir;
0807     QString _currentWorkingDir;
0808     QUrl _reportedWorkingUrl;
0809 
0810     ProcessInfo *_sessionProcessInfo;
0811     ProcessInfo *_foregroundProcessInfo;
0812     int _foregroundPid;
0813 
0814     // ZModem
0815     bool _zmodemBusy;
0816     KProcess *_zmodemProc;
0817     ZModemDialog *_zmodemProgress;
0818 
0819     bool _hasDarkBackground;
0820 
0821     QSize _preferredSize;
0822 
0823     bool _readOnly;
0824     static int lastSessionId;
0825 
0826     bool _isPrimaryScreen;
0827 };
0828 
0829 /**
0830  * Provides a group of sessions which is divided into master and slave sessions.
0831  * Activity in master sessions can be propagated to all sessions within the group.
0832  * The type of activity which is propagated and method of propagation is controlled
0833  * by the masterMode() flags.
0834  */
0835 class SessionGroup : public QObject
0836 {
0837     Q_OBJECT
0838 
0839 public:
0840     /** Constructs an empty session group. */
0841     explicit SessionGroup(QObject *parent);
0842     /** Destroys the session group and removes all connections between master and slave sessions. */
0843     ~SessionGroup() Q_DECL_OVERRIDE;
0844 
0845     /** Adds a session to the group. */
0846     void addSession(Session *session);
0847     /** Removes a session from the group. */
0848     void removeSession(Session *session);
0849 
0850     /** Returns the list of sessions currently in the group. */
0851     QList<Session *> sessions() const;
0852 
0853     /**
0854      * Sets whether a particular session is a master within the group.
0855      * Changes or activity in the group's master sessions may be propagated
0856      * to all the sessions in the group, depending on the current masterMode()
0857      *
0858      * @param session The session whose master status should be changed.
0859      * @param master True to make this session a master or false otherwise
0860      */
0861     void setMasterStatus(Session *session, bool master);
0862     /** Returns the master status of a session.  See setMasterStatus() */
0863     bool masterStatus(Session *session) const;
0864 
0865     /**
0866      * This enum describes the options for propagating certain activity or
0867      * changes in the group's master sessions to all sessions in the group.
0868      */
0869     enum MasterMode {
0870         /**
0871          * Any input key presses in the master sessions are sent to all
0872          * sessions in the group.
0873          */
0874         CopyInputToAll = 1
0875     };
0876 
0877     /**
0878      * Specifies which activity in the group's master sessions is propagated
0879      * to all sessions in the group.
0880      *
0881      * @param mode A bitwise OR of MasterMode flags.
0882      */
0883     void setMasterMode(int mode);
0884     /**
0885      * Returns a bitwise OR of the active MasterMode flags for this group.
0886      * See setMasterMode()
0887      */
0888     int masterMode() const;
0889 
0890 private Q_SLOTS:
0891     void sessionFinished();
0892     void forwardData(const QByteArray &data);
0893 
0894 private:
0895     QList<Session *> masters() const;
0896 
0897     // maps sessions to their master status
0898     QHash<Session *, bool> _sessions;
0899 
0900     int _masterMode;
0901 };
0902 }
0903 
0904 #endif