• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • KDevelop Platform Libraries
  • Sitemap
  • Contact Us
 

util

commandexecutor.cpp

00001 /* KDevelop Util Library
00002  *
00003  * Copyright 2007 Andreas Pakulat <apaku@gmx.de>
00004  *
00005  * This program is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU General Public License
00007  * as published by the Free Software Foundation; either version 2
00008  * of the License, or (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software
00017  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00018  * 02110-1301, USA.
00019  */
00020 
00021 #include "commandexecutor.h"
00022 
00023 #include "processlinemaker.h"
00024 #include <QtCore/QMap>
00025 #include <QtCore/QStringList>
00026 #include <QtCore/QString>
00027 #include <kprocess.h>
00028 
00029 namespace KDevelop
00030 {
00031 
00032 class CommandExecutorPrivate
00033 {
00034 public:
00035     CommandExecutorPrivate( CommandExecutor* cmd )
00036         : m_exec(cmd)
00037     {
00038     }
00039     CommandExecutor* m_exec;
00040     KProcess* m_process;
00041     ProcessLineMaker* m_lineMaker;
00042     QString m_command;
00043     QStringList m_args;
00044     QString m_workDir;
00045     QMap<QString,QString> m_env;
00046     void procError( QProcess::ProcessError error )
00047     {
00048         Q_UNUSED(error)
00049         m_lineMaker->flushBuffers();
00050         emit m_exec->failed();
00051     }
00052     void procFinished( int code, QProcess::ExitStatus status )
00053     {
00054         Q_UNUSED(code)
00055         m_lineMaker->flushBuffers();
00056         if( status == QProcess::NormalExit )
00057             emit m_exec->completed();
00058     }
00059 };
00060 
00061 CommandExecutor::CommandExecutor( const QString& command, QObject* parent )
00062   : QObject(parent), d(new CommandExecutorPrivate(this))
00063 {
00064     d->m_process = new KProcess(this);
00065     d->m_process->setOutputChannelMode( KProcess::SeparateChannels );
00066     d->m_lineMaker = new ProcessLineMaker( d->m_process );
00067     d->m_command = command;
00068     connect( d->m_lineMaker, SIGNAL(receivedStdoutLines( const QStringList& ) ),
00069              this, SIGNAL( receivedStandardOutput( const QStringList& ) ) );
00070     connect( d->m_lineMaker, SIGNAL(receivedStderrLines( const QStringList& ) ),
00071              this, SIGNAL( receivedStandardError( const QStringList& ) ) );
00072     connect( d->m_process, SIGNAL( error( QProcess::ProcessError ) ),
00073              this, SLOT( procError( QProcess::ProcessError ) ) );
00074     connect( d->m_process, SIGNAL( finished( int, QProcess::ExitStatus ) ),
00075              this, SLOT( procFinished( int, QProcess::ExitStatus ) ) );
00076 }
00077 
00078 
00079 CommandExecutor::~CommandExecutor()
00080 {
00081     delete d->m_process;
00082     delete d->m_lineMaker;
00083     delete d;
00084 }
00085 
00086 void CommandExecutor::setEnvironment( const QMap<QString,QString>& env )
00087 {
00088     d->m_env = env;
00089 }
00090 
00091 void CommandExecutor::setArguments( const QStringList& args )
00092 {
00093     d->m_args = args;
00094 }
00095 
00096 void CommandExecutor::setWorkingDirectory( const QString& dir )
00097 {
00098     d->m_workDir = dir;
00099 }
00100 
00101 void CommandExecutor::start()
00102 {
00103     Q_FOREACH( QString s, d->m_env.keys() )
00104     {
00105         d->m_process->setEnv( s, d->m_env[s] );
00106     }
00107     d->m_process->setWorkingDirectory( d->m_workDir );
00108     d->m_process->setProgram( d->m_command, d->m_args );
00109     d->m_process->start();
00110 }
00111 
00112 void CommandExecutor::setCommand( const QString& command )
00113 {
00114     d->m_command = command;
00115 }
00116 
00117 void CommandExecutor::kill()
00118 {
00119     d->m_process->close();
00120 }
00121 
00122 }
00123 
00124 #include "commandexecutor.moc"

util

Skip menu "util"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Class Members
  • Related Pages

KDevelop Platform Libraries

Skip menu "KDevelop Platform Libraries"
  • interfaces
  • language
  •   codegen
  •   duchain
  •   editor
  • outputview
  •     interfaces
  • project
  • shell
  • sublime
  • util
  • vcs
Generated for KDevelop Platform Libraries by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal