File indexing completed on 2024-04-21 05:51:10

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