-
Notifications
You must be signed in to change notification settings - Fork 0
/
copythread.hpp
74 lines (61 loc) · 2.42 KB
/
copythread.hpp
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
#include <QThread>
#include <QFileInfo>
#include <QDir>
class CopyThread : public QThread {
Q_OBJECT
signals:
void updateProgress(qint64 current, qint64 total);
void copyFinished(bool success);
void copyCancelled();
public:
CopyThread(QString source, QString destination) :
m_source(source), m_destination(destination) {}
void cancelCopy() {
m_cancelled = true;
}
protected:
void run() {
QFileInfo sourceFileInfo(m_source);
if (sourceFileInfo.isDir()) {
QDir destinationDir(m_destination);
destinationDir.mkdir(sourceFileInfo.fileName());
QDir sourceDir(m_source);
QStringList fileList = sourceDir.entryList(QDir::Files | QDir::Dirs | QDir::NoDotAndDotDot);
for (int i = 0; i < fileList.size(); i++) {
QString fileName = fileList.at(i);
QString sourceFilePath = m_source + "/" + fileName;
QString destinationFilePath = m_destination + "/" + sourceFileInfo.fileName() + "/" + fileName;
QFileInfo fileInfo(sourceFilePath);
if (fileInfo.isDir()) {
CopyThread copyThread(sourceFilePath, destinationFilePath);
connect(©Thread, SIGNAL(updateProgress(qint64, qint64)), this, SIGNAL(updateProgress(qint64, qint64)));
connect(©Thread, SIGNAL(copyFinished(bool)), this, SIGNAL(copyFinished(bool)));
connect(this, SIGNAL(copyCancelled()), ©Thread, SLOT(cancelCopy()));
copyThread.start();
copyThread.wait();
} else {
if (m_cancelled) {
emit copyCancelled();
return;
}
if (!QFile::copy(sourceFilePath, destinationFilePath)) {
emit copyFinished(false);
return;
}
emit updateProgress(sourceFileInfo.size(), sourceFileInfo.size());
}
}
} else {
if (!QFile::copy(m_source, m_destination)) {
emit copyFinished(false);
return;
}
emit updateProgress(sourceFileInfo.size(), sourceFileInfo.size());
}
emit copyFinished(true);
}
private:
QString m_source;
QString m_destination;
bool m_cancelled = false;
};