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
21 #include "commandexecutor.h"
23 #include "processlinemaker.h"
24 #include <QtCore/QMap>
25 #include <QtCore/QStringList>
26 #include <QtCore/QString>
32 class CommandExecutorPrivate
35 CommandExecutorPrivate( CommandExecutor
* cmd
)
39 CommandExecutor
* m_exec
;
41 ProcessLineMaker
* m_lineMaker
;
45 QMap
<QString
,QString
> m_env
;
46 void procError( QProcess::ProcessError error
)
49 m_lineMaker
->flushBuffers();
50 emit m_exec
->failed();
52 void procFinished( int code
, QProcess::ExitStatus status
)
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()
82 delete d
->m_lineMaker
;
86 void CommandExecutor::setEnvironment( const QMap
<QString
,QString
>& env
)
91 void CommandExecutor::setArguments( const QStringList
& args
)
96 void CommandExecutor::setWorkingDirectory( const QString
& 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"