File: TerminalSession.cpp

package info (click to toggle)
qmlkonsole 25.12.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,680 kB
  • sloc: cpp: 12,794; xml: 345; makefile: 3; sh: 1
file content (321 lines) | stat: -rw-r--r-- 8,036 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*
    This file is part of Konsole QML plugin,
    which is a terminal emulator from KDE.

    SPDX-FileCopyrightText: 2013 Dmitry Zagnoyko <hiroshidi@gmail.com>

    SPDX-License-Identifier: GPL-2.0-or-later

    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.
*/

// Own
#include "TerminalSession.h"

// Qt
#include <QTextCodec>

// Konsole
#include "HistorySearch.h"
#include "KeyboardTranslator.h"

TerminalSession::TerminalSession(QObject *parent)
    : QObject(parent)
    , m_session(createSession(QString()))
{
    connect(m_session.get(), &Konsole::Session::started, this, &TerminalSession::started);
    connect(m_session.get(), &Konsole::Session::finished, this, &TerminalSession::sessionFinished);
    connect(m_session.get(), &Konsole::Session::titleChanged, this, &TerminalSession::titleChanged);
}

TerminalSession::~TerminalSession()
{
    if (m_session) {
        m_session->close();
        m_session->disconnect();
    }
}

void TerminalSession::setTitle(QString name)
{
    m_session->setTitle(Session::NameRole, name);
}

std::unique_ptr<Session> TerminalSession::createSession(QString name)
{
    auto session = std::make_unique<Session>();

    session->setTitle(Session::NameRole, name);

    /* Thats a freaking bad idea!!!!
     * /bin/bash is not there on every system
     * better set it to the current $SHELL
     * Maybe you can also make a list available and then let the widget-owner decide what to use.
     * By setting it to $SHELL right away we actually make the first filecheck obsolete.
     * But as iam not sure if you want to do anything else ill just let both checks in and set this to $SHELL anyway.
     */

    // cool-old-term: There is another check in the code. Not sure if useful.

    QString envshell = qEnvironmentVariable("SHELL");
    QString shellProg = !envshell.isNull() ? envshell : QStringLiteral("/bin/bash");
    session->setProgram(shellProg);

    setenv("TERM", "xterm", 1);

    // session->setProgram();

    QStringList args;
    session->setArguments(args);
    session->setAutoClose(true);

    session->setCodec(QTextCodec::codecForName("UTF-8"));

    session->setFlowControlEnabled(true);
    session->setHistoryType(HistoryTypeBuffer(1000));

    session->setDarkBackground(true);

    session->setKeyBindings(QString());

    return session;
}

/////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////

int TerminalSession::getRandomSeed()
{
    return m_session->sessionId() * 31;
}

void TerminalSession::addView(TerminalDisplay *display)
{
    m_session->setView(display);
}

void TerminalSession::removeView(TerminalDisplay *display)
{
    m_session->removeView(display);
}

void TerminalSession::sessionFinished()
{
    Q_EMIT finished();
}

void TerminalSession::selectionChanged(bool textSelected)
{
    Q_UNUSED(textSelected)
}

void TerminalSession::startShellProgram()
{
    if (m_session->isRunning()) {
        return;
    }

    m_session->run();
}

bool TerminalSession::sendSignal(int signal)
{
    if (!m_session->isRunning()) {
        return false;
    }

    return m_session->sendSignal(signal);
}

int TerminalSession::getShellPID()
{
    return m_session->processId();
}

void TerminalSession::changeDir(const QString &dir)
{
    /*
       this is a very hackish way of trying to determine if the shell is in
       the foreground before attempting to change the directory.  It may not
       be portable to anything other than Linux.
    */
    QString strCmd;
    strCmd.setNum(getShellPID());
    strCmd.prepend(u"ps -j ");
    strCmd.append(u" | tail -1 | awk '{ print $5 }' | grep -q \\+");
    int retval = system(strCmd.toStdString().c_str());

    if (!retval) {
        QString cmd = QStringLiteral(u"cd ") + dir + QStringLiteral("\n");
        sendText(cmd);
    }
}

