diff options
author | Arno <arno@disconnect.de> | 2018-02-01 07:24:43 +0100 |
---|---|---|
committer | Arno <arno@disconnect.de> | 2018-02-01 07:24:43 +0100 |
commit | f2604b62b777d7cab001bb5b7d80ad25b586e554 (patch) | |
tree | 7b8ef0abd8b87e9e71e33d246f705415aaa11814 | |
parent | d6a44b289027db999e50a2b346327e6650a4f43c (diff) | |
download | ShemovCleaner-f2604b62b777d7cab001bb5b7d80ad25b586e554.tar.gz ShemovCleaner-f2604b62b777d7cab001bb5b7d80ad25b586e554.tar.bz2 ShemovCleaner-f2604b62b777d7cab001bb5b7d80ad25b586e554.zip |
Improve FileCopier
This endeavor started out quite innocently: fix the connect syntax in
ProgressDialog, but it quickly became much more. Vom Höxchen aufs
Stöxchen :)
First I thought it would be nice to add a total count to the
ProgressDialog. Then I realized that I had QElapsedTimer::restart()
totally wrong. It returns the ms *since the last restart*! It doesn't
reset to zero, so fix that.
While testing that I noticed that the download speed was quite below
average, so change the buffer size and only check if we were cancelled
when the timer elapsed. But that wasn't it. Eventually I dug into my
firewall rules and routings to get it right.
-rw-r--r-- | filecopier.cpp | 42 | ||||
-rw-r--r-- | filecopier.h | 6 | ||||
-rw-r--r-- | filewidget.cpp | 11 | ||||
-rw-r--r-- | filewidget.h | 2 | ||||
-rw-r--r-- | progressdialog.cpp | 2 | ||||
-rw-r--r-- | progressdialog.h | 4 |
6 files changed, 40 insertions, 27 deletions
diff --git a/filecopier.cpp b/filecopier.cpp index 001b317..1588d6e 100644 --- a/filecopier.cpp +++ b/filecopier.cpp @@ -4,24 +4,21 @@ #include "filecopier.h" -FileCopier::FileCopier(QObject *parent) : QThread(parent), mCancel(false){} +FileCopier::FileCopier(QObject *parent) : QThread(parent), mCancel(false), mTotal(0), mCopied(0){} void FileCopier::addJob(const QString &source, const QString &dest){ QMutexLocker l(&mAddJobMutex); if(!mJobs.contains(source)){ mJobs[source] = dest; + ++mTotal; } } -QHash<QString, QString> FileCopier::jobs(){ - QMutexLocker l(&mAddJobMutex); - return mJobs; -} - void FileCopier::run(){ - int bufsize = 16 * 1024 * 1024; + int bufsize = 32 * 1024 * 1024; char *buf = new char[bufsize]; mCancel = false; + mCopied = 0; QElapsedTimer et; while(!mJobs.isEmpty()){ mAddJobMutex.lock(); @@ -45,31 +42,40 @@ void FileCopier::run(){ 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 = et.elapsed(); + if(elapsed > 1000){ + mCancelMutex.lock(); + bool cancel = mCancel; + mCancelMutex.unlock(); + if(cancel){ + dFile.close(); + QFile::remove(dest); + emit success(false, source); + goto cleanup; + } + mCountMutex.lock(); + emit bytesReadIntval(bytesSinceEl, elapsed, mTotal, mCopied); + mCountMutex.unlock(); elapsed = 0; bytesSinceEl = 0; + et.restart(); } emit bytesRead(total); } emit success(true, source); + ++mCopied; + emit bytesReadIntval(bytesSinceEl, elapsed, mTotal, mCopied); + et.restart(); } cleanup: delete buf; + mTotal = 0; + mJobs.clear(); } void FileCopier::cancel(){ diff --git a/filecopier.h b/filecopier.h index 81d8496..6a9e37f 100644 --- a/filecopier.h +++ b/filecopier.h @@ -10,7 +10,6 @@ class FileCopier : public QThread { public: FileCopier(QObject *parent = 0); void addJob(const QString &source, const QString &dest); - QHash<QString, QString> jobs(); virtual void run(); void cancel(); @@ -18,13 +17,16 @@ class FileCopier : public QThread { void newFile(const QString &source, const QString & dest, qint64 size); void success(bool s, QString source); void bytesRead(qint64 bytes); - void bytesReadIntval(qint64 bytes, qint64 msecs); + void bytesReadIntval(qint64 bytes, qint64 msecs, int total, int copied); private: QHash<QString, QString> mJobs; QMutex mAddJobMutex; QMutex mCancelMutex; + QMutex mCountMutex; bool mCancel; + int mTotal; + int mCopied; }; #endif // FILECOPIER_H diff --git a/filewidget.cpp b/filewidget.cpp index ab4f240..f1d8873 100644 --- a/filewidget.cpp +++ b/filewidget.cpp @@ -704,11 +704,12 @@ void FileWidget::setCopyProgress(qint64 bytes){ mCopyProgress->setValue(val); } -void FileWidget::setCopySummary(qint64 bytes, qint64 elapsed){ - QHash<QString,QString> jobs = mFileCopier->jobs(); - float rate = bytes / (elapsed / 1000) / 1024 / 1024.0; - QString sum = QString("%1 file(s), @ %2 MB/s").arg(QString::number(jobs.count() + 1)).arg(QString::number(rate, 'f', 2)); - mCopyProgress->setSummary(sum); +void FileWidget::setCopySummary(qint64 bytes, qint64 elapsed, int total, int copying){ + if(elapsed != 0){ + float rate = bytes * 8 / (elapsed / 1000.0) / 1024 / 1024.0; + QString sum = QString("%1/%2 file(s), @ %3 MB/s").arg(QString::number(copying+1)).arg(QString::number(total)).arg(QString::number(rate, 'f', 2)); + mCopyProgress->setSummary(sum); + } } void FileWidget::copyFiles(QString destDir){ diff --git a/filewidget.h b/filewidget.h index 5f14c4a..0f4bc90 100644 --- a/filewidget.h +++ b/filewidget.h @@ -73,7 +73,7 @@ class FileWidget : public QWidget { void addAsOrigin(); void setupProgress(QString file, QString dest, qint64 size); void setCopyProgress(qint64 bytes); - void setCopySummary(qint64 bytes, qint64 elapsed); + void setCopySummary(qint64 bytes, qint64 elapsed, int total, int copying); void copyFiles(QString destDir); void hideCopyProgress(); void copySuccess(bool success, QString source); diff --git a/progressdialog.cpp b/progressdialog.cpp index d95f1e1..0690d65 100644 --- a/progressdialog.cpp +++ b/progressdialog.cpp @@ -10,7 +10,7 @@ ProgressDialog::ProgressDialog(QWidget *parent, Qt::WindowFlags f) : QDialog(par mProgress = new QProgressBar; mSum = new QLabel(tr("Wait for it...")); QPushButton *cancelBtn = new QPushButton(tr("Cancel")); - connect(cancelBtn, SIGNAL(clicked()), this, SIGNAL(cancelled())); + connect(cancelBtn, &QPushButton::clicked, this, &ProgressDialog::cancelled); QHBoxLayout *btnLayout = new QHBoxLayout; btnLayout->addStretch(); btnLayout->addWidget(cancelBtn); diff --git a/progressdialog.h b/progressdialog.h index 86a31bc..01657c8 100644 --- a/progressdialog.h +++ b/progressdialog.h @@ -15,6 +15,8 @@ class ProgressDialog : public QDialog { public slots: void setValue(int val); + void setTotal(int total) { mTotal = total; } + void setCopied(int copied) { mCopied = copied; } signals: void cancelled(); @@ -24,6 +26,8 @@ class ProgressDialog : public QDialog { QLabel *mDst; QLabel *mSum; QProgressBar *mProgress; + int mTotal; + int mCopied; }; |