KDevelop API Documentation

execcommand.cpp

Go to the documentation of this file.
00001 /* This file is part of the KDE project
00002    Copyright (C) 2002 Harald Fernengel <harry@kdevelop.org>
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., 59 Temple Place - Suite 330,
00017    Boston, MA 02111-1307, USA.
00018 */
00019 
00020 #include "execcommand.h"
00021 
00022 #include <kprocess.h>
00023 #include <kprogress.h>
00024 #include <klocale.h>
00025 #include <kmessagebox.h>
00026 
00027 ExecCommand::ExecCommand( const QString& executable, const QStringList& args, 
00028                           const QString& workingDir, const QStringList& env,
00029                           QObject* parent, const char* name ):
00030     QObject( parent, name ), out( "" ) /* make sure out is not QString::null since that would mean "error" */
00031 
00032 {
00033   progressDlg = 0;
00034 
00035   proc = new KProcess();
00036   proc->setWorkingDirectory( workingDir );
00037   for ( QStringList::ConstIterator it = env.begin(); it != env.end(); ++it )
00038     proc->setEnvironment( (*it).section( '=', 0, 0 ), (*it).section( '=', 1, 1 ) );
00039   *proc << executable;
00040   *proc << args;
00041 
00042   connect( proc, SIGNAL(processExited(KProcess*)),
00043            this, SLOT(processExited()) );
00044   connect( proc, SIGNAL(receivedStdout(KProcess*,char*,int)),
00045            this, SLOT(receivedStdout(KProcess*,char*,int)) );
00046   connect( proc, SIGNAL(receivedStderr(KProcess*,char*,int)),
00047            this, SLOT(receivedStderr(KProcess*,char*,int)) );
00048 
00049   bool ok = proc->start( KProcess::NotifyOnExit, KProcess::AllOutput );
00050 
00051   if ( !ok ) {
00052     KMessageBox::error( 0, i18n("Could not invoke \"%1\". Please make sure it is installed correctly").arg( executable ),
00053                         i18n("Error Invoking Command") );
00054 
00055     emit finished( QString::null, QString::null );
00056     deleteLater();
00057 
00058   } else {
00059     progressDlg = new KProgressDialog( 0, 0, i18n("Command running..."),
00060                       i18n("Please wait until the \"%1\" command finishes.").arg( executable ), false );
00061     connect( progressDlg, SIGNAL(cancelClicked()),
00062              this, SLOT(cancelClicked()) );
00063   }
00064 }
00065 
00066 void ExecCommand::receivedStdout (KProcess*, char *buffer, int buflen)
00067 {
00068   out += QString::fromUtf8( buffer, buflen );  
00069 }
00070 
00071 void ExecCommand::receivedStderr (KProcess*, char *buffer, int buflen)
00072 {
00073   err += QString::fromUtf8( buffer, buflen );
00074 }
00075 
00076 void ExecCommand::processExited()
00077 {
00078   delete progressDlg;
00079   progressDlg = 0;
00080 
00081   emit finished( out, err );
00082   deleteLater();
00083 }
00084 
00085 void ExecCommand::cancelClicked()
00086 {
00087   delete progressDlg;
00088   progressDlg = 0;
00089   proc->kill();
00090 
00091   emit finished( QString::null, QString::null );
00092   deleteLater();
00093 }
00094 
00095 ExecCommand::~ExecCommand()
00096 {
00097   delete proc;
00098   delete progressDlg;
00099 }
00100 
00101 #include "execcommand.moc"
KDE Logo
This file is part of the documentation for KDevelop Version 3.1.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Tue Feb 22 09:22:36 2005 by doxygen 1.3.9.1 written by Dimitri van Heesch, © 1997-2003