externallanguage.cpp

00001 /***************************************************************************
00002  *   Copyright (C) 2005 by David Saxton                                    *
00003  *   david@bluehaze.org                                                    *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  ***************************************************************************/
00010 
00011 #include "externallanguage.h"
00012 #include "languagemanager.h"
00013 #include "logview.h"
00014 
00015 #include <kdebug.h>
00016 #include <kprocess.h>
00017 #include <qregexp.h>
00018 #include <qtimer.h>
00019 
00020 ExternalLanguage::ExternalLanguage( ProcessChain *processChain, KTechlab *parent, const QString &name )
00021  : Language( processChain, parent, name )
00022 {
00023         m_languageProcess = 0;
00024 }
00025 
00026 
00027 ExternalLanguage::~ExternalLanguage()
00028 {
00029         deleteLanguageProcess();
00030 }
00031 
00032 
00033 void ExternalLanguage::deleteLanguageProcess()
00034 {
00035         if (!m_languageProcess)
00036                 return;
00037         
00038         // I'm not too sure if this combination of killing the process is the best way....
00039 //      m_languageProcess->tryTerminate();
00040 //      QTimer::singleShot( 5000, m_languageProcess, SLOT( kill() ) );
00041 //      delete m_languageProcess;
00042         m_languageProcess->kill();
00043         m_languageProcess->deleteLater();
00044         
00045         m_languageProcess = 0;
00046 }
00047 
00048 
00049 void ExternalLanguage::receivedStdout( KProcess *, char * buffer, int buflen )
00050 {
00051         QStringList lines = QStringList::split( '\n', QString::fromLocal8Bit( buffer, buflen ), false );
00052         QStringList::iterator end = lines.end();
00053         
00054         for ( QStringList::iterator it = lines.begin(); it != end; ++it )
00055         {
00056                 if ( isError( *it ) )
00057                 {
00058                         outputError( *it );
00059                         outputtedError( *it );
00060                 }
00061                 else if ( isWarning( *it ) )
00062                 {
00063                         outputWarning( *it );
00064                         outputtedWarning( *it );
00065                 }
00066                 else
00067                 {
00068                         outputMessage( *it );
00069                         outputtedMessage( *it );
00070                 }
00071         }
00072 }
00073 
00074 
00075 void ExternalLanguage::receivedStderr( KProcess *, char * buffer, int buflen )
00076 {
00077         QStringList lines = QStringList::split( '\n', QString::fromLocal8Bit( buffer, buflen ), false );
00078         QStringList::iterator end = lines.end();
00079         
00080         for ( QStringList::iterator it = lines.begin(); it != end; ++it )
00081         {
00082                 if ( isStderrOutputFatal( *it ) )
00083                 {
00084                         outputError( *it );
00085                         outputtedError( *it );
00086                 }
00087                 else
00088                 {
00089                         outputWarning( *it );
00090                         outputtedWarning( *it );
00091                 }
00092         }
00093 }
00094 
00095 
00096 void ExternalLanguage::processExited( KProcess * )
00097 {
00098         if ( !m_languageProcess )
00099                 return;
00100         bool allOk = processExited( m_languageProcess->normalExit() && m_errorCount == 0 );
00101         finish(allOk);
00102         deleteLanguageProcess();
00103 }
00104 
00105 
00106 void ExternalLanguage::processInitFailed()
00107 {
00108         finish(false);
00109         deleteLanguageProcess();
00110 }
00111 
00112 
00113 bool ExternalLanguage::start()
00114 {
00115         displayProcessCommand();
00116         
00117         return m_languageProcess->start( KProcess::NotifyOnExit, KProcess::All );
00118 }
00119 
00120 
00121 void ExternalLanguage::resetLanguageProcess()
00122 {
00123         reset();
00124         deleteLanguageProcess();
00125         m_errorCount = 0;
00126         
00127         m_languageProcess = new KProcess(this);
00128         
00129         connect( m_languageProcess, SIGNAL(receivedStdout( KProcess*, char*, int )),
00130                          this, SLOT(receivedStdout( KProcess*, char*, int )) );
00131         
00132         connect( m_languageProcess, SIGNAL(receivedStderr( KProcess*, char*, int )),
00133                          this, SLOT(receivedStderr( KProcess*, char*, int )) );
00134         
00135         connect( m_languageProcess, SIGNAL(processExited( KProcess* )),
00136                          this, SLOT(processExited( KProcess* )) );
00137 }
00138 
00139 
00140 void ExternalLanguage::displayProcessCommand()
00141 {
00142         QStringList quotedArguments;
00143         QValueList<QCString> arguments = m_languageProcess->args();
00144         
00145         if ( arguments.size() == 1 )
00146                 quotedArguments << arguments[0];
00147                 
00148         else
00149         {
00150                 QValueList<QCString>::const_iterator end = arguments.end();
00151         
00152                 for ( QValueList<QCString>::const_iterator it = arguments.begin(); it != end; ++it )
00153                 {
00154                         if ( (*it).isEmpty() || (*it).contains( QRegExp("[\\s]") ) )
00155                                 quotedArguments << KProcess::quote( *it );
00156                         else
00157                                 quotedArguments << *it;
00158                 }
00159         }
00160         
00161 //      outputMessage( "<i>" + quotedArguments.join(" ") + "</i>" );
00162         outputMessage( quotedArguments.join(" ") );
00163 //      LanguageManager::self()->logView()->addOutput( quotedArguments.join(" "), LogView::ot_info );
00164 }
00165 
00166 
00167 #include "externallanguage.moc"

Generated on Tue May 8 17:05:29 2007 for KTechLab by  doxygen 1.5.1