1 /***************************************************************************
2 * Copyright (C) 2005-09 by the Quassel Project *
3 * devel@quassel-irc.org *
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) version 3. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
23 #include "execwrapper.h"
26 #include "messagemodel.h"
29 ExecWrapper::ExecWrapper(QObject* parent) : QObject(parent) {
30 connect(&_process, SIGNAL(readyReadStandardOutput()), SLOT(processReadStdout()));
31 connect(&_process, SIGNAL(readyReadStandardError()), SLOT(processReadStderr()));
32 connect(&_process, SIGNAL(finished(int, QProcess::ExitStatus)), SLOT(processFinished(int, QProcess::ExitStatus)));
33 connect(&_process, SIGNAL(error(QProcess::ProcessError)), SLOT(processError(QProcess::ProcessError)));
35 connect(this, SIGNAL(stdout(QString)), SLOT(postStdout(QString)));
36 connect(this, SIGNAL(stderr(QString)), SLOT(postStderr(QString)));
39 void ExecWrapper::start(const BufferInfo &info, const QString &scriptName, const QStringList& params) {
41 _scriptName = scriptName;
42 foreach(QString scriptDir, Quassel::scriptDirPaths()) {
43 QString fileName = scriptDir + '/' + scriptName;
44 if(!QFile::exists(fileName))
46 _process.start(fileName, params);
49 emit stderr(tr("Could not find script \"%1\"").arg(scriptName));
53 void ExecWrapper::postStdout(const QString &msg) {
54 if(_bufferInfo.isValid())
55 Client::userInput(_bufferInfo, msg);
58 void ExecWrapper::postStderr(const QString &msg) {
59 if(_bufferInfo.isValid())
60 Client::messageModel()->insertErrorMessage(_bufferInfo, msg);
63 void ExecWrapper::processFinished(int exitCode, QProcess::ExitStatus status) {
64 if(status == QProcess::CrashExit) {
65 emit stderr(tr("Script \"%1\" crashed with exit code %2.").arg(_scriptName).arg(exitCode));
73 void ExecWrapper::processError(QProcess::ProcessError error) {
74 emit stderr(tr("Script \"%1\" caused error %2.").arg(_scriptName).arg(error));
77 void ExecWrapper::processReadStdout() {
78 _stdoutBuffer.append(_process.readAllStandardOutput());
80 while((idx = _stdoutBuffer.indexOf('\n')) >= 0) {
81 emit stdout(_stdoutBuffer.left(idx));
82 _stdoutBuffer = _stdoutBuffer.mid(idx + 1);
86 void ExecWrapper::processReadStderr() {
87 _stderrBuffer.append(_process.readAllStandardError());
89 while((idx = _stderrBuffer.indexOf('\n')) >= 0) {
90 emit stdout(_stderrBuffer.left(idx));
91 _stderrBuffer = _stderrBuffer.mid(idx + 1);