/*****************************************************************************
 *
 *  analyzer.cpp -- Base class for media analyzer interfaces.
 *
 *  Copyright (C) 2010 Starbuck <starbuck@labdv.com>
 *  All rights reserved.
 *
 *  This file is part of the JobManager 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 "analyzer.h"
#include <QDebug>
#include <QHashIterator>
#include <QProcessEnvironment>
#include <QString>
#include <QStringList>
#include <QTextEdit>

extern Console *console;

Analyzer::Analyzer()
{
	m_console = console;
}

QString Analyzer::interfaceExec()
{
	return m_interfaceExec;
}

QString Analyzer::interfaceName()
{
	return m_interfaceName;
}

QString Analyzer::interfacePath()
{
	return m_interfacePath;
}

int Analyzer::interfaceState()
{
	return m_interfaceState;
}

QString Analyzer::interfaceVersion()
{
	return m_interfaceVersion;
}

bool Analyzer::run(QStringList *args, QString *stdOut, QString *stdErr, QString *err)
{
	err->clear();

	QProcess process;

	if (!m_interfaceLibPath.isEmpty())
	{
		QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
		env.insert("LD_LIBRARY_PATH", m_interfaceLibPath);
		process.setProcessEnvironment(env);
	}

	process.start(m_interfacePath, *args);
		
	if (!process.waitForStarted(m_interfaceLatency.start))
	{
		*err = tr("process failed to start in %2 milliseconds");	
		process.close();
		return false;
	}

	if (!process.waitForFinished(m_interfaceLatency.finished))
	{
		process.kill();
		if (!process.waitForFinished(m_interfaceLatency.kill))
		{
			qDebug("Analyzer: process still running.");
		}
		else
		{
			qDebug("Analyzer: process killed.");
		}
	}
		
	*stdOut = process.readAllStandardOutput();
	*stdErr = process.readAllStandardError();

	// Report error if crashed and empty output
	if ( process.exitStatus() == QProcess::CrashExit )
	{
		if (stdOut->isEmpty() && stdErr->isEmpty())
		{
			*err = tr("process crashed with no output");	
			process.close();
			return false;
		}
		else
		{
			qDebug("Analyzer: process crashed.");
		}
	}

	process.close();
	return true;
}
