• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

NepomukDaemons

processcontrol.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2006 by Tobias Koenig <tokoe@kde.org>                   *
00003  *   Copyright (C) 2008 by Sebastian Trueg <trueg@kde.org>                 *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU Library General Public License as       *
00007  *   published by the Free Software Foundation; either version 2 of the    *
00008  *   License, or (at your option) any later version.                       *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU Library General Public     *
00016  *   License along with this program; if not, write to the                 *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
00019  ***************************************************************************/
00020 
00021 #include "processcontrol.h"
00022 
00023 #include <QtCore/QDebug>
00024 #include <QtCore/QTimer>
00025 
00026 
00027 ProcessControl::ProcessControl( QObject *parent )
00028     : QObject( parent ), mFailedToStart( false ), mCrashCount( 0 )
00029 {
00030     connect( &mProcess, SIGNAL( error( QProcess::ProcessError ) ),
00031              this, SLOT( slotError( QProcess::ProcessError ) ) );
00032     connect( &mProcess, SIGNAL( finished( int, QProcess::ExitStatus ) ),
00033              this, SLOT( slotFinished( int, QProcess::ExitStatus ) ) );
00034     connect( &mProcess, SIGNAL( readyReadStandardError() ),
00035              this, SLOT( slotErrorMessages() ) );
00036     connect( &mProcess, SIGNAL( readyReadStandardOutput() ),
00037              this, SLOT( slotStdoutMessages() ) );
00038 }
00039 
00040 ProcessControl::~ProcessControl()
00041 {
00042     stop();
00043 }
00044 
00045 bool ProcessControl::start( const QString &application, const QStringList &arguments, CrashPolicy policy, int maxCrash )
00046 {
00047     mFailedToStart = false;
00048 
00049     mApplication = application;
00050     mArguments = arguments;
00051     mPolicy = policy;
00052     mCrashCount = maxCrash;
00053 
00054     return start();
00055 }
00056 
00057 void ProcessControl::setCrashPolicy( CrashPolicy policy )
00058 {
00059     mPolicy = policy;
00060 }
00061 
00062 void ProcessControl::stop()
00063 {
00064     if ( mProcess.state() != QProcess::NotRunning ) {
00065         if ( !mProcess.waitForFinished( 30000 ) ) {
00066             mProcess.terminate();
00067         }
00068     }
00069 }
00070 
00071 void ProcessControl::slotError( QProcess::ProcessError error )
00072 {
00073     switch ( error ) {
00074     case QProcess::Crashed:
00075         // do nothing, we'll respawn in slotFinished
00076         break;
00077     case QProcess::FailedToStart:
00078     default:
00079         mFailedToStart = true;
00080         break;
00081     }
00082 
00083     qDebug( "ProcessControl: Application '%s' stopped unexpected (%s)",
00084             qPrintable( mApplication ), qPrintable( mProcess.errorString() ) );
00085 }
00086 
00087 void ProcessControl::slotFinished( int exitCode, QProcess::ExitStatus exitStatus )
00088 {
00089     if ( exitStatus == QProcess::CrashExit ) {
00090         if ( mPolicy == RestartOnCrash ) {
00091              // don't try to start an unstartable application
00092             if ( !mFailedToStart && --mCrashCount >= 0 )
00093                 start();
00094             else
00095                 emit finished(false);
00096         }
00097     } else {
00098         if ( exitCode != 0 ) {
00099             qDebug( "ProcessControl: Application '%s' returned with exit code %d (%s)",
00100                     qPrintable( mApplication ), exitCode, qPrintable( mProcess.errorString() ) );
00101             mFailedToStart = true;
00102             emit finished(false);
00103         } else {
00104             qDebug( "Application '%s' exited normally...", qPrintable( mApplication ) );
00105             emit finished(true);
00106         }
00107     }
00108 }
00109 
00110 void ProcessControl::slotErrorMessages()
00111 {
00112     QString message = QString::fromUtf8( mProcess.readAllStandardError() );
00113     emit processErrorMessages( message );
00114     qDebug( "[%s] %s", qPrintable( mApplication ), qPrintable( message.trimmed() ) );
00115 }
00116 
00117 bool ProcessControl::start()
00118 {
00119     mProcess.start( mApplication, mArguments );
00120     if ( !mProcess.waitForStarted( ) ) {
00121         qDebug( "ProcessControl: Unable to start application '%s' (%s)",
00122                 qPrintable( mApplication ), qPrintable( mProcess.errorString() ) );
00123         return false;
00124     }
00125     return true;
00126 }
00127 
00128 void ProcessControl::slotStdoutMessages()
00129 {
00130     QString message = QString::fromUtf8( mProcess.readAllStandardOutput() );
00131     qDebug() << mApplication << "[out]" << message;
00132 }
00133 
00134 bool ProcessControl::isRunning() const
00135 {
00136     return mProcess.state() == QProcess::Running;
00137 }
00138 
00139 #include "processcontrol.moc"

NepomukDaemons

Skip menu "NepomukDaemons"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

API Reference

Skip menu "API Reference"
  • KCMShell
  • KNotify
  • KStyles
  • Nepomuk Daemons
Generated for API Reference by doxygen 1.5.7
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal