Fix no newlines warnings. Patch by Peter Oberndorfer
[kdevelopdvcssupport.git] / util / commandexecutor.cpp
blob603e966cafe5230efa594aaa6eb1058636f0cfc7
1 /* KDevelop Util Library
3 * Copyright 2007 Andreas Pakulat <apaku@gmx.de>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
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 Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 * 02110-1301, USA.
21 #include "commandexecutor.h"
23 #include "processlinemaker.h"
24 #include <QtCore/QMap>
25 #include <QtCore/QStringList>
26 #include <QtCore/QString>
27 #include <kprocess.h>
29 namespace KDevelop
32 class CommandExecutorPrivate
34 public:
35 CommandExecutorPrivate( CommandExecutor* cmd )
36 : m_exec(cmd)
39 CommandExecutor* m_exec;
40 KProcess* m_process;
41 ProcessLineMaker* m_lineMaker;
42 QString m_command;
43 QStringList m_args;
44 QString m_workDir;
45 QMap<QString,QString> m_env;
46 void procError( QProcess::ProcessError error )
48 Q_UNUSED(error)
49 m_lineMaker->flushBuffers();
50 emit m_exec->failed();
52 void procFinished( int code, QProcess::ExitStatus status )
54 Q_UNUSED(code)
55 m_lineMaker->flushBuffers();
56 if( status == QProcess::NormalExit )
57 emit m_exec->completed();
61 CommandExecutor::CommandExecutor( const QString& command, QObject* parent )
62 : QObject(parent), d(new CommandExecutorPrivate(this))
64 d->m_process = new KProcess(this);
65 d->m_process->setOutputChannelMode( KProcess::SeparateChannels );
66 d->m_lineMaker = new ProcessLineMaker( d->m_process );
67 d->m_command = command;
68 connect( d->m_lineMaker, SIGNAL(receivedStdoutLines( const QStringList& ) ),
69 this, SIGNAL( receivedStandardOutput( const QStringList& ) ) );
70 connect( d->m_lineMaker, SIGNAL(receivedStderrLines( const QStringList& ) ),
71 this, SIGNAL( receivedStandardError( const QStringList& ) ) );
72 connect( d->m_process, SIGNAL( error( QProcess::ProcessError ) ),
73 this, SLOT( procError( QProcess::ProcessError ) ) );
74 connect( d->m_process, SIGNAL( finished( int, QProcess::ExitStatus ) ),
75 this, SLOT( procFinished( int, QProcess::ExitStatus ) ) );
79 CommandExecutor::~CommandExecutor()
81 delete d->m_process;
82 delete d->m_lineMaker;
83 delete d;
86 void CommandExecutor::setEnvironment( const QMap<QString,QString>& env )
88 d->m_env = env;
91 void CommandExecutor::setArguments( const QStringList& args )
93 d->m_args = args;
96 void CommandExecutor::setWorkingDirectory( const QString& dir )
98 d->m_workDir = dir;
101 void CommandExecutor::start()
103 Q_FOREACH( QString s, d->m_env.keys() )
105 d->m_process->setEnv( s, d->m_env[s] );
107 d->m_process->setWorkingDirectory( d->m_workDir );
108 d->m_process->setProgram( d->m_command, d->m_args );
109 d->m_process->start();
112 void CommandExecutor::setCommand( const QString& command )
114 d->m_command = command;
117 void CommandExecutor::kill()
119 d->m_process->close();
124 #include "commandexecutor.moc"