-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcopydialog.cpp
More file actions
219 lines (188 loc) · 6.94 KB
/
copydialog.cpp
File metadata and controls
219 lines (188 loc) · 6.94 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
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
#include <QtGui>
#include <QMessageBox>
#include <QDir>
#include <QFile>
#include <QFileInfo>
#include <QFileInfoList>
#include <QCoreApplication>
#include "copydialog.h"
/**
CopyDialog::CopyDialog()
Go through the directories in the list and copy the whole source directory
tree to the respective index in the target dir
*/
CopyDialog::CopyDialog(const QStringList & sourceDirs,
const QString & targetDir)
{
this->sourceDirs = sourceDirs;
this->targetDir = targetDir;
QProgressDialog();
qDebug() << "Copy dialog for" << sourceDirs.count() << "files into"
<< targetDir;
}
/**
CopyDialog::dirSize()
determine the size of the whole directory tree
may take a while!
*/
long CopyDialog::dirSize(const QString & dirPath)
{
qDebug() << "CopyDialog::dirSize() << " << dirPath;
QFileInfo fileInfo;
fileInfo = QFileInfo(dirPath);
if (fileInfo.exists() && fileInfo.isFile()) {
qDebug() << fileInfo.fileName() << "=" << fileInfo.size() << "B";
return (long)fileInfo.size();
}
QDir dir(dirPath);
if (!dir.exists()) {
return 0;
}
long sum = 0;
foreach (fileInfo, dir.entryInfoList(QDir::NoDotAndDotDot |
QDir::Dirs | QDir::Files)) {
if (fileInfo.isDir()) {
sum += dirSize(fileInfo.absoluteFilePath());
} else if (fileInfo.isFile()) {
qDebug() << fileInfo.fileName() << "=" << fileInfo.size() << "B";
sum += (long)fileInfo.size();
}
}
qDebug() << "dirSize of " << dirPath << "is" << sum;
return sum;
}
bool CopyDialog::process()
{
//determine the size of the directory structure to be copied
//for showing the accurate progress bar
QString current;
long treeSize = 0;
foreach (current, sourceDirs) {
qDebug() << "CopyDialog::process(): dirSize(" << current << ")";
treeSize += dirSize(current);
}
qDebug() << treeSize/1e6 << " Mb to copy";
QProgressDialog(tr("File copy"), tr("Abort"), 0, treeSize/1e6);
foreach (current, sourceDirs) {
qDebug() << "copying tree" << current << " >> " << targetDir;
if (!copyDir(current, targetDir) || wasCanceled()) {
break;
}
}
setValue(maximum());
return true;
}
/**
CopyDialog::copyDir()
copy the whole directory tree from somewhere to somewhere else
*/
bool CopyDialog::copyDir(const QString & sourceDir,
const QString & targetDir)
{
qDebug() << "CopyDialog::copyDir(" << sourceDir << "," << targetDir << ")";
if (sourceDir == targetDir) {
qDebug() << "copyDir(): source == target";
return true;
}
QDir srcDir(sourceDir);
QFileInfo srcInfo(sourceDir);
QDir destDir(targetDir);
QString msg;
if (!destDir.exists()) {
if (!destDir.mkpath(destDir.absolutePath())) {
msg.append(tr("Could not create destination directory %1").
arg(destDir.absolutePath()));
QMessageBox::critical(this, "Copy Dir", msg);
qDebug() << "Could not create the destination directory"
<< destDir.absolutePath();
return false;
}
}
if (!srcInfo.isDir()) {
//maybe it is file
qDebug() << "CopyDir()" << sourceDir << "is not a directory";
if (srcInfo.isFile()) {
if (destDir.exists(srcInfo.fileName())) {
if (QMessageBox::warning(0, tr("Copy overwrite"),
tr("The file %1 exists in %2. Do you want to overwrite it?").
arg(srcInfo.fileName()).arg(destDir.absolutePath()),
QMessageBox::Yes|QMessageBox::No,
QMessageBox::No) == QMessageBox::Yes) {
//file exists, overwrite?
destDir.remove(srcInfo.fileName());
} else {
return true;
}
}
if (srcInfo.size() > 1e6) {
setValue(value()+(int)(srcInfo.size()/1e6));
}
qDebug() << "copying " << sourceDir << "to" <<
QDir::toNativeSeparators(
destDir.absolutePath().
append("/").append(srcInfo.fileName()));
return QFile::copy(sourceDir, QDir::toNativeSeparators(
destDir.absolutePath().
append("/").append(srcInfo.fileName())));
} else {
qDebug() << srcInfo.fileName() << "is not a file";
return false;
}
} //isDir() for src
// get info on all files in source directory
QFileInfoList srcList = srcDir.entryInfoList(
QDir::Dirs|QDir::Files|QDir::Readable|QDir::NoDotAndDotDot);
qDebug() << "processing" << srcList.count() << "entries";
QFileInfo di;
foreach (di, srcList) {
qDebug() << "Copying file" << di.absoluteFilePath();
msg = QString("Copying file\"%1\"...").
arg(di.absoluteFilePath());
setLabelText(msg);
if (wasCanceled()) {
qDebug() << "operation cancelled by user";
break;
}
if (di.isDir()) {
if (copyDir(di.absoluteFilePath(),
QString(targetDir).append(QDir::separator()).
append(di.fileName()))) {
continue;
} else {
qDebug() << "CopyDialog::CopyDirs() error: " <<
di.absoluteFilePath();
return false;
}
} else if (di.isFile()) {
if (!di.isReadable()) {
//implicit behaviour is to copy everything possible
qDebug() << di.absoluteFilePath() << "not readable!";
continue;
}
QFile in(di.absoluteFilePath());
QFile out(QDir::toNativeSeparators(QString(destDir.absolutePath()).
append("/").append(di.fileName())));
if (out.exists() &&
QMessageBox::warning(0, tr("Copy overwrite"),
tr("The file %1 exists. Do you want to overwrite it?").
arg(out.fileName()), QMessageBox::Yes|QMessageBox::No,
QMessageBox::No) == QMessageBox::No) {
//file exists, overwrite?
continue;
}
//all checks performed
//now we can start the copy procedure
qDebug() << di.absoluteFilePath() << "to" <<
QDir::toNativeSeparators(QString(destDir.absolutePath()).
append("/").append(di.fileName()));
if (!in.copy(QDir::toNativeSeparators(QString(destDir.absolutePath()).
append("/").append(di.fileName())))) {
return false;
}
if (in.size() > 1e6) {
setValue(value()+(int)(in.size()/1e6));
}
} //isFile()
} //foreach
return true;
}