-
Notifications
You must be signed in to change notification settings - Fork 0
/
filedatabase.cpp
169 lines (139 loc) · 3.94 KB
/
filedatabase.cpp
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#include "filedatabase.h"
FileDataBase::FileDataBase(const QString filePath) :
m_filePath(filePath),
m_fileIsLoaded(false),
m_domDoc("AirLine_Seat_Allocation"),
m_watcher()
{
}
FileDataBase::~FileDataBase()
{
}
bool FileDataBase::writeData(const QString id, const bool taken)
{
// first read? lazy file loading
if (!m_fileIsLoaded) {
if (!loadData()) {
return false;
}
}
// update internal DB
m_seats[id] = taken;
// update DomDocument
QDomElement docElem = m_domDoc.documentElement();
QDomNodeList nodes = docElem.childNodes();
// remove node it exists
/// @note smartness needed: can I modify existing ones?
int found(-1);
for (unsigned int i = 0; i < nodes.length(); i++) {
QDomElement e = nodes.item(i).toElement();
if(!e.isNull() && e.attribute("id") == id) {
found = i;
}
}
if (found != -1) {
docElem.removeChild(nodes.item(found));
}
// add node
QDomElement cn = m_domDoc.createElement("seat");
cn.setAttribute( "id", id);
cn.setAttribute( "taken", taken);
docElem.appendChild(cn);
// write back DomDocument to file
QFile file(m_filePath);
if (!file.open(QIODevice::WriteOnly)) {
emit notification("Couldn't open file to write");
return false;
}
QTextStream ts( &file );
ts << m_domDoc.toString();
file.close();
return true;
}
bool FileDataBase::readData(const QString id, bool &taken)
{
// first read? lazy file loading
if (!m_fileIsLoaded) {
if (!loadData()) {
return false;
}
}
if (m_seats.find(id) != m_seats.end()) {
taken = m_seats[id];
return true;
}
// data not found
return false;
}
void FileDataBase::fileModified(const QString& path)
{
Q_UNUSED(path)
// calculate diff and emit dataChanged(id) signals
loadData(true);
}
bool FileDataBase::loadData(const bool calculateDiff)
{
QFile file(m_filePath);
// open file
if (!file.exists()) {
if (!initFile()) {
return false;
}
}
if (!file.open(QIODevice::ReadOnly)) {
emit notification("Couldn't read file");
return false;
}
// load DomDoc
if (!m_domDoc.setContent(&file)) {
emit notification("Couldn't parse XML file");
file.close();
return false;
}
file.close();
// load internal DB
QDomElement docElem = m_domDoc.documentElement();
QDomNodeList nodes = docElem.childNodes();
for (unsigned int i = 0; i < nodes.length(); i++) {
QDomElement e = nodes.item(i).toElement();
if(!e.isNull()) {
QString id(e.attribute("id"));
bool taken(e.attribute("taken") == "1" ? true : false);
if (calculateDiff) {
if (m_seats.find(id) == m_seats.end() ||
m_seats[id] != taken) {
// value has to be set before emitting the signal,
// since it will lead to a read call
m_seats[id] = taken;
emit dataChanged(id);
}
} else {
m_seats[id] = taken;
}
}
}
// file is loaded for the first time, listen to modifications
if (!m_fileIsLoaded) {
m_watcher.addPath(m_filePath);
connect(&m_watcher, SIGNAL(fileChanged(const QString&)),
this, SLOT(fileModified(const QString&)));
}
m_fileIsLoaded = true;
return true;
}
bool FileDataBase::initFile()
{
// create XML doc object
QDomElement root = m_domDoc.createElement("Seats");
m_domDoc.appendChild(root);
// write XML doc object to file
QFile file(m_filePath);
if (!file.open(QIODevice::WriteOnly)) {
emit notification("Couldn't create new file");
return false;
}
QTextStream ts( &file );
ts << m_domDoc.toString();
file.close();
return true;
}