/*
Copyright (c) 2010 by Drake Justice <djustice.kde@gmail.com>

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

*/

#include "burnthread.h"

#include <QDebug>
#include <QProcess>
#include <QTimer>

BurnThread::BurnThread(const QString& tmpDir)
{
    m_tmpDir = tmpDir;
    m_process = new QProcess;
    m_timer = new QTimer;
}

void BurnThread::run()
{
    m_process->setProcessChannelMode(QProcess::MergedChannels);
    m_process->start("wodim -v -overburn -dev=/dev/dvd \"" + m_tmpDir + "/dvd.iso\"");

    connect(m_process, SIGNAL(finished(int)), this, SLOT(burnFinished()));
    connect(m_timer, SIGNAL(timeout()), this, SLOT(updateProgress()));
    m_timer->start(800);
}

void BurnThread::updateProgress()
{
    QString out = m_process->readAll();
    qDebug() << out;
    foreach (QString str, out.split("\n")) {
        if(str.contains("written (fifo")) {
            QStringList parts = str.simplified().trimmed().split(' ', QString::SkipEmptyParts);
            emit progressMax(parts.at(4).toInt());
            emit progress(parts.at(2).toInt());
        }
    }
}

void BurnThread::burnFinished(/* int */) // catch this error_code int
{
    m_timer->stop();
    emit finished();
}
