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

util

commandexecutor.cpp

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

util

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

KDevelop Platform Libraries

Skip menu "KDevelop Platform Libraries"
  • interfaces
  • language
  •   codegen
  •   duchain
  •   editor
  • outputview
  • project
  • shell
  • sublime
  • util
  • vcs
Generated for KDevelop Platform Libraries by doxygen 1.5.9-20090814
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