-
Notifications
You must be signed in to change notification settings - Fork 1
/
taskmanager.cpp
279 lines (260 loc) · 10.3 KB
/
taskmanager.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
#include "taskmanager.h"
//新建一个数据库用于存储任务信息并建表
void TaskManager::createConnection(QString account)
{
db = QSqlDatabase::addDatabase("QSQLITE","taskManager");
db.setDatabaseName(account+"_task.db");
//如果数据库不存在则创建数据库
if (!db.open())
{
qDebug() << "无法建立数据库连接";
return;
}
QSqlQuery query(db);
// 检查任务表是否存在
query.exec("SELECT name FROM sqlite_master WHERE type='table' AND name='uptask'");
if (!query.next()) {
// 如果任务表不存在,则创建任务表
query.exec("CREATE TABLE uptask (id INTEGER PRIMARY KEY,uploadId INTEGER, remotePath VARCHAR, localPath VARCHAR, dataSize BIGINT, totalPiece INTEGER, etags TEXT, isPause INTEGER)");
}
query.exec("SELECT name FROM sqlite_master WHERE type='table' AND name='downtask'");
if (!query.next()) {
query.exec("CREATE TABLE downtask (id INTEGER PRIMARY KEY, remotePath VARCHAR, localPath VARCHAR, dataSize BIGINT, totalPiece INTEGER, etags TEXT, isPause INTEGER)");
}
query.exec("SELECT name FROM sqlite_master WHERE type='table' AND name='finishtask'");
if (!query.next()) {
query.exec("CREATE TABLE finishtask (id INTEGER PRIMARY KEY,taskId INTEGER, remotePath VARCHAR, localPath VARCHAR, dataSize BIGINT, sycnTime DATE, status INTEGER)");
}
}
void TaskManager::insertUpTask(int uploadId,QString remotePath, QString localPath, quint64 dataSize, int totalPiece, QMap<int,QString> etags, bool isPause)
{
QSqlQuery query(db);
//检查任务信息是否已存在,如果存在则更新,不存在则插入
query.prepare("SELECT * FROM uptask WHERE localPath = :localPath");
query.bindValue(":localPath", localPath);
query.exec();
if(query.next())
{
updateUpTask(uploadId,remotePath, localPath, dataSize, totalPiece, etags, isPause);
return;
}
else{
query.prepare("INSERT INTO uptask (uploadId,remotePath, localPath, dataSize, totalPiece, etags, isPause) VALUES (:uploadId,:remotePath, :localPath, :dataSize, :totalPiece, :etags, :isPause)");
query.bindValue(":uploadId", uploadId);
query.bindValue(":remotePath", remotePath);
query.bindValue(":localPath", localPath);
query.bindValue(":dataSize", dataSize);
query.bindValue(":totalPiece", totalPiece);
query.bindValue(":etags", processEtag(etags));
query.bindValue(":isPause", isPause);
query.exec();
}
}
void TaskManager::insertDownTask(QString remotePath, QString localPath, quint64 dataSize, int totalPiece, QMap<int, QString> etags, bool isPause)
{
QSqlQuery query(db);
//检查任务信息是否已存在,如果存在则更新,不存在则插入
query.prepare("SELECT * FROM downtask WHERE localPath = :localPath");
query.bindValue(":localPath", localPath);
query.exec();
if(query.next())
{
updateDownTask(remotePath, localPath, dataSize, totalPiece, etags, isPause);
return;
}
else{
query.prepare("INSERT INTO downtask (remotePath, localPath, dataSize, totalPiece, etags, isPause) VALUES (:remotePath, :localPath, :dataSize, :totalPiece, :etags, :isPause)");
query.bindValue(":remotePath", remotePath);
query.bindValue(":localPath", localPath);
query.bindValue(":dataSize", dataSize);
query.bindValue(":totalPiece", totalPiece);
query.bindValue(":etags", processEtag(etags));
query.bindValue(":isPause", isPause);
query.exec();
}
}
bool TaskManager::insertFinishTask(int taskId,QString remotePath, QString localPath, quint64 dataSize, QDate sycnTime, int status)
{
QSqlQuery query(db);
//检查任务信息是否已存在,如果存在则更新,不存在则插入
query.prepare("SELECT * FROM finishtask WHERE localPath = :localPath");
query.bindValue(":localPath", localPath);
query.exec();
if(query.next())
{
updateFinishTask(taskId,remotePath, localPath, dataSize, sycnTime, status);
return false;
}
else{
query.prepare("INSERT INTO finishtask (taskId,remotePath, localPath, dataSize, sycnTime, status) VALUES (:taskId,:remotePath, :localPath, :dataSize, :sycnTime, :status)");
query.bindValue(":taskId", taskId);
query.bindValue(":remotePath", remotePath);
query.bindValue(":localPath", localPath);
query.bindValue(":dataSize", dataSize);
query.bindValue(":sycnTime", sycnTime);
query.bindValue(":status", status);
query.exec();
return true;
}
}
void TaskManager::deleteUpTask(QString localPath)
{
QSqlQuery query(db);
query.prepare("DELETE FROM uptask WHERE localPath = :localPath");
query.bindValue(":localPath", localPath);
query.exec();
}
void TaskManager::deleteDownTask(QString localPath)
{
QSqlQuery query(db);
query.prepare("DELETE FROM downtask WHERE localPath = :localPath");
query.bindValue(":localPath", localPath);
query.exec();
}
void TaskManager::deleteFinishTask(QString localPath)
{
QSqlQuery query(db);
query.prepare("DELETE FROM finishtask WHERE localPath = :localPath");
query.bindValue(":localPath", localPath);
query.exec();
}
void TaskManager::updateUpTask(int uploadId,QString remotePath, QString localPath, quint64 dataSize, int totalPiece, QMap<int, QString> etags, bool isPause)
{
QSqlQuery query(db);
query.prepare("UPDATE uptask SET uploadId = :uploadId,remotePath = :remotePath, dataSize = :dataSize, totalPiece = :totalPiece, etags = :etags, isPause = :isPause WHERE localPath = :localPath");
query.bindValue(":uploadId", uploadId);
query.bindValue(":remotePath", remotePath);
query.bindValue(":localPath", localPath);
query.bindValue(":dataSize", dataSize);
query.bindValue(":totalPiece", totalPiece);
query.bindValue(":etags", processEtag(etags));
query.bindValue(":isPause", isPause);
query.exec();
}
void TaskManager::updateDownTask(QString remotePath, QString localPath, quint64 dataSize, int totalPiece, QMap<int, QString> etags, bool isPause)
{
QSqlQuery query(db);
query.prepare("UPDATE downtask SET remotePath = :remotePath, dataSize = :dataSize, totalPiece = :totalPiece, etags = :etags, isPause = :isPause WHERE localPath = :localPath");
query.bindValue(":remotePath", remotePath);
query.bindValue(":localPath", localPath);
query.bindValue(":dataSize", dataSize);
query.bindValue(":totalPiece", totalPiece);
query.bindValue(":etags", processEtag(etags));
query.bindValue(":isPause", isPause);
query.exec();
}
void TaskManager::updateFinishTask(int taskId,QString remotePath, QString localPath, quint64 dataSize, QDate sycnTime, int status)
{
QSqlQuery query(db);
query.prepare("UPDATE finishtask SET taskId = :taskId,remotePath = :remotePath, dataSize = :dataSize, sycnTime = :sycnTime, status = :status WHERE localPath = :localPath");
query.bindValue(":taskId", taskId);
query.bindValue(":remotePath", remotePath);
query.bindValue(":localPath", localPath);
query.bindValue(":dataSize", dataSize);
query.bindValue(":sycnTime", sycnTime);
query.bindValue(":status", status);
query.exec();
}
QList<upTask> TaskManager::readUpTask()
{
QList<upTask> tasks;
QSqlQuery query(db);
query.exec("SELECT * FROM uptask");
while (query.next())
{
upTask task;
task.uploadId = query.value(1).toInt();
task.remotePath = query.value(2).toString();
task.localPath = query.value(3).toString();
task.dataSize = query.value(4).toULongLong();
task.totalPiece = query.value(5).toInt();
task.etags = processJson(query.value(6).toString());
task.isPause = query.value(7).toBool();
tasks.append(task);
}
return tasks;
}
QList<downTask> TaskManager::readDownTask()
{
QList<downTask> tasks;
QSqlQuery query(db);
query.exec("SELECT * FROM downtask");
while (query.next())
{
downTask task;
task.remotePath = query.value(1).toString();
task.localPath = query.value(2).toString();
task.dataSize = query.value(3).toULongLong();
task.totalPiece = query.value(4).toInt();
task.etags = processJson(query.value(5).toString());
task.isPause = query.value(6).toBool();
tasks.append(task);
}
return tasks;
}
QList<finishTask> TaskManager::readFinishTask()
{
QList<finishTask> tasks;
QSqlQuery query(db);
query.exec("SELECT * FROM finishtask");
while (query.next())
{
finishTask task;
task.taskId = query.value(1).toInt();
task.remotePath = query.value(2).toString();
task.localPath = query.value(3).toString();
task.dataSize = query.value(4).toULongLong();
task.sycnTime = query.value(5).toDate();
task.status = query.value(6).toInt();
tasks.append(task);
}
return tasks;
}
finishTask TaskManager::getFinishTask(QString localPath)
{
QSqlQuery query(db);
finishTask task;
query.prepare("SELECT * FROM finishtask WHERE localPath = :localPath");
query.bindValue(":localPath", localPath);
query.exec();
if(query.next())
{
task.taskId = query.value(1).toInt();
task.remotePath = query.value(2).toString();
task.localPath = query.value(3).toString();
task.dataSize = query.value(4).toULongLong();
task.sycnTime = query.value(5).toDate();
task.status = query.value(6).toInt();
}
return task;
}
void TaskManager::closeConnection()
{
QSqlDatabase::database().close();
}
QMap<int, QString> TaskManager::processJson(QString etags)
{
QMap<int, QString> etagMap;
QJsonDocument jsonDocument = QJsonDocument::fromJson(etags.toUtf8());
QJsonArray jsonArray = jsonDocument.array();
for(int i = 0; i < jsonArray.size(); i++)
{
QJsonObject jsonObject = jsonArray.at(i).toObject();
etagMap.insert(jsonObject.keys().at(0).toInt(), jsonObject.value(jsonObject.keys().at(0)).toString());
}
return etagMap;
}
QString TaskManager::processEtag(QMap<int, QString> etags)
{
// 把etag转化为json格式
QJsonArray jsonArray;
for(auto it = etags.begin(); it != etags.end(); it++)
{
QJsonObject jsonObject;
jsonObject.insert(QString::number(it.key()), it.value());
jsonArray.append(jsonObject);
}
QJsonDocument jsonDocument;
jsonDocument.setArray(jsonArray);
return jsonDocument.toJson();
}