-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhoimodel.cpp
298 lines (273 loc) · 9.41 KB
/
hoimodel.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#include "hoimodel.h"
#include <QSqlQuery>
#include <QFile>
#include <QSqlError>
#include "hoifileparser.h"
HoiModel::HoiModel(QObject *parent): QAbstractItemModel(parent)
{
m_db = QSqlDatabase::addDatabase("QSQLITE");
m_db.setDatabaseName("hoi.db");
m_db.open();
qInfo() << "db is open?:" << m_db.isOpen();
createTable();
m_saving = false;
m_restoring = false;
initListeningFiles();
}
void HoiModel::initData()
{
QSqlQuery query;
query.prepare("select `id`,`parent`,`src_path`,`file_path`,`update_time`,`player`,`ideology`,`date`,`difficulty`,`version`,`ironman`,`tag`,`depth` "
"from save_node "
"where src_path = ? "
"order by depth asc");
query.addBindValue(currentFileName());
if(query.exec()) {
beginResetModel();
m_rows.clear();
int lastDepth = 0;
QList<HoiSaveNode*> * saveNodes = new QList<HoiSaveNode*>();
m_rows.append(saveNodes);
QMap<int, HoiSaveNode*> nodemap;
while (query.next()) {
int depth = query.value(12).toInt();
if(depth > lastDepth) {
std::sort(saveNodes->begin(), saveNodes->end(), [](HoiSaveNode * node0, HoiSaveNode * node1) {
return node0->parentId() < node1->parentId();
});
saveNodes = new QList<HoiSaveNode*>();
m_rows.append(saveNodes);
lastDepth = depth;
}
HoiSaveNode* node = new HoiSaveNode(
query.value(0).toInt(),
query.value(1).toInt(),
query.value(2).toString(),
query.value(3).toString(),
query.value(4).toDateTime(),
query.value(5).toString(),
query.value(6).toString(),
query.value(7).toString(),
query.value(8).toString(),
query.value(9).toString(),
query.value(10).toString(),
query.value(11).toString(),
query.value(12).toInt()
);
HoiSaveNode * parentNode = nodemap.value(node->parentId(), nullptr);
if (parentNode) {
node->setParentNode(nodemap.value(node->parentId(), nullptr));
parentNode->appendChild(node);
}
nodemap.insert(node->id(), node);
saveNodes->append(
node
);
}
endResetModel();
} else {
qWarning() << query.lastError();
}
}
void HoiModel::createTable()
{
QSqlQuery sql;
qDebug() << sql.exec("create table if not exists `save_node` ("
"`id` integer NOT NULL,"
"`parent` integer DEFAULT NULL,"
"`src_path` TEXT,"
"`file_path` text,"
"`update_time` datetime,"
"`player` text,"
"`ideology` text,"
"`date` text,"
"`difficulty` text,"
"`version` text,"
"`ironman` text,"
"`tag` text,"
"`depth` integer,"
"PRIMARY KEY (`id`),"
"CONSTRAINT `file_path` UNIQUE (`file_path` ASC)"
");"
);
qDebug() << sql.lastError();
qDebug() << sql.exec(
"CREATE INDEX if not exists `idx_src_path`"
"ON `save_node` ("
"`src_path` COLLATE BINARY ASC"
"); ");
qDebug() << sql.lastError();
}
void HoiModel::initListeningFiles()
{
QSqlQuery query;
query.prepare("select src_path from save_node group by src_path order by id ");
QList<QString> resList;
if(query.exec()) {
while (query.next()) {
resList.append(query.value(0).toString());
}
}
setListeningFiles(resList);
}
void HoiModel::addSaveNodeFromPath(const QString &path)
{
if(saving()) {
qInfo() << "is parsing and saving node";
return;
}
setSaving(true);
HoiSaveNode* savenode = HoiFileParser::parse(path);
QProcess * process = HoiFileParser::save(savenode);
QObject::connect(process, &QProcess::finished, process, [ =, this ]() {
setSaving(false);
if(process->exitCode()) {
qWarning() << QString::fromLocal8Bit(process->readAllStandardError());
qInfo() << "save failed remove file:" << QDir(savenode->filePath()).remove(savenode->filePath());
process->close();
process->deleteLater();
return;
}
process->close();
process->deleteLater();
QSqlQuery selectLastUpdateNode;
selectLastUpdateNode.prepare("select `id`,`date`,`depth` "
"from save_node where src_path = ? "
"order by update_time desc limit 1");
selectLastUpdateNode.addBindValue(savenode->srcPath());
selectLastUpdateNode.exec();
selectLastUpdateNode.first();
int id = selectLastUpdateNode.value(0).toInt();
int depth = selectLastUpdateNode.value(2).toInt();
QString date = selectLastUpdateNode.value(1).toString();
qDebug() << date << savenode->date();
if(date == savenode->date()) {
qDebug() << "not need insert";
return;
}
savenode->setParentId(id);
savenode->setDepth(depth + 1);
QSqlQuery query;
query.prepare("insert into `save_node` (`parent`,`src_path`,`file_path`,`update_time`,`player`,`ideology`,`date`,`difficulty`,`version`,`ironman`, `depth`) "
"values (?,?,?,?,?,?,?,?,?,?,?);");
query.addBindValue(savenode->parentId());
query.addBindValue(savenode->srcPath());
query.addBindValue(savenode->filePath());
query.addBindValue(savenode->updateTime());
query.addBindValue(savenode->player());
query.addBindValue(savenode->ideology());
query.addBindValue(savenode->date());
query.addBindValue(savenode->difficulty());
query.addBindValue(savenode->version());
query.addBindValue(savenode->ironman());
query.addBindValue(savenode->depth());
if(!query.exec()) {
qWarning() << "insert save_node error:" << query.lastError();
} else {
qInfo() << "insert success";
initListeningFiles();
initData();
}
});
}
void HoiModel::restore(HoiSaveNode *node)
{
setRestoring(true);
QProcess* process = HoiFileParser::restore(node);
QObject::connect(process, &QProcess::finished, process, [ =, this ]() {
if(process->exitCode()) {
qWarning() << QString::fromLocal8Bit(process->readAllStandardError());
emit notify("回档失败", "解压文件失败" + process->readAllStandardError());
process->close();
process->deleteLater();
setRestoring(false);
return;
}
process->close();
process->deleteLater();
QSqlQuery query;
query.prepare("update save_node set update_time=? where id=?");
query.addBindValue(QDateTime::currentDateTime());
query.addBindValue(node->id());
if(!query.exec()) {
qCritical() << "update update_time failed";
emit notify("回档失败", "");
} else {
emit notify("回档成功", "");
};
setRestoring(false);
});
}
bool HoiModel::saving() const
{
return m_saving;
}
void HoiModel::setSaving(bool newSaving)
{
m_saving = newSaving;
}
QModelIndex HoiModel::index(int row, int column, const QModelIndex &parent) const
{
return createIndex(row, column);
}
QModelIndex HoiModel::parent(const QModelIndex &child) const
{
return QModelIndex();
}
int HoiModel::rowCount(const QModelIndex &parent) const
{
return m_rows.size();
}
int HoiModel::columnCount(const QModelIndex &parent) const
{
return 1;
}
QVariant HoiModel::data(const QModelIndex &index, int role) const
{
QVariant data("");
switch (role) {
case Qt::DisplayRole:
data.setValue( * (m_rows[index.row()]));
break;
default:
break;
}
return data;
}
QString HoiModel::currentFileName() const
{
return m_currentFileName;
}
void HoiModel::setCurrentFileName(const QString &newCurrentFileName)
{
if (m_currentFileName == newCurrentFileName) {
return;
}
m_currentFileName = newCurrentFileName;
emit currentFileNameChanged();
initData();
}
bool HoiModel::restoring() const
{
return m_restoring;
}
void HoiModel::setRestoring(bool newRestoring)
{
if (m_restoring == newRestoring) {
return;
}
m_restoring = newRestoring;
emit restoringChanged();
}
QList<QString> HoiModel::listeningFiles() const
{
return m_listeningFiles;
}
void HoiModel::setListeningFiles(const QList<QString> &newListeningFiles)
{
if (m_listeningFiles == newListeningFiles) {
return;
}
m_listeningFiles = newListeningFiles;
emit listeningFilesChanged();
}