-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcopyworker.cpp
More file actions
162 lines (149 loc) · 4.82 KB
/
copyworker.cpp
File metadata and controls
162 lines (149 loc) · 4.82 KB
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
#include "copyworker.h"
CopyWorker::CopyWorker(const QString &src, const QString &dst, QDialog *window, QObject *parent) : QObject(parent)
{
if (window != nullptr && !src.isEmpty() && !dst.isEmpty()){
Window = window;
source = src;
destination = dst;
}
}
bool CopyWorker::Stop() const
{
return m_Stop;
}
bool CopyWorker::Pause() const
{
return m_Pause;
}
void CopyWorker::setOptions(const copy_options &value)
{
options = value;
}
void CopyWorker::setMergeFolders(int value)
{
mergeFolders = value;
}
void CopyWorker::copy_recursively(const QString &source, const QString &destination, error_code &code)
{
QDir dir(source);
QFileInfoList list = dir.entryInfoList(QDir::AllEntries | QDir::NoDotAndDotDot);
for (int i = 0; i < list.count(); ++i){
QFileInfo fileInfo = list.at(i);
if (fileInfo.isFile()){
while(Pause()){
QThread::sleep(1);
}
if (Stop()){
return;
}
QString newFilePath = destination + "/" + fileInfo.fileName();
qDebug() << "newFilePath: " << newFilePath;
if (QFile::exists(newFilePath)){
emit askForCopyOptionsOverwriteFile(fileInfo.absoluteFilePath(), newFilePath);
pause();
}
copy(fileInfo.canonicalFilePath().toStdString(), destination.toStdString(), options, code);
if (code.value() != 0){
return;
}
}
else if (fileInfo.isDir()){
QString dstPath = destination + "/" + fileInfo.fileName();
QString srcPath = source + "/" + fileInfo.fileName();
QDir newDir(dstPath);
if (!newDir.exists()){
if(!newDir.mkpath(dstPath)){
code.assign(1, generic_category());
return;
}
}
else {
emit askForCopyOptionsMergeFolders(srcPath, dstPath);
pause();
if (mergeFolders == QMessageBox::No){
continue;
}
}
copy_recursively(fileInfo.canonicalFilePath(), dstPath, code);
}
}
}
void CopyWorker::pause()
{
setPause(true);
while(Pause()){
QThread::sleep(1);
}
}
void CopyWorker::start_copying()
{
error_code error;
QFileInfo fileInfo;
fileInfo.setFile(source);
if (is_directory(source.toStdString())){
QString dirName = fileInfo.fileName();
QDir dir(destination);
dir.mkdir(dirName);
fileInfo.setFile(destination + "/" + dirName);
copy_recursively(source, fileInfo.absoluteFilePath(), error);
}
else{
QString newFilePath = destination + "/" + fileInfo.fileName();
if (QFile::exists(newFilePath)){
emit askForCopyOptionsOverwriteFile(fileInfo.absoluteFilePath(), newFilePath);
pause();
}
copy(source.toStdString(), destination.toStdString(), options, error);
}
if (error.value() != 0){
emit errorSignal("Something happened during operation. Code: " + QString::number(error.value()) +
" Message: " + QString::fromStdString((error.message())));
}
emit copying_finished();
}
void CopyWorker::start_moving()
{
error_code error_copy;
error_code error_delete;
QFileInfo fileInfo;
fileInfo.setFile(source);
if (is_directory(source.toStdString())){
QString dirName = fileInfo.fileName();
QDir dir(destination);
dir.mkdir(dirName);
fileInfo.setFile(destination + "/" + dirName);
copy_recursively(source, fileInfo.absoluteFilePath(), error_copy);
}
else{
QString newFilePath = destination + "/" + fileInfo.fileName();
if (QFile::exists(newFilePath)){
emit askForCopyOptionsOverwriteFile(fileInfo.absoluteFilePath(), newFilePath);
pause();
}
copy(source.toStdString(), destination.toStdString(), options, error_copy);
}
if (error_copy.value() != 0){
emit errorSignal("Something happened during operation. Code: " + QString::number(error_copy.value()) +
" Message: " + QString::fromStdString((error_copy.message())));
}
remove_all(source.toStdString(), error_delete);
if (error_delete.value() != 0){
emit errorSignal("Something happened during operation. Code: " + QString::number(error_delete.value()) +
" Message: " + QString::fromStdString((error_delete.message())));
}
emit copying_finished();
}
void CopyWorker::setStop(bool Stop)
{
if (m_Stop == Stop)
return;
m_Stop = Stop;
emit StopChanged(m_Stop);
}
void CopyWorker::setPause(bool Pause)
{
if (m_Pause == Pause)
return;
m_Pause = Pause;
emit PauseChanged(m_Pause);
}