2 Copyright (c) 2006 Paolo Capriotti <p.capriotti@gmail.com>
3 (c) 2006 Maurizio Monge <maurizio.monge@kdemail.net>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
13 #include <QTextStream>
20 using namespace boost
;
22 Engine::Engine(const QString
& path
, const QStringList
& arguments
)
24 , m_arguments(arguments
)
26 connect(&m_engine
, SIGNAL(readyReadStandardOutput()), this, SLOT(processInput()));
27 connect(&m_engine
, SIGNAL(started()), this, SLOT(engineRunning()));
28 connect(&m_engine
, SIGNAL(finished(int, QProcess::ExitStatus
)),
29 this, SIGNAL(lostConnection()));
34 void Engine::start() {
35 if (!m_workPath
.isNull())
36 m_engine
.setWorkingDirectory(m_workPath
);
38 m_engine
.setOutputChannelMode(KProcess::OnlyStdoutChannel
);
39 m_engine
.setProgram(m_path
, m_arguments
);
44 void Engine::engineRunning() {
45 Q_ASSERT(m_engine
.state() == QProcess::Running
);
46 while (!m_command_queue
.empty()) {
47 sendCommand(m_command_queue
.front(), false);
48 m_command_queue
.pop();
52 void Engine::sendCommand(const QString
& command
, bool echo
) {
53 if (echo
&& m_console
)
54 m_console
->echo(command
);
56 if (m_engine
.state() == QProcess::Running
) {
57 QTextStream
os(&m_engine
);
58 os
<< command
<< "\n";
60 kDebug() << "" << m_side
<< ">" << command
;
64 m_command_queue
.push(command
);
68 void Engine::processInput() {
69 // process only full lines
70 while (m_engine
.canReadLine()) {
71 QString line
= m_engine
.readLine();
72 line
.remove("\n").remove("\r");
74 kDebug() << m_side
<< "<" << line
;
77 m_console
->displayText(line
+ "\n", 0);
78 receivedCommand(line
);
82 void Engine::textNotify(const QString
& text
) {
83 sendCommand(text
, false);
87 void Engine::setNotifier(const shared_ptr
<EngineNotifier
>& notifier
) {
88 m_notifier
= notifier
;
91 void Engine::setConsole(const shared_ptr
<Console
>& console
) {