cookbook.cpp 3.12 KB
#include "cookbook.h"

#include <QApplication>
#include <QErrorMessage>

#include "config.h"

static QErrorMessage *errorDialog = NULL;
static void showError(QString errorMessage)
{
    if (errorDialog == NULL)
    {
        errorDialog = new QErrorMessage;
        errorDialog->setWindowModality(Qt::ApplicationModal);
        errorDialog->setGeometry(QRect(0, 426, 900, 426));
    }

    errorDialog->showMessage(errorMessage);
    errorDialog->exec();
}

CookBook::CookBook(Define::CookType type)
    : type(type)
{
    switch (type)
    {
    case Define::Poultry:
        root = QString("/prime/cookbook/poultry/");
        break;
    case Define::Meat:
        root = QString("/prime/cookbook/meat/");
        break;
    case Define::Fish:
        root = QString("/prime/cookbook/fish/");
        break;
    case Define::Desert:
        root = QString("/prime/cookbook/desert/");
        break;
    case Define::Vegetable:
        root = QString("/prime/cookbook/vegetable/");
        break;
    case Define::Bread:
        root = QString("/prime/cookbook/bread/");
        break;
    case Define::Etc:
        root = QString("/prime/cookbook/etc/");
        break;
    default:
        return;
    }

    QFile file(root + "list.csv");
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        showError("File not found: " + file.fileName());
        return;
    }

    int cookNameSection;
    Define::language_type lang = (Define::language_type) Config::getInstance()->getConfigValue(Define::config_language).d32;
    switch (lang)
    {
    case Define::language_kr:
        cookNameSection = 1;
        break;
    case Define::language_en:
        cookNameSection = 2;
        break;
    case Define::language_ch:
        cookNameSection = 3;
        break;
    default:
        cookNameSection = 1;
    }

    int lineCount = 0;
    while (!file.atEnd())
    {
        lineCount++;

        QString line = QString::fromUtf8(file.readLine()).trimmed();
        if (line.isEmpty())
            continue;

        if (line.startsWith("directory"))
            continue;

        QString errorMessage = QString("%3: %1, line %2").arg(file.fileName()).arg(lineCount);

        QString directory = line.section(',', 0, 0).trimmed();
        if (directory.isEmpty())
        {
            showError(errorMessage.arg("Directory name is missed"));
            continue;
        }

        QString cookname = line.section(',', cookNameSection, cookNameSection).trimmed();
        if (cookname.isEmpty())
        {
            showError(errorMessage.arg("Cook name is missed"));
            continue;
        }

        list.append(cookname);
        book.append(ListEntity { directory, cookname });
    }

    file.close();
}

Cook CookBook::get(int index)
{
    if (index < book.size())
    {
        ListEntity e = book.at(index);
        return Cook(type, root + e.directory, e.name);
    }

    return Cook();
}

QString CookBook::name(QString root)
{
    QString directory = root.replace(this->root, "").replace("/", "").trimmed();
    foreach (ListEntity e, book)
    {
        if (e.directory == directory)
            return e.name;
    }

    return QString();
}