/*
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 "mkisothread.h"

#include <QFileInfo>
#include <QDebug>
#include <QDir>

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

void MkISOThread::run()
{
    m_process->setProcessChannelMode(QProcess::MergedChannels);
    m_process->start("mkisofs -dvd-video -o \"" + m_tmpDir + "/dvd.iso\" \"" + m_tmpDir + "/dvd\"");

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

void MkISOThread::updateProgress()
{
    QString out = m_process->readAll();
    qDebug() << out;
    foreach (QString str, out.split("\n")) {
        if (str.contains("estimate")) {
            emit progressMax(100);
            QString mb = str.split(".", QString::SkipEmptyParts).at(0);
            emit progress(mb.toInt());
        }
    }
}

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