00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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( "" )
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"