add more spacing
[personal-kdebase.git] / runtime / nepomuk / server / processcontrol.cpp
blobc00b6f58ccdbf319fe7a4b86820c2dd021775fcd
1 /***************************************************************************
2 * Copyright (C) 2006 by Tobias Koenig <tokoe@kde.org> *
3 * Copyright (C) 2008 by Sebastian Trueg <trueg@kde.org> *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU Library General Public License as *
7 * published by the Free Software Foundation; either version 2 of the *
8 * License, or (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU Library General Public *
16 * License along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
21 #include "processcontrol.h"
23 #include <QtCore/QDebug>
24 #include <QtCore/QTimer>
27 ProcessControl::ProcessControl( QObject *parent )
28 : QObject( parent ), mFailedToStart( false ), mCrashCount( 0 )
30 connect( &mProcess, SIGNAL( error( QProcess::ProcessError ) ),
31 this, SLOT( slotError( QProcess::ProcessError ) ) );
32 connect( &mProcess, SIGNAL( finished( int, QProcess::ExitStatus ) ),
33 this, SLOT( slotFinished( int, QProcess::ExitStatus ) ) );
34 connect( &mProcess, SIGNAL( readyReadStandardError() ),
35 this, SLOT( slotErrorMessages() ) );
36 connect( &mProcess, SIGNAL( readyReadStandardOutput() ),
37 this, SLOT( slotStdoutMessages() ) );
40 ProcessControl::~ProcessControl()
42 stop();
45 bool ProcessControl::start( const QString &application, const QStringList &arguments, CrashPolicy policy, int maxCrash )
47 mFailedToStart = false;
49 mApplication = application;
50 mArguments = arguments;
51 mPolicy = policy;
52 mCrashCount = maxCrash;
54 return start();
57 void ProcessControl::setCrashPolicy( CrashPolicy policy )
59 mPolicy = policy;
62 void ProcessControl::stop()
64 if ( mProcess.state() != QProcess::NotRunning ) {
65 if ( !mProcess.waitForFinished( 30000 ) ) {
66 mProcess.terminate();
71 void ProcessControl::slotError( QProcess::ProcessError error )
73 switch ( error ) {
74 case QProcess::Crashed:
75 // do nothing, we'll respawn in slotFinished
76 break;
77 case QProcess::FailedToStart:
78 default:
79 mFailedToStart = true;
80 break;
83 qDebug( "ProcessControl: Application '%s' stopped unexpected (%s)",
84 qPrintable( mApplication ), qPrintable( mProcess.errorString() ) );
87 void ProcessControl::slotFinished( int exitCode, QProcess::ExitStatus exitStatus )
89 if ( exitStatus == QProcess::CrashExit ) {
90 if ( mPolicy == RestartOnCrash ) {
91 // don't try to start an unstartable application
92 if ( !mFailedToStart && --mCrashCount >= 0 )
93 start();
94 else
95 emit finished(false);
97 } else {
98 if ( exitCode != 0 ) {
99 qDebug( "ProcessControl: Application '%s' returned with exit code %d (%s)",
100 qPrintable( mApplication ), exitCode, qPrintable( mProcess.errorString() ) );
101 mFailedToStart = true;
102 emit finished(false);
103 } else {
104 qDebug( "Application '%s' exited normally...", qPrintable( mApplication ) );
105 emit finished(true);
110 void ProcessControl::slotErrorMessages()
112 QString message = QString::fromUtf8( mProcess.readAllStandardError() );
113 emit processErrorMessages( message );
114 qDebug( "[%s] %s", qPrintable( mApplication ), qPrintable( message.trimmed() ) );
117 bool ProcessControl::start()
119 mProcess.start( mApplication, mArguments );
120 if ( !mProcess.waitForStarted( ) ) {
121 qDebug( "ProcessControl: Unable to start application '%s' (%s)",
122 qPrintable( mApplication ), qPrintable( mProcess.errorString() ) );
123 return false;
125 return true;
128 void ProcessControl::slotStdoutMessages()
130 QString message = QString::fromUtf8( mProcess.readAllStandardOutput() );
131 qDebug() << mApplication << "[out]" << message;
134 bool ProcessControl::isRunning() const
136 return mProcess.state() == QProcess::Running;
139 #include "processcontrol.moc"