klfblockprocess.cpp

00001 /***************************************************************************
00002  *   file klfblockprocess.cpp
00003  *   This file is part of the KLatexFormula Project.
00004  *   Copyright (C) 2007 by Philippe Faist
00005  *   philippe.faist@bluewin.ch
00006  *                                                                         *
00007  *   This program is free software; you can redistribute it and/or modify  *
00008  *   it under the terms of the GNU General Public License as published by  *
00009  *   the Free Software Foundation; either version 2 of the License, or     *
00010  *   (at your option) any later version.                                   *
00011  *                                                                         *
00012  *   This program is distributed in the hope that it will be useful,       *
00013  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00014  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00015  *   GNU General Public License for more details.                          *
00016  *                                                                         *
00017  *   You should have received a copy of the GNU General Public License     *
00018  *   along with this program; if not, write to the                         *
00019  *   Free Software Foundation, Inc.,                                       *
00020  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00021  ***************************************************************************/
00022 
00023 #include <qprocess.h>
00024 #include <qapplication.h>
00025 #include <qeventloop.h>
00026 
00027 #include "klfblockprocess.h"
00028 
00029 KLFBlockProcess::KLFBlockProcess(QObject *p)  : QProcess(p)
00030 {
00031 #ifdef KLFBACKEND_QT4
00032   connect(this, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(ourProcExited()));
00033 #else
00034   connect(this, SIGNAL(wroteToStdin()), this, SLOT(ourProcGotOurStdinData()));
00035   connect(this, SIGNAL(processExited()), this, SLOT(ourProcExited()));
00036 #endif
00037 }
00038 
00039 
00040 KLFBlockProcess::~KLFBlockProcess()
00041 {
00042 }
00043 
00044 void KLFBlockProcess::ourProcGotOurStdinData()
00045 {
00046 #ifndef KLFBACKEND_QT4
00047   closeStdin();
00048 #endif
00049 }
00050 
00051 void KLFBlockProcess::ourProcExited()
00052 {
00053   _runstatus = 1; // exited
00054 }
00055 #ifndef KLFBACKEND_QT4
00056 bool KLFBlockProcess::startProcess(QStringList cmd, QCString str, QStringList env)
00057 {
00058   return startProcess(cmd, QByteArray().duplicate(str.data(), str.length()), env);
00059 }
00060 #endif
00061 bool KLFBlockProcess::startProcess(QStringList cmd, QStringList env)
00062 {
00063   return startProcess(cmd, QByteArray(), env);
00064 }
00065 
00066 bool KLFBlockProcess::startProcess(QStringList cmd, QByteArray stdindata, QStringList env)
00067 {
00068   _runstatus = 0;
00069 
00070 #ifdef KLFBACKEND_QT4
00071   if (env.size() > 0)
00072     setEnvironment(env);
00073 
00074   QString program = cmd.front();
00075   QStringList args = cmd;
00076   args.erase(args.begin());
00077   start(program, args);
00078   if ( ! waitForStarted() )
00079     return false;
00080 
00081   write(stdindata.constData(), stdindata.size());
00082   closeWriteChannel();
00083 #else
00084   setArguments(cmd);
00085   QStringList *e = &env;
00086   if (e->size() == 0)
00087     e = 0;
00088 
00089   if (! start(e) )
00090     return false;
00091 
00092   writeToStdin(stdindata);
00093   // slot ourProcGotOutStdinData() should be called, which closes input
00094 #endif
00095 
00096   while (_runstatus == 0) {
00097 #ifdef KLFBACKEND_QT4
00098     qApp->processEvents(QEventLoop::ExcludeUserInputEvents);
00099 #else
00100     qApp->eventLoop()->processEvents(QEventLoop::ExcludeUserInput);
00101 #endif
00102   }
00103 
00104   if (_runstatus < 0) { // some error occurred somewhere
00105     return false;
00106   }
00107 
00108   return true;
00109 }

Generated on Mon May 4 01:27:08 2009 for KLFBackend by  doxygen 1.5.3