Quantum GIS API Documentation  1.7.5-Wroclaw
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
qgsrunprocess.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsrunprocess.cpp
3 
4  A class that runs an external program
5 
6  -------------------
7  begin : Jan 2005
8  copyright : (C) 2005 by Gavin Macaulay
9  email : gavin at macaulay dot co dot nz
10  ***************************************************************************/
11 
12 /***************************************************************************
13  * *
14  * This program is free software; you can redistribute it and/or modify *
15  * it under the terms of the GNU General Public License as published by *
16  * the Free Software Foundation; either version 2 of the License, or *
17  * (at your option) any later version. *
18  * *
19  ***************************************************************************/
20 /* $Id$ */
21 
22 #include "qgsrunprocess.h"
23 
24 #include "qgslogger.h"
25 #include "qgsmessageoutput.h"
26 #include <QProcess>
27 #include <QMessageBox>
28 
29 QgsRunProcess::QgsRunProcess( const QString& action, bool capture )
30  : mProcess( NULL ), mOutput( NULL )
31 {
32  // Make up a string from the command and arguments that we'll use
33  // for display purposes
34  QgsDebugMsg( "Running command: " + action );
35 
36  mCommand = action;
37 
38  mProcess = new QProcess;
39 
40  if ( capture )
41  {
42  connect( mProcess, SIGNAL( error( QProcess::ProcessError ) ), this, SLOT( processError( QProcess::ProcessError ) ) );
43  connect( mProcess, SIGNAL( readyReadStandardOutput() ), this, SLOT( stdoutAvailable() ) );
44  connect( mProcess, SIGNAL( readyReadStandardError() ), this, SLOT( stderrAvailable() ) );
45  // We only care if the process has finished if we are capturing
46  // the output from the process, hence this connect() call is
47  // inside the capture if() statement.
48  connect( mProcess, SIGNAL( finished( int, QProcess::ExitStatus ) ), this, SLOT( processExit( int, QProcess::ExitStatus ) ) );
49 
50  // Use QgsMessageOutput for displaying output to user
51  // It will delete itself when the dialog box is closed.
53  mOutput->setTitle( action );
54  mOutput->setMessage( tr( "<b>Starting %1...</b>" ).arg( action ), QgsMessageOutput::MessageHtml );
55  mOutput->showMessage( false ); // non-blocking
56 
57  // get notification of delete if it's derived from QObject
58  QObject* mOutputObj = dynamic_cast<QObject *>( mOutput );
59  if ( mOutputObj )
60  {
61  connect( mOutputObj, SIGNAL( destroyed() ), this, SLOT( dialogGone() ) );
62  }
63 
64  // start the process!
65  mProcess->start( action );
66  }
67  else
68  {
69  if ( ! mProcess->startDetached( action ) ) // let the program run by itself
70  {
71  QMessageBox::critical( 0, tr( "Action" ),
72  tr( "Unable to run command\n%1" ).arg( action ),
73  QMessageBox::Ok, Qt::NoButton );
74  }
75  // We're not capturing the output from the process, so we don't
76  // need to exist anymore.
77  die();
78  }
79 }
80 
82 {
83  delete mProcess;
84 }
85 
87 {
88  // safe way to do "delete this" for QObjects
89  deleteLater();
90 }
91 
93 {
94  QString line( mProcess->readAllStandardOutput() );
95 
96  // Add the new output to the dialog box
97  mOutput->appendMessage( line );
98 }
99 
101 {
102  QString line( mProcess->readAllStandardError() );
103 
104  // Add the new output to the dialog box, but color it red
105  mOutput->appendMessage( "<font color=red>" + line + "</font>" );
106 }
107 
108 void QgsRunProcess::processExit( int, QProcess::ExitStatus )
109 {
110  // Because we catch the dialog box going (the dialogGone()
111  // function), and delete this instance, control will only pass to
112  // this function if the dialog box still exists when the process
113  // exits, so it's always safe to use the pointer to the dialog box
114  // (unless it was never created in the first case, which is what the
115  // test against 0 is for).
116 
117  if ( mOutput != 0 )
118  {
119  mOutput->appendMessage( "<b>" + tr( "Done" ) + "</b>" );
120  }
121 
122  // Since the dialog box takes care of deleting itself, and the
123  // process has gone, there's no need for this instance to stay
124  // around, so we disappear too.
125  die();
126 }
127 
129 {
130  // The dialog has gone, so the user is no longer interested in the
131  // output from the process. Since the process will run happily
132  // without the QProcess object, this instance and its data can then
133  // go too, but disconnect the signals to prevent further functions in this
134  // class being called after it has been deleted (Qt seems not to be
135  // disconnecting them itself)
136 
137  mOutput = 0;
138 
139  disconnect( mProcess, SIGNAL( error( QProcess::ProcessError ) ), this, SLOT( processError( QProcess::ProcessError ) ) );
140  disconnect( mProcess, SIGNAL( readyReadStandardOutput() ), this, SLOT( stdoutAvailable() ) );
141  disconnect( mProcess, SIGNAL( readyReadStandardError() ), this, SLOT( stderrAvailable() ) );
142  disconnect( mProcess, SIGNAL( finished( int, QProcess::ExitStatus ) ), this, SLOT( processExit( int, QProcess::ExitStatus ) ) );
143 
144  die();
145 }
146 
147 void QgsRunProcess::processError( QProcess::ProcessError err )
148 {
149  if ( err == QProcess::FailedToStart )
150  {
152  output->setMessage( tr( "Unable to run command %1" ).arg( mCommand ), QgsMessageOutput::MessageText );
153  // Didn't work, so no need to hang around
154  die();
155  }
156  else
157  {
158  QgsDebugMsg( "Got error: " + QString( "%d" ).arg( err ) );
159  }
160 }