blob: 001b31748f24b6998eef0c8be40b4f270e5de146 (
plain)
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
|
#include <QMutexLocker>
#include <QFile>
#include <QElapsedTimer>
#include "filecopier.h"
FileCopier::FileCopier(QObject *parent) : QThread(parent), mCancel(false){}
void FileCopier::addJob(const QString &source, const QString &dest){
QMutexLocker l(&mAddJobMutex);
if(!mJobs.contains(source)){
mJobs[source] = dest;
}
}
QHash<QString, QString> FileCopier::jobs(){
QMutexLocker l(&mAddJobMutex);
return mJobs;
}
void FileCopier::run(){
int bufsize = 16 * 1024 * 1024;
char *buf = new char[bufsize];
mCancel = false;
QElapsedTimer et;
while(!mJobs.isEmpty()){
mAddJobMutex.lock();
auto first = mJobs.constBegin();
auto source = first.key();
auto dest = first.value();
mJobs.remove(source);
mAddJobMutex.unlock();
QFile sFile(source);
QFile dFile(dest);
bool openSource = sFile.open(QIODevice::ReadOnly);
bool openDest = dFile.open(QIODevice::WriteOnly);
if(!openSource || !openDest){
emit success(false, source);
goto cleanup;
}
emit newFile(source, dest, sFile.size());
int read = 0;
qint64 total = 0;
qint64 elapsed = 0;
qint64 bytesSinceEl = 0;
et.start();
while(!sFile.atEnd()){
mCancelMutex.lock();
bool cancel = mCancel;
mCancelMutex.unlock();
if(cancel){
QFile::remove(dest);
emit success(false, source);
goto cleanup;
}
read = sFile.read(buf, bufsize);
dFile.write(buf, read);
total += read;
bytesSinceEl += read;
elapsed += et.restart();
if(elapsed > 5000){ //5 seconds
emit bytesReadIntval(bytesSinceEl, elapsed);
elapsed = 0;
bytesSinceEl = 0;
}
emit bytesRead(total);
}
emit success(true, source);
}
cleanup:
delete buf;
}
void FileCopier::cancel(){
QMutexLocker l(&mCancelMutex);
mCancel = true;
}
|