~larryprice/acolyterm/release-0.1

« back to all changes in this revision

Viewing changes to src/plugin/konsole/ksession.cpp

  • Committer: Larry Price
  • Date: 2016-06-15 14:47:59 UTC
  • Revision ID: larry.price@canonical.com-20160615144759-6wopn0gxwgta3x1n
Updating QMLTermWidget and removing unnecessary konsole codebase

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
    This file is part of Konsole QML plugin,
3
 
    which is a terminal emulator from KDE.
4
 
 
5
 
    Copyright 2013      by Dmitry Zagnoyko <hiroshidi@gmail.com>
6
 
 
7
 
    This program is free software; you can redistribute it and/or modify
8
 
    it under the terms of the GNU General Public License as published by
9
 
    the Free Software Foundation; either version 2 of the License, or
10
 
    (at your option) any later version.
11
 
 
12
 
    This program is distributed in the hope that it will be useful,
13
 
    but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 
    GNU General Public License for more details.
16
 
 
17
 
    You should have received a copy of the GNU General Public License
18
 
    along with this program; if not, write to the Free Software
19
 
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20
 
    02110-1301  USA.
21
 
*/
22
 
 
23
 
// Own
24
 
#include "ksession.h"
25
 
 
26
 
// Qt
27
 
#include <QTextCodec>
28
 
 
29
 
// Konsole
30
 
#include "KeyboardTranslator.h"
31
 
#include "TerminalDisplay.h"
32
 
 
33
 
 
34
 
KSession::KSession(QObject *parent) :
35
 
    QObject(parent), m_session(createSession("KSession"))
36
 
{
37
 
    connect(m_session, SIGNAL(finished()), this, SLOT(sessionFinished()));
38
 
 
39
 
    m_session->run();
40
 
}
41
 
 
42
 
KSession::~KSession()
43
 
{
44
 
    if (m_session) {
45
 
        m_session->close();
46
 
        m_session->disconnect();
47
 
        delete m_session;
48
 
    }
49
 
}
50
 
 
51
 
void KSession::setTitle(QString name)
52
 
{
53
 
    m_session->setTitle(Session::NameRole, name);
54
 
}
55
 
 
56
 
 
57
 
Session *KSession::createSession(QString name)
58
 
{
59
 
    Session *session = new Session();
60
 
 
61
 
    session->setTitle(Session::NameRole, name);
62
 
 
63
 
    /* Thats a freaking bad idea!!!!
64
 
     * /bin/bash is not there on every system
65
 
     * better set it to the current $SHELL
66
 
     * Maybe you can also make a list available and then let the widget-owner decide what to use.
67
 
     * By setting it to $SHELL right away we actually make the first filecheck obsolete.
68
 
     * But as iam not sure if you want to do anything else ill just let both checks in and set this to $SHELL anyway.
69
 
     */
70
 
    session->setProgram("/bin/bash");
71
 
 
72
 
    //session->setProgram(getenv("SHELL"));
73
 
 
74
 
    QStringList args("");
75
 
    session->setArguments(args);
76
 
    session->setAutoClose(true);
77
 
 
78
 
    session->setCodec(QTextCodec::codecForName("UTF-8"));
79
 
 
80
 
    session->setFlowControlEnabled(true);
81
 
    session->setHistoryType(HistoryTypeBuffer(1000));
82
 
 
83
 
    session->setDarkBackground(true);
84
 
 
85
 
    session->setKeyBindings("");
86
 
 
87
 
    return session;
88
 
}
89
 
 
90
 
/////////////////////////////////////////////////////////////////////////////////////
91
 
/////////////////////////////////////////////////////////////////////////////////////
92
 
 
93
 
 
94
 
int  KSession::getRandomSeed()
95
 
{
96
 
    return m_session->sessionId() * 31;
97
 
}
98
 
 
99
 
void  KSession::addView(KTerminalDisplay *displa)
100
 
{
101
 
    m_session->addView(displa);
102
 
}
103
 
 
104
 
void KSession::sessionFinished()
105
 
{
106
 
    emit finished();
107
 
}
108
 
 
109
 
void KSession::selectionChanged(bool textSelected)
110
 
