[KLF Application][KLF Tools][KLF Backend][KLF Home]
KLatexFormula Project

src/klfbackend/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 /* $Id: klfblockprocess.cpp 541 2010-10-04 00:43:14Z philippe $ */
00023 
00024 #include <qprocess.h>
00025 #include <qapplication.h>
00026 #include <qeventloop.h>
00027 
00028 #include "klfblockprocess.h"
00029 
00030 KLFBlockProcess::KLFBlockProcess(QObject *p)  : QProcess(p)
00031 {
00032 #ifdef KLFBACKEND_QT4
00033   mProcessAppEvents = true;
00034   connect(this, SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(ourProcExited()));
00035 #else
00036   connect(this, SIGNAL(wroteToStdin()), this, SLOT(ourProcGotOurStdinData()));
00037   connect(this, SIGNAL(processExited()), this, SLOT(ourProcExited()));
00038 #endif
00039 }
00040 
00041 
00042 KLFBlockProcess::~KLFBlockProcess()
00043 {
00044 }
00045 
00046 void KLFBlockProcess::ourProcGotOurStdinData()
00047 {
00048 #ifndef KLFBACKEND_QT4
00049   closeStdin();
00050 #endif
00051 }
00052 
00053 void KLFBlockProcess::ourProcExited()
00054 {
00055   _runstatus = 1; // exited
00056 }
00057 #ifndef KLFBACKEND_QT4
00058 bool KLFBlockProcess::startProcess(QStringList cmd, QCString str, QStringList env)
00059 {
00060   return startProcess(cmd, QByteArray().duplicate(str.data(), str.length()), env);
00061 }
00062 #endif
00063 bool KLFBlockProcess::startProcess(QStringList cmd, QStringList env)
00064 {
00065   return startProcess(cmd, QByteArray(), env);
00066 }
00067 
00068 bool KLFBlockProcess::startProcess(QStringList cmd, QByteArray stdindata, QStringList env)
00069 {
00070   _runstatus = 0;
00071 
00072 #ifdef KLFBACKEND_QT4
00073   if (env.size() > 0) {
00074     setEnvironment(env);
00075   }
00076 
00077   QString program = cmd.front();
00078   QStringList args = cmd;
00079   args.erase(args.begin());
00080   start(program, args);
00081   if ( ! waitForStarted() )
00082     return false;
00083 
00084   write(stdindata.constData(), stdindata.size());
00085   closeWriteChannel();
00086 #else
00087   setArguments(cmd);
00088   QStringList *e = &env;
00089   if (e->size() == 0)
00090     e = 0;
00091 
00092   if (! start(e) )
00093     return false;
00094 
00095   writeToStdin(stdindata);
00096   // slot ourProcGotOutStdinData() should be called, which closes input
00097 #endif
00098 
00099 #ifdef KLFBACKEND_QT4
00100   if (mProcessAppEvents) {
00101     while (_runstatus == 0) {
00102       qApp->processEvents(QEventLoop::ExcludeUserInputEvents);
00103     }
00104   } else {
00105     waitForFinished();
00106   }
00107 #else
00108   while (_runstatus == 0) {
00109     qApp->processEvents(QEventLoop::ExcludeUserInput);
00110   }
00111 #endif
00112 
00113   if (_runstatus < 0) { // some error occurred somewhere
00114     return false;
00115   }
00116 
00117   return true;
00118 }
00119 
00120 
00121 
00122 KLF_EXPORT QStringList klf_cur_environ()
00123 {
00124   QStringList curenvironment;
00125 #ifdef KLFBACKEND_QT4
00126   curenvironment = QProcess::systemEnvironment();
00127 #else
00128   extern char ** environ;
00129   int k;
00130   for (k = 0; environ[k] != NULL; ++k) {
00131     curenvironment.append(QString::fromLocal8Bit(environ[k]));
00132   }
00133 #endif
00134   return curenvironment;
00135 }

Generated by doxygen 1.7.3