-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilecopydialog.cpp
More file actions
315 lines (272 loc) · 10.7 KB
/
Copy pathfilecopydialog.cpp
File metadata and controls
315 lines (272 loc) · 10.7 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
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#include "filecopydialog.h"
#include <QCryptographicHash>
#include <QHash>
#include <QMessageBox>
#include <QSet>
#include <QThread>
#include <QTimer>
#include "ui_filecopydialog.h"
fileCopyDialog::fileCopyDialog(const QList<fileInfoStruct> &list,
const QString &importFolder,
const QString &projectName,
const QString &fileNameFormat,
const bool &md5Check,
const bool &deleteAfterImport,
const bool &deleteExisting,
const QString &importBackupFolder,
QWidget *parent)
: QDialog(parent)
, ui(new Ui::fileCopyDialog)
{
ui->setupUi(this);
ui->progressBar->setRange(0, 100);
// Total file count across all workers
count = list.count();
m_totalFiles = count;
ui->status->setText(QString(tr("Copy file %1 of %2.")).arg(0).arg(count));
ui->progressBar->setValue(0);
m_importFolder = importFolder;
m_projectName = projectName;
m_fileNameFormat = fileNameFormat;
m_md5Check = md5Check;
m_deleteAfterImport = deleteAfterImport;
m_deleteExisting = deleteExisting;
m_importBackupFolder = importBackupFolder;
// With duplicate destination paths two workers could write to the same
// target file concurrently, so stay single-threaded in that case.
// Checking the import destinations also covers the backup destinations:
// both use the same resolved file name, only the folder prefix differs.
bool hasDuplicateDestinations = false;
QSet<QString> seenDestinations;
QHash<QString, QString> sourceByDestination;
for (const fileInfoStruct &file : list) {
const QList<QString> fileTodo = fileCopyWorker::processNewFileName(importFolder,
projectName,
fileCopyWorker::captureTimestamp(file.fileInfo, file.imageInfo),
file.imageInfo,
file.fileInfo,
fileNameFormat);
const QString destinationPath = fileTodo.value(2);
if (seenDestinations.contains(destinationPath)) {
hasDuplicateDestinations = true;
qWarning() << "Duplicate import destination detected:" << destinationPath
<< "for" << sourceByDestination.value(destinationPath)
<< "and" << file.fileInfo.filePath();
break;
}
seenDestinations.insert(destinationPath);
sourceByDestination.insert(destinationPath, file.fileInfo.filePath());
}
m_allowSecondWorker = !hasDuplicateDestinations;
// All workers share one queue. Start with a single worker; a second one
// is only started from handleFileProcessed() when the measured
// throughput shows a fast reader (e.g. CFexpress instead of SD).
m_totalSourceBytes = 0;
for (const fileInfoStruct &file : list)
m_totalSourceBytes += file.fileInfo.size();
m_queue = std::make_shared<fileCopyQueue>(list);
m_workerCount = 1;
m_copyTimer.start();
startWorker(0);
}
void fileCopyDialog::handleBytesAccounted(qint64 delta)
{
m_bytesAccounted += delta;
if (m_totalSourceBytes > 0) {
const qint64 pct = (m_bytesAccounted * 100) / m_totalSourceBytes;
const int progress = int(qMin<qint64>(qMax<qint64>(pct, 0), 100));
ui->progressBar->setValue(progress);
}
}
void fileCopyDialog::startWorker(int workerIndex)
{
auto *thread = new QThread(this);
auto *worker = new fileCopyWorker(m_queue,
m_importFolder,
m_projectName,
m_fileNameFormat,
m_md5Check,
m_deleteAfterImport,
m_deleteExisting,
m_importBackupFolder);
if (workerIndex == 0) {
m_thread = thread;
m_worker = worker;
} else {
m_thread2 = thread;
m_worker2 = worker;
}
worker->moveToThread(thread);
connect(thread, &QThread::started, worker, &fileCopyWorker::copyImages);
connect(worker, &fileCopyWorker::progressUpdated, this,
[this, workerIndex](int progress, int done, int cnt, int fail, int del){
handleProgressFromWorker(workerIndex, progress, done, cnt, fail, del);
});
connect(worker, &fileCopyWorker::fileProcessed,
this, &fileCopyDialog::handleFileProcessed);
connect(worker, &fileCopyWorker::bytesAccounted,
this, &fileCopyDialog::handleBytesAccounted);
connect(worker, &fileCopyWorker::lastLocationImportedTo,
this, &fileCopyDialog::lastLocationImportedToSlot);
// Collect errors instead of opening a modal box per failed file — a
// re-import of an existing set would otherwise stack dozens of dialogs.
connect(worker, &fileCopyWorker::errorOccurred, this, [this](const QString &msg){
m_errors.append(msg);
}, Qt::QueuedConnection);
connect(worker, &fileCopyWorker::copyingFinished, this, &fileCopyDialog::handleWorkerFinished);
connect(thread, &QThread::finished, worker, &QObject::deleteLater);
connect(thread, &QThread::finished, thread, &QObject::deleteLater);
connect(thread, &QThread::finished, this, [this, workerIndex]() { handleThreadStopped(workerIndex); });
thread->start();
}
void fileCopyDialog::handleFileProcessed(qint64 copiedBytes)
{
m_bytesProcessed += copiedBytes;
if (m_secondWorkerDecided || !m_allowSecondWorker)
return;
// Decide once, after enough data has flowed to give a stable measurement.
constexpr qint64 kProbeBytes = 200LL * 1000 * 1000; // 200 MB
constexpr double kMinCardReadMBps = 300.0; // below this a 2nd worker hurts
if (m_bytesProcessed < kProbeBytes)
return;
m_secondWorkerDecided = true;
const double seconds = m_copyTimer.elapsed() / 1000.0;
if (seconds <= 0)
return;
// The tee-copy reads the source exactly once (MD5 is hashed during the
// copy), so copied bytes equal card reads.
const double cardMBps = m_bytesProcessed / 1e6 / seconds;
qDebug() << "Copy throughput probe:" << cardMBps << "MB/s (card read estimate)";
if (cardMBps >= kMinCardReadMBps && m_queue->remaining() > 1
&& m_finishedWorkers == 0 && !m_cancelRequested) {
qDebug() << "Fast reader detected, starting second copy worker";
m_workerCount = 2;
startWorker(1);
}
}
fileCopyDialog::~fileCopyDialog()
{
if (m_thread && m_thread->isRunning()) {
m_thread->quit();
m_thread->wait();
}
if (m_thread2 && m_thread2->isRunning()) {
m_thread2->quit();
m_thread2->wait();
}
delete ui;
}
void fileCopyDialog::on_cancelButton_clicked()
{
ui->cancelButton->setEnabled(false);
if (m_worker) m_worker->cancel();
if (m_worker2) m_worker2->cancel();
}
void fileCopyDialog::handleProgress(int progress, int done, int cnt, int fail, int del)
{
QString err;
if (cnt > 0)
err += QString(tr("%1 copied. ")).arg(cnt);
if (fail > 0)
err += QString(tr("%1 failed. ")).arg(fail);
if (del > 0)
err += QString(tr("%1 deleted. ")).arg(del);
ui->status->setText(
QString(tr("Copying: %1 of %2 processed. %3")).arg(done).arg(count).arg(err));
ui->progressBar->setValue(progress);
}
void fileCopyDialog::handleFinished()
{
handleWorkerFinished();
}
void fileCopyDialog::lastLocationImportedToSlot(QString str)
{
lastFilePath = str;
}
QString fileCopyDialog::getLastFilePath()
{
return lastFilePath;
}
void fileCopyDialog::handleProgressFromWorker(int workerIndex, int progress, int done, int cnt, int fail, int del)
{
if (workerIndex < 0 || workerIndex > 1) return;
m_done[workerIndex] = done;
m_cnt[workerIndex] = cnt;
m_fail[workerIndex] = fail;
m_del[workerIndex] = del;
int totalDone = m_done[0] + m_done[1];
int totalCnt = m_cnt[0] + m_cnt[1];
int totalFail = m_fail[0] + m_fail[1];
int totalDel = m_del[0] + m_del[1];
QString err;
if (totalCnt > 0)
err += QString(tr("%1 copied. ")).arg(totalCnt);
if (totalFail > 0)
err += QString(tr("%1 failed. ")).arg(totalFail);
if (totalDel > 0)
err += QString(tr("%1 deleted. ")).arg(totalDel);
ui->status->setText(QString(tr("Copying: %1 of %2 processed. %3")).arg(totalDone).arg(m_totalFiles).arg(err));
// The progress bar is normally driven byte-by-byte via
// handleBytesAccounted(); fall back to file counts when the total size
// is unknown (e.g. all files report size 0).
if (m_totalSourceBytes <= 0) {
int overallProgress = (m_totalFiles > 0) ? ((totalDone * 100) / m_totalFiles) : progress;
ui->progressBar->setValue(overallProgress);
}
}
void fileCopyDialog::handleWorkerFinished()
{
QObject *s = sender();
if (s == m_worker && m_thread) {
m_thread->quit();
} else if (s == m_worker2 && m_thread2) {
m_thread2->quit();
}
m_finishedWorkers++;
if ((m_worker && m_worker->wasCancelled()) || (m_worker2 && m_worker2->wasCancelled())) {
m_cancelRequested = true;
}
finalizeIfReady();
}
void fileCopyDialog::handleThreadStopped(int workerIndex)
{
if (workerIndex == 0) {
m_thread = nullptr;
m_worker = nullptr;
} else if (workerIndex == 1) {
m_thread2 = nullptr;
m_worker2 = nullptr;
}
m_stoppedThreads++;
finalizeIfReady();
}
void fileCopyDialog::finalizeIfReady()
{
if (m_closeScheduled)
return;
if (m_finishedWorkers < m_workerCount || m_stoppedThreads < m_workerCount)
return;
m_closeScheduled = true;
showErrorSummary();
if (m_cancelRequested) {
reject();
} else {
accept();
}
}
void fileCopyDialog::showErrorSummary()
{
if (m_errors.isEmpty())
return;
constexpr int kMaxShown = 12;
QString text = m_errors.mid(0, kMaxShown).join(QLatin1Char('\n'));
if (m_errors.size() > kMaxShown)
text += QLatin1Char('\n') + tr("... and %1 more.").arg(m_errors.size() - kMaxShown);
QMessageBox box(this);
box.setIcon(QMessageBox::Warning);
box.setWindowTitle(tr("Import finished with problems"));
box.setText(tr("%1 file(s) could not be imported.").arg(m_errors.size()));
box.setInformativeText(text);
box.setDetailedText(m_errors.join(QLatin1Char('\n')));
box.exec();
}