/*****************************************************************************
 *
 *  task.cpp -- 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.
 *
 *****************************************************************************/

#include "task.h"
#include "macros.h"
#include <QDebug>

#ifdef WIN32
#include <wtypes.h>
#endif

class Process;

Task::Task() : QObject(), data(0)
{
	reset();
}

void Task::setCommandLine(void *data, const QString &cmd, const QStringList &args,
	const QString &dir, const QString &friendlyName, const QString &title) 
{
	reset();

	this->data = data;
	this->command = cmd;
	this->arguments = args;
	this->directory = dir;
	this->name = friendlyName;
	this->title = title;
	state = Waiting;
}
	
void Task::reset()
{
	state = Idle;
	pid = 0;
/*
	if (data)
	{
		delete(data);
		data = 0;
	}
*/
	command.clear();
	arguments.clear();
	directory.clear();
	name.clear();
	jobRef = 0;
	jobId.clear();
	jobName.clear();
	tty = 0;
	process = 0;
}

void Task::setState(const State state)
{ 
	this->state = state; 

	emit stateChanged();
};

void Task::killParentProcess()
{
	emit killProcess();
}

bool Task::isFinished() 
{ 
	return state == Done || state == Terminated 
		|| state == Aborted || state == Cancelled
		|| state == Idle || state == StartFailure; 
}

bool Task::isProcessing() 
{ 
	return state == Submitting || state == Running;
}

void Task::setJobInfo(void *ref, const QString &id, const QString &name)
{
	this->jobRef = ref;
	this->jobId = id;
	this->jobName = name;
}

void Task::setPid(const Q_PID pid)
{ 
	this->pid = pid; 
	
	if (pid == 0) state = Waiting;
}

void Task::setProcess(void *process)
{ 
	this->process = process; 
}

int Task::getPidNumber() const 
{
	if ( pid == 0 ) return 0;

#ifdef WIN32
	typedef struct _PROCESS_INFORMATION {
		HANDLE hProcess;
		HANDLE hThread;
		DWORD  dwProcessId;
		DWORD  dwThreadId;
	} PROCESS_INFORMATION, *LPPROCESS_INFORMATION;
	_PROCESS_INFORMATION *pidnum = (_PROCESS_INFORMATION *)pid;
	return int(pidnum->dwProcessId);
#else
	return int((qint64)pid);
#endif
}
