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