HelperProcess.cpp

Go to the documentation of this file.
00001 /*
00002 **  This file is part of Vidalia, and is subject to the license terms in the
00003 **  LICENSE file, found in the top level directory of this distribution. If you
00004 **  did not receive the LICENSE file with this file, you may obtain it from the
00005 **  Vidalia source package distributed by the Vidalia Project at
00006 **  http://www.vidalia-project.net/. No part of Vidalia, including this file,
00007 **  may be copied, modified, propagated, or distributed except according to the
00008 **  terms described in the LICENSE file.
00009 */
00010 /*
00011 **  This file was originally written by Steven J. Murdoch, and 
00012 **  modified by Matt Edman. It is distributed under the following
00013 **  license:
00014 **
00015 **  Copyright (C) 2007, Matt Edman
00016 **  Copyright (C) 2007, Steven J. Murdoch 
00017 **                      <http://www.cl.cam.ac.uk/users/sjm217/>
00018 **
00019 **  This program is free software; you can redistribute it and/or
00020 **  modify it under the terms of the GNU General Public License
00021 **  as published by the Free Software Foundation; either version 2
00022 **  of the License, or (at your option) any later version.
00023 **
00024 **  This program is distributed in the hope that it will be useful,
00025 **  but WITHOUT ANY WARRANTY; without even the implied warranty of
00026 **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00027 **  GNU General Public License for more details.
00028 **
00029 **  You should have received a copy of the GNU General Public License
00030 **  along with this program; if not, write to the Free Software
00031 **  Foundation, Inc., 51 Franklin Street, Fifth Floor, 
00032 **  Boston, MA  02110-1301, USA.
00033 */
00034 
00035 /*
00036 ** \file helperprocess.cpp
00037 ** \version $Id: HelperProcess.cpp 4132 2009-09-24 02:28:18Z edmanm $
00038 ** \brief Invokes a web browser process (originally by Steven. J. Murdoch)
00039 */
00040 
00041 #include "HelperProcess.h"
00042 #include "Vidalia.h"
00043 
00044 #include "stringutil.h"
00045 
00046 #include <QString>
00047 #include <QFileInfo>
00048 
00049 
00050 /** Default constructor */
00051 HelperProcess::HelperProcess(QObject *parent)
00052 : QProcess(parent)
00053 {
00054   // Call error handling routine on errors
00055   QObject::connect(this, SIGNAL(error(QProcess::ProcessError)),
00056                    this, SLOT(onError(QProcess::ProcessError)));
00057 
00058   // Call output handling routines on process output
00059   QObject::connect(this, SIGNAL(readyReadStandardError()),
00060                    this, SLOT(onReadyReadStandardError()));
00061   QObject::connect(this, SIGNAL(readyReadStandardOutput()),
00062                    this, SLOT(onReadyReadStandardOutput()));
00063 }
00064 
00065 /** Invoked when output is written to the process's stderr. */
00066 void
00067 HelperProcess::onReadyReadStandardError()
00068 {
00069   QString output = QString(readAllStandardError());
00070   foreach (QString line, output.split("\n")) {
00071     vInfo("(%1:stderr): %2").arg(_processName).arg(line);
00072   }
00073 }
00074 
00075 /** Invoked when output is written to the process's stdout. */
00076 void
00077 HelperProcess::onReadyReadStandardOutput()
00078 {
00079   QString output = QString(readAllStandardOutput());
00080   foreach (QString line, output.split("\n")) {
00081     vInfo("(%1:stdout): %2").arg(_processName).arg(line);
00082   }
00083 }
00084 
00085 void
00086 HelperProcess::start(const QString &app, const QString &args)
00087 {
00088   QFileInfo fi(app);
00089   _processName = fi.fileName();
00090 
00091   QString commandLine = QString("\"%1\" %2").arg(app).arg(args);
00092 
00093    // Log the process name and arguments
00094   vNotice("Launching helper process with command line '%1'")
00095                                            .arg(commandLine);
00096 
00097   QProcess::start(commandLine, QIODevice::ReadOnly | QIODevice::Text);
00098 }
00099 
00100 /** Start the specified application. */
00101 void
00102 HelperProcess::start(const QString &app, const QStringList &args) 
00103 {
00104   // Remember the executable name of the process
00105   QFileInfo fi(app);
00106   _processName = fi.fileName();
00107 
00108   // Log the process name and arguments
00109   vNotice("Launching helper process '%1' with arguments '%2'").arg(app)
00110                                      .arg(string_format_arguments(args));
00111 
00112   // Start the specified application
00113   QProcess::start(app, args, QIODevice::ReadOnly | QIODevice::Text);
00114 }
00115 
00116 /** Invoked when underlying QProcess fails. */
00117 void
00118 HelperProcess::onError(QProcess::ProcessError error)
00119 {
00120   // Pass up error messages on startup, but ignore the rest
00121   if (error == QProcess::FailedToStart) {
00122     vWarn("Helper process '%1' failed to start: %2").arg(_processName)
00123                                                     .arg(errorString());
00124     emit startFailed(errorString());
00125   }
00126 }
00127 
00128 /** Returns true iff process is not running. */
00129 bool
00130 HelperProcess::isDone() const
00131 {
00132   return state() == NotRunning;
00133 }
00134 

Generated on Mon Aug 30 19:14:02 2010 for Vidalia by  doxygen 1.5.9