00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "qgsrunprocess.h"
00023
00024 #include "qgslogger.h"
00025 #include "qgsmessageoutput.h"
00026 #include <QProcess>
00027 #include <QMessageBox>
00028
00029 QgsRunProcess::QgsRunProcess( const QString& action, bool capture )
00030 : mProcess( NULL ), mOutput( NULL )
00031 {
00032
00033
00034 QgsDebugMsg( "Running command: " + action );
00035
00036 mCommand = action;
00037
00038 mProcess = new QProcess;
00039
00040 if ( capture )
00041 {
00042 connect( mProcess, SIGNAL( error( QProcess::ProcessError ) ), this, SLOT( processError( QProcess::ProcessError ) ) );
00043 connect( mProcess, SIGNAL( readyReadStandardOutput() ), this, SLOT( stdoutAvailable() ) );
00044 connect( mProcess, SIGNAL( readyReadStandardError() ), this, SLOT( stderrAvailable() ) );
00045
00046
00047
00048 connect( mProcess, SIGNAL( finished( int, QProcess::ExitStatus ) ), this, SLOT( processExit( int, QProcess::ExitStatus ) ) );
00049
00050
00051
00052 mOutput = QgsMessageOutput::createMessageOutput();
00053 mOutput->setTitle( action );
00054 mOutput->setMessage( tr( "<b>Starting %1...</b>" ).arg( action ), QgsMessageOutput::MessageHtml );
00055 mOutput->showMessage( false );
00056
00057
00058 QObject* mOutputObj = dynamic_cast<QObject *>( mOutput );
00059 if ( mOutputObj )
00060 {
00061 connect( mOutputObj, SIGNAL( destroyed() ), this, SLOT( dialogGone() ) );
00062 }
00063
00064
00065 mProcess->start( action );
00066 }
00067 else
00068 {
00069 if ( ! mProcess->startDetached( action ) )
00070 {
00071 QMessageBox::critical( 0, tr( "Action" ),
00072 tr( "Unable to run command\n%1" ).arg( action ),
00073 QMessageBox::Ok, Qt::NoButton );
00074 }
00075
00076
00077 die();
00078 }
00079 }
00080
00081 QgsRunProcess::~QgsRunProcess()
00082 {
00083 delete mProcess;
00084 }
00085
00086 void QgsRunProcess::die()
00087 {
00088
00089 deleteLater();
00090 }
00091
00092 void QgsRunProcess::stdoutAvailable()
00093 {
00094 QString line( mProcess->readAllStandardOutput() );
00095
00096
00097 mOutput->appendMessage( line );
00098 }
00099
00100 void QgsRunProcess::stderrAvailable()
00101 {
00102 QString line( mProcess->readAllStandardError() );
00103
00104
00105 mOutput->appendMessage( "<font color=red>" + line + "</font>" );
00106 }
00107
00108 void QgsRunProcess::processExit( int, QProcess::ExitStatus )
00109 {
00110
00111
00112
00113
00114
00115
00116
00117 if ( mOutput != 0 )
00118 {
00119 mOutput->appendMessage( "<b>" + tr( "Done" ) + "</b>" );
00120 }
00121
00122
00123
00124
00125 die();
00126 }
00127
00128 void QgsRunProcess::dialogGone()
00129 {
00130
00131
00132
00133
00134
00135
00136
00137 mOutput = 0;
00138
00139 disconnect( mProcess, SIGNAL( error( QProcess::ProcessError ) ), this, SLOT( processError( QProcess::ProcessError ) ) );
00140 disconnect( mProcess, SIGNAL( readyReadStandardOutput() ), this, SLOT( stdoutAvailable() ) );
00141 disconnect( mProcess, SIGNAL( readyReadStandardError() ), this, SLOT( stderrAvailable() ) );
00142 disconnect( mProcess, SIGNAL( finished( int, QProcess::ExitStatus ) ), this, SLOT( processExit( int, QProcess::ExitStatus ) ) );
00143
00144 die();
00145 }
00146
00147 void QgsRunProcess::processError( QProcess::ProcessError err )
00148 {
00149 if ( err == QProcess::FailedToStart )
00150 {
00151 QgsMessageOutput* output = mOutput ? mOutput : QgsMessageOutput::createMessageOutput();
00152 output->setMessage( tr( "Unable to run command %1" ).arg( mCommand ), QgsMessageOutput::MessageText );
00153
00154 die();
00155 }
00156 else
00157 {
00158 QgsDebugMsg( "Got error: " + QString( "%d" ).arg( err ) );
00159 }
00160 }