Vidalia 0.2.12

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 ** \brief Invokes a web browser process (originally by Steven. J. Murdoch)
00038 */
00039 
00040 #include "HelperProcess.h"
00041 #include "Vidalia.h"
00042 
00043 #include "stringutil.h"
00044 
00045 #include <QString>
00046 #include <QFileInfo>
00047 
00048 
00049 /** Default constructor */
00050 HelperProcess::HelperProcess(QObject *parent)
00051 : QProcess(parent)
00052 {
00053   // Call error handling routine on errors
00054   QObject::connect(this, SIGNAL(error(QProcess::ProcessError)),
00055                    this, SLOT(onError(QProcess::ProcessError)));
00056 
00057   // Call output handling routines on process output
00058   QObject::connect(this, SIGNAL(readyReadStandardError()),
00059                    this, SLOT(onReadyReadStandardError()));
00060   QObject::connect(this, SIGNAL(readyReadStandardOutput()),
00061                    this, SLOT(onReadyReadStandardOutput()));
00062 }
00063 
00064 /** Invoked when output is written to the process's stderr. */
00065 void
00066 HelperProcess::onReadyReadStandardError()
00067 {
00068   QString output = QString(readAllStandardError());
00069   foreach (QString line, output.split("\n")) {
00070     vInfo("(%1:stderr): %2").arg(_processName).arg(line);
00071   }
00072 }
00073 
00074 /** Invoked when output is written to the process's stdout. */
00075 void
00076 HelperProcess::onReadyReadStandardOutput()
00077 {
00078   QString output = QString(readAllStandardOutput());
00079   foreach (QString line, output.split("\n")) {
00080     vInfo("(%1:stdout): %2").arg(_processName).arg(line);
00081   }
00082 }
00083 
00084 void
00085 HelperProcess::start(const QString &app, const QString &args)
00086 {
00087   QFileInfo fi(app);
00088   _processName = fi.fileName();
00089 
00090   QString commandLine = QString("\"%1\" %2").arg(app).arg(args);
00091 
00092    // Log the process name and arguments
00093   vNotice("Launching helper process with command line '%1'")
00094                                            .arg(commandLine);
00095 
00096   QProcess::start(commandLine, QIODevice::ReadOnly | QIODevice::Text);
00097 }
00098 
00099 /** Start the specified application. */
00100 void
00101 HelperProcess::start(const QString &app, const QStringList &args) 
00102 {
00103   // Remember the executable name of the process
00104   QFileInfo fi(app);
00105   _processName = fi.fileName();
00106 
00107   // Log the process name and arguments
00108   vNotice("Launching helper process '%1' with arguments '%2'").arg(app)
00109                                      .arg(string_format_arguments(args));
00110 
00111   // Start the specified application
00112   QProcess::start(app, args, QIODevice::ReadOnly | QIODevice::Text);
00113 }
00114 
00115 /** Invoked when underlying QProcess fails. */
00116 void
00117 HelperProcess::onError(QProcess::ProcessError error)
00118 {
00119   // Pass up error messages on startup, but ignore the rest
00120   if (error == QProcess::FailedToStart) {
00121     vWarn("Helper process '%1' failed to start: %2").arg(_processName)
00122                                                     .arg(errorString());
00123     emit startFailed(errorString());
00124   }
00125 }
00126 
00127 /** Returns true iff process is not running. */
00128 bool
00129 HelperProcess::isDone() const
00130 {
00131   return state() == NotRunning;
00132 }
00133