| 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 <[email protected]> |
| 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 "HistorySearch.h" |
| 32 | |
| 33 | KSession::KSession(QObject *parent) : |
| 34 | QObject(parent), m_session(createSession("")) |
| 35 | { |
| 36 | connect(m_session, SIGNAL(started()), this, SIGNAL(started())); |
| 37 | connect(m_session, SIGNAL(finished()), this, SLOT(sessionFinished())); |
| 38 | connect(m_session, SIGNAL(titleChanged()), this, SIGNAL(titleChanged())); |
| 39 | } |
| 40 | |
| 41 | KSession::~KSession() |
| 42 | { |
| 43 | if (m_session) { |
| 44 | m_session->close(); |
| 45 | m_session->disconnect(); |
| 46 | delete m_session; |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | void KSession::setTitle(QString name) |
| 51 | { |
| 52 | m_session->setTitle(Session::NameRole, name); |
| 53 | } |
| 54 | |
| 55 | |
| 56 | Session *KSession::createSession(QString name) |
| 57 | { |
| 58 | Session *session = new Session(); |
| 59 | |
| 60 | session->setTitle(Session::NameRole, name); |
| 61 | |
| 62 | /* Thats a freaking bad idea!!!! |
| 63 | * /bin/bash is not there on every system |
| 64 | * better set it to the current $SHELL |
| 65 | * Maybe you can also make a list available and then let the widget-owner decide what to use. |
| 66 | * By setting it to $SHELL right away we actually make the first filecheck obsolete. |
| 67 | * But as iam not sure if you want to do anything else ill just let both checks in and set this to $SHELL anyway. |
| 68 | */ |
| 69 | |
| 70 | //cool-old-term: There is another check in the code. Not sure if useful. |
| 71 | |
| 72 | QString envshell = getenv("SHELL"); |
| 73 | QString shellProg = envshell != NULL ? envshell : "/bin/bash"; |
| 74 | session->setProgram(shellProg); |
| 75 | |
| 76 | setenv("TERM", "xterm", 1); |
| 77 | |
| 78 | //session->setProgram(); |
| 79 | |
| 80 | QStringList args(""); |
| 81 | session->setArguments(args); |
| 82 | session->setAutoClose(true); |
| 83 | |
| 84 | session->setCodec(QTextCodec::codecForName("UTF-8")); |
| 85 | |
| 86 | session->setFlowControlEnabled(true); |
| 87 | session->setHistoryType(HistoryTypeBuffer(1000)); |
| 88 | |
| 89 | session->setDarkBackground(true); |
| 90 | |
| 91 | session->setKeyBindings(""); |
| 92 | |
| 93 | return session; |
| 94 | } |
| 95 | |
| 96 | ///////////////////////////////////////////////////////////////////////////////////// |
| 97 | ///////////////////////////////////////////////////////////////////////////////////// |
| 98 | |
| 99 | |
| 100 | int KSession::getRandomSeed() |
| 101 | { |
| 102 | return m_session->sessionId() * 31; |
| 103 | } |
| 104 | |
| 105 | void KSession::addView(TerminalDisplay *display) |
| 106 | { |
| 107 | m_session->addView(display); |
| 108 | } |
| 109 | |
| 110 | void KSession::removeView(TerminalDisplay *display) |
| 111 | { |
| 112 | m_session->removeView(display); |
| 113 | } |
| 114 | |
| 115 | void KSession::sessionFinished() |
| 116 | { |
| 117 | emit finished(); |
| 118 | } |
| 119 | |
| 120 | void KSession::selectionChanged(bool textSelected) |
| 121 | { |
| 122 | Q_UNUSED(textSelected) |
| 123 | } |
| 124 | |
| 125 | void KSession::startShellProgram() |
| 126 | { |
| 127 | if ( m_session->isRunning() ) { |
| 128 | return; |
| 129 | } |
| 130 | |
| 131 | m_session->run(); |
| 132 | } |
| 133 | |
| 134 | bool KSession::sendSignal(int signal) |
| 135 | { |
| 136 | if ( !m_session->isRunning() ) { |
| 137 | return false; |
| 138 | } |
| 139 | |
| 140 | return m_session->sendSignal(signal); |
| 141 | } |
| 142 | |
| 143 | int KSession::getShellPID() |
| 144 | { |
| 145 | return m_session->processId(); |
| 146 | } |
| 147 | |
| 148 | void KSession::changeDir(const QString &dir) |
| 149 | { |
| 150 | /* |
| 151 | this is a very hackish way of trying to determine if the shell is in |
| 152 | the foreground before attempting to change the directory. It may not |
| 153 | be portable to anything other than Linux. |
| 154 | */ |
| 155 | QString strCmd; |
| 156 | strCmd.setNum(getShellPID()); |
| 157 | strCmd.prepend("ps -j "); |
| 158 | strCmd.append(" | tail -1 | awk '{ print $5 }' | grep -q \\+"); |
| 159 | int retval = system(strCmd.toStdString().c_str()); |
| 160 | |
| 161 | if (!retval) { |
| 162 | QString cmd = "cd " + dir + "\n"; |
| 163 | sendText(cmd); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | void KSession::setEnvironment(const QStringList &environment) |
| 168 | { |
| 169 | m_session->setEnvironment(environment); |
| 170 | } |
| 171 | |
| 172 | |
| 173 | void KSession::setShellProgram(const QString &progname) |
| 174 | { |
| 175 | m_session->setProgram(progname); |
| 176 | } |
| 177 | |
| 178 | void KSession::setInitialWorkingDirectory(const QString &dir) |
| 179 | { |
| 180 | if ( _initialWorkingDirectory != dir ) { |
| 181 | _initialWorkingDirectory = dir; |
| 182 | m_session->setInitialWorkingDirectory(dir); |
| 183 | emit initialWorkingDirectoryChanged(); |
| 184 | } } |
| 185 | |
| 186 | QString KSession::getInitialWorkingDirectory() |
| 187 | { |
| 188 | return _initialWorkingDirectory; |
| 189 | } |
| 190 | |
| 191 | void KSession::setArgs(const QStringList &args) |
| 192 | { |
| 193 | m_session->setArguments(args); |
| 194 | } |
| 195 | |
| 196 | void KSession::setTextCodec(QTextCodec *codec) |
| 197 | { |
| 198 | m_session->setCodec(codec); |
| 199 | } |
| 200 | |
| 201 | void KSession::setHistorySize(int lines) |
| 202 | { |
| 203 | if ( historySize() != lines ) { |
| 204 | if (lines < 0) |
| 205 | m_session->setHistoryType(HistoryTypeFile()); |
| 206 | else |
| 207 | m_session->setHistoryType(HistoryTypeBuffer(lines)); |
| 208 | emit historySizeChanged(); |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | int KSession::historySize() const |
| 213 | { |
| 214 | if ( m_session->historyType().isUnlimited() ) { |
| 215 | return -1; |
| 216 | } else { |
| 217 | return m_session->historyType().maximumLineCount(); |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | QString KSession::getHistory() const |
| 222 | { |
| 223 | QString history; |
| 224 | QTextStream historyStream(&history); |
| 225 | PlainTextDecoder historyDecoder; |
| 226 | |
| 227 | historyDecoder.begin(&historyStream); |
| 228 | m_session->emulation()->writeToStream(&historyDecoder); |
| 229 | historyDecoder.end(); |
| 230 | |
| 231 | return history; |
| 232 | } |
| 233 | |
| 234 | void KSession::sendText(QString text) |
| 235 | { |
| 236 | m_session->sendText(text); |
| 237 | } |
| 238 | |
| 239 | void KSession::sendKey(int rep, int key, int mod) const |
| 240 | { |
| 241 | //TODO implement or remove this function. |
| 242 | // Qt::KeyboardModifier kbm = Qt::KeyboardModifier(mod); |
| 243 | |
| 244 | // QKeyEvent qkey(QEvent::KeyPress, key, kbm); |
| 245 | |
| 246 | // while (rep > 0){ |
| 247 | // m_session->sendKey(&qkey); |
| 248 | // --rep; |
| 249 | // } |
| 250 | } |
| 251 | |
| 252 | void KSession::clearScreen() |
| 253 | { |
| 254 | m_session->emulation()->clearEntireScreen(); |
| 255 | } |
| 256 | |
| 257 | void KSession::search(const QString ®exp, int startLine, int startColumn, bool forwards) |
| 258 | { |
| 259 | HistorySearch *history = new HistorySearch( QPointer<Emulation>(m_session->emulation()), QRegExp(regexp), forwards, startColumn, startLine, this); |
| 260 | connect( history, SIGNAL(matchFound(int,int,int,int)), this, SIGNAL(matchFound(int,int,int,int))); |
| 261 | connect( history, SIGNAL(noMatchFound()), this, SIGNAL(noMatchFound())); |
| 262 | history->search(); |
| 263 | } |
| 264 | |
| 265 | void KSession::setFlowControlEnabled(bool enabled) |
| 266 | { |
| 267 | m_session->setFlowControlEnabled(enabled); |
| 268 | } |
| 269 | |
| 270 | bool KSession::flowControlEnabled() |
| 271 | { |
| 272 | return m_session->flowControlEnabled(); |
| 273 | } |
| 274 | |
| 275 | void KSession::setKeyBindings(const QString &kb) |
| 276 | { |
| 277 | m_session->setKeyBindings(kb); |
| 278 | emit changedKeyBindings(kb); |
| 279 | } |
| 280 | |
| 281 | QString KSession::getKeyBindings() |
| 282 | { |
| 283 | return m_session->keyBindings(); |
| 284 | } |
| 285 | |
| 286 | QStringList KSession::availableKeyBindings() |
| 287 | { |
| 288 | return KeyboardTranslatorManager::instance()->allTranslators(); |
| 289 | } |
| 290 | |
| 291 | QString KSession::keyBindings() |
| 292 | { |
| 293 | return m_session->keyBindings(); |
| 294 | } |
| 295 | |
| 296 | QString KSession::getTitle() |
| 297 | { |
| 298 | return m_session->userTitle(); |
| 299 | } |
| 300 | |
| 301 | bool KSession::hasActiveProcess() const |
| 302 | { |
| 303 | return m_session->processId() != m_session->foregroundProcessId(); |
| 304 | } |
| 305 | |
| 306 | QString KSession::foregroundProcessName() |
| 307 | { |
| 308 | return m_session->foregroundProcessName(); |
| 309 | } |
| 310 | |
| 311 | QString KSession::currentDir() |
| 312 | { |
| 313 | return m_session->currentDir(); |
| 314 | } |