#include "processmediainfo.h"
#include "src/view/modaldialog.h"
#include "src/data/datacdrom.h"
#include "src/po/translation.h"
#include <QDebug>
#include <QFileInfo>

ProcessMediaInfo::ProcessMediaInfo(void *d, QObject *parent) :
    ProcessBasic(parent),
    m_oCDROM(d),
    m_u64Size(0)
{
	m_oProcessName = _("Media Info");
    cloneCDROM = ((DataCDROM *)d)->clone();
}

bool ProcessMediaInfo::prepare()
{
    disconnect(this, SIGNAL(success(QString)), ModalDialog::instance()->getPageByIndex(ModalDialog::Tip), SIGNAL(success(QString)));
    disconnect(this, SIGNAL(fail(QString)), ModalDialog::instance()->getPageByIndex(ModalDialog::Tip), SIGNAL(fail(QString)));

    m_oProcess->setProgram("dvd+rw-mediainfo");
    m_oArguments << cloneCDROM->device();
    m_oProcess->setArguments(m_oArguments);

    return true;
}

void ProcessMediaInfo::slotsError()
{
    QString error = m_oProcess->readAllStandardError();
}

void ProcessMediaInfo::slotsOutput()
{
    int     i;
    unsigned long t;
    unsigned long u;
    QString output;
    QStringList ss;
    QStringList sss;

    if (m_u64Size) return;

    output = m_oProcess->readAllStandardOutput();
    ss = output.split("\n");
    for (i = ss.size() - 1; i > 0; --i)
    {
        if (ss.at(i).startsWith("READ FORMAT CAPACITIES:")) break;
    }

    if (cloneCDROM->mediumType().contains("DVD+RW"))
    {
        ss = ss.takeAt(i + 1).split("=");
        m_u64Size = ss.last().toULong();
    }

    if (cloneCDROM->mediumType().contains("DVD-RW"))
    {
        //解决bug:70940和83628擦除后总容量显示错误
        sss = ss.takeAt(i + 2).split("=");
        t = sss.last().toULong();
        m_u64Size = t;

    }
}

ProcessMediaInfo::~ProcessMediaInfo()
{
    if(m_oProcess){
         m_oProcess->close();
    }
}