{
111
 
    Q_UNUSED(textSelected)
112
 
}
113
 
 
114
 
void KSession::startShellProgram()
115
 
{
116
 
    if ( m_session->isRunning() ) {
117
 
        return;
118
 
    }
119
 
 
120
 
    m_session->run();
121
 
}
122
 
 
123
 
int KSession::getShellPID()
124
 
{
125
 
    return m_session->processId();
126
 
}
127
 
 
128
 
void KSession::changeDir(const QString &dir)
129
 
{
130
 
    /*
131
 
       this is a very hackish way of trying to determine if the shell is in
132
 
       the foreground before attempting to change the directory.  It may not
133
 
       be portable to anything other than Linux.
134
 
    */
135
 
    QString strCmd;
136
 
    strCmd.setNum(getShellPID());
137
 
    strCmd.prepend("ps -j ");
138
 
    strCmd.append(" | tail -1 | awk '{ print $5 }' | grep -q \\+");
139
 
    int retval = system(strCmd.toStdString().c_str());
140
 
 
141
 
    if (!retval) {
142
 
        QString cmd = "cd " + dir + "\n";
143
 
        sendText(cmd);
144
 
    }
145
 
}
146
 
 
147
 
void KSession::setEnvironment(const QStringList &environment)
148
 
{
149
 
    m_session->setEnvironment(environment);
150
 
}
151
 
 
152
 
 
153
 
void KSession::setShellProgram(const QString &progname)
154
 
{
155
 
    if (!m_session)
156
 
        return;
157
 
 
158
 
    m_session->setProgram(progname);
159
 
}
160
 
 
161
 
void KSession::setWorkingDirectory(const QString &dir)
162
 
{
163
 
    if (!m_session)
164
 
        return;
165
 
 
166
 
    m_session->setInitialWorkingDirectory(dir);
167
 
}
168
 
 
169
 
void KSession::setArgs(QStringList &args)
170
 
{
171
 
    if (!m_session)
172
 
        return;
173
 
 
174
 
    m_session->setArguments(args);
175
 
}
176
 
 
177
 
void KSession::setTextCodec(QTextCodec *codec)
178
 
{
179
 
    if (!m_session)
180
 
        return;
181
 
 
182
 
    m_session->setCodec(codec);
183
 
}
184
 
 
185
 
void KSession::setHistorySize(int lines)
186
 
{
187
 
    if (lines < 0)
188
 
        m_session->setHistoryType(HistoryTypeFile());
189
 
    else
190
 
        m_session->setHistoryType(HistoryTypeBuffer(lines));
191
 
}
192
 
 
193
 
void KSession::sendText(QString text)
194
 
{
195
 
    m_session->sendText(text);
196
 
}
197
 
 
198
 
void KSession::sendKey(int rep, int key, int mod) const
199
 
{
200
 
    Qt::KeyboardModifier kbm = Qt::KeyboardModifier(mod);
201
 
 
202
 
    QKeyEvent qkey(QEvent::KeyPress, key, kbm);
203
 
 
204
 
    while (rep > 0){
205
 
        m_session->sendKey(&qkey);
206
 
        --rep;
207
 
    }
208
 
}
209
 
 
210
 
void KSession::setFlowControlEnabled(bool enabled)
211
 
{
212
 
    m_session->setFlowControlEnabled(enabled);
213
 
}
214
 
 
215
 
bool KSession::flowControlEnabled()
216
 
{
217
 
    return m_session->flowControlEnabled();
218
 
}
219
 
 
220
 
void KSession::setKeyBindings(const QString &kb)
221
 
{
222
 
    m_session->setKeyBindings(kb);
223
 
    emit changedKeyBindings(kb);
224
 
}
225
 
 
226
 
QString KSession::getKeyBindings()
227
 
{
228
 
   return m_session->keyBindings();
229
 
}
230
 
 
231
 
 
232
 
QStringList KSession::availableKeyBindings()
233
 
{
234
 
    return KeyboardTranslatorManager::instance()->allTranslators();
235
 
}
236
 
 
237
 
QString KSession::keyBindings()
238
 
{
239
 
    return m_session->keyBindings();
240
 
}
241
 
 
242