void TerminalSession::setEnvironment(const QStringList &environment)
{
    m_session->setEnvironment(environment);
}

QString TerminalSession::shellProgram() const
{
    return m_session->program();
}

void TerminalSession::setShellProgram(const QString &progname)
{
    m_session->setProgram(progname);
    Q_EMIT shellProgramChanged();
}

void TerminalSession::setInitialWorkingDirectory(const QString &dir)
{
    if (_initialWorkingDirectory != dir) {
        _initialWorkingDirectory = dir;
        m_session->setInitialWorkingDirectory(dir);
        Q_EMIT initialWorkingDirectoryChanged();
    }
}

QString TerminalSession::getInitialWorkingDirectory()
{
    return _initialWorkingDirectory;
}

QStringList TerminalSession::args() const
{
    return m_session->arguments();
}

void TerminalSession::setArgs(const QStringList &args)
{
    m_session->setArguments(args);
    Q_EMIT argsChanged();
}

void TerminalSession::setTextCodec(QTextCodec *codec)
{
    m_session->setCodec(codec);
}

void TerminalSession::setHistorySize(int lines)
{
    if (historySize() != lines) {
        if (lines < 0)
            m_session->setHistoryType(HistoryTypeFile());
        else
            m_session->setHistoryType(HistoryTypeBuffer(lines));
        Q_EMIT historySizeChanged();
    }
}

int TerminalSession::historySize() const
{
    if (m_session->historyType().isUnlimited()) {
        return -1;
    } else {
        return m_session->historyType().maximumLineCount();
    }
}

QString TerminalSession::getHistory() const
{
    QString history;
    QTextStream historyStream(&history);
    PlainTextDecoder historyDecoder;

    historyDecoder.begin(&historyStream);
    m_session->emulation()->writeToStream(&historyDecoder);
    historyDecoder.end();

    return history;
}

void TerminalSession::sendText(QString text)
{
    m_session->sendText(text);
}

void TerminalSession::sendKey(int, int, int) const
{
    // TODO implement or remove this function.
    //    Qt::KeyboardModifier kbm = Qt::KeyboardModifier(mod);

    //    QKeyEvent qkey(QEvent::KeyPress, key, kbm);

    //    while (rep > 0){
    //        m_session->sendKey(&qkey);
    //        --rep;
    //    }
}

void TerminalSession::clearScreen()
{
    m_session->emulation()->clearEntireScreen();
}

void TerminalSession::search(const QString &regexp, int startLine, int startColumn, bool forwards)
{
    HistorySearch *history = new HistorySearch(QPointer<Emulation>(m_session->emulation()), QRegExp(regexp), forwards, startColumn, startLine, this);
    connect(history, &HistorySearch::matchFound, this, &TerminalSession::matchFound);
    connect(history, &HistorySearch::noMatchFound, this, &TerminalSession::noMatchFound);
    history->search();
}

void TerminalSession::setFlowControlEnabled(bool enabled)
{
    m_session->setFlowControlEnabled(enabled);
}

bool TerminalSession::flowControlEnabled()
{
    return m_session->flowControlEnabled();
}

void TerminalSession::setKeyBindings(const QString &kb)
{
    m_session->setKeyBindings(kb);
    Q_EMIT changedKeyBindings(kb);
}

QString TerminalSession::getKeyBindings()
{
    return m_session->keyBindings();
}

QStringList TerminalSession::availableKeyBindings()
{
    return KeyboardTranslatorManager::instance()->allTranslators();
}

QString TerminalSession::keyBindings()
{
    return m_session->keyBindings();
}

QString TerminalSession::getTitle()
{
    return m_session->userTitle();
}

bool TerminalSession::hasActiveProcess() const
{
    return m_session->processId() != m_session->foregroundProcessId();
}

QString TerminalSession::foregroundProcessName()
{
    return m_session->foregroundProcessName();
}

QString TerminalSession::currentDir()
{
    return m_session->currentDir();
}