00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <QString>
00021 #include <QProcess>
00022 #include <QTcpSocket>
00023 #include <QTextStream>
00024
00025 #include "qgscontexthelp.h"
00026 #include "qgsapplication.h"
00027 #include "qgslogger.h"
00028
00029
00030
00031
00032
00033
00034 QgsContextHelp *QgsContextHelp::gContextHelp = NULL;
00035
00036 void QgsContextHelp::run( QString context )
00037 {
00038 if ( gContextHelp == NULL )
00039 {
00040
00041 gContextHelp = new QgsContextHelp( context );
00042 }
00043 else
00044 {
00045 gContextHelp->showContext( context );
00046 }
00047 }
00048
00049 QgsContextHelp::QgsContextHelp( QString context )
00050 {
00051 mProcess = start( context );
00052 #ifdef QGSCONTEXTHELP_REUSE
00053
00054 mSocket = new QTcpSocket( this );
00055 connect( mProcess, SIGNAL( readyReadStandardOutput() ), SLOT( readPort() ) );
00056 #else
00057
00058 mNextProcess = NULL;
00059 #endif
00060 }
00061
00062 QgsContextHelp::~QgsContextHelp()
00063 {
00064 #ifdef QGSCONTEXTHELP_REUSE
00065 delete mSocket;
00066 #else
00067
00068 delete mNextProcess;
00069 #endif
00070 delete mProcess;
00071 }
00072
00073 QProcess *QgsContextHelp::start( QString context )
00074 {
00075
00076 QString helpPath = QgsApplication::helpAppPath();
00077 QgsDebugMsg( QString( "Help path is %1" ).arg( helpPath ) );
00078
00079 QProcess *process = new QProcess;
00080 process->start( helpPath, QStringList( context ) );
00081
00082
00083 connect( process, SIGNAL( finished( int, QProcess::ExitStatus ) ), SLOT( processExited() ) );
00084
00085
00086 connect( qApp, SIGNAL( aboutToQuit() ), process, SLOT( terminate() ) );
00087
00088 return process;
00089 }
00090
00091 void QgsContextHelp::readPort()
00092 {
00093 #ifdef QGSCONTEXTHELP_REUSE
00094
00095 QString p = mProcess->readAllStandardOutput();
00096 quint16 port = p.toUShort();
00097 mSocket->connectToHost( "localhost", port );
00098 disconnect( mProcess, SIGNAL( readyReadStandardOutput() ), this, SLOT( readPort() ) );
00099 #endif
00100 }
00101
00102 void QgsContextHelp::showContext( QString context )
00103 {
00104
00105 #ifdef QGSCONTEXTHELP_REUSE
00106
00107 QTextStream os( mSocket );
00108 os << context << "\n";
00109 QgsDebugMsg( QString( "Sending help process context %1" ).arg( context ) );
00110 #else
00111
00112
00113 delete mNextProcess;
00114
00115 mNextProcess = start( context );
00116
00117 mProcess->terminate();
00118 #endif
00119 }
00120
00121 void QgsContextHelp::processExited()
00122 {
00123 #ifndef QGSCONTEXTHELP_REUSE
00124 if ( mNextProcess )
00125 {
00126
00127 delete mProcess;
00128 mProcess = mNextProcess;
00129 mNextProcess = NULL;
00130 }
00131 else
00132 #endif
00133 {
00134
00135 delete gContextHelp;
00136 gContextHelp = NULL;
00137 }
00138 }