/*
 * process.h
 *
 * Copyright (c) 2003 Frerich Raabe <raabe@kde.org>
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. For licensing and distribution details, check the
 * accompanying file 'COPYING'.
 */
#ifndef PROCESS_H
#define PROCESS_H

#include <kdialogbase.h>
#include <kprocess.h>

#include <signal.h>

class KTempFile;
class KTextEdit;

namespace Barry
{
	class Process : public QObject
	{
		Q_OBJECT
		public:
			enum Communication {
				NoCommunication = KProcess::NoCommunication,
				Stdout = KProcess::Stdout,
				Stderr = KProcess::Stderr
			};

			static QString execute( const QString &command,
			                        Communication = NoCommunication,
			                        bool *ok = 0 );
			static QString execute( const QStringList &command,
			                        Communication = NoCommunication,
			                        bool *ok = 0 );

			Process();

			QString runBlocking( const QString &command,
			                     Communication = NoCommunication,
			                     bool *ok = 0 );
			QString runBlocking( const QStringList &command,
			                     Communication = NoCommunication,
			                     bool *ok = 0 );
			void run( const QString &command,
			          Communication comm = NoCommunication );
			void run( const QStringList &command,
			          Communication comm = NoCommunication );
			bool kill( int signo = SIGTERM );

		signals:
			void receivedStdout( Process *proc, const QString &text );
			void receivedStderr( Process *proc, const QString &text );
			void finished( Process *proc, bool ok );

		private slots:
			void slotReceivedStdout( KProcess *proc, char *data, int len );
			void slotReceivedStderr( KProcess *proc, char *data, int len );
			void slotReceivedOutput( KProcess *proc, char *data, int len );
			void slotProcessExited( KProcess *proc );

		private:
			KProcess *m_process;
#if __FreeBSD__ == 5
			KTempFile *m_stdoutTempFile;
			KTempFile *m_stderrTempFile;
#endif
			QString m_buffer;
	};

	class ProcessMonitor : public KDialogBase
	{
		Q_OBJECT
		public:
			ProcessMonitor( QWidget *parent, const char *name = 0 );
			virtual ~ProcessMonitor();

			Process &process() { return *m_process; }
			const Process &process() const { return *m_process; }

		protected slots:
			virtual void slotCancel();

		private slots:
			void receivedStdout( Process *process, const QString &data );
			void receivedStderr( Process *process, const QString &data );
			void finished( Process *process, bool ok );

		private:
			Process *m_process;
			KTextEdit *m_outputView;
	};
};

#endif // BARRY_PROCESS_H
// vim:ts=4:sw=4:noet:list
