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>
21 using namespace boost
;
23 Engine::Engine(const QString
& path
, const QStringList
& arguments
)
25 , m_arguments(arguments
)
27 connect(&m_engine
, SIGNAL(readyReadStandardOutput()), this, SLOT(processInput()));
28 connect(&m_engine
, SIGNAL(started()), this, SLOT(engineRunning()));
29 connect(&m_engine
, SIGNAL(finished(int, QProcess::ExitStatus
)),
30 this, SIGNAL(lostConnection()));
35 void Engine::start() {
36 if (!m_workPath
.isNull())
37 m_engine
.setWorkingDirectory(m_workPath
);
39 m_engine
.setOutputChannelMode(KProcess::OnlyStdoutChannel
);
40 m_engine
.setProgram(m_path
, m_arguments
);
45 void Engine::engineRunning() {
46 Q_ASSERT(m_engine
.state() == QProcess::Running
);
47 while (!m_command_queue
.empty()) {
48 sendCommand(m_command_queue
.front(), false);
49 m_command_queue
.pop();
53 void Engine::sendCommand(const QString
& command
, bool echo
) {
54 if (echo
&& m_console
)
55 m_console
->echo(command
);
57 if (m_engine
.state() == QProcess::Running
) {
58 QTextStream
os(&m_engine
);
59 os
<< command
<< "\n";
65 std::cout
<< "> " << command
<< std::endl
;
69 m_command_queue
.push(command
);
73 void Engine::processInput() {
74 // process only full lines
75 while (m_engine
.canReadLine()) {
76 QString line
= m_engine
.readLine();
77 line
.remove("\n").remove("\r");
83 std::cout
<< "< " << line
<< std::endl
;
86 m_console
->displayText(line
+ "\n", 0);
87 receivedCommand(line
);
91 void Engine::textNotify(const QString
& text
) {
92 sendCommand(text
, false);
96 void Engine::setNotifier(const shared_ptr
<EngineNotifier
>& notifier
) {
97 m_notifier
= notifier
;
100 void Engine::setConsole(const shared_ptr
<Console
>& console
) {