/*****************************************************************************
 *
 *  task.h -- Task (command line) class.
 *
 *  Copyright (C) 2010 Starbuck <starbuck@labdv.com>
 *  All rights reserved.
 *
 *  This file is part of the Task module of the DVDx 4 application.
 *
 *  GNU Lesser General Public License Usage
 *  This file may be used under the terms of the GNU Lesser General
 *  Public License version 2.1 as published by the Free Software
 *  Foundation and appearing in the file LICENSE.LGPL included in the
 *  packaging of this file.  Please review the following information to
 *  ensure the GNU Lesser General Public License version 2.1 requirements
 *  will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
 *
 *  GNU General Public License Usage
 *  Alternatively, this file may be used under the terms of the GNU
 *  General Public License version 3.0 as published by the Free Software
 *  Foundation and appearing in the file LICENSE.GPL included in the
 *  packaging of this file.  Please review the following information to
 *  ensure the GNU General Public License version 3.0 requirements will be
 *  met: http://www.gnu.org/copyleft/gpl.html.
 *
 *****************************************************************************/

#ifndef TASK_H
#define TASK_H

#include <QList>
#include <QString>
#include <QStringList>
#include <QObject>
#include <QProcess>

class Task : public QObject
{
	Q_OBJECT

public:
	typedef enum { Idle = 0, Waiting, Submitting, Running, Terminated, Done, Aborted, Cancelled, StartFailure } State;

	Task();

	void setCommandLine(void *, const QString &, const QStringList &, 
		const QString &, const QString &, const QString &);

	inline void *getData() const { return data; };
	inline QString getCommand() const { return command; };
	inline QStringList getArguments() const { return arguments; };
	inline QString getCommandLine() const { return command + " " + arguments.join(" "); };
	inline QString getDirectory() const { return directory; };
	inline QString getName() const { return name; };
	inline QString getTitle() const { return title; };
	inline void *getJobRef() const { return (void *)jobRef; };
	inline QString getJobId() const { return jobId; };
	inline QString getJobName() const { return jobName; };
	inline Q_PID getPid() const { return pid; };
	inline State getState() const { return state; };
	inline void *getConsole() const { return tty; };
	inline void *getProcess() const { return process; };
	int getPidNumber() const;
	
	bool isFinished();
	bool isProcessing();

	void killParentProcess();
	void reset();
	void setJobInfo(void *, const QString &, const QString &);
	void setPid(const Q_PID);
	void setProcess(void *);
	void setState(const State);

	inline void attachConsole(void *tty) { this->tty = tty; };
	inline void detachConsole() { this->tty = 0; };

signals:
	void stateChanged();
	void killProcess();
	void stopProcess();

protected:
	void *data;				// application data
	QString command;
	QStringList arguments;
	QString directory;		// working dir
	QString name;			// friendly name
	QString title;			// Encoding/Encoding 1st-pass/Encoding 2nd-pass/Muxing/Ripping DVD
	void *jobRef;			// cross-reference pointer the Job (so is a Job*)
	QString jobId;
	QString jobName;
	void *process;			// cross-reference pointer the Process (so is a Process*)
	void *tty;

	Q_PID pid;
	State state;
};

#endif
