cookbook.cpp
3.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#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();
}