File indexing completed on 2024-05-19 05:28:21

0001 /*
0002     This file is part of Konsole QML plugin,
0003     which is a terminal emulator from KDE.
0004 
0005     SPDX-FileCopyrightText: 2013 Dmitry Zagnoyko <hiroshidi@gmail.com>
0006 
0007     SPDX-License-Identifier: GPL-2.0-or-later
0008 
0009     This program is distributed in the hope that it will be useful,
0010     but WITHOUT ANY WARRANTY; without even the implied warranty of
0011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0012     GNU General Public License for more details.
0013 
0014     You should have received a copy of the GNU General Public License
0015     along with this program; if not, write to the Free Software
0016     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
0017     02110-1301  USA.
0018 */
0019 
0020 #ifndef KSESSION_H
0021 #define KSESSION_H
0022 
0023 #include <QObject>
0024 
0025 // Konsole
0026 #include "Session.h"
0027 
0028 using namespace Konsole;
0029 
0030 class TerminalSession : public QObject
0031 {
0032     Q_OBJECT
0033     Q_PROPERTY(QString kbScheme READ getKeyBindings WRITE setKeyBindings NOTIFY changedKeyBindings)
0034     Q_PROPERTY(QString initialWorkingDirectory READ getInitialWorkingDirectory WRITE setInitialWorkingDirectory NOTIFY initialWorkingDirectoryChanged)
0035     Q_PROPERTY(QString title READ getTitle WRITE setTitle NOTIFY titleChanged)
0036     Q_PROPERTY(QString shellProgram READ shellProgram WRITE setShellProgram NOTIFY shellProgramChanged)
0037     Q_PROPERTY(QStringList shellProgramArgs READ args WRITE setArgs NOTIFY argsChanged)
0038     Q_PROPERTY(QString history READ getHistory)
0039     Q_PROPERTY(bool hasActiveProcess READ hasActiveProcess)
0040     Q_PROPERTY(QString foregroundProcessName READ foregroundProcessName)
0041     Q_PROPERTY(QString currentDir READ currentDir)
0042 
0043 public:
0044     TerminalSession(QObject *parent = nullptr);
0045     ~TerminalSession() override;
0046 
0047 public:
0048     // bool setup();
0049     void addView(TerminalDisplay *display);
0050     void removeView(TerminalDisplay *display);
0051 
0052     int getRandomSeed();
0053     QString getKeyBindings();
0054 
0055     // look-n-feel, if you don`t like defaults
0056 
0057     // environment
0058     void setEnvironment(const QStringList &environment);
0059 
0060     // Initial working directory
0061     void setInitialWorkingDirectory(const QString &dir);
0062     QString getInitialWorkingDirectory();
0063 
0064     // Text codec, default is UTF-8
0065     void setTextCodec(QTextCodec *codec);
0066 
0067     // History size for scrolling
0068     void setHistorySize(int lines); // infinite if lines < 0
0069     int historySize() const;
0070 
0071     QString getHistory() const;
0072 
0073     // Sets whether flow control is enabled
0074     void setFlowControlEnabled(bool enabled);
0075 
0076     // Returns whether flow control is enabled
0077     bool flowControlEnabled(void);
0078 
0079     /**
0080      * Sets whether the flow control warning box should be shown
0081      * when the flow control stop key (Ctrl+S) is pressed.
0082      */
0083     // void setFlowControlWarningEnabled(bool enabled);
0084 
0085     /*! Get all available keyboard bindings
0086      */
0087     static QStringList availableKeyBindings();
0088 
0089     //! Return current key bindings
0090     QString keyBindings();
0091 
0092     QString getTitle();
0093 
0094     /**
0095      * Returns \c true if the session has an active subprocess running in it
0096      * spawned from the initial shell.
0097      */
0098     bool hasActiveProcess() const;
0099 
0100     /**
0101      * Returns the name of the terminal's foreground process.
0102      */
0103     QString foregroundProcessName();
0104 
0105     /**
0106      * Returns the current working directory of the process.
0107      */
0108     QString currentDir();
0109 
0110 Q_SIGNALS:
0111     void started();
0112     void finished();
0113     void copyAvailable(bool);
0114 
0115     void termGetFocus();
0116     void termLostFocus();
0117 
0118     void termKeyPressed(QKeyEvent *, bool);
0119 
0120     void changedKeyBindings(QString kb);
0121 
0122     void titleChanged();
0123 
0124     void historySizeChanged();
0125 
0126     void initialWorkingDirectoryChanged();
0127 
0128     void matchFound(int startColumn, int startLine, int endColumn, int endLine);
0129     void noMatchFound();
0130 
0131     void shellProgramChanged();
0132     void argsChanged();
0133 
0134 public Q_SLOTS:
0135     /*! Set named key binding for given widget
0136      */
0137     void setKeyBindings(const QString &kb);
0138     void setTitle(QString name);
0139 
0140     void startShellProgram();
0141 
0142     bool sendSignal(int signal);
0143 
0144     //  Shell program, default is /bin/bash
0145     QString shellProgram() const;
0146     void setShellProgram(const QString &progname);
0147 
0148     // Shell program args, default is none
0149     QStringList args() const;
0150     void setArgs(const QStringList &args);
0151 
0152     int getShellPID();
0153     void changeDir(const QString &dir);
0154 
0155     // Send some text to terminal
0156     void sendText(QString text);
0157     // Send some text to terminal
0158     void sendKey(int rep, int key, int mod) const;
0159 
0160     void clearScreen();
0161 
0162     // Search history
0163     void search(const QString &regexp, int startLine = 0, int startColumn = 0, bool forwards = true);
0164 
0165     void selectionChanged(bool textSelected);
0166 
0167 protected Q_SLOTS:
0168     void sessionFinished();
0169 
0170 private Q_SLOTS:
0171     std::unique_ptr<Session> createSession(QString name);
0172 
0173 private:
0174     QString _initialWorkingDirectory;
0175     std::unique_ptr<Session> m_session;
0176 };
0177 
0178 #endif // KSESSION_H