#include "utils.h"
#include "libkysysinfo.h"

#include <QStringList>

QString SystemInfo::m_os;
QString SystemInfo::m_arch;
QString Utils::m_burnerVersion = "";

QString Utils::getVersion()
{
    if (!m_burnerVersion.isEmpty())
        return m_burnerVersion;

    size_t len = 0;
    ssize_t read;
    char *q = nullptr;
    QString version;

    FILE *pp = popen("dpkg -l kylin-burner", "r");
    if(!pp)
        return version;

    char *line = nullptr;
    while((read = getline(&line, &len, pp)) != -1){
        q = strrchr(line, '\n');
        *q = '\0';

        QString content = line;
        QStringList list = content.split(" ");

        list.removeAll("");

        if (list.size() >= 3)
            version = list.at(2);
    }

    free(line);
    pclose(pp);
    m_burnerVersion = version;
    return version;
}

QString Utils::getArch()
{
    char *cArch;
    QString sArch;
    cArch = kdk_system_get_architecture();
    if (cArch) {
        sArch = QString(cArch);
        free(cArch);
    }
    return sArch;
}

QString Utils::getOs()
{
    char *cSystem;
    QString sSystem;
    cSystem = kdk_system_get_systemName();
    if (cSystem) {
        sSystem = QString(cSystem);
        free(cSystem);
    }
    return sSystem;
}

void Utils::initSystemInfo()
{
    SystemInfo::m_os = Utils::getOs();
    SystemInfo::m_arch = Utils::getArch();

}
