#include "processmd5sum.h"
#include "src/frame/logger.h"
#include "src/data/datacdrom.h"
#include "src/po/translation.h"
#include "src/view/modaldialog.h"
#include "src/frame/auditlog.h"
#include <kysdk/kysdk-system/libkysysinfo.h>
#include <QDir>
#include <QFileInfo>
#include <QCoreApplication>

ProcessMd5sum::ProcessMd5sum(void *cdrom, QString checkFile, QObject *parent) :
    ProcessBasic(parent),
    m_oCDROM(cdrom),
    m_oCheckFile(checkFile),
    m_iProgress(0)
{
    m_oProcessName = _("Disk Check");
    LOGGER->registerName(this, _("PROCESS_DC"));
    title(_("Checking disk"));
}

bool ProcessMd5sum::prepare()
{
    DataCDROM *cdrom;

    PROGRESS;
    MODAL->show();

    cdrom = static_cast<DataCDROM *>(m_oCDROM);
    if (cdrom->mediumMountPoint().isEmpty()) return false;
    QDir::setCurrent(cdrom->mediumMountPoint());
    m_oProcess->setProgram("md5sum");
    m_oArguments << "-c";
    m_oArguments << m_oCheckFile;
    m_oProcess->setArguments(m_oArguments);
    setValue(++m_iProgress, m_oIcon, _("Checking..."));
    return true;
}

bool ProcessMd5sum::worked()
{
    if (m_bCancel) return false;
    if (QProcess::NormalExit != m_oProcess->exitStatus()) return false;
    return m_oKeep;
}


void ProcessMd5sum::slotsOutput()
{
    QString output;
    QStringList ss;

    output = m_oProcess->readAllStandardOutput();
    ss = output.split("\n");
    for(int i = 0; i < ss.size();i++)
    {
	info(ss.at(i));
    }
    setValue(++m_iProgress, m_oIcon, _("Checking..."));
    if (output.contains(_("Fail")) || output.contains(_("FAILED")))
    {
        m_oKeep = false;
        m_oProcess->kill();
        QDir::setCurrent(QDir::homePath());
    }
}

void ProcessMd5sum::slotsError()
{
    QString error;
    QStringList ss;
    m_oKeep = false;
    error = m_oProcess->readAllStandardError();
    ss = error.split("\n");
    this->error(_("Error happened when checking disk,content: %1").arg(error));
}

void ProcessMd5sum::slotsFinished(int errCode, QProcess::ExitStatus status)
{
    if (QProcess::NormalExit == status && 0 == errCode) {
        info( QString("file:[%1] line:[%2] : slotsFinished() Medium md5sum data success.").arg(__FILE__).arg(__LINE__) );
        QDir::setCurrent(QDir::homePath());
    }
    else
    {
        m_oKeep = false;
        error( QString("file:[%1] line:[%2] : slotsFinished() Medium md5sum data failure. [%3]").arg(__FILE__).arg(__LINE__).arg(m_oProcess->errorString()) );
        QDir::setCurrent(QDir::homePath());
    }

    bool m_isSpecialMachine;
     AuditLog md5checkLog;
  //  m_isSpecialMachine=kdk_system_is_zyj();
    auditburner=new QGSettings("org.ukui.kylinburner");
    auditlogpath="/var/log/kylin-burner/os_audit.log";
    m_isSpecialMachine=auditburner->get("burneraudit-isrunning").toBool();
    if(m_isSpecialMachine==true){
        auditburner->set("burner-os-auditlog-name",auditlogpath);
        DataCDROM* cdrom = (DataCDROM*)m_oCDROM;
        md5checkLog.outputResult(cdrom->display(), cdrom->mediumType(), m_oProcessName, true==m_oKeep);
    }else{
        auditburner->set("burner-os-auditlog-name","");
    }
}

ProcessMd5sum::~ProcessMd5sum()
{}
