#include "processisoinfo.h"
#include "src/view/modaldialog.h"
#include "src/data/datacdrom.h"
#include "src/po/translation.h"
#include "src/frame/logger.h"

ProcessISOInfo::ProcessISOInfo(void *d, QObject *parent) :
    ProcessBasic(parent),
    m_oCDROM(d)
{
    m_oProcessName = _("load");
    m_bIsEmpty = true;
}

bool ProcessISOInfo::prepare()
{
    DataCDROM *cdrom;
    cdrom = static_cast<DataCDROM *>(m_oCDROM);
    if (!cdrom) {
        LOGGER->error(_("ERROR:ISOINFO-PREPARE"));
        return false;
    }
    m_oProcess->setProgram("isoinfo");
    m_oArguments << "-d" << "-i" << cdrom->device();
    return true;
}

void ProcessISOInfo::slotsError()
{
    QString error;

    error = m_oProcess->readAllStandardError();
    if (error.contains("CD-ROM is NOT in ISO 9660 format")) {
        m_bIsEmpty = true;
    }
}

void ProcessISOInfo::slotsOutput()
{
    QString s;
    QStringList st;

    QString output = m_oProcess->readAllStandardOutput();
    if (output.contains("CD-ROM is NOT in ISO 9660 format")) {
        m_bIsEmpty = true;
    }

    QStringList ss = output.split("\n");
    DataCDROM *cdrom = static_cast<DataCDROM *>(m_oCDROM);

    foreach (s, ss)
    {
        if (s.startsWith("Volume id:"))
        {
            m_bIsEmpty = false;
            st = s.split(":");
        }
        else if (s.startsWith("Publisher id:"))
        {
            m_bIsEmpty = false;
            st = s.split(":");
        }
        else if (s.startsWith("Data preparer id:"))
        {
            m_bIsEmpty = false;
            st = s.split(":");
        }
        else if (s.startsWith("Copyright File id:"))
        {
            m_bIsEmpty = false;
            st = s.split(":");
        }
    }
}

ProcessISOInfo::~ProcessISOInfo()
{}
